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