16 static git_repository *repo;
18 static const char *relpath = "";
19 static const char *repodir;
21 static char description[255] = "Repositories";
22 static char *name = "";
23 static char owner[255];
27 pledge(const char *promises, const char *paths[])
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);
68 fputs("<!DOCTYPE html>\n"
69 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
70 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
71 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
72 xmlencode(fp, description, strlen(description));
73 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
74 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
75 fputs("</head>\n<body>\n", fp);
76 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
77 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
78 relpath, name, description);
79 fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
80 "<table id=\"index\"><thead>\n"
81 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
82 "</thead><tbody>\n", fp);
90 return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
96 git_commit *commit = NULL;
97 const git_signature *author;
98 git_revwalk *w = NULL;
100 char *stripped_name = NULL, *p;
103 git_revwalk_new(&w, repo);
104 git_revwalk_push_head(w);
105 git_revwalk_sorting(w, GIT_SORT_TIME);
106 git_revwalk_simplify_first_parent(w);
108 if (git_revwalk_next(&id, w) ||
109 git_commit_lookup(&commit, repo, &id)) {
114 author = git_commit_author(commit);
116 /* strip .git suffix */
117 if (!(stripped_name = strdup(name)))
119 if ((p = strrchr(stripped_name, '.')))
120 if (!strcmp(p, ".git"))
123 fputs("<tr><td><a href=\"", fp);
124 xmlencode(fp, stripped_name, strlen(stripped_name));
125 fputs("/log.html\">", fp);
126 xmlencode(fp, stripped_name, strlen(stripped_name));
127 fputs("</a></td><td>", fp);
128 xmlencode(fp, description, strlen(description));
129 fputs("</td><td>", fp);
130 xmlencode(fp, owner, strlen(owner));
131 fputs("</td><td>", fp);
133 printtimeshort(fp, &(author->when));
134 fputs("</td></tr>", fp);
136 git_commit_free(commit);
145 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
149 r = snprintf(buf, bufsiz, "%s%s%s",
150 repodir, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
151 if (r == -1 || (size_t)r >= bufsiz)
152 errx(1, "path truncated: '%s%s%s'",
153 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
157 main(int argc, char *argv[])
159 const git_error *e = NULL;
161 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
164 if (pledge("stdio rpath", NULL) == -1)
168 fprintf(stderr, "%s [repodir...]\n", argv[0]);
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)) {
183 fprintf(stderr, "%s: %s\n", argv[0], e->message);
188 /* use directory name as name */
189 if ((name = strrchr(repodirabs, '/')))
194 /* read description or .git/description */
195 joinpath(path, sizeof(path), repodir, "description");
196 if (!(fp = fopen(path, "r"))) {
197 joinpath(path, sizeof(path), repodir, ".git/description");
198 fp = fopen(path, "r");
200 description[0] = '\0';
202 if (!fgets(description, sizeof(description), fp))
203 description[0] = '\0';
207 /* read owner or .git/owner */
208 joinpath(path, sizeof(path), repodir, "owner");
209 if (!(fp = fopen(path, "r"))) {
210 joinpath(path, sizeof(path), repodir, ".git/owner");
211 fp = fopen(path, "r");
215 if (!fgets(owner, sizeof(owner), fp))
217 owner[strcspn(owner, "\n")] = '\0';
225 git_repository_free(repo);
226 git_libgit2_shutdown();