16 static git_repository *repo;
18 static const char *relpath = "";
20 static char description[255] = "Repositories";
21 static char *name = "";
22 static char owner[255];
25 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
29 r = snprintf(buf, bufsiz, "%s%s%s",
30 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
31 if (r < 0 || (size_t)r >= bufsiz)
32 errx(1, "path truncated: '%s%s%s'",
33 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
36 /* Escape characters below as HTML 2.0 / XML 1.0. */
38 xmlencode(FILE *fp, const char *s, size_t len)
42 for (i = 0; *s && i < len; s++, i++) {
44 case '<': fputs("<", fp); break;
45 case '>': fputs(">", fp); break;
46 case '\'': fputs("'" , fp); break;
47 case '&': fputs("&", fp); break;
48 case '"': fputs(""", fp); break;
49 default: fputc(*s, fp);
55 printtimeshort(FILE *fp, const git_time *intime)
61 t = (time_t)intime->time;
62 if (!(intm = gmtime(&t)))
64 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
71 fputs("<!DOCTYPE html>\n"
73 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
75 xmlencode(fp, description, strlen(description));
76 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
77 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
78 fputs("</head>\n<body>\n", fp);
79 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
80 "<td><span class=\"desc\">", relpath);
81 xmlencode(fp, description, strlen(description));
82 fputs("</span></td></tr><tr><td></td><td>\n"
83 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
84 "<table id=\"index\"><thead>\n"
85 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
86 "<td><b>Last commit</b></td></tr>"
87 "</thead><tbody>\n", fp);
93 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
99 git_commit *commit = NULL;
100 const git_signature *author;
101 git_revwalk *w = NULL;
103 char *stripped_name = NULL, *p;
106 git_revwalk_new(&w, repo);
107 git_revwalk_push_head(w);
108 git_revwalk_simplify_first_parent(w);
110 if (git_revwalk_next(&id, w) ||
111 git_commit_lookup(&commit, repo, &id)) {
116 author = git_commit_author(commit);
118 /* strip .git suffix */
119 if (!(stripped_name = strdup(name)))
121 if ((p = strrchr(stripped_name, '.')))
122 if (!strcmp(p, ".git"))
125 fputs("<tr><td><a href=\"", fp);
126 xmlencode(fp, stripped_name, strlen(stripped_name));
127 fputs("/log.html\">", fp);
128 xmlencode(fp, stripped_name, strlen(stripped_name));
129 fputs("</a></td><td>", fp);
130 xmlencode(fp, description, strlen(description));
131 fputs("</td><td>", fp);
132 xmlencode(fp, owner, strlen(owner));
133 fputs("</td><td>", fp);
135 printtimeshort(fp, &(author->when));
136 fputs("</td></tr>", fp);
138 git_commit_free(commit);
147 main(int argc, char *argv[])
150 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
155 fprintf(stderr, "%s [repodir...]\n", argv[0]);
162 for (i = 1; i < argc; i++)
163 if (unveil(argv[i], "r") == -1)
164 err(1, "unveil: %s", argv[i]);
165 if (pledge("stdio rpath", NULL) == -1)
171 for (i = 1; i < argc; i++) {
173 if (!realpath(repodir, repodirabs))
176 if (git_repository_open_ext(&repo, repodir,
177 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
178 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
183 /* use directory name as name */
184 if ((name = strrchr(repodirabs, '/')))
189 /* read description or .git/description */
190 joinpath(path, sizeof(path), repodir, "description");
191 if (!(fp = fopen(path, "r"))) {
192 joinpath(path, sizeof(path), repodir, ".git/description");
193 fp = fopen(path, "r");
195 description[0] = '\0';
197 if (!fgets(description, sizeof(description), fp))
198 description[0] = '\0';
202 /* read owner or .git/owner */
203 joinpath(path, sizeof(path), repodir, "owner");
204 if (!(fp = fopen(path, "r"))) {
205 joinpath(path, sizeof(path), repodir, ".git/owner");
206 fp = fopen(path, "r");
210 if (!fgets(owner, sizeof(owner), fp))
212 owner[strcspn(owner, "\n")] = '\0';
220 git_repository_free(repo);
221 git_libgit2_shutdown();