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