stagit.c (38969B)
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 id=\"code\"><span class=\"s\">%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)</span>\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 class=\"s\">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 s\">", 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 char summary[41]; 807 if(strlen(ci->summary) > 41) { 808 memcpy(summary, ci->summary, 37*sizeof(char)); 809 memcpy(&summary[37], "...", 3*sizeof(char)); 810 summary[40] = '\0'; 811 } else { 812 strcpy(summary, ci->summary); 813 } 814 xmlencode(fp, summary, strlen(summary)); 815 fputs("</a>", fp); 816 } 817 fputs("</td><td>", fp); 818 if(ci->author) 819 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 820 fputs("</td><td class=\"num\" align=\"right\">", fp); 821 fprintf(fp, "%zu", ci->filecount); 822 fputs("</td><td class=\"num\" align=\"right\">", fp); 823 fprintf(fp, "+%zu", ci->addcount); 824 fputs("</td><td class=\"num\" align=\"right\">", fp); 825 fprintf(fp, "-%zu", ci->delcount); 826 fputs("</td></tr>\n", fp); 827 } 828 829 int 830 writelog(FILE *fp, const git_oid *oid) { 831 struct commitinfo *ci; 832 git_revwalk *w = NULL; 833 git_oid id; 834 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1]; 835 FILE *fpfile; 836 size_t remcommits = 0; 837 int r; 838 839 git_revwalk_new(&w, repo); 840 git_revwalk_push(w, oid); 841 842 while(!git_revwalk_next(&id, w)) { 843 relpath = ""; 844 845 if(cachefile && !memcmp(&id, &lastoid, sizeof(id))) 846 break; 847 848 git_oid_tostr(oidstr, sizeof(oidstr), &id); 849 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr); 850 if(r < 0 || (size_t)r >= sizeof(path)) 851 errx(1, "path truncated: 'commit/%s.html'", oidstr); 852 r = access(path, F_OK); 853 854 /* optimization: if there are no log lines to write and 855 the commit file already exists: skip the diffstat */ 856 if(!nlogcommits) { 857 remcommits++; 858 if(!r) 859 continue; 860 } 861 862 if(!(ci = commitinfo_getbyoid(&id))) 863 break; 864 /* diffstat: for stagit HTML required for the log.html line */ 865 if(commitinfo_getstats(ci) == -1) 866 goto err; 867 868 if(nlogcommits != 0) { 869 writelogline(fp, ci); 870 if(nlogcommits > 0) 871 nlogcommits--; 872 } 873 874 if(cachefile) 875 writelogline(wcachefp, ci); 876 877 /* check if file exists if so skip it */ 878 if(r) { 879 relpath = "../"; 880 fpfile = efopen(path, "w"); 881 writeheader(fpfile, ci->summary); 882 fputs("<pre>", fpfile); 883 printshowfile(fpfile, ci); 884 fputs("</pre>\n", fpfile); 885 writefooter(fpfile); 886 checkfileerror(fpfile, path, 'w'); 887 fclose(fpfile); 888 } 889 err: 890 commitinfo_free(ci); 891 } 892 git_revwalk_free(w); 893 894 if(nlogcommits == 0 && remcommits != 0) { 895 fprintf(fp, 896 "<tr><td></td><td colspan=\"5\">" 897 "%zu more commits remaining, fetch the repository" 898 "</td></tr>\n", remcommits); 899 } 900 901 relpath = ""; 902 903 return 0; 904 } 905 906 void 907 printcommitatom(FILE *fp, struct commitinfo *ci, const char *tag) { 908 fputs("<entry>\n", fp); 909 910 fprintf(fp, "<id>%s</id>\n", ci->oid); 911 if(ci->author) { 912 fputs("<published>", fp); 913 printtimez(fp, &(ci->author->when)); 914 fputs("</published>\n", fp); 915 } 916 if(ci->committer) { 917 fputs("<updated>", fp); 918 printtimez(fp, &(ci->committer->when)); 919 fputs("</updated>\n", fp); 920 } 921 if(ci->summary) { 922 fputs("<title type=\"text\">", fp); 923 if(tag && tag[0]) { 924 fputs("[", fp); 925 xmlencode(fp, tag, strlen(tag)); 926 fputs("] ", fp); 927 } 928 xmlencode(fp, ci->summary, strlen(ci->summary)); 929 fputs("</title>\n", fp); 930 } 931 fprintf(fp, 932 "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n", 933 baseurl, ci->oid); 934 935 if(ci->author) { 936 fputs("<author>\n<name>", fp); 937 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 938 fputs("</name>\n<email>", fp); 939 xmlencode(fp, ci->author->email, strlen(ci->author->email)); 940 fputs("</email>\n</author>\n", fp); 941 } 942 943 fputs("<content type=\"text\">", fp); 944 fprintf(fp, "commit %s\n", ci->oid); 945 if(ci->parentoid[0]) 946 fprintf(fp, "parent %s\n", ci->parentoid); 947 if(ci->author) { 948 fputs("Author: ", fp); 949 xmlencode(fp, ci->author->name, strlen(ci->author->name)); 950 fputs(" <", fp); 951 xmlencode(fp, ci->author->email, strlen(ci->author->email)); 952 fputs(">\nDate: ", fp); 953 printtime(fp, &(ci->author->when)); 954 putc('\n', fp); 955 } 956 if(ci->msg) { 957 putc('\n', fp); 958 xmlencode(fp, ci->msg, strlen(ci->msg)); 959 } 960 fputs("\n</content>\n</entry>\n", fp); 961 } 962 963 int 964 writeatom(FILE *fp, int all) { 965 struct referenceinfo *ris = NULL; 966 size_t refcount = 0; 967 struct commitinfo *ci; 968 git_revwalk *w = NULL; 969 git_oid id; 970 size_t i, m = 100; /* last 'm' commits */ 971 972 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 973 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp); 974 xmlencode(fp, strippedname, strlen(strippedname)); 975 fputs(", branch HEAD</title>\n<subtitle>", fp); 976 xmlencode(fp, description, strlen(description)); 977 fputs("</subtitle>\n", fp); 978 979 /* all commits or only tags? */ 980 if(all) { 981 git_revwalk_new(&w, repo); 982 git_revwalk_push_head(w); 983 for(i = 0; i < m && !git_revwalk_next(&id, w); i++) { 984 if(!(ci = commitinfo_getbyoid(&id))) 985 break; 986 printcommitatom(fp, ci, ""); 987 commitinfo_free(ci); 988 } 989 git_revwalk_free(w); 990 } else if(getrefs(&ris, &refcount) != -1) { 991 /* references: tags */ 992 for(i = 0; i < refcount; i++) { 993 if(git_reference_is_tag(ris[i].ref)) 994 printcommitatom(fp, ris[i].ci, 995 git_reference_shorthand(ris[i]. 996 ref)); 997 998 commitinfo_free(ris[i].ci); 999 git_reference_free(ris[i].ref); 1000 } 1001 free(ris); 1002 } 1003 1004 fputs("</feed>\n", fp); 1005 1006 return 0; 1007 } 1008 1009 int 1010 writemarkdownblob(FILE *fp, git_blob *blob) { 1011 char *buf; 1012 size_t bufsz; 1013 1014 struct lowdown_opts opt = { 1015 .type = LOWDOWN_HTML, 1016 .feat = LOWDOWN_DEFINITION | LOWDOWN_FENCED | LOWDOWN_FOOTNOTES 1017 | LOWDOWN_METADATA | LOWDOWN_STRIKE | LOWDOWN_SUPER | 1018 LOWDOWN_TABLES, 1019 }; 1020 1021 lowdown_buf(&opt, git_blob_rawcontent(blob), git_blob_rawsize(blob), 1022 &buf, &bufsz, NULL); 1023 1024 fwrite(buf, 1, bufsz, fp); 1025 free(buf); 1026 1027 return 0; 1028 } 1029 1030 void 1031 writerawblob(git_blob *blob, const char *fpath) { 1032 FILE *fp; 1033 1034 fp = efopen(fpath, "w"); 1035 1036 // TODO: Add check to file pointer 1037 1038 fwrite(git_blob_rawcontent(blob), git_blob_rawsize(blob), 1, fp); 1039 1040 fclose(fp); 1041 } 1042 1043 size_t 1044 writeblob(git_object *obj, const char *fpath, const char *filename, 1045 size_t filesize) { 1046 char tmp[PATH_MAX] = "", *d; 1047 char rawpath[PATH_MAX] = ""; 1048 const char *p; 1049 size_t lc = 0; 1050 FILE *fp; 1051 1052 if(strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp)) 1053 errx(1, "path truncated: '%s'", fpath); 1054 if(!(d = dirname(tmp))) 1055 err(1, "dirname"); 1056 if(mkdirp(d)) 1057 return -1; 1058 1059 for(p = fpath, tmp[0] = '\0'; *p; p++) { 1060 if(*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp)) 1061 errx(1, "path truncated: '../%s'", tmp); 1062 } 1063 relpath = tmp; 1064 1065 fp = efopen(fpath, "w"); 1066 writeheader(fp, filename); 1067 fputs("<p> ", fp); 1068 xmlencode(fp, filename, strlen(filename)); 1069 fprintf(fp, " (%zuB)", filesize); 1070 fputs("</p><hr/>", fp); 1071 1072 if(git_blob_is_binary((git_blob *) obj)) { 1073 fputs("<p>Binary file.</p>\n", fp); 1074 memcpy(rawpath, fpath, strlen(fpath) - 5); 1075 writerawblob((git_blob *) obj, rawpath); 1076 } else if(strlen(filename) >= 3 1077 && !strcmp(filename + strlen(filename) - 3, ".md")) { 1078 lc = writemarkdownblob(fp, (git_blob *) obj); 1079 } else 1080 lc = writeblobhtml(fp, (git_blob *) obj); 1081 1082 writefooter(fp); 1083 checkfileerror(fp, fpath, 'w'); 1084 fclose(fp); 1085 1086 relpath = ""; 1087 1088 return lc; 1089 } 1090 1091 const char * 1092 filemode(git_filemode_t m) { 1093 static char mode[11]; 1094 1095 memset(mode, '-', sizeof(mode) - 1); 1096 mode[10] = '\0'; 1097 1098 if(S_ISREG(m)) 1099 mode[0] = '-'; 1100 else if(S_ISBLK(m)) 1101 mode[0] = 'b'; 1102 else if(S_ISCHR(m)) 1103 mode[0] = 'c'; 1104 else if(S_ISDIR(m)) 1105 mode[0] = 'd'; 1106 else if(S_ISFIFO(m)) 1107 mode[0] = 'p'; 1108 else if(S_ISLNK(m)) 1109 mode[0] = 'l'; 1110 else if(S_ISSOCK(m)) 1111 mode[0] = 's'; 1112 else 1113 mode[0] = '?'; 1114 1115 if(m & S_IRUSR) 1116 mode[1] = 'r'; 1117 if(m & S_IWUSR) 1118 mode[2] = 'w'; 1119 if(m & S_IXUSR) 1120 mode[3] = 'x'; 1121 if(m & S_IRGRP) 1122 mode[4] = 'r'; 1123 if(m & S_IWGRP) 1124 mode[5] = 'w'; 1125 if(m & S_IXGRP) 1126 mode[6] = 'x'; 1127 if(m & S_IROTH) 1128 mode[7] = 'r'; 1129 if(m & S_IWOTH) 1130 mode[8] = 'w'; 1131 if(m & S_IXOTH) 1132 mode[9] = 'x'; 1133 1134 if(m & S_ISUID) 1135 mode[3] = (mode[3] == 'x') ? 's' : 'S'; 1136 if(m & S_ISGID) 1137 mode[6] = (mode[6] == 'x') ? 's' : 'S'; 1138 if(m & S_ISVTX) 1139 mode[9] = (mode[9] == 'x') ? 't' : 'T'; 1140 1141 return mode; 1142 } 1143 1144 int 1145 writefilestree(FILE *fp, git_tree *tree, const char *path) { 1146 const git_tree_entry *entry = NULL; 1147 git_object *obj = NULL; 1148 const char *entryname; 1149 char filepath[PATH_MAX], entrypath[PATH_MAX], oid[8]; 1150 size_t count, i, lc, filesize; 1151 int r, ret; 1152 1153 count = git_tree_entrycount(tree); 1154 for(i = 0; i < count; i++) { 1155 if(!(entry = git_tree_entry_byindex(tree, i)) 1156 || !(entryname = git_tree_entry_name(entry))) 1157 return -1; 1158 joinpath(entrypath, sizeof(entrypath), path, entryname); 1159 1160 r = snprintf(filepath, sizeof(filepath), "file/%s.html", 1161 entrypath); 1162 if(r < 0 || (size_t)r >= sizeof(filepath)) 1163 errx(1, "path truncated: 'file/%s.html'", entrypath); 1164 1165 if(!git_tree_entry_to_object(&obj, repo, entry)) { 1166 switch (git_object_type(obj)) { 1167 case GIT_OBJ_BLOB: 1168 break; 1169 case GIT_OBJ_TREE: 1170 /* NOTE: recurses */ 1171 ret = writefilestree(fp, (git_tree *) obj, 1172 entrypath); 1173 git_object_free(obj); 1174 if(ret) 1175 return ret; 1176 continue; 1177 default: 1178 git_object_free(obj); 1179 continue; 1180 } 1181 1182 filesize = git_blob_rawsize((git_blob *) obj); 1183 lc = writeblob(obj, filepath, entryname, filesize); 1184 1185 fputs("<tr><td>", fp); 1186 fputs(filemode(git_tree_entry_filemode(entry)), fp); 1187 fprintf(fp, "</td><td><a href=\"%s", relpath); 1188 percentencode(fp, filepath, strlen(filepath)); 1189 fputs("\">", fp); 1190 xmlencode(fp, entrypath, strlen(entrypath)); 1191 fputs("</a></td><td class=\"num\" align=\"right\">", 1192 fp); 1193 if(lc > 0) 1194 fprintf(fp, "%zuL", lc); 1195 else 1196 fprintf(fp, "%zuB", filesize); 1197 fputs("</td></tr>\n", fp); 1198 git_object_free(obj); 1199 } else if(git_tree_entry_type(entry) == GIT_OBJ_COMMIT) { 1200 /* commit object in tree is a submodule */ 1201 fprintf(fp, 1202 "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">", 1203 relpath); 1204 xmlencode(fp, entrypath, strlen(entrypath)); 1205 fputs("</a> @ ", fp); 1206 git_oid_tostr(oid, sizeof(oid), 1207 git_tree_entry_id(entry)); 1208 xmlencode(fp, oid, strlen(oid)); 1209 fputs("</td><td class=\"num\" align=\"right\"></td></tr>\n", fp); 1210 } 1211 } 1212 1213 return 0; 1214 } 1215 1216 int 1217 writefiles(FILE *fp, const git_oid *id) { 1218 git_tree *tree = NULL; 1219 git_commit *commit = NULL; 1220 int ret = -1; 1221 1222 fputs("<table id=\"files\"><thead>\n<tr>" 1223 "<td><b>Mode</b></td><td><b>Name</b></td>" 1224 "<td class=\"num\" align=\"right\"><b>Size</b></td>" 1225 "</tr>\n</thead><tbody>\n", fp); 1226 1227 if(!git_commit_lookup(&commit, repo, id) 1228 && !git_commit_tree(&tree, commit)) 1229 ret = writefilestree(fp, tree, ""); 1230 1231 fputs("</tbody></table>", fp); 1232 1233 git_commit_free(commit); 1234 git_tree_free(tree); 1235 1236 return ret; 1237 } 1238 1239 int 1240 writerefs(FILE *fp) { 1241 struct referenceinfo *ris = NULL; 1242 struct commitinfo *ci; 1243 size_t count, i, j, refcount; 1244 const char *titles[] = { "Branches", "Tags" }; 1245 const char *ids[] = { "branches", "tags" }; 1246 const char *s; 1247 1248 if(getrefs(&ris, &refcount) == -1) 1249 return -1; 1250 1251 for(i = 0, j = 0, count = 0; i < refcount; i++) { 1252 if(j == 0 && git_reference_is_tag(ris[i].ref)) { 1253 if(count) 1254 fputs("</tbody></table><br/>\n", fp); 1255 count = 0; 1256 j = 1; 1257 } 1258 1259 /* print header if it has an entry (first). */ 1260 if(++count == 1) { 1261 fprintf(fp, 1262 "<h2>%s</h2><table id=\"%s\">" 1263 "<thead>\n<tr><td><b>Name</b></td>" 1264 "<td><b>Last commit date</b></td>" 1265 "<td><b>Author</b></td>\n</tr>\n" 1266 "</thead><tbody>\n", titles[j], ids[j]); 1267 } 1268 1269 ci = ris[i].ci; 1270 s = git_reference_shorthand(ris[i].ref); 1271 1272 fputs("<tr><td>", fp); 1273 1274 /* put link to tar archive if the ref is a tag */ 1275 if(j == 1) { 1276 fputs("<a href=\"archive/", fp); 1277 xmlencode(fp, strippedname, strlen(strippedname)); 1278 fputs("-", fp); 1279 xmlencode(fp, s, strlen(s)); 1280 fputs(".tar.gz", fp); 1281 fputs("\">", fp); 1282 xmlencode(fp, s, strlen(s)); 1283 fputs("</a>", fp); 1284 } else { 1285 xmlencode(fp, s, strlen(s)); 1286 } 1287 fputs("</td><td>", fp); 1288 if(ci->author) 1289 printtimeshort(fp, &(ci->author->when)); 1290 fputs("</td><td>", fp); 1291 if(ci->author) 1292 xmlencode(fp, ci->author->name, 1293 strlen(ci->author->name)); 1294 fputs("</td></tr>\n", fp); 1295 } 1296 /* table footer */ 1297 if(count) 1298 fputs("</tbody></table><br/>\n", fp); 1299 1300 for(i = 0; i < refcount; i++) { 1301 commitinfo_free(ris[i].ci); 1302 git_reference_free(ris[i].ref); 1303 } 1304 free(ris); 1305 1306 return 0; 1307 } 1308 1309 void 1310 usage(char *argv0) { 1311 fprintf(stderr, 1312 "%s [-c cachefile | -l commits] " "[-u baseurl] repodir\n", 1313 argv0); 1314 exit(1); 1315 } 1316 1317 int 1318 main(int argc, char *argv[]) { 1319 git_object *obj = NULL; 1320 const git_oid *head = NULL; 1321 mode_t mask; 1322 FILE *fp, *fpread; 1323 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p; 1324 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ]; 1325 size_t n; 1326 int i, fd; 1327 1328 for(i = 1; i < argc; i++) { 1329 if(argv[i][0] != '-') { 1330 if(repodir) 1331 usage(argv[0]); 1332 repodir = argv[i]; 1333 } else if(argv[i][1] == 'c') { 1334 if(nlogcommits > 0 || i + 1 >= argc) 1335 usage(argv[0]); 1336 cachefile = argv[++i]; 1337 } else if(argv[i][1] == 'l') { 1338 if(cachefile || i + 1 >= argc) 1339 usage(argv[0]); 1340 errno = 0; 1341 nlogcommits = strtoll(argv[++i], &p, 10); 1342 if(argv[i][0] == '\0' || *p != '\0' || nlogcommits <= 0 1343 || errno) 1344 usage(argv[0]); 1345 } else if(argv[i][1] == 'u') { 1346 if(i + 1 >= argc) 1347 usage(argv[0]); 1348 baseurl = argv[++i]; 1349 } 1350 } 1351 if(!repodir) 1352 usage(argv[0]); 1353 1354 if(!realpath(repodir, repodirabs)) 1355 err(1, "realpath"); 1356 1357 /* do not search outside the git repository: 1358 GIT_CONFIG_LEVEL_APP is the highest level currently */ 1359 git_libgit2_init(); 1360 for(i = 1; i <= GIT_CONFIG_LEVEL_APP; i++) 1361 git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, ""); 1362 /* do not require the git repository to be owned by the current user */ 1363 git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0); 1364 1365 #ifdef __OpenBSD__ 1366 if(unveil(repodir, "r") == -1) 1367 err(1, "unveil: %s", repodir); 1368 if(unveil(".", "rwc") == -1) 1369 err(1, "unveil: ."); 1370 if(cachefile && unveil(cachefile, "rwc") == -1) 1371 err(1, "unveil: %s", cachefile); 1372 1373 if(cachefile) { 1374 if(pledge("stdio rpath wpath cpath fattr", NULL) == -1) 1375 err(1, "pledge"); 1376 } else { 1377 if(pledge("stdio rpath wpath cpath", NULL) == -1) 1378 err(1, "pledge"); 1379 } 1380 #endif 1381 1382 if(git_repository_open_ext 1383 (&repo, repodir, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) { 1384 fprintf(stderr, "%s: cannot open repository\n", argv[0]); 1385 return 1; 1386 } 1387 1388 /* find HEAD */ 1389 if(!git_revparse_single(&obj, repo, "HEAD")) 1390 head = git_object_id(obj); 1391 git_object_free(obj); 1392 1393 /* use directory name as name */ 1394 if((name = strrchr(repodirabs, '/'))) 1395 name++; 1396 else 1397 name = ""; 1398 1399 /* strip .git suffix */ 1400 if(!(strippedname = strdup(name))) 1401 err(1, "strdup"); 1402 if((p = strrchr(strippedname, '.'))) 1403 if(!strcmp(p, ".git")) 1404 *p = '\0'; 1405 1406 /* read description or .git/description */ 1407 joinpath(path, sizeof(path), repodir, "description"); 1408 if(!(fpread = fopen(path, "r"))) { 1409 joinpath(path, sizeof(path), repodir, ".git/description"); 1410 fpread = fopen(path, "r"); 1411 } 1412 if(fpread) { 1413 if(!fgets(description, sizeof(description), fpread)) 1414 description[0] = '\0'; 1415 checkfileerror(fpread, path, 'r'); 1416 fclose(fpread); 1417 } 1418 1419 /* read url or .git/url */ 1420 joinpath(path, sizeof(path), repodir, "url"); 1421 if(!(fpread = fopen(path, "r"))) { 1422 joinpath(path, sizeof(path), repodir, ".git/url"); 1423 fpread = fopen(path, "r"); 1424 } 1425 if(fpread) { 1426 if(!fgets(cloneurl, sizeof(cloneurl), fpread)) 1427 cloneurl[0] = '\0'; 1428 checkfileerror(fpread, path, 'r'); 1429 fclose(fpread); 1430 cloneurl[strcspn(cloneurl, "\n")] = '\0'; 1431 } 1432 1433 /* check LICENSE */ 1434 for(i = 0; i < LEN(licensefiles) && !license; i++) { 1435 if(!git_revparse_single(&obj, repo, licensefiles[i]) 1436 && git_object_type(obj) == GIT_OBJ_BLOB) 1437 license = licensefiles[i] + strlen("HEAD:"); 1438 git_object_free(obj); 1439 } 1440 1441 /* check README */ 1442 for(i = 0; i < LEN(readmefiles) && !readme; i++) { 1443 if(!git_revparse_single(&obj, repo, readmefiles[i]) 1444 && git_object_type(obj) == GIT_OBJ_BLOB) 1445 readme = readmefiles[i] + strlen("HEAD:"); 1446 git_object_free(obj); 1447 } 1448 1449 if(!git_revparse_single(&obj, repo, "HEAD:.gitmodules") 1450 && git_object_type(obj) == GIT_OBJ_BLOB) 1451 submodules = ".gitmodules"; 1452 git_object_free(obj); 1453 1454 /* log for HEAD */ 1455 fp = efopen("log.html", "w"); 1456 relpath = ""; 1457 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO); 1458 writeheader(fp, "Log"); 1459 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>" 1460 "<td><b>Commit message</b></td>" 1461 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>" 1462 "<td class=\"num\" align=\"right\"><b>+</b></td>" 1463 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", 1464 fp); 1465 1466 if(cachefile && head) { 1467 /* read from cache file (does not need to exist) */ 1468 if((rcachefp = fopen(cachefile, "r"))) { 1469 if(!fgets(lastoidstr, sizeof(lastoidstr), rcachefp)) 1470 errx(1, "%s: no object id", cachefile); 1471 if(git_oid_fromstr(&lastoid, lastoidstr)) 1472 errx(1, "%s: invalid object id", cachefile); 1473 } 1474 1475 /* write log to (temporary) cache */ 1476 if((fd = mkstemp(tmppath)) == -1) 1477 err(1, "mkstemp"); 1478 if(!(wcachefp = fdopen(fd, "w"))) 1479 err(1, "fdopen: '%s'", tmppath); 1480 /* write last commit id (HEAD) */ 1481 git_oid_tostr(buf, sizeof(buf), head); 1482 fprintf(wcachefp, "%s\n", buf); 1483 1484 writelog(fp, head); 1485 1486 if(rcachefp) { 1487 /* append previous log to log.html and the new cache */ 1488 while(!feof(rcachefp)) { 1489 n = fread(buf, 1, sizeof(buf), rcachefp); 1490 if(ferror(rcachefp)) 1491 break; 1492 if(fwrite(buf, 1, n, fp) != n 1493 || fwrite(buf, 1, n, wcachefp) != n) 1494 break; 1495 } 1496 checkfileerror(rcachefp, cachefile, 'r'); 1497 fclose(rcachefp); 1498 } 1499 checkfileerror(wcachefp, tmppath, 'w'); 1500 fclose(wcachefp); 1501 } else { 1502 if(head) 1503 writelog(fp, head); 1504 } 1505 1506 fputs("</tbody></table>", fp); 1507 writefooter(fp); 1508 checkfileerror(fp, "log.html", 'w'); 1509 fclose(fp); 1510 1511 /* files for HEAD */ 1512 fp = efopen("files.html", "w"); 1513 writeheader(fp, "Files"); 1514 if(head) 1515 writefiles(fp, head); 1516 writefooter(fp); 1517 checkfileerror(fp, "files.html", 'w'); 1518 fclose(fp); 1519 1520 /* summary page with branches and tags */ 1521 fp = efopen("refs.html", "w"); 1522 writeheader(fp, "Refs"); 1523 writerefs(fp); 1524 writefooter(fp); 1525 checkfileerror(fp, "refs.html", 'w'); 1526 fclose(fp); 1527 1528 /* Atom feed */ 1529 fp = efopen("atom.xml", "w"); 1530 writeatom(fp, 1); 1531 checkfileerror(fp, "atom.xml", 'w'); 1532 fclose(fp); 1533 1534 /* Atom feed for tags / releases */ 1535 fp = efopen("tags.xml", "w"); 1536 writeatom(fp, 0); 1537 checkfileerror(fp, "tags.xml", 'w'); 1538 fclose(fp); 1539 1540 /* rename new cache file on success */ 1541 if(cachefile && head) { 1542 if(rename(tmppath, cachefile)) 1543 err(1, "rename: '%s' to '%s'", tmppath, cachefile); 1544 umask((mask = umask(0))); 1545 if(chmod 1546 (cachefile, 1547 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) 1548 & ~mask)) 1549 err(1, "chmod: '%s'", cachefile); 1550 } 1551 1552 /* cleanup */ 1553 git_repository_free(repo); 1554 git_libgit2_shutdown(); 1555 1556 return 0; 1557 }