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