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);
52 if (!(intm = gmtime(&t)))
54 strftime(out, sizeof(out), fmt, intm);
59 printtimeshort(FILE *fp, const git_time *intime)
61 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
67 fputs("<!DOCTYPE html>\n"
68 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
69 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
70 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
71 xmlencode(fp, description, strlen(description));
72 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
73 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
74 fputs("</head>\n<body>\n", fp);
75 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
76 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
77 relpath, name, description);
78 fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
79 "<table id=\"index\"><thead>\n"
80 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
81 "</thead><tbody>\n", fp);
89 return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
95 char *stripped_name = NULL, *p;
96 git_commit *commit = NULL;
97 const git_signature *author;
98 git_revwalk *w = NULL;
102 git_revwalk_new(&w, repo);
103 git_revwalk_push_head(w);
104 git_revwalk_sorting(w, GIT_SORT_TIME);
105 git_revwalk_simplify_first_parent(w);
107 if (git_revwalk_next(&id, w) ||
108 git_commit_lookup(&commit, repo, &id)) {
113 author = git_commit_author(commit);
115 /* strip .git suffix */
116 if (!(stripped_name = strdup(name)))
118 if ((p = strrchr(stripped_name, '.')))
119 if (!strcmp(p, ".git"))
122 fputs("<tr><td><a href=\"", fp);
123 xmlencode(fp, stripped_name, strlen(stripped_name));
124 fputs("/log.html\">", fp);
125 xmlencode(fp, stripped_name, strlen(stripped_name));
126 fputs("</a></td><td>", fp);
127 xmlencode(fp, description, strlen(description));
128 fputs("</td><td>", fp);
129 xmlencode(fp, owner, strlen(owner));
130 fputs("</td><td>", fp);
132 printtimeshort(fp, &(author->when));
133 fputs("</td></tr>", fp);
135 git_commit_free(commit);
144 main(int argc, char *argv[])
146 const git_error *e = NULL;
148 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
152 fprintf(stderr, "%s [repodir...]\n", argv[0]);
159 for (i = 1; i < argc; i++) {
161 if (!realpath(repodir, repodirabs))
164 if (git_repository_open_ext(&repo, repodir,
165 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
167 fprintf(stderr, "%s: %s\n", argv[0], e->message);
172 /* use directory name as name */
173 if ((name = strrchr(repodirabs, '/')))
178 /* read description or .git/description */
179 description[0] = '\0';
180 r = snprintf(path, sizeof(path), "%s%s%s",
181 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
182 if (r == -1 || (size_t)r >= sizeof(path))
183 errx(1, "path truncated: '%s%s%s'",
184 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
185 if (!(fp = fopen(path, "r"))) {
186 r = snprintf(path, sizeof(path), "%s%s%s",
187 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
188 if (r == -1 || (size_t)r >= sizeof(path))
189 errx(1, "path truncated: '%s%s%s'",
190 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
191 fp = fopen(path, "r");
194 if (!fgets(description, sizeof(description), fp))
195 description[0] = '\0';
199 /* read owner or .git/owner */
201 r = snprintf(path, sizeof(path), "%s%s%s",
202 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
203 if (r == -1 || (size_t)r >= sizeof(path))
204 errx(1, "path truncated: '%s%s%s'",
205 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
206 if (!(fp = fopen(path, "r"))) {
207 r = snprintf(path, sizeof(path), "%s%s%s",
208 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
209 if (r == -1 || (size_t)r >= sizeof(path))
210 errx(1, "path truncated: '%s%s%s'",
211 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
212 fp = fopen(path, "r");
215 if (!fgets(owner, sizeof(owner), fp))
217 owner[strcspn(owner, "\n")] = '\0';
225 git_repository_free(repo);
226 git_libgit2_shutdown();