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