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 #define pledge(p1,p2) 0
29 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
33 r = snprintf(buf, bufsiz, "%s%s%s",
34 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
35 if (r == -1 || (size_t)r >= bufsiz)
36 errx(1, "path truncated: '%s%s%s'",
37 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
40 /* Escape characters below as HTML 2.0 / XML 1.0. */
42 xmlencode(FILE *fp, const char *s, size_t len)
46 for (i = 0; *s && i < len; s++, i++) {
48 case '<': fputs("<", fp); break;
49 case '>': fputs(">", fp); break;
50 case '\'': fputs("'" , fp); break;
51 case '&': fputs("&", fp); break;
52 case '"': fputs(""", fp); break;
53 default: fputc(*s, fp);
59 printtimeshort(FILE *fp, const git_time *intime)
65 t = (time_t)intime->time;
66 if (!(intm = gmtime(&t)))
68 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
75 fputs("<!DOCTYPE html>\n"
77 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
79 xmlencode(fp, description, strlen(description));
80 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
81 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
82 fputs("</head>\n<body>\n", fp);
83 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
84 "<td><span class=\"desc\">", relpath);
85 xmlencode(fp, description, strlen(description));
86 fputs("</span></td></tr><tr><td></td><td>\n"
87 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
88 "<table id=\"index\"><thead>\n"
89 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
90 "<td><b>Last commit</b></td></tr>"
91 "</thead><tbody>\n", fp);
97 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
103 git_commit *commit = NULL;
104 const git_signature *author;
105 git_revwalk *w = NULL;
107 char *stripped_name = NULL, *p;
110 git_revwalk_new(&w, repo);
111 git_revwalk_push_head(w);
112 git_revwalk_sorting(w, GIT_SORT_TIME);
113 git_revwalk_simplify_first_parent(w);
115 if (git_revwalk_next(&id, w) ||
116 git_commit_lookup(&commit, repo, &id)) {
121 author = git_commit_author(commit);
123 /* strip .git suffix */
124 if (!(stripped_name = strdup(name)))
126 if ((p = strrchr(stripped_name, '.')))
127 if (!strcmp(p, ".git"))
130 fputs("<tr><td><a href=\"", fp);
131 xmlencode(fp, stripped_name, strlen(stripped_name));
132 fputs("/log.html\">", fp);
133 xmlencode(fp, stripped_name, strlen(stripped_name));
134 fputs("</a></td><td>", fp);
135 xmlencode(fp, description, strlen(description));
136 fputs("</td><td>", fp);
137 xmlencode(fp, owner, strlen(owner));
138 fputs("</td><td>", fp);
140 printtimeshort(fp, &(author->when));
141 fputs("</td></tr>", fp);
143 git_commit_free(commit);
152 main(int argc, char *argv[])
154 const git_error *e = NULL;
156 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
161 fprintf(stderr, "%s [repodir...]\n", argv[0]);
167 if (pledge("stdio rpath", NULL) == -1)
172 for (i = 1; i < argc; i++) {
174 if (!realpath(repodir, repodirabs))
177 if (git_repository_open_ext(&repo, repodir,
178 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
180 fprintf(stderr, "%s: %s\n", argv[0], e->message);
185 /* use directory name as name */
186 if ((name = strrchr(repodirabs, '/')))
191 /* read description or .git/description */
192 joinpath(path, sizeof(path), repodir, "description");
193 if (!(fp = fopen(path, "r"))) {
194 joinpath(path, sizeof(path), repodir, ".git/description");
195 fp = fopen(path, "r");
197 description[0] = '\0';
199 if (!fgets(description, sizeof(description), fp))
200 description[0] = '\0';
204 /* read owner or .git/owner */
205 joinpath(path, sizeof(path), repodir, "owner");
206 if (!(fp = fopen(path, "r"))) {
207 joinpath(path, sizeof(path), repodir, ".git/owner");
208 fp = fopen(path, "r");
212 if (!fgets(owner, sizeof(owner), fp))
214 owner[strcspn(owner, "\n")] = '\0';
222 git_repository_free(repo);
223 git_libgit2_shutdown();