29 char oid[GIT_OID_HEXSZ + 1];
30 char parentoid[GIT_OID_HEXSZ + 1];
32 const git_signature *author;
33 const git_signature *committer;
40 git_tree *commit_tree;
41 git_tree *parent_tree;
47 struct deltainfo **deltas;
51 static git_repository *repo;
53 static const char *relpath = "";
54 static const char *repodir;
56 static char *name = "";
57 static char *strippedname = "";
58 static char description[255];
59 static char cloneurl[1024];
60 static char *submodules;
61 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
63 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
65 static long long nlogcommits = -1; /* < 0 indicates not used */
68 static git_oid lastoid;
69 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
70 static FILE *rcachefp, *wcachefp;
71 static const char *cachefile;
73 int cp(char fileSource[], char fileDestination[])
76 FILE *stream_R, *stream_W;
78 stream_R = fopen(fileSource, "r");
81 stream_W = fopen(fileDestination, "w"); //create and write to file
87 while ((c = fgetc(stream_R)) != EOF)
96 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
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);
108 deltainfo_free(struct deltainfo *di)
112 git_patch_free(di->patch);
113 memset(di, 0, sizeof(*di));
118 commitinfo_getstats(struct commitinfo *ci)
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;
130 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
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))) {
135 ci->parent_tree = NULL;
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))
146 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
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))
154 ndeltas = git_diff_num_deltas(ci->diff);
155 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
158 for (i = 0; i < ndeltas; i++) {
159 if (git_patch_from_diff(&patch, ci->diff, i))
162 if (!(di = calloc(1, sizeof(struct deltainfo))))
167 delta = git_patch_get_delta(patch);
169 /* skip stats for binary data */
170 if (delta->flags & GIT_DIFF_FLAG_BINARY)
173 nhunks = git_patch_num_hunks(patch);
174 for (j = 0; j < nhunks; j++) {
175 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
178 if (git_patch_get_line_in_hunk(&line, patch, j, k))
180 if (line->old_lineno == -1) {
183 } else if (line->new_lineno == -1) {
196 git_diff_free(ci->diff);
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);
206 for (i = 0; i < ci->ndeltas; i++)
207 deltainfo_free(ci->deltas[i]);
219 commitinfo_free(struct commitinfo *ci)
226 for (i = 0; i < ci->ndeltas; i++)
227 deltainfo_free(ci->deltas[i]);
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));
240 commitinfo_getbyoid(const git_oid *id)
242 struct commitinfo *ci;
244 if (!(ci = calloc(1, sizeof(struct commitinfo))))
247 if (git_commit_lookup(&(ci->commit), repo, id))
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));
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);
268 efopen(const char *name, const char *flags)
272 if (!(fp = fopen(name, flags)))
273 err(1, "fopen: '%s'", name);
278 /* Escape characters below as HTML 2.0 / XML 1.0. */
280 xmlencode(FILE *fp, const char *s, size_t len)
284 for (i = 0; *s && i < len; s++, i++) {
286 case '<': fputs("<", fp); break;
287 case '>': fputs(">", fp); break;
288 case '\'': fputs("'", fp); break;
289 case '&': fputs("&", fp); break;
290 case '"': fputs(""", fp); break;
291 default: fputc(*s, fp);
297 mkdirp(const char *path)
299 char tmp[PATH_MAX], *p;
301 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
302 errx(1, "path truncated: '%s'", path);
303 for (p = tmp + (tmp[0] == '/'); *p; p++) {
307 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
311 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
317 printtimez(FILE *fp, const git_time *intime)
323 t = (time_t)intime->time;
324 if (!(intm = gmtime(&t)))
326 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
331 printtime(FILE *fp, const git_time *intime)
337 t = (time_t)intime->time + (intime->offset * 60);
338 if (!(intm = gmtime(&t)))
340 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
341 if (intime->offset < 0)
342 fprintf(fp, "%s -%02d%02d", out,
343 -(intime->offset) / 60, -(intime->offset) % 60);
345 fprintf(fp, "%s +%02d%02d", out,
346 intime->offset / 60, intime->offset % 60);
350 printtimeshort(FILE *fp, const git_time *intime)
356 t = (time_t)intime->time;
357 if (!(intm = gmtime(&t)))
359 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
364 writeheader(FILE *fp, const char *title)
366 fputs("<!DOCTYPE html>\n"
368 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
369 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
371 xmlencode(fp, title, strlen(title));
372 if (title[0] && strippedname[0])
374 xmlencode(fp, strippedname, strlen(strippedname));
377 xmlencode(fp, description, strlen(description));
378 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
379 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
381 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.min.css\" />\n", relpath);
382 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
383 fputs("</head>\n<body>\n<table><tr><td>", fp);
384 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
386 fputs("</td><td><h1>", fp);
387 xmlencode(fp, strippedname, strlen(strippedname));
388 fputs("</h1><span class=\"desc\">", fp);
389 xmlencode(fp, description, strlen(description));
390 fputs("</span></td></tr>", fp);
392 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
393 xmlencode(fp, cloneurl, strlen(cloneurl));
395 xmlencode(fp, cloneurl, strlen(cloneurl));
396 fputs("</a></span></td></tr>", fp);
398 fputs("<tr><td></td><td>\n", fp);
399 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
400 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
401 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
403 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
404 relpath, submodules);
406 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
409 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
411 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
415 writefooter(FILE *fp)
417 fputs("</div>\n</body>\n</html>\n", fp);
421 syntax_highlight(const char *filename, FILE *fp, const char *s, size_t len)
426 int stdout_copy = dup(1);
430 // Python Pygments script for syntax highlighting.
431 FILE *child = popen("/usr/local/share/doc/stagit/highlight.py", "w");
433 printf("child is null: %s", strerror(errno));
436 // Give filename through STDIN:
437 fprintf(child, "%s\n", filename);
438 // Give code to highlight through STDIN:
441 for (i = 0; *s && i < len; s++, i++) {
442 if (*s == '\n') lc++;
443 fprintf(child, "%c", *s);
449 dup2(stdout_copy, 1);
454 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
457 const char *s = git_blob_rawcontent(blob);
458 git_off_t len = git_blob_rawsize(blob);
461 lc = syntax_highlight(filename, fp, s, len);
468 printcommit(FILE *fp, struct commitinfo *ci)
470 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
471 relpath, ci->oid, ci->oid);
473 if (ci->parentoid[0])
474 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
475 relpath, ci->parentoid, ci->parentoid);
478 fputs("<b>Author:</b> ", fp);
479 xmlencode(fp, ci->author->name, strlen(ci->author->name));
480 fputs(" <<a href=\"mailto:", fp);
481 xmlencode(fp, ci->author->email, strlen(ci->author->email));
483 xmlencode(fp, ci->author->email, strlen(ci->author->email));
484 fputs("</a>>\n<b>Date:</b> ", fp);
485 printtime(fp, &(ci->author->when));
490 xmlencode(fp, ci->msg, strlen(ci->msg));
496 printshowfile(FILE *fp, struct commitinfo *ci)
498 const git_diff_delta *delta;
499 const git_diff_hunk *hunk;
500 const git_diff_line *line;
502 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
511 if (ci->filecount > 1000 ||
512 ci->ndeltas > 1000 ||
513 ci->addcount > 100000 ||
514 ci->delcount > 100000) {
515 fputs("Diff is too large, output suppressed.\n", fp);
520 fputs("<b>Diffstat:</b>\n<table>", fp);
521 for (i = 0; i < ci->ndeltas; i++) {
522 delta = git_patch_get_delta(ci->deltas[i]->patch);
524 switch (delta->status) {
525 case GIT_DELTA_ADDED: c = 'A'; break;
526 case GIT_DELTA_COPIED: c = 'C'; break;
527 case GIT_DELTA_DELETED: c = 'D'; break;
528 case GIT_DELTA_MODIFIED: c = 'M'; break;
529 case GIT_DELTA_RENAMED: c = 'R'; break;
530 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
531 default: c = ' '; break;
534 fprintf(fp, "<tr><td>%c", c);
536 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
538 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
539 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
540 if (strcmp(delta->old_file.path, delta->new_file.path)) {
541 fputs(" -> ", fp);
542 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
545 add = ci->deltas[i]->addcount;
546 del = ci->deltas[i]->delcount;
548 total = sizeof(linestr) - 2;
549 if (changed > total) {
551 add = ((float)total / changed * add) + 1;
553 del = ((float)total / changed * del) + 1;
555 memset(&linestr, '+', add);
556 memset(&linestr[add], '-', del);
558 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
559 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
560 fwrite(&linestr, 1, add, fp);
561 fputs("</span><span class=\"d\">", fp);
562 fwrite(&linestr[add], 1, del, fp);
563 fputs("</span></td></tr>\n", fp);
565 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
566 ci->filecount, ci->filecount == 1 ? "" : "s",
567 ci->addcount, ci->addcount == 1 ? "" : "s",
568 ci->delcount, ci->delcount == 1 ? "" : "s");
572 for (i = 0; i < ci->ndeltas; i++) {
573 patch = ci->deltas[i]->patch;
574 delta = git_patch_get_delta(patch);
575 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
576 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
577 fputs(".html\">", fp);
578 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
579 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
580 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
581 fprintf(fp, ".html\">");
582 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
583 fprintf(fp, "</a></b>\n");
585 /* check binary data */
586 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
587 fputs("Binary files differ.\n", fp);
591 nhunks = git_patch_num_hunks(patch);
592 for (j = 0; j < nhunks; j++) {
593 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
596 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
597 xmlencode(fp, hunk->header, hunk->header_len);
601 if (git_patch_get_line_in_hunk(&line, patch, j, k))
603 if (line->old_lineno == -1)
604 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
606 else if (line->new_lineno == -1)
607 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
611 xmlencode(fp, line->content, line->content_len);
612 if (line->old_lineno == -1 || line->new_lineno == -1)
620 writelogline(FILE *fp, struct commitinfo *ci)
622 fputs("<tr><td>", fp);
624 printtimeshort(fp, &(ci->author->when));
625 fputs("</td><td>", fp);
627 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
628 xmlencode(fp, ci->summary, strlen(ci->summary));
631 fputs("</td><td>", fp);
633 xmlencode(fp, ci->author->name, strlen(ci->author->name));
634 fputs("</td><td class=\"num\" align=\"right\">", fp);
635 fprintf(fp, "%zu", ci->filecount);
636 fputs("</td><td class=\"num\" align=\"right\">", fp);
637 fprintf(fp, "+%zu", ci->addcount);
638 fputs("</td><td class=\"num\" align=\"right\">", fp);
639 fprintf(fp, "-%zu", ci->delcount);
640 fputs("</td></tr>\n", fp);
644 writelog(FILE *fp, const git_oid *oid)
646 struct commitinfo *ci;
647 git_revwalk *w = NULL;
649 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
653 git_revwalk_new(&w, repo);
654 git_revwalk_push(w, oid);
655 git_revwalk_simplify_first_parent(w);
657 while (!git_revwalk_next(&id, w)) {
660 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
663 git_oid_tostr(oidstr, sizeof(oidstr), &id);
664 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
665 if (r < 0 || (size_t)r >= sizeof(path))
666 errx(1, "path truncated: 'commit/%s.html'", oidstr);
667 r = access(path, F_OK);
669 /* optimization: if there are no log lines to write and
670 the commit file already exists: skip the diffstat */
671 if (!nlogcommits && !r)
674 if (!(ci = commitinfo_getbyoid(&id)))
676 /* diffstat: for stagit HTML required for the log.html line */
677 if (commitinfo_getstats(ci) == -1)
680 if (nlogcommits < 0) {
681 writelogline(fp, ci);
682 } else if (nlogcommits > 0) {
683 writelogline(fp, ci);
685 if (!nlogcommits && ci->parentoid[0])
686 fputs("<tr><td></td><td colspan=\"5\">"
687 "More commits remaining [...]</td>"
692 writelogline(wcachefp, ci);
694 /* check if file exists if so skip it */
697 fpfile = efopen(path, "w");
698 writeheader(fpfile, ci->summary);
699 fputs("<pre>", fpfile);
700 printshowfile(fpfile, ci);
701 fputs("</pre>\n", fpfile);
716 printcommitatom(FILE *fp, struct commitinfo *ci)
718 fputs("<entry>\n", fp);
720 fprintf(fp, "<id>%s</id>\n", ci->oid);
722 fputs("<published>", fp);
723 printtimez(fp, &(ci->author->when));
724 fputs("</published>\n", fp);
727 fputs("<updated>", fp);
728 printtimez(fp, &(ci->committer->when));
729 fputs("</updated>\n", fp);
732 fputs("<title type=\"text\">", fp);
733 xmlencode(fp, ci->summary, strlen(ci->summary));
734 fputs("</title>\n", fp);
736 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
740 fputs("<author>\n<name>", fp);
741 xmlencode(fp, ci->author->name, strlen(ci->author->name));
742 fputs("</name>\n<email>", fp);
743 xmlencode(fp, ci->author->email, strlen(ci->author->email));
744 fputs("</email>\n</author>\n", fp);
747 fputs("<content type=\"text\">", fp);
748 fprintf(fp, "commit %s\n", ci->oid);
749 if (ci->parentoid[0])
750 fprintf(fp, "parent %s\n", ci->parentoid);
752 fputs("Author: ", fp);
753 xmlencode(fp, ci->author->name, strlen(ci->author->name));
755 xmlencode(fp, ci->author->email, strlen(ci->author->email));
756 fputs(">\nDate: ", fp);
757 printtime(fp, &(ci->author->when));
762 xmlencode(fp, ci->msg, strlen(ci->msg));
764 fputs("\n</content>\n</entry>\n", fp);
770 struct commitinfo *ci;
771 git_revwalk *w = NULL;
773 size_t i, m = 100; /* last 'm' commits */
775 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
776 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
777 xmlencode(fp, strippedname, strlen(strippedname));
778 fputs(", branch HEAD</title>\n<subtitle>", fp);
779 xmlencode(fp, description, strlen(description));
780 fputs("</subtitle>\n", fp);
782 git_revwalk_new(&w, repo);
783 git_revwalk_push_head(w);
784 git_revwalk_simplify_first_parent(w);
786 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
787 if (!(ci = commitinfo_getbyoid(&id)))
789 printcommitatom(fp, ci);
794 fputs("</feed>\n", fp);
800 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
802 char tmp[PATH_MAX] = "", *d;
807 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
808 errx(1, "path truncated: '%s'", fpath);
809 if (!(d = dirname(tmp)))
814 for (p = fpath, tmp[0] = '\0'; *p; p++) {
815 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
816 errx(1, "path truncated: '../%s'", tmp);
820 fp = efopen(fpath, "w");
821 writeheader(fp, filename);
823 xmlencode(fp, filename, strlen(filename));
824 fprintf(fp, " (%juB)", (uintmax_t)filesize);
825 fputs("</p><hr/>", fp);
827 if (git_blob_is_binary((git_blob *)obj)) {
828 fputs("<p>Binary file.</p>\n", fp);
830 lc = writeblobhtml(filename, fp, (git_blob *)obj);
843 filemode(git_filemode_t m)
845 static char mode[11];
847 memset(mode, '-', sizeof(mode) - 1);
858 else if (S_ISFIFO(m))
862 else if (S_ISSOCK(m))
867 if (m & S_IRUSR) mode[1] = 'r';
868 if (m & S_IWUSR) mode[2] = 'w';
869 if (m & S_IXUSR) mode[3] = 'x';
870 if (m & S_IRGRP) mode[4] = 'r';
871 if (m & S_IWGRP) mode[5] = 'w';
872 if (m & S_IXGRP) mode[6] = 'x';
873 if (m & S_IROTH) mode[7] = 'r';
874 if (m & S_IWOTH) mode[8] = 'w';
875 if (m & S_IXOTH) mode[9] = 'x';
877 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
878 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
879 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
885 writefilestree(FILE *fp, git_tree *tree, const char *path)
887 const git_tree_entry *entry = NULL;
888 git_submodule *module = NULL;
889 git_object *obj = NULL;
891 const char *entryname;
892 char filepath[PATH_MAX], entrypath[PATH_MAX];
896 count = git_tree_entrycount(tree);
897 for (i = 0; i < count; i++) {
898 if (!(entry = git_tree_entry_byindex(tree, i)) ||
899 !(entryname = git_tree_entry_name(entry)))
901 joinpath(entrypath, sizeof(entrypath), path, entryname);
903 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
905 if (r < 0 || (size_t)r >= sizeof(filepath))
906 errx(1, "path truncated: 'file/%s.html'", entrypath);
908 if (!git_tree_entry_to_object(&obj, repo, entry)) {
909 switch (git_object_type(obj)) {
914 ret = writefilestree(fp, (git_tree *)obj,
916 git_object_free(obj);
921 git_object_free(obj);
925 filesize = git_blob_rawsize((git_blob *)obj);
926 lc = writeblob(obj, filepath, entryname, filesize);
928 fputs("<tr><td>", fp);
929 fputs(filemode(git_tree_entry_filemode(entry)), fp);
930 fprintf(fp, "</td><td><a href=\"%s", relpath);
931 xmlencode(fp, filepath, strlen(filepath));
933 xmlencode(fp, entrypath, strlen(entrypath));
934 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
936 fprintf(fp, "%dL", lc);
938 fprintf(fp, "%juB", (uintmax_t)filesize);
939 fputs("</td></tr>\n", fp);
940 git_object_free(obj);
941 } else if (!git_submodule_lookup(&module, repo, entryname)) {
942 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
944 xmlencode(fp, entrypath, strlen(entrypath));
945 git_submodule_free(module);
946 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
954 writefiles(FILE *fp, const git_oid *id)
956 git_tree *tree = NULL;
957 git_commit *commit = NULL;
960 fputs("<table id=\"files\"><thead>\n<tr>"
961 "<td><b>Mode</b></td><td><b>Name</b></td>"
962 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
963 "</tr>\n</thead><tbody>\n", fp);
965 if (!git_commit_lookup(&commit, repo, id) &&
966 !git_commit_tree(&tree, commit))
967 ret = writefilestree(fp, tree, "");
969 fputs("</tbody></table>", fp);
971 git_commit_free(commit);
978 refs_cmp(const void *v1, const void *v2)
980 git_reference *r1 = (*(git_reference **)v1);
981 git_reference *r2 = (*(git_reference **)v2);
984 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
987 return strcmp(git_reference_shorthand(r1),
988 git_reference_shorthand(r2));
994 struct commitinfo *ci;
995 const git_oid *id = NULL;
996 git_object *obj = NULL;
997 git_reference *dref = NULL, *r, *ref = NULL;
998 git_reference_iterator *it = NULL;
999 git_reference **refs = NULL;
1000 size_t count, i, j, refcount;
1001 const char *titles[] = { "Branches", "Tags" };
1002 const char *ids[] = { "branches", "tags" };
1005 if (git_reference_iterator_new(&it, repo))
1008 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1009 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1011 refs[refcount] = ref;
1013 git_reference_iterator_free(it);
1015 /* sort by type then shorthand name */
1016 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1018 for (j = 0; j < 2; j++) {
1019 for (i = 0, count = 0; i < refcount; i++) {
1020 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1021 !(git_reference_is_tag(refs[i]) && j == 1))
1024 switch (git_reference_type(refs[i])) {
1025 case GIT_REF_SYMBOLIC:
1026 if (git_reference_resolve(&dref, refs[i]))
1036 if (!git_reference_target(r) ||
1037 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1039 if (!(id = git_object_id(obj)))
1041 if (!(ci = commitinfo_getbyoid(id)))
1044 /* print header if it has an entry (first). */
1046 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1047 "<thead>\n<tr><td><b>Name</b></td>"
1048 "<td><b>Last commit date</b></td>"
1049 "<td><b>Author</b></td>\n</tr>\n"
1050 "</thead><tbody>\n",
1055 name = git_reference_shorthand(r);
1057 fputs("<tr><td>", fp);
1058 xmlencode(fp, name, strlen(name));
1059 fputs("</td><td>", fp);
1061 printtimeshort(fp, &(ci->author->when));
1062 fputs("</td><td>", fp);
1064 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1065 fputs("</td></tr>\n", fp);
1069 commitinfo_free(ci);
1070 git_object_free(obj);
1072 git_reference_free(dref);
1077 fputs("</tbody></table><br/>", fp);
1081 git_object_free(obj);
1082 git_reference_free(dref);
1084 for (i = 0; i < refcount; i++)
1085 git_reference_free(refs[i]);
1094 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1099 main(int argc, char *argv[])
1101 git_object *obj = NULL;
1102 const git_oid *head = NULL;
1105 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1106 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1110 for (i = 1; i < argc; i++) {
1111 if (argv[i][0] != '-') {
1115 } else if (argv[i][1] == 'c') {
1116 if (nlogcommits > 0 || i + 1 >= argc)
1118 cachefile = argv[++i];
1119 } else if (argv[i][1] == 'l') {
1120 if (cachefile || i + 1 >= argc)
1123 nlogcommits = strtoll(argv[++i], &p, 10);
1124 if (argv[i][0] == '\0' || *p != '\0' ||
1125 nlogcommits <= 0 || errno)
1132 if (!realpath(repodir, repodirabs))
1138 if (unveil(repodir, "r") == -1)
1139 err(1, "unveil: %s", repodir);
1140 if (unveil(".", "rwc") == -1)
1141 err(1, "unveil: .");
1142 if (cachefile && unveil(cachefile, "rwc") == -1)
1143 err(1, "unveil: %s", cachefile);
1146 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1149 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1154 if (git_repository_open_ext(&repo, repodir,
1155 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1156 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1161 if (!git_revparse_single(&obj, repo, "HEAD"))
1162 head = git_object_id(obj);
1163 git_object_free(obj);
1165 /* use directory name as name */
1166 if ((name = strrchr(repodirabs, '/')))
1173 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1174 cp("/usr/local/share/doc/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1175 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1176 cp("/usr/local/share/doc/stagit/style.min.css", strcat(cwd, "/style.min.css"));
1178 /* strip .git suffix */
1179 if (!(strippedname = strdup(name)))
1181 if ((p = strrchr(strippedname, '.')))
1182 if (!strcmp(p, ".git"))
1185 /* read description or .git/description */
1186 joinpath(path, sizeof(path), repodir, "description");
1187 if (!(fpread = fopen(path, "r"))) {
1188 joinpath(path, sizeof(path), repodir, ".git/description");
1189 fpread = fopen(path, "r");
1192 if (!fgets(description, sizeof(description), fpread))
1193 description[0] = '\0';
1197 /* read url or .git/url */
1198 joinpath(path, sizeof(path), repodir, "url");
1199 if (!(fpread = fopen(path, "r"))) {
1200 joinpath(path, sizeof(path), repodir, ".git/url");
1201 fpread = fopen(path, "r");
1204 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1206 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1211 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1212 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1213 git_object_type(obj) == GIT_OBJ_BLOB)
1214 license = licensefiles[i] + strlen("HEAD:");
1215 git_object_free(obj);
1219 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1220 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1221 git_object_type(obj) == GIT_OBJ_BLOB)
1222 readme = readmefiles[i] + strlen("HEAD:");
1223 git_object_free(obj);
1226 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1227 git_object_type(obj) == GIT_OBJ_BLOB)
1228 submodules = ".gitmodules";
1229 git_object_free(obj);
1232 fp = efopen("log.html", "w");
1234 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1235 writeheader(fp, "Log");
1236 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1237 "<td><b>Commit</b></td>"
1238 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1239 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1240 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1242 if (cachefile && head) {
1243 /* read from cache file (does not need to exist) */
1244 if ((rcachefp = fopen(cachefile, "r"))) {
1245 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1246 errx(1, "%s: no object id", cachefile);
1247 if (git_oid_fromstr(&lastoid, lastoidstr))
1248 errx(1, "%s: invalid object id", cachefile);
1251 /* write log to (temporary) cache */
1252 if ((fd = mkstemp(tmppath)) == -1)
1254 if (!(wcachefp = fdopen(fd, "w")))
1255 err(1, "fdopen: '%s'", tmppath);
1256 /* write last commit id (HEAD) */
1257 git_oid_tostr(buf, sizeof(buf), head);
1258 fprintf(wcachefp, "%s\n", buf);
1263 /* append previous log to log.html and the new cache */
1264 while (!feof(rcachefp)) {
1265 n = fread(buf, 1, sizeof(buf), rcachefp);
1266 if (ferror(rcachefp))
1268 if (fwrite(buf, 1, n, fp) != n ||
1269 fwrite(buf, 1, n, wcachefp) != n)
1280 fputs("</tbody></table>", fp);
1284 /* files for HEAD */
1285 fp = efopen("files.html", "w");
1286 writeheader(fp, "Files");
1288 writefiles(fp, head);
1292 /* summary page with branches and tags */
1293 fp = efopen("refs.html", "w");
1294 writeheader(fp, "Refs");
1300 fp = efopen("atom.xml", "w");
1304 /* rename new cache file on success */
1305 if (cachefile && head) {
1306 if (rename(tmppath, cachefile))
1307 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1308 umask((mask = umask(0)));
1309 if (chmod(cachefile,
1310 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1311 err(1, "chmod: '%s'", cachefile);
1315 git_repository_free(repo);
1316 git_libgit2_shutdown();