11 static git_repository *repo;
13 static const char *relpath = "";
15 static char description[255] = "Repositories";
16 static char *name = "";
17 static char owner[255];
20 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
24 r = snprintf(buf, bufsiz, "%s%s%s",
25 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
26 if (r < 0 || (size_t)r >= bufsiz)
27 errx(1, "path truncated: '%s%s%s'",
28 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
31 /* Escape characters below as HTML 2.0 / XML 1.0. */
33 xmlencode(FILE *fp, const char *s, size_t len)
37 for (i = 0; *s && i < len; s++, i++) {
39 case '<': fputs("<", fp); break;
40 case '>': fputs(">", fp); break;
41 case '\'': fputs("'" , fp); break;
42 case '&': fputs("&", fp); break;
43 case '"': fputs(""", fp); break;
44 default: fputc(*s, fp);
50 printtimeshort(FILE *fp, const git_time *intime)
56 t = (time_t)intime->time;
57 if (!(intm = gmtime(&t)))
59 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
66 fputs("<!DOCTYPE html>\n"
68 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
70 xmlencode(fp, description, strlen(description));
71 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
72 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
73 fputs("</head>\n<body>\n", fp);
74 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
75 "<td><span class=\"desc\">", relpath);
76 xmlencode(fp, description, strlen(description));
77 fputs("</span></td></tr><tr><td></td><td>\n"
78 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
79 "<table id=\"index\"><thead>\n"
80 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
81 "<td><b>Last commit</b></td></tr>"
82 "</thead><tbody>\n", fp);
88 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
94 git_commit *commit = NULL;
95 const git_signature *author;
96 git_revwalk *w = NULL;
98 char *stripped_name = NULL, *p;
101 git_revwalk_new(&w, repo);
102 git_revwalk_push_head(w);
103 git_revwalk_simplify_first_parent(w);
105 if (git_revwalk_next(&id, w) ||
106 git_commit_lookup(&commit, repo, &id)) {
111 author = git_commit_author(commit);
113 /* strip .git suffix */
114 if (!(stripped_name = strdup(name)))
116 if ((p = strrchr(stripped_name, '.')))
117 if (!strcmp(p, ".git"))
120 fputs("<tr><td><a href=\"", fp);
121 xmlencode(fp, stripped_name, strlen(stripped_name));
122 fputs("/log.html\">", fp);
123 xmlencode(fp, stripped_name, strlen(stripped_name));
124 fputs("</a></td><td>", fp);
125 xmlencode(fp, description, strlen(description));
126 fputs("</td><td>", fp);
127 xmlencode(fp, owner, strlen(owner));
128 fputs("</td><td>", fp);
130 printtimeshort(fp, &(author->when));
131 fputs("</td></tr>", fp);
133 git_commit_free(commit);
142 main(int argc, char *argv[])
145 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
150 fprintf(stderr, "%s [repodir...]\n", argv[0]);
157 for (i = 1; i < argc; i++)
158 if (unveil(argv[i], "r") == -1)
159 err(1, "unveil: %s", argv[i]);
161 if (pledge("stdio rpath", NULL) == -1)
167 for (i = 1; i < argc; i++) {
169 if (!realpath(repodir, repodirabs))
172 if (git_repository_open_ext(&repo, repodir,
173 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
174 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
179 /* use directory name as name */
180 if ((name = strrchr(repodirabs, '/')))
185 /* read description or .git/description */
186 joinpath(path, sizeof(path), repodir, "description");
187 if (!(fp = fopen(path, "r"))) {
188 joinpath(path, sizeof(path), repodir, ".git/description");
189 fp = fopen(path, "r");
191 description[0] = '\0';
193 if (!fgets(description, sizeof(description), fp))
194 description[0] = '\0';
198 /* read owner or .git/owner */
199 joinpath(path, sizeof(path), repodir, "owner");
200 if (!(fp = fopen(path, "r"))) {
201 joinpath(path, sizeof(path), repodir, ".git/owner");
202 fp = fopen(path, "r");
206 if (!fgets(owner, sizeof(owner), fp))
208 owner[strcspn(owner, "\n")] = '\0';
216 git_repository_free(repo);
217 git_libgit2_shutdown();