stagit.c (36628B)
1 #include <sys/stat.h> 2 #include <sys/types.h> 3 4 #include <err.h> 5 #include <errno.h> 6 #include <libgen.h> 7 #include <limits.h> 8 #include <stdint.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <time.h> 13 #include <unistd.h> 14 15 #include <git2.h> 16 17 #include "compat.h" 18 19 #define LEN(s) (sizeof(s)/sizeof(*s)) 20 21 struct deltainfo { 22 git_patch *patch; 23 24 size_t addcount; 25 size_t delcount; 26 }; 27 28 struct commitinfo { 29 const git_oid *id; 30 31 char oid[GIT_OID_HEXSZ + 1]; 32 char parentoid[GIT_OID_HEXSZ + 1]; 33 34 const git_signature *author; 35 const git_signature *committer; 36 const char *summary; 37 const char *msg; 38 39 git_diff *diff; 40 git_commit *commit; 41 git_commit *parent; 42 git_tree *commit_tree; 43 git_tree *parent_tree; 44 45 size_t addcount; 46 size_t delcount; 47 size_t filecount; 48 49 struct deltainfo **deltas; 50 size_t ndeltas; 51 }; 52 53 /* reference and associated data for sorting */ 54 struct referenceinfo { 55 struct git_reference *ref; 56 struct commitinfo *ci; 57 }; 58 59 static git_repository *repo; 60 61 static const char *baseurl = ""; /* base URL to make absolute RSS/Atom URI */ 62 static const char *relpath = ""; 63 static const char *repodir; 64 65 static char *name = ""; 66 static char *strippedname = ""; 67 static char description[255]; 68 static char cloneurl[1024]; 69 static char *submodules; 70 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:LICENSE.txt", "HEAD:COPYING" }; 71 static char *license; 72 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md", "HEAD:readme.md", "HEAD:README.org", "HEAD:readme.org" }; 73 static char *readme; 74 static long long nlogcommits = -1; /* -1 indicates not used */ 75 76 static long long nlogsummary = 80; 77 78 /* cache */ 79 static git_oid lastoid; 80 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */ 81 static FILE *rcachefp, *wcachefp; 82 static const char *cachefile; 83 84 /* Handle read or write errors for a FILE * stream */ 85 void 86 checkfileerror(FILE *fp, const char *name, int mode) 87 { 88 if (mode == 'r' && ferror(fp)) 89 errx(1, "read error: %s", name); 90 else if (mode == 'w' && (fflush(fp) || ferror(fp))) 91 errx(1, "write error: %s", name); 92 } 93 94 void 95 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2) 96 { 97 int r; 98 99 r = snprintf(buf, bufsiz, "%s%s%s", 100 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2); 101 if (r < 0 || (size_t)r >= bufsiz) 102 errx(1, "path truncated: '%s%s%s'", 103 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2); 104 } 105 106 void 107 deltainfo_free(struct deltainfo *di) 108 { 109 if (!di) 110 return; 111 git_patch_free(di->patch); 112 memset(di, 0, sizeof(*di)); 113 free(di); 114 } 115 116 int 117 commitinfo_getstats(struct commitinfo *ci) 118 { 119 struct deltainfo *di; 120 git_diff_options opts; 121 git_diff_find_options fopts; 122 const git_diff_delta *delta; 123 const git_diff_hunk *hunk; 124 const git_diff_line *line; 125 git_patch *patch = NULL; 126 size_t ndeltas, nhunks, nhunklines; 127 size_t i, j, k; 128 129 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit))) 130 goto err; 131 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) { 132 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) { 133 ci->parent = NULL; 134 ci->parent_tree = NULL; 135 } 136 } 137 138 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION); 139 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH | 140 GIT_DIFF_IGNORE_SUBMODULES | 141 GIT_DIFF_INCLUDE_TYPECHANGE; 142 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)) 143 goto err; 144 145 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION)) 146 goto err; 147 /* find renames and copies, exact matches (no heuristic) for renames. */ 148 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES | 149 GIT_DIFF_FIND_EXACT_MATCH_ONLY; 150 if (git_diff_find_similar(ci->diff, &fopts)) 151 goto err; 152 153 ndeltas = git_diff_num_deltas(ci->diff); 154 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *)))) 155 err(1, "calloc"); 156 157 for (i = 0; i < ndeltas; i++) { 158 if (git_patch_from_diff(&patch, ci->diff, i)) 159 goto err; 160 161 if (!(di = calloc(1, sizeof(struct deltainfo)))) 162 err(1, "calloc"); 163 di->patch = patch; 164 ci->deltas[i] = di; 165 166 delta = git_patch_get_delta(patch); 167 168 /* skip stats for binary data */ 169 if (delta->flags & GIT_DIFF_FLAG_BINARY) 170 continue; 171 172 nhunks = git_patch_num_hunks(patch); 173 for (j = 0; j < nhunks; j++) { 174 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j)) 175 break; 176 for (k = 0; ; k++) { 177 if (git_patch_get_line_in_hunk(&line, patch, j, k)) 178 break; 179 if (line->old_lineno == -1) { 180 di->addcount++; 181 ci->addcount++; 182 } else if (line->new_lineno == -1) { 183 di->delcount++; 184 ci->delcount++; 185 } 186 } 187 } 188 } 189 ci->ndeltas = i; 190 ci->filecount = i; 191 192 return 0; 193 194 err: 195 git_diff_free(ci->diff); 196 ci->diff = NULL; 197 git_tree_free(ci->commit_tree); 198 ci->commit_tree = NULL; 199 git_tree_free(ci->parent_tree); 200 ci->parent_tree = NULL; 201 git_commit_free(ci->parent); 202 ci->parent = NULL; 203 204 if (ci->deltas) 205 for (i = 0; i < ci->ndeltas; i++) 206 deltainfo_free(ci->deltas[i]); 207 free(ci->deltas); 208 ci->deltas = NULL; 209 ci->ndeltas = 0; 210 ci->addcount = 0; 211 ci->delcount = 0; 212 ci->filecount = 0; 213 214 return -1; 215 } 216 217 void 218 commitinfo_free(struct commitinfo *ci) 219 { 220 size_t i; 221 222 if (!ci) 223 return; 224 if (ci->deltas) 225 for (i = 0; i < ci->ndeltas; i++) 226 deltainfo_free(ci->deltas[i]); 227 228 free(ci->deltas); 229 git_diff_free(ci->diff); 230 git_tree_free(ci->commit_tree); 231 git_tree_free(ci->parent_tree); 232 git_commit_free(ci->commit); 233 git_commit_free(ci->parent); 234 memset(ci, 0, sizeof(*ci)); 235 free(ci); 236 } 237 238 struct commitinfo * 239 commitinfo_getbyoid(const git_oid *id) 240 { 241 struct commitinfo *ci; 242 243 if (!(ci = calloc(1, sizeof(struct commitinfo)))) 244 err(1, "calloc"); 245 246 if (git_commit_lookup(&(ci->commit), repo, id)) 247 goto err; 248 ci->id = id; 249 250 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit)); 251 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0)); 252 253 ci->author = git_commit_author(ci->commit); 254 ci->committer = git_commit_committer(ci->commit); 255 ci->summary = git_commit_summary(ci->commit); 256 ci->msg = git_commit_message(ci->commit); 257 258 return ci; 259 260 err: 261 commitinfo_free(ci); 262 263 return NULL; 264 } 265 266 int 267 refs_cmp(const void *v1, const void *v2) 268 { 269 const struct referenceinfo *r1 = v1, *r2 = v2; 270 time_t t1, t2; 271 int r; 272 273 if ((r = git_reference_is_tag(r1->ref) - git_reference_is_tag(r2->ref))) 274 return r; 275 276 t1 = r1->ci->author ? r1->ci->author->when.time : 0; 277 t2 = r2->ci->author ? r2->ci->author->when.time : 0; 278 if ((r = t1 > t2 ? -1 : (t1 == t2 ? 0 : 1))) 279 return r; 280 281 return strcmp(git_reference_shorthand(r1->ref), 282 git_reference_shorthand(r2->ref)); 283 } 284 285 int 286 getrefs(struct referenceinfo **pris, size_t *prefcount) 287 { 288 struct referenceinfo *ris = NULL; 289 struct commitinfo *ci = NULL; 290 git_reference_iterator *it = NULL; 291 const git_oid *id = NULL; 292 git_object *obj = NULL; 293 git_reference *dref = NULL, *r, *ref = NULL; 294 size_t i, refcount; 295 296 *pris = NULL; 297 *prefcount = 0; 298 299 if (git_reference_iterator_new(&it, repo)) 300 return -1; 301 302 for (refcount = 0; !git_reference_next(&ref, it); ) { 303 if (!git_reference_is_branch(ref) && !git_reference_is_tag(ref)) { 304 git_reference_free(ref); 305 ref = NULL; 306 continue; 307 } 308 309 switch (git_reference_type(ref)) { 310 case GIT_REF_SYMBOLIC: 311 if (git_reference_resolve(&dref, ref)) 312 goto err; 313 r = dref; 314 break; 315 case GIT_REF_OID: 316 r = ref; 317 break; 318 default: 319 continue; 320 } 321 if (!git_reference_target(r) || 322 git_reference_peel(&obj, r, GIT_OBJ_ANY)) 323 goto err; 324 if (!(id = git_object_id(obj))) 325 goto err; 326 if (!(ci = commitinfo_getbyoid(id))) 327 break; 328 329 if (!(ris = reallocarray(ris, refcount + 1, sizeof(*ris)))) 330 err(1, "realloc"); 331 ris[refcount].ci = ci; 332 ris[refcount].ref = r; 333 refcount++; 334 335 git_object_free(obj); 336 obj = NULL; 337 git_reference_free(dref); 338 dref = NULL; 339 } 340 git_reference_iterator_free(it); 341 342 /* sort by type, date then shorthand name */ 343 qsort(ris, refcount, sizeof(*ris), refs_cmp); 344 345 *pris = ris; 346 *prefcount = refcount; 347 348 return 0; 349 350 err: 351 git_object_free(obj); 352 git_reference_free(dref); 353 commitinfo_free(ci); 354 for (i = 0; i < refcount; i++) { 355 commitinfo_free(ris[i].ci); 356 git_reference_free(ris[i].ref); 357 } 358 free(ris); 359 360 return -1; 361 } 362 363 FILE * 364 efopen(const char *filename, const char *flags) 365 { 366 FILE *fp; 367 368 if (!(fp = fopen(filename, flags))) 369 err(1, "fopen: '%s'", filename); 370 371 return fp; 372 } 373 374 /* Percent-encode, see RFC3986 section 2.1. */ 375 void 376 percentencode(FILE *fp, const char *s, size_t len) 377 { 378 static char tab[] = "0123456789ABCDEF"; 379 unsigned char uc; 380 size_t i; 381 382 for (i = 0; *s && i < len; s++, i++) { 383 uc = *s; 384 /* NOTE: do not encode '/' for paths or ",-." */ 385 if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') || 386 uc == '[' || uc == ']') { 387 putc('%', fp); 388 putc(tab[(uc >> 4) & 0x0f], fp); 389 putc(tab[uc & 0x0f], fp); 390 } else { 391 putc(uc, fp); 392 } 393 } 394 } 395 396 /* Escape characters below as HTML 2.0 / XML 1.0. */ 397 void 398 xmlencode(FILE *fp, const char *s, size_t len) 399 { 400 size_t i; 401 402 for (i = 0; *s && i < len; s++, i++) { 403 switch(*s) { 404 case '<': fputs("&lt;", fp); break; 405 case '>': fputs("&gt;", fp); break; 406 case '\'': fputs("&#39;", fp); break; 407 case '&': fputs("&amp;", fp); break; 408 case '"': fputs("&quot;", fp); break; 409 default: putc(*s, fp); 410 } 411 } 412 } 413 414 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\r', '\n' */ 415 void 416 xmlencodeline(FILE *fp, const char *s, size_t len) 417 { 418 size_t i; 419 420 for (i = 0; *s && i < len; s++, i++) { 421 switch(*s) { 422 case '<': fputs("&lt;", fp); break; 423 case '>': fputs("&gt;", fp); break; 424 case '\'': fputs("&#39;", fp); break; 425 case '&': fputs("&amp;", fp); break; 426 case '"': fputs("&quot;", fp); break; 427 case '\r': break; /* ignore CR */ 428 case '\n': break; /* ignore LF */ 429 default: putc(*s, fp); 430 } 431 } 432 } 433 434 int 435 mkdirp(const char *path) 436 { 437 char tmp[PATH_MAX], *p; 438 439 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp)) 440 errx(1, "path truncated: '%s'", path); 441 for (p = tmp + (tmp[0] == '/'); *p; p++) { 442 if (*p != '/') 443 continue; 444 *p = '\0'; 445 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) 446 return -1; 447 *p = '/'; 448 } 449 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) 450 return -1; 451 return 0; 452 } 453 454 void 455 printtimez(FILE *fp, const git_time *intime) 456 { 457 struct tm *intm; 458 time_t t; 459 char out[32]; 460 461 t = (time_t)intime->time; 462 if (!(intm = gmtime(&t))) 463 return; 464 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm); 465 fputs(out, fp); 466 } 467 468 void 469 printtime(FILE *fp, const git_time *intime) 470 { 471 struct tm *intm; 472 time_t t; 473 char out[32]; 474 475 t = (time_t)intime->time + (intime->offset * 60); 476 if (!(intm = gmtime(&t))) 477 return; 478 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm); 479 if (intime->offset < 0) 480 fprintf(fp, "%s -%02d%02d", out, 481 -(intime->offset) / 60, -(intime->offset) % 60); 482 else 483 fprintf(fp, "%s +%02d%02d", out, 484 intime->offset / 60, intime->offset % 60); 485 } 486 487 void 488 printtimeshort(FILE *fp, const git_time *intime) 489 { 490 struct tm *intm; 491 time_t t; 492 char out[32]; 493 494 t = (time_t)intime->time; 495 if (!(intm = gmtime(&t))) 496 return; 497 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm); 498 fputs(out, fp); 499 } 500 501 void 502 writeheader(FILE *fp, const char *title) 503 { 504 fputs("<!DOCTYPE html>\n" 505 "<html>\n<head>\n" 506 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" 507 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" 508 "<title>", fp); 509 xmlencode(fp, title, strlen(title)); 510 if (title[0] && strippedname[0]) 511 fputs(" - ", fp); 512 xmlencode(fp, strippedname, strlen(strippedname)); 513 if (description[0]) 514 fputs(" - ", fp); 515 xmlencode(fp, description, strlen(description)); 516 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath); 517 fputs("<link rel=\"alternate\" type=\"application/atom+xml\" title=\"", fp); 518 xmlencode(fp, name, strlen(name)); 519 fprintf(fp, " Atom Feed\" href=\"%satom.xml\" />\n", relpath); 520 fputs("<link rel=\"alternate\" type=\"application/atom+xml\" title=\"", fp); 521 xmlencode(fp, name, strlen(name)); 522 fprintf(fp, " Atom Feed (tags)\" href=\"%stags.xml\" />\n", relpath); 523 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath); 524 fputs("</head>\n<body>\n<table><tr><td>", fp); 525 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>", 526 relpath, relpath); 527 fputs("</td><td><h1>", fp); 528 xmlencode(fp, strippedname, strlen(strippedname)); 529 fputs("</h1><span class=\"desc\">", fp); 530 xmlencode(fp, description, strlen(description)); 531 fputs("</span></td></tr>", fp); 532 if (cloneurl[0]) { 533 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp); 534 xmlencode(fp, cloneurl, strlen(cloneurl)); /* not percent-encoded */ 535 fputs("\">", fp); 536 xmlencode(fp, cloneurl, strlen(cloneurl)); 537 fputs("</a></td></tr>", fp); 538 } 539 fputs("<tr><td></td><td>\n", fp); 540 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath); 541 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath); 542 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath); 543 if (submodules) 544 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>", 545 relpath, submodules); 546 if (readme) 547 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>", 548 relpath, readme); 549 if (license) 550 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>", 551 relpath, license); 552 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp); 553 } 554 555 void 556 writefooter(FILE *fp) 557 { 558 fputs("</div>\n</body>\n</html>\n", fp); 559 } 560 561 size_t 562 writeblobhtml(FILE *fp, const git_blob *blob) 563 { 564 size_t n = 0, i, len, prev; 565 const char *nfmt = "<a href=\"#l%zu\" class=\"line\" id=\"l%zu\">%7zu</a> "; 566 const char *s = git_blob_rawcontent(blob); 567 568 len = git_blob_rawsize(blob); 569 fputs("<pre id=\"blob\">\n", fp); 570 571 if (len > 0) { 572 for (i = 0, prev = 0; i < len; i++) { 573 if (s[i] != '\n') 574 continue; 575 n++; 576 fprintf(fp, nfmt, n, n, n); 577 xmlencodeline(fp, &s[prev], i - prev + 1); 578 putc('\n', fp); 579 prev = i + 1; 580 } 581 /* trailing data */ 582 if ((len - prev) > 0) { 583 n++; 584 fprintf(fp, nfmt, n, n, n); 585 xmlencodeline(fp, &s[prev], len - prev); 586 } 587 } 588 589 fputs("</pre>\n", fp); 590 591 return n; 592 } 593 594 void 595 printcommit(FILE *fp, struct commitinfo *ci) 596 { 597 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n", 598 relpath, ci->oid, ci->oid); 599 600 if (ci->parentoid[0]) 601 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n", 602 relpath, ci->parentoid, ci->parentoid); 603 604 if (ci->author) { 605 fputs("<b>Author:</b> ", fp); 606 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 607 fputs(" &lt;<a href=\"mailto:", fp); 608 xmlencode(fp, ci->author->email, strlen(ci->author->email)); /* not percent-encoded */ 609 fputs("\">", fp); 610 xmlencode(fp, ci->author->email, strlen(ci->author->email)); 611 fputs("</a>&gt;\n<b>Date:</b> ", fp); 612 printtime(fp, &(ci->author->when)); 613 putc('\n', fp); 614 } 615 if (ci->msg) { 616 putc('\n', fp); 617 xmlencode(fp, ci->msg, strlen(ci->msg)); 618 putc('\n', fp); 619 } 620 } 621 622 void 623 printshowfile(FILE *fp, struct commitinfo *ci) 624 { 625 const git_diff_delta *delta; 626 const git_diff_hunk *hunk; 627 const git_diff_line *line; 628 git_patch *patch; 629 size_t nhunks, nhunklines, changed, add, del, total, i, j, k; 630 char linestr[80]; 631 int c; 632 633 printcommit(fp, ci); 634 635 if (!ci->deltas) 636 return; 637 638 if (ci->filecount > 1000 || 639 ci->ndeltas > 1000 || 640 ci->addcount > 100000 || 641 ci->delcount > 100000) { 642 fputs("Diff is too large, output suppressed.\n", fp); 643 return; 644 } 645 646 /* diff stat */ 647 fputs("<b>Diffstat:</b>\n<table>", fp); 648 for (i = 0; i < ci->ndeltas; i++) { 649 delta = git_patch_get_delta(ci->deltas[i]->patch); 650 651 switch (delta->status) { 652 case GIT_DELTA_ADDED: c = 'A'; break; 653 case GIT_DELTA_COPIED: c = 'C'; break; 654 case GIT_DELTA_DELETED: c = 'D'; break; 655 case GIT_DELTA_MODIFIED: c = 'M'; break; 656 case GIT_DELTA_RENAMED: c = 'R'; break; 657 case GIT_DELTA_TYPECHANGE: c = 'T'; break; 658 default: c = ' '; break; 659 } 660 if (c == ' ') 661 fprintf(fp, "<tr><td>%c", c); 662 else 663 fprintf(fp, "<tr><td class=\"%c\">%c", c, c); 664 665 fprintf(fp, "</td><td><a href=\"#h%zu\">", i); 666 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path)); 667 if (strcmp(delta->old_file.path, delta->new_file.path)) { 668 fputs(" -&gt; ", fp); 669 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path)); 670 } 671 672 add = ci->deltas[i]->addcount; 673 del = ci->deltas[i]->delcount; 674 changed = add + del; 675 total = sizeof(linestr) - 2; 676 if (changed > total) { 677 if (add) 678 add = ((float)total / changed * add) + 1; 679 if (del) 680 del = ((float)total / changed * del) + 1; 681 } 682 memset(&linestr, '+', add); 683 memset(&linestr[add], '-', del); 684 685 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">", 686 ci->deltas[i]->addcount + ci->deltas[i]->delcount); 687 fwrite(&linestr, 1, add, fp); 688 fputs("</span><span class=\"d\">", fp); 689 fwrite(&linestr[add], 1, del, fp); 690 fputs("</span></td></tr>\n", fp); 691 } 692 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n", 693 ci->filecount, ci->filecount == 1 ? "" : "s", 694 ci->addcount, ci->addcount == 1 ? "" : "s", 695 ci->delcount, ci->delcount == 1 ? "" : "s"); 696 697 fputs("<hr/>", fp); 698 699 for (i = 0; i < ci->ndeltas; i++) { 700 patch = ci->deltas[i]->patch; 701 delta = git_patch_get_delta(patch); 702 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath); 703 percentencode(fp, delta->old_file.path, strlen(delta->old_file.path)); 704 fputs(".html\">", fp); 705 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path)); 706 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath); 707 percentencode(fp, delta->new_file.path, strlen(delta->new_file.path)); 708 fprintf(fp, ".html\">"); 709 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path)); 710 fprintf(fp, "</a></b>\n"); 711 712 /* check binary data */ 713 if (delta->flags & GIT_DIFF_FLAG_BINARY) { 714 fputs("Binary files differ.\n", fp); 715 continue; 716 } 717 718 nhunks = git_patch_num_hunks(patch); 719 for (j = 0; j < nhunks; j++) { 720 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j)) 721 break; 722 723 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j); 724 xmlencode(fp, hunk->header, hunk->header_len); 725 fputs("</a>", fp); 726 727 for (k = 0; ; k++) { 728 if (git_patch_get_line_in_hunk(&line, patch, j, k)) 729 break; 730 if (line->old_lineno == -1) 731 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+", 732 i, j, k, i, j, k); 733 else if (line->new_lineno == -1) 734 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-", 735 i, j, k, i, j, k); 736 else 737 putc(' ', fp); 738 xmlencodeline(fp, line->content, line->content_len); 739 putc('\n', fp); 740 if (line->old_lineno == -1 || line->new_lineno == -1) 741 fputs("</a>", fp); 742 } 743 } 744 } 745 } 746 747 void 748 writelogline(FILE *fp, struct commitinfo *ci) 749 { 750 size_t len; 751 char *copy; 752 fputs("<tr><td>", fp); 753 if (ci->author) 754 printtimeshort(fp, &(ci->author->when)); 755 fputs("</td><td>", fp); 756 if (ci->summary) { 757 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid); 758 len = strlen(ci->summary); 759 if (len > nlogsummary) { 760 copy = strdup(ci->summary); 761 copy[nlogsummary] = '\0'; 762 memset(copy + nlogsummary - 3, '.', 3); 763 xmlencode(fp, copy, strlen(ci->summary)); 764 free(copy); 765 } else { 766 xmlencode(fp, ci->summary, strlen(ci->summary)); 767 } 768 fputs("</a>", fp); 769 } 770 fputs("</td><td>", fp); 771 if (ci->author) 772 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 773 fputs("</td><td class=\"num\" align=\"right\">", fp); 774 fprintf(fp, "%zu", ci->filecount); 775 fputs("</td><td class=\"num\" align=\"right\">", fp); 776 fprintf(fp, "+%zu", ci->addcount); 777 fputs("</td><td class=\"num\" align=\"right\">", fp); 778 fprintf(fp, "-%zu", ci->delcount); 779 fputs("</td></tr>\n", fp); 780 } 781 782 int 783 writelog(FILE *fp, const git_oid *oid) 784 { 785 struct commitinfo *ci; 786 git_revwalk *w = NULL; 787 git_oid id; 788 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1]; 789 FILE *fpfile; 790 size_t remcommits = 0; 791 int r; 792 793 git_revwalk_new(&w, repo); 794 git_revwalk_push(w, oid); 795 796 while (!git_revwalk_next(&id, w)) { 797 relpath = ""; 798 799 if (cachefile && !memcmp(&id, &lastoid, sizeof(id))) 800 break; 801 802 git_oid_tostr(oidstr, sizeof(oidstr), &id); 803 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr); 804 if (r < 0 || (size_t)r >= sizeof(path)) 805 errx(1, "path truncated: 'commit/%s.html'", oidstr); 806 r = access(path, F_OK); 807 808 /* optimization: if there are no log lines to write and 809 the commit file already exists: skip the diffstat */ 810 if (!nlogcommits) { 811 remcommits++; 812 if (!r) 813 continue; 814 } 815 816 if (!(ci = commitinfo_getbyoid(&id))) 817 break; 818 /* diffstat: for stagit HTML required for the log.html line */ 819 if (commitinfo_getstats(ci) == -1) 820 goto err; 821 822 if (nlogcommits != 0) { 823 writelogline(fp, ci); 824 if (nlogcommits > 0) 825 nlogcommits--; 826 } 827 828 if (cachefile) 829 writelogline(wcachefp, ci); 830 831 /* check if file exists if so skip it */ 832 if (r) { 833 relpath = "../"; 834 fpfile = efopen(path, "w"); 835 writeheader(fpfile, ci->summary); 836 fputs("<pre>", fpfile); 837 printshowfile(fpfile, ci); 838 fputs("</pre>\n", fpfile); 839 writefooter(fpfile); 840 checkfileerror(fpfile, path, 'w'); 841 fclose(fpfile); 842 } 843 err: 844 commitinfo_free(ci); 845 } 846 git_revwalk_free(w); 847 848 if (nlogcommits == 0 && remcommits != 0) { 849 fprintf(fp, "<tr><td></td><td colspan=\"5\">" 850 "%zu more commits remaining, fetch the repository" 851 "</td></tr>\n", remcommits); 852 } 853 854 relpath = ""; 855 856 return 0; 857 } 858 859 void 860 printcommitatom(FILE *fp, struct commitinfo *ci, const char *tag) 861 { 862 fputs("<entry>\n", fp); 863 864 fprintf(fp, "<id>%s</id>\n", ci->oid); 865 if (ci->author) { 866 fputs("<published>", fp); 867 printtimez(fp, &(ci->author->when)); 868 fputs("</published>\n", fp); 869 } 870 if (ci->committer) { 871 fputs("<updated>", fp); 872 printtimez(fp, &(ci->committer->when)); 873 fputs("</updated>\n", fp); 874 } 875 if (ci->summary) { 876 fputs("<title>", fp); 877 if (tag && tag[0]) { 878 fputs("[", fp); 879 xmlencode(fp, tag, strlen(tag)); 880 fputs("] ", fp); 881 } 882 xmlencode(fp, ci->summary, strlen(ci->summary)); 883 fputs("</title>\n", fp); 884 } 885 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n", 886 baseurl, ci->oid); 887 888 if (ci->author) { 889 fputs("<author>\n<name>", fp); 890 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 891 fputs("</name>\n<email>", fp); 892 xmlencode(fp, ci->author->email, strlen(ci->author->email)); 893 fputs("</email>\n</author>\n", fp); 894 } 895 896 fputs("<content>", fp); 897 fprintf(fp, "commit %s\n", ci->oid); 898 if (ci->parentoid[0]) 899 fprintf(fp, "parent %s\n", ci->parentoid); 900 if (ci->author) { 901 fputs("Author: ", fp); 902 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 903 fputs(" &lt;", fp); 904 xmlencode(fp, ci->author->email, strlen(ci->author->email)); 905 fputs("&gt;\nDate: ", fp); 906 printtime(fp, &(ci->author->when)); 907 putc('\n', fp); 908 } 909 if (ci->msg) { 910 putc('\n', fp); 911 xmlencode(fp, ci->msg, strlen(ci->msg)); 912 } 913 fputs("\n</content>\n</entry>\n", fp); 914 } 915 916 int 917 writeatom(FILE *fp, int all) 918 { 919 struct referenceinfo *ris = NULL; 920 size_t refcount = 0; 921 struct commitinfo *ci; 922 git_revwalk *w = NULL; 923 git_oid id; 924 size_t i, m = 100; /* last 'm' commits */ 925 926 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 927 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp); 928 xmlencode(fp, strippedname, strlen(strippedname)); 929 fputs(", branch HEAD</title>\n<subtitle>", fp); 930 xmlencode(fp, description, strlen(description)); 931 fputs("</subtitle>\n", fp); 932 933 /* all commits or only tags? */ 934 if (all) { 935 git_revwalk_new(&w, repo); 936 git_revwalk_push_head(w); 937 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) { 938 if (!(ci = commitinfo_getbyoid(&id))) 939 break; 940 printcommitatom(fp, ci, ""); 941 commitinfo_free(ci); 942 } 943 git_revwalk_free(w); 944 } else if (getrefs(&ris, &refcount) != -1) { 945 /* references: tags */ 946 for (i = 0; i < refcount; i++) { 947 if (git_reference_is_tag(ris[i].ref)) 948 printcommitatom(fp, ris[i].ci, 949 git_reference_shorthand(ris[i].ref)); 950 951 commitinfo_free(ris[i].ci); 952 git_reference_free(ris[i].ref); 953 } 954 free(ris); 955 } 956 957 fputs("</feed>\n", fp); 958 959 return 0; 960 } 961 962 size_t 963 writeblob(git_object *obj, const char *fpath, const char *filename, size_t filesize) 964 { 965 char tmp[PATH_MAX] = "", *d; 966 const char *p; 967 size_t lc = 0; 968 FILE *fp; 969 970 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp)) 971 errx(1, "path truncated: '%s'", fpath); 972 if (!(d = dirname(tmp))) 973 err(1, "dirname"); 974 if (mkdirp(d)) 975 return -1; 976 977 for (p = fpath, tmp[0] = '\0'; *p; p++) { 978 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp)) 979 errx(1, "path truncated: '../%s'", tmp); 980 } 981 relpath = tmp; 982 983 fp = efopen(fpath, "w"); 984 writeheader(fp, filename); 985 fputs("<p> ", fp); 986 xmlencode(fp, filename, strlen(filename)); 987 fprintf(fp, " (%zuB)", filesize); 988 fputs("</p><hr/>", fp); 989 990 if (git_blob_is_binary((git_blob *)obj)) 991 fputs("<p>Binary file.</p>\n", fp); 992 else 993 lc = writeblobhtml(fp, (git_blob *)obj); 994 995 writefooter(fp); 996 checkfileerror(fp, fpath, 'w'); 997 fclose(fp); 998 999 relpath = ""; 1000 1001 return lc; 1002 } 1003 1004 const char * 1005 filemode(git_filemode_t m) 1006 { 1007 static char mode[11]; 1008 1009 memset(mode, '-', sizeof(mode) - 1); 1010 mode[10] = '\0'; 1011 1012 if (S_ISREG(m)) 1013 mode[0] = '-'; 1014 else if (S_ISBLK(m)) 1015 mode[0] = 'b'; 1016 else if (S_ISCHR(m)) 1017 mode[0] = 'c'; 1018 else if (S_ISDIR(m)) 1019 mode[0] = 'd'; 1020 else if (S_ISFIFO(m)) 1021 mode[0] = 'p'; 1022 else if (S_ISLNK(m)) 1023 mode[0] = 'l'; 1024 else if (S_ISSOCK(m)) 1025 mode[0] = 's'; 1026 else 1027 mode[0] = '?'; 1028 1029 if (m & S_IRUSR) mode[1] = 'r'; 1030 if (m & S_IWUSR) mode[2] = 'w'; 1031 if (m & S_IXUSR) mode[3] = 'x'; 1032 if (m & S_IRGRP) mode[4] = 'r'; 1033 if (m & S_IWGRP) mode[5] = 'w'; 1034 if (m & S_IXGRP) mode[6] = 'x'; 1035 if (m & S_IROTH) mode[7] = 'r'; 1036 if (m & S_IWOTH) mode[8] = 'w'; 1037 if (m & S_IXOTH) mode[9] = 'x'; 1038 1039 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S'; 1040 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S'; 1041 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T'; 1042 1043 return mode; 1044 } 1045 1046 int 1047 writefilestree(FILE *fp, git_tree *tree, const char *path) 1048 { 1049 const git_tree_entry *entry = NULL; 1050 git_object *obj = NULL; 1051 const char *entryname; 1052 char filepath[PATH_MAX], entrypath[PATH_MAX], oid[8]; 1053 size_t count, i, lc, filesize; 1054 int r, ret; 1055 1056 count = git_tree_entrycount(tree); 1057 for (i = 0; i < count; i++) { 1058 if (!(entry = git_tree_entry_byindex(tree, i)) || 1059 !(entryname = git_tree_entry_name(entry))) 1060 return -1; 1061 joinpath(entrypath, sizeof(entrypath), path, entryname); 1062 1063 r = snprintf(filepath, sizeof(filepath), "file/%s.html", 1064 entrypath); 1065 if (r < 0 || (size_t)r >= sizeof(filepath)) 1066 errx(1, "path truncated: 'file/%s.html'", entrypath); 1067 1068 if (!git_tree_entry_to_object(&obj, repo, entry)) { 1069 switch (git_object_type(obj)) { 1070 case GIT_OBJ_BLOB: 1071 break; 1072 case GIT_OBJ_TREE: 1073 /* NOTE: recurses */ 1074 ret = writefilestree(fp, (git_tree *)obj, 1075 entrypath); 1076 git_object_free(obj); 1077 if (ret) 1078 return ret; 1079 continue; 1080 default: 1081 git_object_free(obj); 1082 continue; 1083 } 1084 1085 filesize = git_blob_rawsize((git_blob *)obj); 1086 lc = writeblob(obj, filepath, entryname, filesize); 1087 1088 fputs("<tr><td>", fp); 1089 fputs(filemode(git_tree_entry_filemode(entry)), fp); 1090 fprintf(fp, "</td><td><a href=\"%s", relpath); 1091 percentencode(fp, filepath, strlen(filepath)); 1092 fputs("\">", fp); 1093 xmlencode(fp, entrypath, strlen(entrypath)); 1094 fputs("</a></td><td class=\"num\" align=\"right\">", fp); 1095 if (lc > 0) 1096 fprintf(fp, "%zuL", lc); 1097 else 1098 fprintf(fp, "%zuB", filesize); 1099 fputs("</td></tr>\n", fp); 1100 git_object_free(obj); 1101 } else if (git_tree_entry_type(entry) == GIT_OBJ_COMMIT) { 1102 /* commit object in tree is a submodule */ 1103 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">", 1104 relpath); 1105 xmlencode(fp, entrypath, strlen(entrypath)); 1106 fputs("</a> @ ", fp); 1107 git_oid_tostr(oid, sizeof(oid), git_tree_entry_id(entry)); 1108 xmlencode(fp, oid, strlen(oid)); 1109 fputs("</td><td class=\"num\" align=\"right\"></td></tr>\n", fp); 1110 } 1111 } 1112 1113 return 0; 1114 } 1115 1116 int 1117 writefiles(FILE *fp, const git_oid *id) 1118 { 1119 git_tree *tree = NULL; 1120 git_commit *commit = NULL; 1121 int ret = -1; 1122 1123 fputs("<table id=\"files\"><thead>\n<tr>" 1124 "<td><b>Mode</b></td><td><b>Name</b></td>" 1125 "<td class=\"num\" align=\"right\"><b>Size</b></td>" 1126 "</tr>\n</thead><tbody>\n", fp); 1127 1128 if (!git_commit_lookup(&commit, repo, id) && 1129 !git_commit_tree(&tree, commit)) 1130 ret = writefilestree(fp, tree, ""); 1131 1132 fputs("</tbody></table>", fp); 1133 1134 git_commit_free(commit); 1135 git_tree_free(tree); 1136 1137 return ret; 1138 } 1139 1140 int 1141 writerefs(FILE *fp) 1142 { 1143 struct referenceinfo *ris = NULL; 1144 struct commitinfo *ci; 1145 size_t count, i, j, refcount; 1146 const char *titles[] = { "Branches", "Tags" }; 1147 const char *ids[] = { "branches", "tags" }; 1148 const char *s; 1149 1150 if (getrefs(&ris, &refcount) == -1) 1151 return -1; 1152 1153 for (i = 0, j = 0, count = 0; i < refcount; i++) { 1154 if (j == 0 && git_reference_is_tag(ris[i].ref)) { 1155 if (count) 1156 fputs("</tbody></table><br/>\n", fp); 1157 count = 0; 1158 j = 1; 1159 } 1160 1161 /* print header if it has an entry (first). */ 1162 if (++count == 1) { 1163 fprintf(fp, "<h2>%s</h2><table id=\"%s\">" 1164 "<thead>\n<tr><td><b>Name</b></td>" 1165 "<td><b>Last commit date</b></td>" 1166 "<td><b>Author</b></td>\n</tr>\n" 1167 "</thead><tbody>\n", 1168 titles[j], ids[j]); 1169 } 1170 1171 ci = ris[i].ci; 1172 s = git_reference_shorthand(ris[i].ref); 1173 1174 fputs("<tr><td>", fp); 1175 xmlencode(fp, s, strlen(s)); 1176 fputs("</td><td>", fp); 1177 if (ci->author) 1178 printtimeshort(fp, &(ci->author->when)); 1179 fputs("</td><td>", fp); 1180 if (ci->author) 1181 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 1182 fputs("</td></tr>\n", fp); 1183 } 1184 /* table footer */ 1185 if (count) 1186 fputs("</tbody></table><br/>\n", fp); 1187 1188 for (i = 0; i < refcount; i++) { 1189 commitinfo_free(ris[i].ci); 1190 git_reference_free(ris[i].ref); 1191 } 1192 free(ris); 1193 1194 return 0; 1195 } 1196 1197 void 1198 usage(char *argv0) 1199 { 1200 fprintf(stderr, "usage: %s [-c cachefile | -l commits] " 1201 "[-u baseurl] repodir\n", argv0); 1202 exit(1); 1203 } 1204 1205 int 1206 main(int argc, char *argv[]) 1207 { 1208 git_object *obj = NULL; 1209 const git_oid *head = NULL; 1210 mode_t mask; 1211 FILE *fp, *fpread; 1212 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p; 1213 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ]; 1214 size_t n; 1215 int i, fd; 1216 1217 for (i = 1; i < argc; i++) { 1218 if (argv[i][0] != '-') { 1219 if (repodir) 1220 usage(argv[0]); 1221 repodir = argv[i]; 1222 } else if (argv[i][1] == 'c') { 1223 if (nlogcommits > 0 || i + 1 >= argc) 1224 usage(argv[0]); 1225 cachefile = argv[++i]; 1226 } else if (argv[i][1] == 'l') { 1227 if (cachefile || i + 1 >= argc) 1228 usage(argv[0]); 1229 errno = 0; 1230 nlogcommits = strtoll(argv[++i], &p, 10); 1231 if (argv[i][0] == '\0' || *p != '\0' || 1232 nlogcommits <= 0 || errno) 1233 usage(argv[0]); 1234 } else if (argv[i][1] == 'u') { 1235 if (i + 1 >= argc) 1236 usage(argv[0]); 1237 baseurl = argv[++i]; 1238 } 1239 } 1240 if (!repodir) 1241 usage(argv[0]); 1242 1243 if (!realpath(repodir, repodirabs)) 1244 err(1, "realpath"); 1245 1246 /* do not search outside the git repository: 1247 GIT_CONFIG_LEVEL_APP is the highest level currently */ 1248 git_libgit2_init(); 1249 for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++) 1250 git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, ""); 1251 /* do not require the git repository to be owned by the current user */ 1252 git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0); 1253 1254 #ifdef __OpenBSD__ 1255 if (unveil(repodir, "r") == -1) 1256 err(1, "unveil: %s", repodir); 1257 if (unveil(".", "rwc") == -1) 1258 err(1, "unveil: ."); 1259 if (cachefile && unveil(cachefile, "rwc") == -1) 1260 err(1, "unveil: %s", cachefile); 1261 1262 if (cachefile) { 1263 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1) 1264 err(1, "pledge"); 1265 } else { 1266 if (pledge("stdio rpath wpath cpath", NULL) == -1) 1267 err(1, "pledge"); 1268 } 1269 #endif 1270 1271 if (git_repository_open_ext(&repo, repodir, 1272 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) { 1273 fprintf(stderr, "%s: cannot open repository\n", argv[0]); 1274 return 1; 1275 } 1276 1277 /* find HEAD */ 1278 if (!git_revparse_single(&obj, repo, "HEAD")) 1279 head = git_object_id(obj); 1280 git_object_free(obj); 1281 1282 /* use directory name as name */ 1283 if ((name = strrchr(repodirabs, '/'))) 1284 name++; 1285 else 1286 name = ""; 1287 1288 /* strip .git suffix */ 1289 if (!(strippedname = strdup(name))) 1290 err(1, "strdup"); 1291 if ((p = strrchr(strippedname, '.'))) 1292 if (!strcmp(p, ".git")) 1293 *p = '\0'; 1294 1295 /* read description or .git/description */ 1296 joinpath(path, sizeof(path), repodir, "description"); 1297 if (!(fpread = fopen(path, "r"))) { 1298 joinpath(path, sizeof(path), repodir, ".git/description"); 1299 fpread = fopen(path, "r"); 1300 } 1301 if (fpread) { 1302 if (!fgets(description, sizeof(description), fpread)) 1303 description[0] = '\0'; 1304 checkfileerror(fpread, path, 'r'); 1305 fclose(fpread); 1306 } 1307 1308 /* read url or .git/url */ 1309 joinpath(path, sizeof(path), repodir, "url"); 1310 if (!(fpread = fopen(path, "r"))) { 1311 joinpath(path, sizeof(path), repodir, ".git/url"); 1312 fpread = fopen(path, "r"); 1313 } 1314 if (fpread) { 1315 if (!fgets(cloneurl, sizeof(cloneurl), fpread)) 1316 cloneurl[0] = '\0'; 1317 checkfileerror(fpread, path, 'r'); 1318 fclose(fpread); 1319 cloneurl[strcspn(cloneurl, "\n")] = '\0'; 1320 } 1321 1322 /* check LICENSE */ 1323 for (i = 0; i < LEN(licensefiles) && !license; i++) { 1324 if (!git_revparse_single(&obj, repo, licensefiles[i]) && 1325 git_object_type(obj) == GIT_OBJ_BLOB) 1326 license = licensefiles[i] + strlen("HEAD:"); 1327 git_object_free(obj); 1328 } 1329 1330 /* check README */ 1331 for (i = 0; i < LEN(readmefiles) && !readme; i++) { 1332 if (!git_revparse_single(&obj, repo, readmefiles[i]) && 1333 git_object_type(obj) == GIT_OBJ_BLOB) 1334 readme = readmefiles[i] + strlen("HEAD:"); 1335 git_object_free(obj); 1336 } 1337 1338 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") && 1339 git_object_type(obj) == GIT_OBJ_BLOB) 1340 submodules = ".gitmodules"; 1341 git_object_free(obj); 1342 1343 /* log for HEAD */ 1344 fp = efopen("log.html", "w"); 1345 relpath = ""; 1346 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO); 1347 writeheader(fp, "Log"); 1348 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>" 1349 "<td><b>Commit message</b></td>" 1350 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>" 1351 "<td class=\"num\" align=\"right\"><b>+</b></td>" 1352 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp); 1353 1354 if (cachefile && head) { 1355 /* read from cache file (does not need to exist) */ 1356 if ((rcachefp = fopen(cachefile, "r"))) { 1357 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp)) 1358 errx(1, "%s: no object id", cachefile); 1359 if (git_oid_fromstr(&lastoid, lastoidstr)) 1360 errx(1, "%s: invalid object id", cachefile); 1361 } 1362 1363 /* write log to (temporary) cache */ 1364 if ((fd = mkstemp(tmppath)) == -1) 1365 err(1, "mkstemp"); 1366 if (!(wcachefp = fdopen(fd, "w"))) 1367 err(1, "fdopen: '%s'", tmppath); 1368 /* write last commit id (HEAD) */ 1369 git_oid_tostr(buf, sizeof(buf), head); 1370 fprintf(wcachefp, "%s\n", buf); 1371 1372 writelog(fp, head); 1373 1374 if (rcachefp) { 1375 /* append previous log to log.html and the new cache */ 1376 while (!feof(rcachefp)) { 1377 n = fread(buf, 1, sizeof(buf), rcachefp); 1378 if (ferror(rcachefp)) 1379 break; 1380 if (fwrite(buf, 1, n, fp) != n || 1381 fwrite(buf, 1, n, wcachefp) != n) 1382 break; 1383 } 1384 checkfileerror(rcachefp, cachefile, 'r'); 1385 fclose(rcachefp); 1386 } 1387 checkfileerror(wcachefp, tmppath, 'w'); 1388 fclose(wcachefp); 1389 } else { 1390 if (head) 1391 writelog(fp, head); 1392 } 1393 1394 fputs("</tbody></table>", fp); 1395 writefooter(fp); 1396 checkfileerror(fp, "log.html", 'w'); 1397 fclose(fp); 1398 1399 /* files for HEAD */ 1400 fp = efopen("files.html", "w"); 1401 writeheader(fp, "Files"); 1402 if (head) 1403 writefiles(fp, head); 1404 writefooter(fp); 1405 checkfileerror(fp, "files.html", 'w'); 1406 fclose(fp); 1407 1408 /* summary page with branches and tags */ 1409 fp = efopen("refs.html", "w"); 1410 writeheader(fp, "Refs"); 1411 writerefs(fp); 1412 writefooter(fp); 1413 checkfileerror(fp, "refs.html", 'w'); 1414 fclose(fp); 1415 1416 /* Atom feed */ 1417 fp = efopen("atom.xml", "w"); 1418 writeatom(fp, 1); 1419 checkfileerror(fp, "atom.xml", 'w'); 1420 fclose(fp); 1421 1422 /* Atom feed for tags / releases */ 1423 fp = efopen("tags.xml", "w"); 1424 writeatom(fp, 0); 1425 checkfileerror(fp, "tags.xml", 'w'); 1426 fclose(fp); 1427 1428 /* rename new cache file on success */ 1429 if (cachefile && head) { 1430 if (rename(tmppath, cachefile)) 1431 err(1, "rename: '%s' to '%s'", tmppath, cachefile); 1432 umask((mask = umask(0))); 1433 if (chmod(cachefile, 1434 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask)) 1435 err(1, "chmod: '%s'", cachefile); 1436 } 1437 1438 /* cleanup */ 1439 git_repository_free(repo); 1440 git_libgit2_shutdown(); 1441 1442 return 0; 1443 }