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