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[])
34 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
38 r = snprintf(buf, bufsiz, "%s%s%s",
39 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
40 if (r == -1 || (size_t)r >= bufsiz)
41 errx(1, "path truncated: '%s%s%s'",
42 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
45 /* Escape characters below as HTML 2.0 / XML 1.0. */
47 xmlencode(FILE *fp, const char *s, size_t len)
51 for (i = 0; *s && i < len; s++, i++) {
53 case '<': fputs("<", fp); break;
54 case '>': fputs(">", fp); break;
55 case '\'': fputs("'", fp); break;
56 case '&': fputs("&", fp); break;
57 case '"': fputs(""", fp); break;
58 default: fputc(*s, fp);
64 printtimeshort(FILE *fp, const git_time *intime)
70 t = (time_t)intime->time;
71 if (!(intm = gmtime(&t)))
73 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
80 fputs("<!DOCTYPE html>\n"
81 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
82 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
83 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
84 xmlencode(fp, description, strlen(description));
85 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
86 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
87 fputs("</head>\n<body>\n", fp);
88 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
89 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
90 relpath, name, description);
91 fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
92 "<table id=\"index\"><thead>\n"
93 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
94 "</thead><tbody>\n", fp);
100 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
106 git_commit *commit = NULL;
107 const git_signature *author;
108 git_revwalk *w = NULL;
110 char *stripped_name = NULL, *p;
113 git_revwalk_new(&w, repo);
114 git_revwalk_push_head(w);
115 git_revwalk_sorting(w, GIT_SORT_TIME);
116 git_revwalk_simplify_first_parent(w);
118 if (git_revwalk_next(&id, w) ||
119 git_commit_lookup(&commit, repo, &id)) {
124 author = git_commit_author(commit);
126 /* strip .git suffix */
127 if (!(stripped_name = strdup(name)))
129 if ((p = strrchr(stripped_name, '.')))
130 if (!strcmp(p, ".git"))
133 fputs("<tr><td><a href=\"", fp);
134 xmlencode(fp, stripped_name, strlen(stripped_name));
135 fputs("/log.html\">", fp);
136 xmlencode(fp, stripped_name, strlen(stripped_name));
137 fputs("</a></td><td>", fp);
138 xmlencode(fp, description, strlen(description));
139 fputs("</td><td>", fp);
140 xmlencode(fp, owner, strlen(owner));
141 fputs("</td><td>", fp);
143 printtimeshort(fp, &(author->when));
144 fputs("</td></tr>", fp);
146 git_commit_free(commit);
155 main(int argc, char *argv[])
157 const git_error *e = NULL;
159 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
162 if (pledge("stdio rpath", NULL) == -1)
166 fprintf(stderr, "%s [repodir...]\n", argv[0]);
173 for (i = 1; i < argc; i++) {
175 if (!realpath(repodir, repodirabs))
178 if (git_repository_open_ext(&repo, repodir,
179 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
181 fprintf(stderr, "%s: %s\n", argv[0], e->message);
186 /* use directory name as name */
187 if ((name = strrchr(repodirabs, '/')))
192 /* read description or .git/description */
193 joinpath(path, sizeof(path), repodir, "description");
194 if (!(fp = fopen(path, "r"))) {
195 joinpath(path, sizeof(path), repodir, ".git/description");
196 fp = fopen(path, "r");
198 description[0] = '\0';
200 if (!fgets(description, sizeof(description), fp))
201 description[0] = '\0';
205 /* read owner or .git/owner */
206 joinpath(path, sizeof(path), repodir, "owner");
207 if (!(fp = fopen(path, "r"))) {
208 joinpath(path, sizeof(path), repodir, ".git/owner");
209 fp = fopen(path, "r");
213 if (!fgets(owner, sizeof(owner), fp))
215 owner[strcspn(owner, "\n")] = '\0';
223 git_repository_free(repo);
224 git_libgit2_shutdown();