18 static git_repository *repo;
20 static const char *relpath = "";
21 static const char *repodir;
23 static char description[255] = "Repositories";
24 static char name[255];
25 static char owner[255];
28 efopen(const char *name, const char *flags)
32 if (!(fp = fopen(name, flags)))
38 /* Escape characters below as HTML 2.0 / XML 1.0. */
40 xmlencode(FILE *fp, const char *s, size_t len)
44 for (i = 0; *s && i < len; s++, i++) {
46 case '<': fputs("<", fp); break;
47 case '>': fputs(">", fp); break;
48 case '\'': fputs("'", fp); break;
49 case '&': fputs("&", fp); break;
50 case '"': fputs(""", fp); break;
51 default: fputc(*s, fp);
56 /* Some implementations of basename(3) return a pointer to a static
57 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
58 * This is a wrapper function that is compatible with both versions.
59 * The program will error out if basename(3) failed, this can only happen
60 * with the OpenBSD version. */
62 xbasename(const char *path)
66 if (!(p = strdup(path)))
68 if (!(b = basename(p)))
78 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
84 t = (time_t) intime->time + (intime->offset * 60);
86 strftime(out, sizeof(out), fmt, intm);
91 printtimeshort(FILE *fp, const git_time *intime)
93 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
99 fputs("<!DOCTYPE HTML>"
100 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
101 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
102 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
103 xmlencode(fp, description, strlen(description));
104 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
105 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
106 fputs("</head>\n<body>\n", fp);
107 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
108 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
109 relpath, name, description);
110 fputs("</td></tr>\n</table>\n<hr/><div id=\"content\">\n"
111 "<table id=\"index\"><thead>\n"
112 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
113 "</thead><tbody>\n", fp);
119 writefooter(FILE *fp)
121 return !fputs("</tbody></table></div></body>\n</html>", fp);
127 git_commit *commit = NULL;
128 const git_signature *author;
129 git_revwalk *w = NULL;
133 git_revwalk_new(&w, repo);
134 git_revwalk_push_head(w);
135 git_revwalk_sorting(w, GIT_SORT_TIME);
136 git_revwalk_simplify_first_parent(w);
138 if (git_revwalk_next(&id, w) ||
139 git_commit_lookup(&commit, repo, &id)) {
144 author = git_commit_author(commit);
146 fputs("<tr><td><a href=\"", fp);
147 xmlencode(fp, name, strlen(name));
148 fputs("/log.html\">", fp);
149 xmlencode(fp, name, strlen(name));
150 fputs("</a></td><td>", fp);
151 xmlencode(fp, description, strlen(description));
152 fputs("</td><td>", fp);
153 xmlencode(fp, owner, strlen(owner));
154 fputs("</td><td>", fp);
156 printtimeshort(fp, &(author->when));
157 fputs("</td></tr>", fp);
159 git_commit_free(commit);
167 main(int argc, char *argv[])
169 const git_error *e = NULL;
171 char path[PATH_MAX], *p;
175 fprintf(stderr, "%s [repodir...]\n", argv[0]);
182 for (i = 1; i < argc; i++) {
185 if (git_repository_open_ext(&repo, repodir,
186 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
188 fprintf(stderr, "%s: %s\n", argv[0], e->message);
193 /* use directory name as name */
194 p = xbasename(repodir);
195 snprintf(name, sizeof(name), "%s", p);
198 /* read description or .git/description */
199 description[0] = '\0';
200 snprintf(path, sizeof(path), "%s%s%s",
201 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
202 if (!(fp = fopen(path, "r"))) {
203 snprintf(path, sizeof(path), "%s%s%s",
204 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
205 fp = fopen(path, "r");
208 if (!fgets(description, sizeof(description), fp))
209 description[0] = '\0';
213 /* read owner or .git/owner */
215 snprintf(path, sizeof(path), "%s%s%s",
216 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
217 if (!(fp = fopen(path, "r"))) {
218 snprintf(path, sizeof(path), "%s%s%s",
219 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
220 fp = fopen(path, "r");
223 if (!fgets(owner, sizeof(owner), fp))
225 owner[strcspn(owner, "\n")] = '\0';
233 git_repository_free(repo);
234 git_libgit2_shutdown();