diff options
| author | 2006-12-09 23:18:17 (JST) | |
|---|---|---|
| committer | 2006-12-09 23:18:17 (JST) | |
| commit | 0d169ada2ba81210ab1191a5f2212662e90db77e (patch) | |
| tree | 402b54583db269323ebb28e5fbf2075c4c1b3d85 | |
| download | cgit-0d169ada2ba81210ab1191a5f2212662e90db77e.zip cgit-0d169ada2ba81210ab1191a5f2212662e90db77e.tar.gz | |
Import cgit prototype from git tree
This enables basic cgit functionality, using libgit.a and xdiff/lib.a from
git + a custom "git.h" + openssl for sha1 routines.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
| -rw-r--r-- | Makefile | 24 | ||||
| -rw-r--r-- | cgit.c | 410 | ||||
| -rw-r--r-- | cgit.css | 63 | ||||
| -rw-r--r-- | cgit.h | 21 | ||||
| -rw-r--r-- | config.c | 73 | ||||
| -rw-r--r-- | git.h | 399 | ||||
| -rw-r--r-- | html.c | 100 |
7 files changed, 1090 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1470c0a --- /dev/null +++ b/Makefile | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | |||
| 2 | INSTALL_BIN = /var/www/htdocs/cgit.cgi | ||
| 3 | INSTALL_CSS = /var/www/htdocs/cgit.css | ||
| 4 | |||
| 5 | EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto | ||
| 6 | OBJECTS = cgit.o config.o html.o | ||
| 7 | |||
| 8 | all: cgit | ||
| 9 | |||
| 10 | install: all | ||
| 11 | install cgit $(INSTALL_BIN) | ||
| 12 | install cgit.css $(INSTALL_CSS) | ||
| 13 | |||
| 14 | clean: | ||
| 15 | rm -f cgit *.o | ||
| 16 | |||
| 17 | cgit: $(OBJECTS) | ||
| 18 | $(CC) -o cgit $(OBJECTS) $(EXTLIBS) | ||
| 19 | |||
| 20 | cgit.o: cgit.h git.h config.o html.o | ||
| 21 | |||
| 22 | config.o: cgit.h git.h html.c | ||
| 23 | |||
| 24 | html.o: cgit.h git.h html.c | ||
| @@ -0,0 +1,410 @@ | |||
| 1 | #include "cgit.h" | ||
| 2 | |||
| 3 | static const char cgit_doctype[] = | ||
| 4 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | ||
| 5 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | ||
| 6 | |||
| 7 | static const char cgit_error[] = | ||
| 8 | "<div class='error'>%s</div>"; | ||
| 9 | |||
| 10 | static const char cgit_lib_error[] = | ||
| 11 | "<div class='error'>%s: %s</div>"; | ||
| 12 | |||
| 13 | |||
| 14 | char *cgit_root = "/var/git"; | ||
| 15 | char *cgit_root_title = "Git repository browser"; | ||
| 16 | char *cgit_css = "/cgit.css"; | ||
| 17 | char *cgit_logo = "/git-logo.png"; | ||
| 18 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; | ||
| 19 | char *cgit_virtual_root = NULL; | ||
| 20 | |||
| 21 | char *cgit_repo_name = NULL; | ||
| 22 | char *cgit_repo_desc = NULL; | ||
| 23 | char *cgit_repo_owner = NULL; | ||
| 24 | |||
| 25 | char *cgit_query_repo = NULL; | ||
| 26 | char *cgit_query_page = NULL; | ||
| 27 | char *cgit_query_head = NULL; | ||
| 28 | |||
| 29 | int cgit_parse_query(char *txt, configfn fn) | ||
| 30 | { | ||
| 31 | char *t = txt, *value = NULL, c; | ||
| 32 | |||
| 33 | if (!txt) | ||
| 34 | return 0; | ||
| 35 | |||
| 36 | while((c=*t) != '\0') { | ||
| 37 | if (c=='=') { | ||
| 38 | *t = '\0'; | ||
| 39 | value = t+1; | ||
| 40 | } else if (c=='&') { | ||
| 41 | *t = '\0'; | ||
| 42 | (*fn)(txt, value); | ||
| 43 | txt = t+1; | ||
| 44 | value = NULL; | ||
| 45 | } | ||
| 46 | t++; | ||
| 47 | } | ||
| 48 | if (t!=txt) | ||
| 49 | (*fn)(txt, value); | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | void cgit_global_config_cb(const char *name, const char *value) | ||
| 54 | { | ||
| 55 | if (!strcmp(name, "root")) | ||
| 56 | cgit_root = xstrdup(value); | ||
| 57 | else if (!strcmp(name, "root-title")) | ||
| 58 | cgit_root_title = xstrdup(value); | ||
| 59 | else if (!strcmp(name, "css")) | ||
| 60 | cgit_css = xstrdup(value); | ||
| 61 | else if (!strcmp(name, "logo")) | ||
| 62 | cgit_logo = xstrdup(value); | ||
| 63 | else if (!strcmp(name, "logo-link")) | ||
| 64 | cgit_logo_link = xstrdup(value); | ||
| 65 | else if (!strcmp(name, "virtual-root")) | ||
| 66 | cgit_virtual_root = xstrdup(value); | ||
| 67 | } | ||
| 68 | |||
| 69 | void cgit_repo_config_cb(const char *name, const char *value) | ||
| 70 | { | ||
| 71 | if (!strcmp(name, "name")) | ||
| 72 | cgit_repo_name = xstrdup(value); | ||
| 73 | else if (!strcmp(name, "desc")) | ||
| 74 | cgit_repo_desc = xstrdup(value); | ||
| 75 | else if (!strcmp(name, "owner")) | ||
| 76 | cgit_repo_owner = xstrdup(value); | ||
| 77 | } | ||
| 78 | |||
| 79 | void cgit_querystring_cb(const char *name, const char *value) | ||
| 80 | { | ||
| 81 | if (!strcmp(name,"r")) | ||
| 82 | cgit_query_repo = xstrdup(value); | ||
| 83 | else if (!strcmp(name, "p")) | ||
| 84 | cgit_query_page = xstrdup(value); | ||
| 85 | else if (!strcmp(name, "h")) | ||
| 86 | cgit_query_head = xstrdup(value); | ||
| 87 | } | ||
| 88 | |||
| 89 | char *cgit_repourl(const char *reponame) | ||
| 90 | { | ||
| 91 | if (cgit_virtual_root) { | ||
| 92 | return fmt("%s/%s/", cgit_virtual_root, reponame); | ||
| 93 | } else { | ||
| 94 | return fmt("?r=%s", reponame); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | char *cgit_pageurl(const char *reponame, const char *pagename, | ||
| 99 | const char *query) | ||
| 100 | { | ||
| 101 | if (cgit_virtual_root) { | ||
| 102 | return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, | ||
| 103 | pagename, query); | ||
| 104 | } else { | ||
| 105 | return fmt("?r=%s&p=%s&%s", reponame, pagename, query); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, | ||
| 110 | int flags, void *cb_data) | ||
| 111 | { | ||
| 112 | struct commit *commit; | ||
| 113 | char buf[256], *url; | ||
| 114 | |||
| 115 | commit = lookup_commit(sha1); | ||
| 116 | if (commit && !parse_commit(commit)){ | ||
| 117 | html("<tr><td>"); | ||
| 118 | url = cgit_pageurl(cgit_query_repo, "log", | ||
| 119 | fmt("h=%s", refname)); | ||
| 120 | html_link_open(url, NULL, NULL); | ||
| 121 | strncpy(buf, refname, sizeof(buf)); | ||
| 122 | html_txt(buf); | ||
| 123 | html_link_close(); | ||
| 124 | html("</td><td>"); | ||
| 125 | pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, | ||
| 126 | sizeof(buf), 0, NULL, NULL, 0); | ||
| 127 | html_txt(buf); | ||
| 128 | html("</td></tr>\n"); | ||
| 129 | } else { | ||
| 130 | html("<tr><td>"); | ||
| 131 | html_txt(buf); | ||
| 132 | html("</td><td>"); | ||
| 133 | htmlf("*** bad ref %s", sha1_to_hex(sha1)); | ||
| 134 | html("</td></tr>\n"); | ||
| 135 | } | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | static void cgit_print_docstart(char *title) | ||
| 140 | { | ||
| 141 | html("Content-Type: text/html; charset=utf-8\n"); | ||
| 142 | html("\n"); | ||
| 143 | html(cgit_doctype); | ||
| 144 | html("<html>\n"); | ||
| 145 | html("<head>\n"); | ||
| 146 | html("<title>"); | ||
| 147 | html_txt(title); | ||
| 148 | html("</title>\n"); | ||
| 149 | html("<link rel='stylesheet' type='text/css' href='"); | ||
| 150 | html_attr(cgit_css); | ||
| 151 | html("'/>\n"); | ||
| 152 | html("</head>\n"); | ||
| 153 | html("<body>\n"); | ||
| 154 | } | ||
| 155 | |||
| 156 | static void cgit_print_docend() | ||
| 157 | { | ||
| 158 | html("</body>\n</html>\n"); | ||
| 159 | } | ||
| 160 | |||
| 161 | static void cgit_print_pageheader(char *title) | ||
| 162 | { | ||
| 163 | html("<div id='header'>"); | ||
| 164 | htmlf("<a href='%s'>", cgit_logo_link); | ||
| 165 | htmlf("<img id='logo' src='%s'/>\n", cgit_logo); | ||
| 166 | htmlf("</a>"); | ||
| 167 | html_txt(title); | ||
| 168 | html("</div>"); | ||
| 169 | } | ||
| 170 | |||
| 171 | static void cgit_print_repolist() | ||
| 172 | { | ||
| 173 | DIR *d; | ||
| 174 | struct dirent *de; | ||
| 175 | struct stat st; | ||
| 176 | char *name; | ||
| 177 | |||
| 178 | cgit_print_docstart(cgit_root_title); | ||
| 179 | cgit_print_pageheader(cgit_root_title); | ||
| 180 | |||
| 181 | if (!(d = opendir("."))) { | ||
| 182 | htmlf(cgit_lib_error, "Unable to scan repository directory", | ||
| 183 | strerror(errno)); | ||
| 184 | cgit_print_docend(); | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | |||
| 188 | html("<h2>Repositories</h2>\n"); | ||
| 189 | html("<table class='list'>"); | ||
| 190 | html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n"); | ||
| 191 | while ((de = readdir(d)) != NULL) { | ||
| 192 | if (de->d_name[0] == '.') | ||
| 193 | continue; | ||
| 194 | if (stat(de->d_name, &st) < 0) | ||
| 195 | continue; | ||
| 196 | if (!S_ISDIR(st.st_mode)) | ||
| 197 | continue; | ||
| 198 | |||
| 199 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; | ||
| 200 | name = fmt("%s/.git/info/cgit", de->d_name); | ||
| 201 | if (cgit_read_config(name, cgit_repo_config_cb)) | ||
| 202 | continue; | ||
| 203 | |||
| 204 | html("<tr><td>"); | ||
| 205 | html_link_open(cgit_repourl(de->d_name), NULL, NULL); | ||
| 206 | html_txt(cgit_repo_name); | ||
| 207 | html_link_close(); | ||
| 208 | html("</td><td>"); | ||
| 209 | html_txt(cgit_repo_desc); | ||
| 210 | html("</td><td>"); | ||
| 211 | html_txt(cgit_repo_owner); | ||
| 212 | html("</td></tr>\n"); | ||
| 213 | } | ||
| 214 | closedir(d); | ||
| 215 | html("</table>"); | ||
| 216 | cgit_print_docend(); | ||
| 217 | } | ||
| 218 | |||
| 219 | static void cgit_print_branches() | ||
| 220 | { | ||
| 221 | html("<table class='list'>"); | ||
| 222 | html("<tr><th>Branch name</th><th>Head commit</th></tr>\n"); | ||
| 223 | for_each_branch_ref(cgit_print_branch_cb, NULL); | ||
| 224 | html("</table>"); | ||
| 225 | } | ||
| 226 | |||
| 227 | static int get_one_line(char *txt) | ||
| 228 | { | ||
| 229 | char *t; | ||
| 230 | |||
| 231 | for(t=txt; *t != '\n' && t != '\0'; t++) | ||
| 232 | ; | ||
| 233 | *t = '\0'; | ||
| 234 | return t-txt-1; | ||
| 235 | } | ||
| 236 | |||
| 237 | static void cgit_print_commit_shortlog(struct commit *commit) | ||
| 238 | { | ||
| 239 | char *h, *t, *p; | ||
| 240 | char *tree = NULL, *author = NULL, *subject = NULL; | ||
| 241 | int len; | ||
| 242 | time_t sec; | ||
| 243 | struct tm *time; | ||
| 244 | char buf[32]; | ||
| 245 | |||
| 246 | h = t = commit->buffer; | ||
| 247 | |||
| 248 | if (strncmp(h, "tree ", 5)) | ||
| 249 | die("Bad commit format: %s", | ||
| 250 | sha1_to_hex(commit->object.sha1)); | ||
| 251 | |||
| 252 | len = get_one_line(h); | ||
| 253 | tree = h+5; | ||
| 254 | h += len + 2; | ||
| 255 | |||
| 256 | while (!strncmp(h, "parent ", 7)) | ||
| 257 | h += get_one_line(h) + 2; | ||
| 258 | |||
| 259 | if (!strncmp(h, "author ", 7)) { | ||
| 260 | author = h+7; | ||
| 261 | h += get_one_line(h) + 2; | ||
| 262 | t = author; | ||
| 263 | while(t!=h && *t!='<') | ||
| 264 | t++; | ||
| 265 | *t='\0'; | ||
| 266 | p = t; | ||
| 267 | while(--t!=author && *t==' ') | ||
| 268 | *t='\0'; | ||
| 269 | while(++p!=h && *p!='>') | ||
| 270 | ; | ||
| 271 | while(++p!=h && !isdigit(*p)) | ||
| 272 | ; | ||
| 273 | |||
| 274 | t = p; | ||
| 275 | while(++p && isdigit(*p)) | ||
| 276 | ; | ||
| 277 | *p = '\0'; | ||
| 278 | sec = atoi(t); | ||
| 279 | time = gmtime(&sec); | ||
| 280 | } | ||
| 281 | |||
| 282 | while((len = get_one_line(h)) > 0) | ||
| 283 | h += len+2; | ||
| 284 | |||
| 285 | h++; | ||
| 286 | len = get_one_line(h); | ||
| 287 | |||
| 288 | subject = h; | ||
| 289 | |||
| 290 | html("<tr><td>"); | ||
| 291 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); | ||
| 292 | html_txt(buf); | ||
| 293 | html("</td><td>"); | ||
| 294 | char *qry = fmt("h=%s", sha1_to_hex(commit->object.sha1)); | ||
| 295 | char *url = cgit_pageurl(cgit_query_repo, "view", qry); | ||
| 296 | html_link_open(url, NULL, NULL); | ||
| 297 | html_txt(subject); | ||
| 298 | html_link_close(); | ||
| 299 | html("</td><td>"); | ||
| 300 | html_txt(author); | ||
| 301 | html("</td></tr>\n"); | ||
| 302 | } | ||
| 303 | |||
| 304 | static void cgit_print_log(const char *tip, int ofs, int cnt) | ||
| 305 | { | ||
| 306 | struct rev_info rev; | ||
| 307 | struct commit *commit; | ||
| 308 | const char *argv[2] = {NULL, tip}; | ||
| 309 | int n = 0; | ||
| 310 | |||
| 311 | init_revisions(&rev, NULL); | ||
| 312 | rev.abbrev = DEFAULT_ABBREV; | ||
| 313 | rev.commit_format = CMIT_FMT_DEFAULT; | ||
| 314 | rev.verbose_header = 1; | ||
| 315 | rev.show_root_diff = 0; | ||
| 316 | setup_revisions(2, argv, &rev, NULL); | ||
| 317 | prepare_revision_walk(&rev); | ||
| 318 | |||
| 319 | html("<h2>Log</h2>"); | ||
| 320 | html("<table class='list'>"); | ||
| 321 | html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n"); | ||
| 322 | while ((commit = get_revision(&rev)) != NULL && n++ < 100) { | ||
| 323 | cgit_print_commit_shortlog(commit); | ||
| 324 | free(commit->buffer); | ||
| 325 | commit->buffer = NULL; | ||
| 326 | free_commit_list(commit->parents); | ||
| 327 | commit->parents = NULL; | ||
| 328 | } | ||
| 329 | html("</table>\n"); | ||
| 330 | } | ||
| 331 | |||
| 332 | static void cgit_print_repo_summary() | ||
| 333 | { | ||
| 334 | html("<h2>"); | ||
| 335 | html_txt("Repo summary page"); | ||
| 336 | html("</h2>"); | ||
| 337 | cgit_print_branches(); | ||
| 338 | } | ||
| 339 | |||
| 340 | static void cgit_print_object(char *hex) | ||
| 341 | { | ||
| 342 | unsigned char sha1[20]; | ||
| 343 | //struct object *object; | ||
| 344 | char type[20]; | ||
| 345 | unsigned char *buf; | ||
| 346 | unsigned long size; | ||
| 347 | |||
| 348 | if (get_sha1_hex(hex, sha1)){ | ||
| 349 | htmlf(cgit_error, "Bad hex value"); | ||
| 350 | return; | ||
| 351 | } | ||
| 352 | |||
| 353 | if (sha1_object_info(sha1, type, NULL)){ | ||
| 354 | htmlf(cgit_error, "Bad object name"); | ||
| 355 | return; | ||
| 356 | } | ||
| 357 | |||
| 358 | buf = read_sha1_file(sha1, type, &size); | ||
| 359 | if (!buf) { | ||
| 360 | htmlf(cgit_error, "Error reading object"); | ||
| 361 | return; | ||
| 362 | } | ||
| 363 | |||
| 364 | buf[size] = '\0'; | ||
| 365 | html("<h2>Object view</h2>"); | ||
| 366 | htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size); | ||
| 367 | html("<pre>"); | ||
| 368 | html_txt(buf); | ||
| 369 | html("</pre>"); | ||
| 370 | } | ||
| 371 | |||
| 372 | static void cgit_print_repo_page() | ||
| 373 | { | ||
| 374 | if (chdir(cgit_query_repo) || | ||
| 375 | cgit_read_config(".git/info/cgit", cgit_repo_config_cb)) { | ||
| 376 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | ||
| 377 | cgit_print_docstart(title); | ||
| 378 | cgit_print_pageheader(title); | ||
| 379 | htmlf(cgit_lib_error, "Unable to scan repository", | ||
| 380 | strerror(errno)); | ||
| 381 | cgit_print_docend(); | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | |||
| 385 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); | ||
| 386 | cgit_print_docstart(title); | ||
| 387 | cgit_print_pageheader(title); | ||
| 388 | if (!cgit_query_page) | ||
| 389 | cgit_print_repo_summary(); | ||
| 390 | else if (!strcmp(cgit_query_page, "log")) { | ||
| 391 | cgit_print_log(cgit_query_head, 0, 100); | ||
| 392 | } else if (!strcmp(cgit_query_page, "view")) { | ||
| 393 | cgit_print_object(cgit_query_head); | ||
| 394 | } | ||
| 395 | cgit_print_docend(); | ||
| 396 | } | ||
| 397 | |||
| 398 | int main(int argc, const char **argv) | ||
| 399 | { | ||
| 400 | if (cgit_read_config("/etc/cgitrc", cgit_global_config_cb)) | ||
| 401 | die("Error reading config: %d %s", errno, strerror(errno)); | ||
| 402 | |||
| 403 | chdir(cgit_root); | ||
| 404 | cgit_parse_query(getenv("QUERY_STRING"), cgit_querystring_cb); | ||
| 405 | if (cgit_query_repo) | ||
| 406 | cgit_print_repo_page(); | ||
| 407 | else | ||
| 408 | cgit_print_repolist(); | ||
| 409 | return 0; | ||
| 410 | } | ||
diff --git a/cgit.css b/cgit.css new file mode 100644 index 0000000..3ed0c22 --- /dev/null +++ b/cgit.css | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | body { | ||
| 2 | font-family: arial; | ||
| 3 | font-size: normal; | ||
| 4 | background: white; | ||
| 5 | padding: 0em; | ||
| 6 | margin: 0.5em; | ||
| 7 | } | ||
| 8 | |||
| 9 | |||
| 10 | h2 { | ||
| 11 | font-size: normal; | ||
| 12 | font-weight: bold; | ||
| 13 | margin-bottom: 0.1em; | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | table.list { | ||
| 18 | border: solid 1px black; | ||
| 19 | border-collapse: collapse; | ||
| 20 | border: solid 1px #aaa; | ||
| 21 | } | ||
| 22 | |||
| 23 | table.list th { | ||
| 24 | text-align: left; | ||
| 25 | font-weight: bold; | ||
| 26 | background: #ddd; | ||
| 27 | border-bottom: solid 1px #aaa; | ||
| 28 | padding: 0.1em 0.5em 0.1em; | ||
| 29 | vertical-align: baseline; | ||
| 30 | } | ||
| 31 | table.list td { | ||
| 32 | border: none; | ||
| 33 | padding: 0.1em 0.5em; | ||
| 34 | background: white; | ||
| 35 | } | ||
| 36 | |||
| 37 | img { | ||
| 38 | border: none; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | div#header { | ||
| 43 | background-color: #ddd; | ||
| 44 | padding: 0.25em 0.25em 0.25em 0.5em; | ||
| 45 | font-size: 150%; | ||
| 46 | font-weight: bold; | ||
| 47 | border: solid 1px #aaa; | ||
| 48 | vertical-align: middle; | ||
| 49 | } | ||
| 50 | |||
| 51 | div#header img#logo { | ||
| 52 | float: right; | ||
| 53 | } | ||
| 54 | |||
| 55 | div#content { | ||
| 56 | margin: 0.5em 0.5em; | ||
| 57 | } | ||
| 58 | |||
| 59 | div.error { | ||
| 60 | color: red; | ||
| 61 | font-weight: bold; | ||
| 62 | margin: 1em 2em; | ||
| 63 | } \ No newline at end of file | ||
| @@ -0,0 +1,21 @@ | |||
| 1 | #ifndef CGIT_H | ||
| 2 | #define CGIT_H | ||
| 3 | |||
| 4 | #include "git.h" | ||
| 5 | #include <openssl/sha.h> | ||
| 6 | |||
| 7 | extern char *fmt(const char *format,...); | ||
| 8 | |||
| 9 | extern void html(const char *txt); | ||
| 10 | extern void htmlf(const char *format,...); | ||
| 11 | extern void html_txt(char *txt); | ||
| 12 | extern void html_attr(char *txt); | ||
| 13 | |||
| 14 | extern void html_link_open(char *url, char *title, char *class); | ||
| 15 | extern void html_link_close(void); | ||
| 16 | |||
| 17 | typedef void (*configfn)(const char *name, const char *value); | ||
| 18 | |||
| 19 | extern int cgit_read_config(const char *filename, configfn fn); | ||
| 20 | |||
| 21 | #endif /* CGIT_H */ | ||
diff --git a/config.c b/config.c new file mode 100644 index 0000000..858ab69 --- /dev/null +++ b/config.c | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | #include "cgit.h" | ||
| 2 | |||
| 3 | int next_char(FILE *f) | ||
| 4 | { | ||
| 5 | int c = fgetc(f); | ||
| 6 | if (c=='\r') { | ||
| 7 | c = fgetc(f); | ||
| 8 | if (c!='\n') { | ||
| 9 | ungetc(c, f); | ||
| 10 | c = '\r'; | ||
| 11 | } | ||
| 12 | } | ||
| 13 | return c; | ||
| 14 | } | ||
| 15 | |||
| 16 | void skip_line(FILE *f) | ||
| 17 | { | ||
| 18 | int c; | ||
| 19 | |||
| 20 | while((c=next_char(f)) && c!='\n' && c!=EOF) | ||
| 21 | ; | ||
| 22 | } | ||
| 23 | |||
| 24 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) | ||
| 25 | { | ||
| 26 | int i = 0, isname = 0; | ||
| 27 | |||
| 28 | *value = NULL; | ||
| 29 | while(i<bufsize-1) { | ||
| 30 | int c = next_char(f); | ||
| 31 | if (!isname && (c=='#' || c==';')) { | ||
| 32 | skip_line(f); | ||
| 33 | continue; | ||
| 34 | } | ||
| 35 | if (!isname && isblank(c)) | ||
| 36 | continue; | ||
| 37 | |||
| 38 | if (c=='=' && !*value) { | ||
| 39 | line[i] = 0; | ||
| 40 | *value = &line[i+1]; | ||
| 41 | } else if (c=='\n' && !isname) { | ||
| 42 | i = 0; | ||
| 43 | continue; | ||
| 44 | } else if (c=='\n' || c==EOF) { | ||
| 45 | line[i] = 0; | ||
| 46 | break; | ||
| 47 | } else { | ||
| 48 | line[i]=c; | ||
| 49 | } | ||
| 50 | isname = 1; | ||
| 51 | i++; | ||
| 52 | } | ||
| 53 | line[i+1] = 0; | ||
| 54 | return i; | ||
| 55 | } | ||
| 56 | |||
| 57 | int cgit_read_config(const char *filename, configfn fn) | ||
| 58 | { | ||
| 59 | int ret = 0, len; | ||
| 60 | char line[256]; | ||
| 61 | const char *value; | ||
| 62 | FILE *f = fopen(filename, "r"); | ||
| 63 | |||
| 64 | if (!f) | ||
| 65 | return -1; | ||
| 66 | |||
| 67 | while(len = read_config_line(f, line, &value, sizeof(line))) | ||
| 68 | (*fn)(line, value); | ||
| 69 | |||
| 70 | fclose(f); | ||
| 71 | return ret; | ||
| 72 | } | ||
| 73 | |||
| @@ -0,0 +1,399 @@ | |||
| 1 | #ifndef GIT_H | ||
| 2 | #define GIT_H | ||
| 3 | |||
| 4 | |||
| 5 | /* | ||
| 6 | * from git:git-compat-util.h | ||
| 7 | */ | ||
| 8 | |||
| 9 | |||
| 10 | #ifndef FLEX_ARRAY | ||
| 11 | #if defined(__GNUC__) && (__GNUC__ < 3) | ||
| 12 | #define FLEX_ARRAY 0 | ||
| 13 | #else | ||
| 14 | #define FLEX_ARRAY /* empty */ | ||
| 15 | #endif | ||
| 16 | #endif | ||
| 17 | |||
| 18 | |||
| 19 | #include <unistd.h> | ||
| 20 | #include <stdio.h> | ||
| 21 | #include <sys/stat.h> | ||
| 22 | #include <fcntl.h> | ||
| 23 | #include <stddef.h> | ||
| 24 | #include <stdlib.h> | ||
| 25 | #include <stdarg.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include <errno.h> | ||
| 28 | #include <limits.h> | ||
| 29 | #include <sys/param.h> | ||
| 30 | #include <netinet/in.h> | ||
| 31 | #include <sys/types.h> | ||
| 32 | #include <dirent.h> | ||
| 33 | #include <time.h> | ||
| 34 | |||
| 35 | |||
| 36 | static inline char* xstrdup(const char *str) | ||
| 37 | { | ||
| 38 | char *ret = strdup(str); | ||
| 39 | if (!ret) | ||
| 40 | die("Out of memory, strdup failed"); | ||
| 41 | return ret; | ||
| 42 | } | ||
| 43 | |||
| 44 | static inline void *xmalloc(size_t size) | ||
| 45 | { | ||
| 46 | void *ret = malloc(size); | ||
| 47 | if (!ret && !size) | ||
| 48 | ret = malloc(1); | ||
| 49 | if (!ret) | ||
| 50 | die("Out of memory, malloc failed"); | ||
| 51 | #ifdef XMALLOC_POISON | ||
| 52 | memset(ret, 0xA5, size); | ||
| 53 | #endif | ||
| 54 | return ret; | ||
| 55 | } | ||
| 56 | |||
| 57 | static inline void *xrealloc(void *ptr, size_t size) | ||
| 58 | { | ||
| 59 | void *ret = realloc(ptr, size); | ||
| 60 | if (!ret && !size) | ||
| 61 | ret = realloc(ptr, 1); | ||
| 62 | if (!ret) | ||
| 63 | die("Out of memory, realloc failed"); | ||
| 64 | return ret; | ||
| 65 | } | ||
| 66 | |||
| 67 | static inline void *xcalloc(size_t nmemb, size_t size) | ||
| 68 | { | ||
| 69 | void *ret = calloc(nmemb, size); | ||
| 70 | if (!ret && (!nmemb || !size)) | ||
| 71 | ret = calloc(1, 1); | ||
| 72 | if (!ret) | ||
| 73 | die("Out of memory, calloc failed"); | ||
| 74 | return ret; | ||
| 75 | } | ||
| 76 | |||
| 77 | static inline ssize_t xread(int fd, void *buf, size_t len) | ||
| 78 | { | ||
| 79 | ssize_t nr; | ||
| 80 | while (1) { | ||
| 81 | nr = read(fd, buf, len); | ||
| 82 | if ((nr < 0) && (errno == EAGAIN || errno == EINTR)) | ||
| 83 | continue; | ||
| 84 | return nr; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | static inline ssize_t xwrite(int fd, const void *buf, size_t len) | ||
| 89 | { | ||
| 90 | ssize_t nr; | ||
| 91 | while (1) { | ||
| 92 | nr = write(fd, buf, len); | ||
| 93 | if ((nr < 0) && (errno == EAGAIN || errno == EINTR)) | ||
| 94 | continue; | ||
| 95 | return nr; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | /* | ||
| 103 | * from git:cache.h | ||
| 104 | */ | ||
| 105 | |||
| 106 | |||
| 107 | /* Convert to/from hex/sha1 representation */ | ||
| 108 | #define MINIMUM_ABBREV 4 | ||
| 109 | #define DEFAULT_ABBREV 7 | ||
| 110 | |||
| 111 | |||
| 112 | extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size); | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | /* | ||
| 118 | * from git:object.h | ||
| 119 | */ | ||
| 120 | |||
| 121 | struct object_list { | ||
| 122 | struct object *item; | ||
| 123 | struct object_list *next; | ||
| 124 | }; | ||
| 125 | |||
| 126 | struct object_refs { | ||
| 127 | unsigned count; | ||
| 128 | struct object *base; | ||
| 129 | struct object *ref[FLEX_ARRAY]; /* more */ | ||
| 130 | }; | ||
| 131 | |||
| 132 | struct object_array { | ||
| 133 | unsigned int nr; | ||
| 134 | unsigned int alloc; | ||
| 135 | struct object_array_entry { | ||
| 136 | struct object *item; | ||
| 137 | const char *name; | ||
| 138 | } *objects; | ||
| 139 | }; | ||
| 140 | |||
| 141 | #define TYPE_BITS 3 | ||
| 142 | #define FLAG_BITS 27 | ||
| 143 | |||
| 144 | /* | ||
| 145 | * The object type is stored in 3 bits. | ||
| 146 | */ | ||
| 147 | struct object { | ||
| 148 | unsigned parsed : 1; | ||
| 149 | unsigned used : 1; | ||
| 150 | unsigned type : TYPE_BITS; | ||
| 151 | unsigned flags : FLAG_BITS; | ||
| 152 | unsigned char sha1[20]; | ||
| 153 | }; | ||
| 154 | |||
| 155 | |||
| 156 | /* | ||
| 157 | * from git:tree.h | ||
| 158 | */ | ||
| 159 | |||
| 160 | struct tree { | ||
| 161 | struct object object; | ||
| 162 | void *buffer; | ||
| 163 | unsigned long size; | ||
| 164 | }; | ||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | /* from git:commit.h */ | ||
| 170 | |||
| 171 | struct commit_list { | ||
| 172 | struct commit *item; | ||
| 173 | struct commit_list *next; | ||
| 174 | }; | ||
| 175 | |||
| 176 | struct commit { | ||
| 177 | struct object object; | ||
| 178 | void *util; | ||
| 179 | unsigned long date; | ||
| 180 | struct commit_list *parents; | ||
| 181 | struct tree *tree; | ||
| 182 | char *buffer; | ||
| 183 | }; | ||
| 184 | |||
| 185 | |||
| 186 | /* Commit formats */ | ||
| 187 | enum cmit_fmt { | ||
| 188 | CMIT_FMT_RAW, | ||
| 189 | CMIT_FMT_MEDIUM, | ||
| 190 | CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM, | ||
| 191 | CMIT_FMT_SHORT, | ||
| 192 | CMIT_FMT_FULL, | ||
| 193 | CMIT_FMT_FULLER, | ||
| 194 | CMIT_FMT_ONELINE, | ||
| 195 | CMIT_FMT_EMAIL, | ||
| 196 | |||
| 197 | CMIT_FMT_UNSPECIFIED, | ||
| 198 | }; | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | struct commit *lookup_commit(const unsigned char *sha1); | ||
| 203 | struct commit *lookup_commit_reference(const unsigned char *sha1); | ||
| 204 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, | ||
| 205 | int quiet); | ||
| 206 | |||
| 207 | typedef void (*topo_sort_set_fn_t)(struct commit*, void *data); | ||
| 208 | typedef void* (*topo_sort_get_fn_t)(struct commit*); | ||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | /* | ||
| 214 | * from git:diff.h | ||
| 215 | */ | ||
| 216 | |||
| 217 | |||
| 218 | struct rev_info; | ||
| 219 | struct diff_options; | ||
| 220 | struct diff_queue_struct; | ||
| 221 | |||
| 222 | typedef void (*change_fn_t)(struct diff_options *options, | ||
| 223 | unsigned old_mode, unsigned new_mode, | ||
| 224 | const unsigned char *old_sha1, | ||
| 225 | const unsigned char *new_sha1, | ||
| 226 | const char *base, const char *path); | ||
| 227 | |||
| 228 | typedef void (*add_remove_fn_t)(struct diff_options *options, | ||
| 229 | int addremove, unsigned mode, | ||
| 230 | const unsigned char *sha1, | ||
| 231 | const char *base, const char *path); | ||
| 232 | |||
| 233 | typedef void (*diff_format_fn_t)(struct diff_queue_struct *q, | ||
| 234 | struct diff_options *options, void *data); | ||
| 235 | |||
| 236 | #define DIFF_FORMAT_RAW 0x0001 | ||
| 237 | #define DIFF_FORMAT_DIFFSTAT 0x0002 | ||
| 238 | #define DIFF_FORMAT_NUMSTAT 0x0004 | ||
| 239 | #define DIFF_FORMAT_SUMMARY 0x0008 | ||
| 240 | #define DIFF_FORMAT_PATCH 0x0010 | ||
| 241 | |||
| 242 | /* These override all above */ | ||
| 243 | #define DIFF_FORMAT_NAME 0x0100 | ||
| 244 | #define DIFF_FORMAT_NAME_STATUS 0x0200 | ||
| 245 | #define DIFF_FORMAT_CHECKDIFF 0x0400 | ||
| 246 | |||
| 247 | /* Same as output_format = 0 but we know that -s flag was given | ||
| 248 | * and we should not give default value to output_format. | ||
| 249 | */ | ||
| 250 | #define DIFF_FORMAT_NO_OUTPUT 0x0800 | ||
| 251 | |||
| 252 | #define DIFF_FORMAT_CALLBACK 0x1000 | ||
| 253 | |||
| 254 | struct diff_options { | ||
| 255 | const char *filter; | ||
| 256 | const char *orderfile; | ||
| 257 | const char *pickaxe; | ||
| 258 | const char *single_follow; | ||
| 259 | unsigned recursive:1, | ||
| 260 | tree_in_recursive:1, | ||
| 261 | binary:1, | ||
| 262 | text:1, | ||
| 263 | full_index:1, | ||
| 264 | silent_on_remove:1, | ||
| 265 | find_copies_harder:1, | ||
| 266 | color_diff:1, | ||
| 267 | color_diff_words:1; | ||
| 268 | int context; | ||
| 269 | int break_opt; | ||
| 270 | int detect_rename; | ||
| 271 | int line_termination; | ||
| 272 | int output_format; | ||
| 273 | int pickaxe_opts; | ||
| 274 | int rename_score; | ||
| 275 | int reverse_diff; | ||
| 276 | int rename_limit; | ||
| 277 | int setup; | ||
| 278 | int abbrev; | ||
| 279 | const char *msg_sep; | ||
| 280 | const char *stat_sep; | ||
| 281 | long xdl_opts; | ||
| 282 | |||
| 283 | int stat_width; | ||
| 284 | int stat_name_width; | ||
| 285 | |||
| 286 | int nr_paths; | ||
| 287 | const char **paths; | ||
| 288 | int *pathlens; | ||
| 289 | change_fn_t change; | ||
| 290 | add_remove_fn_t add_remove; | ||
| 291 | diff_format_fn_t format_callback; | ||
| 292 | void *format_callback_data; | ||
| 293 | }; | ||
| 294 | |||
| 295 | enum color_diff { | ||
| 296 | DIFF_RESET = 0, | ||
| 297 | DIFF_PLAIN = 1, | ||
| 298 | DIFF_METAINFO = 2, | ||
| 299 | DIFF_FRAGINFO = 3, | ||
| 300 | DIFF_FILE_OLD = 4, | ||
| 301 | DIFF_FILE_NEW = 5, | ||
| 302 | DIFF_COMMIT = 6, | ||
| 303 | DIFF_WHITESPACE = 7, | ||
| 304 | }; | ||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | /* | ||
| 313 | * from git:revision.h | ||
| 314 | */ | ||
| 315 | |||
| 316 | struct rev_info; | ||
| 317 | struct log_info; | ||
| 318 | |||
| 319 | typedef void (prune_fn_t)(struct rev_info *revs, struct commit *commit); | ||
| 320 | |||
| 321 | struct rev_info { | ||
| 322 | /* Starting list */ | ||
| 323 | struct commit_list *commits; | ||
| 324 | struct object_array pending; | ||
| 325 | |||
| 326 | /* Basic information */ | ||
| 327 | const char *prefix; | ||
| 328 | void *prune_data; | ||
| 329 | prune_fn_t *prune_fn; | ||
| 330 | |||
| 331 | /* Traversal flags */ | ||
| 332 | unsigned int dense:1, | ||
| 333 | no_merges:1, | ||
| 334 | no_walk:1, | ||
| 335 | remove_empty_trees:1, | ||
| 336 | simplify_history:1, | ||
| 337 | lifo:1, | ||
| 338 | topo_order:1, | ||
| 339 | tag_objects:1, | ||
| 340 | tree_objects:1, | ||
| 341 | blob_objects:1, | ||
| 342 | edge_hint:1, | ||
| 343 | limited:1, | ||
| 344 | unpacked:1, /* see also ignore_packed below */ | ||
| 345 | boundary:1, | ||
| 346 | parents:1; | ||
| 347 | |||
| 348 | /* Diff flags */ | ||
| 349 | unsigned int diff:1, | ||
| 350 | full_diff:1, | ||
| 351 | show_root_diff:1, | ||
| 352 | no_commit_id:1, | ||
| 353 | verbose_header:1, | ||
| 354 | ignore_merges:1, | ||
| 355 | combine_merges:1, | ||
| 356 | dense_combined_merges:1, | ||
| 357 | always_show_header:1; | ||
| 358 | |||
| 359 | /* Format info */ | ||
| 360 | unsigned int shown_one:1, | ||
| 361 | abbrev_commit:1, | ||
| 362 | relative_date:1; | ||
| 363 | |||
| 364 | const char **ignore_packed; /* pretend objects in these are unpacked */ | ||
| 365 | int num_ignore_packed; | ||
| 366 | |||
| 367 | unsigned int abbrev; | ||
| 368 | enum cmit_fmt commit_format; | ||
| 369 | struct log_info *loginfo; | ||
| 370 | int nr, total; | ||
| 371 | const char *mime_boundary; | ||
| 372 | const char *message_id; | ||
| 373 | const char *ref_message_id; | ||
| 374 | const char *add_signoff; | ||
| 375 | const char *extra_headers; | ||
| 376 | |||
| 377 | /* Filter by commit log message */ | ||
| 378 | struct grep_opt *grep_filter; | ||
| 379 | |||
| 380 | /* special limits */ | ||
| 381 | int max_count; | ||
| 382 | unsigned long max_age; | ||
| 383 | unsigned long min_age; | ||
| 384 | |||
| 385 | /* diff info for patches and for paths limiting */ | ||
| 386 | struct diff_options diffopt; | ||
| 387 | struct diff_options pruning; | ||
| 388 | |||
| 389 | topo_sort_set_fn_t topo_setter; | ||
| 390 | topo_sort_get_fn_t topo_getter; | ||
| 391 | }; | ||
| 392 | |||
| 393 | |||
| 394 | extern struct commit *get_revision(struct rev_info *revs); | ||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | |||
| 399 | #endif /* GIT_H */ | ||
| @@ -0,0 +1,100 @@ | |||
| 1 | #include "cgit.h" | ||
| 2 | |||
| 3 | char *fmt(const char *format, ...) | ||
| 4 | { | ||
| 5 | static char buf[8][1024]; | ||
| 6 | static int bufidx; | ||
| 7 | int len; | ||
| 8 | va_list args; | ||
| 9 | |||
| 10 | bufidx++; | ||
| 11 | bufidx &= 7; | ||
| 12 | |||
| 13 | va_start(args, format); | ||
| 14 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); | ||
| 15 | va_end(args); | ||
| 16 | if (len>sizeof(buf[bufidx])) | ||
| 17 | die("[html.c] string truncated: %s", format); | ||
| 18 | return buf[bufidx]; | ||
| 19 | } | ||
| 20 | |||
| 21 | void html(const char *txt) | ||
| 22 | { | ||
| 23 | fputs(txt, stdout); | ||
| 24 | } | ||
| 25 | |||
| 26 | void htmlf(const char *format, ...) | ||
| 27 | { | ||
| 28 | va_list args; | ||
| 29 | |||
| 30 | va_start(args, format); | ||
| 31 | vprintf(format, args); | ||
| 32 | va_end(args); | ||
| 33 | } | ||
| 34 | |||
| 35 | void html_txt(char *txt) | ||
| 36 | { | ||
| 37 | char *t = txt; | ||
| 38 | while(*t){ | ||
| 39 | int c = *t; | ||
| 40 | if (c=='<' || c=='>' || c=='&') { | ||
| 41 | *t = '\0'; | ||
| 42 | html(txt); | ||
| 43 | *t = c; | ||
| 44 | if (c=='>') | ||
| 45 | html(">"); | ||
| 46 | else if (c=='<') | ||
| 47 | html("<"); | ||
| 48 | else if (c=='&') | ||
| 49 | html("&"); | ||
| 50 | txt = t+1; | ||
| 51 | } | ||
| 52 | t++; | ||
| 53 | } | ||
| 54 | if (t!=txt) | ||
| 55 | html(txt); | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | void html_attr(char *txt) | ||
| 60 | { | ||
| 61 | char *t = txt; | ||
| 62 | while(*t){ | ||
| 63 | int c = *t; | ||
| 64 | if (c=='<' || c=='>' || c=='\'') { | ||
| 65 | *t = '\0'; | ||
| 66 | html(txt); | ||
| 67 | *t = c; | ||
| 68 | if (c=='>') | ||
| 69 | html(">"); | ||
| 70 | else if (c=='<') | ||
| 71 | html("<"); | ||
| 72 | else if (c=='\'') | ||
| 73 | html(""e;"); | ||
| 74 | txt = t+1; | ||
| 75 | } | ||
| 76 | t++; | ||
| 77 | } | ||
| 78 | if (t!=txt) | ||
| 79 | html(txt); | ||
| 80 | } | ||
| 81 | |||
| 82 | void html_link_open(char *url, char *title, char *class) | ||
| 83 | { | ||
| 84 | html("<a href='"); | ||
| 85 | html_attr(url); | ||
| 86 | if (title) { | ||
| 87 | html("' title='"); | ||
| 88 | html_attr(title); | ||
| 89 | } | ||
| 90 | if (class) { | ||
| 91 | html("' class='"); | ||
| 92 | html_attr(class); | ||
| 93 | } | ||
| 94 | html("'>"); | ||
| 95 | } | ||
| 96 | |||
| 97 | void html_link_close(void) | ||
| 98 | { | ||
| 99 | html("</a>"); | ||
| 100 | } | ||
