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