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[255];
24 static char owner[255];
27 efopen(const char *name, const char *flags)
31 if (!(fp = fopen(name, flags)))
37 /* Escape characters below as HTML 2.0 / XML 1.0. */
39 xmlencode(FILE *fp, const char *s, size_t len)
43 for (i = 0; *s && i < len; s++, i++) {
45 case '<': fputs("<", fp); break;
46 case '>': fputs(">", fp); break;
47 case '\'': fputs("'", fp); break;
48 case '&': fputs("&", fp); break;
49 case '"': fputs(""", fp); break;
50 default: fputc(*s, fp);
55 /* Some implementations of basename(3) return a pointer to a static
56 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
57 * This is a wrapper function that is compatible with both versions.
58 * The program will error out if basename(3) failed, this can only happen
59 * with the OpenBSD version. */
61 xbasename(const char *path)
65 if (!(p = strdup(path)))
67 if (!(b = basename(p)))
77 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
83 t = (time_t) intime->time + (intime->offset * 60);
85 strftime(out, sizeof(out), fmt, intm);
90 printtimeshort(FILE *fp, const git_time *intime)
92 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
98 fputs("<!DOCTYPE HTML>"
99 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
100 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
101 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
102 xmlencode(fp, description, strlen(description));
103 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
104 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
105 fputs("</head>\n<body>\n", fp);
106 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
107 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
108 relpath, name, description);
109 fputs("</td></tr>\n</table>\n<hr/><div id=\"content\">\n"
111 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
112 "</thead><tbody>\n", fp);
118 writefooter(FILE *fp)
120 return !fputs("</tbody></table></div></body>\n</html>", fp);
126 git_commit *commit = NULL;
127 const git_signature *author;
128 git_revwalk *w = NULL;
132 git_revwalk_new(&w, repo);
133 git_revwalk_push_head(w);
134 git_revwalk_sorting(w, GIT_SORT_TIME);
135 git_revwalk_simplify_first_parent(w);
137 if (git_revwalk_next(&id, w) ||
138 git_commit_lookup(&commit, repo, &id)) {
143 author = git_commit_author(commit);
145 fputs("<tr><td><a href=\"", fp);
146 xmlencode(fp, name, strlen(name));
147 fputs("/log.html\">", fp);
148 xmlencode(fp, name, strlen(name));
149 fputs("</a></td><td>", fp);
150 xmlencode(fp, description, strlen(description));
151 fputs("</td><td>", fp);
152 xmlencode(fp, owner, strlen(owner));
153 fputs("</td><td>", fp);
155 printtimeshort(fp, &(author->when));
156 fputs("</td></tr>", fp);
158 git_commit_free(commit);
166 main(int argc, char *argv[])
168 const git_error *e = NULL;
170 char path[PATH_MAX], *p;
174 fprintf(stderr, "%s [repodir...]\n", argv[0]);
181 for (i = 1; i < argc; i++) {
184 if ((status = git_repository_open_ext(&repo, repodir,
185 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
187 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
191 /* use directory name as name */
192 p = xbasename(repodir);
193 snprintf(name, sizeof(name), "%s", p);
196 /* read description or .git/description */
197 description[0] = '\0';
198 snprintf(path, sizeof(path), "%s%s%s",
199 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
200 if (!(fp = fopen(path, "r"))) {
201 snprintf(path, sizeof(path), "%s%s%s",
202 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
203 fp = fopen(path, "r");
206 if (!fgets(description, sizeof(description), fp))
207 description[0] = '\0';
211 /* read owner or .git/owner */
213 snprintf(path, sizeof(path), "%s%s%s",
214 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
215 if (!(fp = fopen(path, "r"))) {
216 snprintf(path, sizeof(path), "%s%s%s",
217 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
218 fp = fopen(path, "r");
221 if (!fgets(owner, sizeof(owner), fp))
230 git_repository_free(repo);
231 git_libgit2_shutdown();