13 static git_repository *repo;
15 static const char *relpath = "";
17 static char description[255] = "Repositories";
18 static char *name = "";
19 static char owner[255];
22 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
26 r = snprintf(buf, bufsiz, "%s%s%s",
27 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
28 if (r < 0 || (size_t)r >= bufsiz)
29 errx(1, "path truncated: '%s%s%s'",
30 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
33 /* Escape characters below as HTML 2.0 / XML 1.0. */
35 xmlencode(FILE *fp, const char *s, size_t len)
39 for (i = 0; *s && i < len; s++, i++) {
41 case '<': fputs("<", fp); break;
42 case '>': fputs(">", fp); break;
43 case '\'': fputs("'" , fp); break;
44 case '&': fputs("&", fp); break;
45 case '"': fputs(""", fp); break;
46 default: fputc(*s, fp);
52 printtimeshort(FILE *fp, const git_time *intime)
58 t = (time_t)intime->time;
59 if (!(intm = gmtime(&t)))
61 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
66 writeheader(char *path)
68 FILE *fp = fopen(path, "w");
69 fputs("<!DOCTYPE html>\n"
71 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
73 xmlencode(fp, description, strlen(description));
74 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
75 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
76 fputs("</head>\n<body>\n", fp);
77 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
79 xmlencode(fp, description, strlen(description));
80 fputs("</h1></td></tr><tr><td></td><td>\n"
81 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
82 "<table id=\"index\"><thead>\n"
83 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
84 "<td><b>Last commit</b></td></tr>"
85 "</thead><tbody>\n", fp);
90 writefooter(char *path)
92 FILE *fp = fopen(path, "a");
93 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
100 FILE *fp = fopen(path, "a");
101 git_commit *commit = NULL;
102 const git_signature *author;
103 git_revwalk *w = NULL;
105 char *stripped_name = NULL, *p;
108 git_revwalk_new(&w, repo);
109 git_revwalk_push_head(w);
110 git_revwalk_simplify_first_parent(w);
112 if (git_revwalk_next(&id, w) ||
113 git_commit_lookup(&commit, repo, &id)) {
118 author = git_commit_author(commit);
120 /* strip .git suffix */
121 if (!(stripped_name = strdup(name)))
123 if ((p = strrchr(stripped_name, '.')))
124 if (!strcmp(p, ".git"))
127 fputs("<tr><td><a href=\"", fp);
128 xmlencode(fp, stripped_name, strlen(stripped_name));
129 fputs("/log.html\">", fp);
130 xmlencode(fp, stripped_name, strlen(stripped_name));
131 fputs("</a></td><td>", fp);
132 xmlencode(fp, description, strlen(description));
133 fputs("</td><td>", fp);
134 xmlencode(fp, owner, strlen(owner));
135 fputs("</td><td>", fp);
137 printtimeshort(fp, &(author->when));
138 fputs("</td></tr>", fp);
140 git_commit_free(commit);
150 main(int argc, char *argv[])
153 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
158 fprintf(stderr, "%s [repodir...]\n", argv[0]);
165 for (i = 1; i < argc; i++)
166 if (unveil(argv[i], "r") == -1)
167 err(1, "unveil: %s", argv[i]);
169 if (pledge("stdio rpath", NULL) == -1)
173 writeheader("index.html");
175 for (i = 1; i < argc; i++) {
177 if (!realpath(repodir, repodirabs))
180 if (git_repository_open_ext(&repo, repodir,
181 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
182 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
187 /* use directory name as name */
188 if ((name = strrchr(repodirabs, '/')))
193 /* read description or .git/description */
194 joinpath(path, sizeof(path), repodir, "description");
195 if (!(fp = fopen(path, "r"))) {
196 joinpath(path, sizeof(path), repodir, ".git/description");
197 fp = fopen(path, "r");
199 description[0] = '\0';
201 if (!fgets(description, sizeof(description), fp))
202 description[0] = '\0';
206 /* read owner or .git/owner */
207 joinpath(path, sizeof(path), repodir, "owner");
208 if (!(fp = fopen(path, "r"))) {
209 joinpath(path, sizeof(path), repodir, ".git/owner");
210 fp = fopen(path, "r");
214 if (!fgets(owner, sizeof(owner), fp))
216 owner[strcspn(owner, "\n")] = '\0';
219 writelog("index.html");
221 writefooter("index.html");
225 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
226 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
229 git_repository_free(repo);
230 git_libgit2_shutdown();