17 static git_repository *repo;
19 static const char *relpath = "";
20 static const char *repodir;
22 static char description[255] = "Repositories";
23 static char *name = "";
24 static char owner[255];
26 /* Escape characters below as HTML 2.0 / XML 1.0. */
28 xmlencode(FILE *fp, const char *s, size_t len)
32 for (i = 0; *s && i < len; s++, i++) {
34 case '<': fputs("<", fp); break;
35 case '>': fputs(">", fp); break;
36 case '\'': fputs("'", fp); break;
37 case '&': fputs("&", fp); break;
38 case '"': fputs(""", fp); break;
39 default: fputc(*s, fp);
45 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
51 t = (time_t) intime->time + (intime->offset * 60);
53 strftime(out, sizeof(out), fmt, intm);
58 printtimeshort(FILE *fp, const git_time *intime)
60 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
66 fputs("<!DOCTYPE html>\n"
67 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
68 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
69 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
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><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
76 relpath, name, description);
77 fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
78 "<table id=\"index\"><thead>\n"
79 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
80 "</thead><tbody>\n", fp);
88 return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
94 char *stripped_name = NULL, *p;
95 git_commit *commit = NULL;
96 const git_signature *author;
97 git_revwalk *w = NULL;
101 git_revwalk_new(&w, repo);
102 git_revwalk_push_head(w);
103 git_revwalk_sorting(w, GIT_SORT_TIME);
104 git_revwalk_simplify_first_parent(w);
106 if (git_revwalk_next(&id, w) ||
107 git_commit_lookup(&commit, repo, &id)) {
112 author = git_commit_author(commit);
114 /* strip .git suffix */
115 if (!(stripped_name = strdup(name)))
117 if ((p = strrchr(stripped_name, '.')))
118 if (!strcmp(p, ".git"))
121 fputs("<tr><td><a href=\"", fp);
122 xmlencode(fp, stripped_name, strlen(stripped_name));
123 fputs("/log.html\">", fp);
124 xmlencode(fp, stripped_name, strlen(stripped_name));
125 fputs("</a></td><td>", fp);
126 xmlencode(fp, description, strlen(description));
127 fputs("</td><td>", fp);
128 xmlencode(fp, owner, strlen(owner));
129 fputs("</td><td>", fp);
131 printtimeshort(fp, &(author->when));
132 fputs("</td></tr>", fp);
134 git_commit_free(commit);
143 main(int argc, char *argv[])
145 const git_error *e = NULL;
147 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
151 fprintf(stderr, "%s [repodir...]\n", argv[0]);
158 for (i = 1; i < argc; i++) {
160 if (!realpath(repodir, repodirabs))
163 if (git_repository_open_ext(&repo, repodir,
164 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
166 fprintf(stderr, "%s: %s\n", argv[0], e->message);
171 /* use directory name as name */
172 if ((name = strrchr(repodirabs, '/')))
177 /* read description or .git/description */
178 description[0] = '\0';
179 r = snprintf(path, sizeof(path), "%s%s%s",
180 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
181 if (r == -1 || (size_t)r >= sizeof(path))
182 errx(1, "path truncated: '%s%s%s'",
183 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
184 if (!(fp = fopen(path, "r"))) {
185 r = snprintf(path, sizeof(path), "%s%s%s",
186 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
187 if (r == -1 || (size_t)r >= sizeof(path))
188 errx(1, "path truncated: '%s%s%s'",
189 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
190 fp = fopen(path, "r");
193 if (!fgets(description, sizeof(description), fp))
194 description[0] = '\0';
198 /* read owner or .git/owner */
200 r = snprintf(path, sizeof(path), "%s%s%s",
201 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
202 if (r == -1 || (size_t)r >= sizeof(path))
203 errx(1, "path truncated: '%s%s%s'",
204 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
205 if (!(fp = fopen(path, "r"))) {
206 r = snprintf(path, sizeof(path), "%s%s%s",
207 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
208 if (r == -1 || (size_t)r >= sizeof(path))
209 errx(1, "path truncated: '%s%s%s'",
210 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
211 fp = fopen(path, "r");
214 if (!fgets(owner, sizeof(owner), fp))
216 owner[strcspn(owner, "\n")] = '\0';
224 git_repository_free(repo);
225 git_libgit2_shutdown();