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"
76 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
77 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
78 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
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><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
85 relpath, name, description);
86 fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
87 "<table id=\"index\"><thead>\n"
88 "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
89 "</thead><tbody>\n", fp);
95 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
101 git_commit *commit = NULL;
102 const git_signature *author;
103 git_revwalk *w = NULL;
105 char *stripped_name = NULL, *p;
108 git_revwalk_new(&w, repo);
109 git_revwalk_push_head(w);
110 git_revwalk_sorting(w, GIT_SORT_TIME);
111 git_revwalk_simplify_first_parent(w);
113 if (git_revwalk_next(&id, w) ||
114 git_commit_lookup(&commit, repo, &id)) {
119 author = git_commit_author(commit);
121 /* strip .git suffix */
122 if (!(stripped_name = strdup(name)))
124 if ((p = strrchr(stripped_name, '.')))
125 if (!strcmp(p, ".git"))
128 fputs("<tr><td><a href=\"", fp);
129 xmlencode(fp, stripped_name, strlen(stripped_name));
130 fputs("/log.html\">", fp);
131 xmlencode(fp, stripped_name, strlen(stripped_name));
132 fputs("</a></td><td>", fp);
133 xmlencode(fp, description, strlen(description));
134 fputs("</td><td>", fp);
135 xmlencode(fp, owner, strlen(owner));
136 fputs("</td><td>", fp);
138 printtimeshort(fp, &(author->when));
139 fputs("</td></tr>", fp);
141 git_commit_free(commit);
150 main(int argc, char *argv[])
152 const git_error *e = NULL;
154 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
158 if (pledge("stdio rpath", NULL) == -1)
162 fprintf(stderr, "%s [repodir...]\n", argv[0]);
169 for (i = 1; i < argc; i++) {
171 if (!realpath(repodir, repodirabs))
174 if (git_repository_open_ext(&repo, repodir,
175 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
177 fprintf(stderr, "%s: %s\n", argv[0], e->message);
182 /* use directory name as name */
183 if ((name = strrchr(repodirabs, '/')))
188 /* read description or .git/description */
189 joinpath(path, sizeof(path), repodir, "description");
190 if (!(fp = fopen(path, "r"))) {
191 joinpath(path, sizeof(path), repodir, ".git/description");
192 fp = fopen(path, "r");
194 description[0] = '\0';
196 if (!fgets(description, sizeof(description), fp))
197 description[0] = '\0';
201 /* read owner or .git/owner */
202 joinpath(path, sizeof(path), repodir, "owner");
203 if (!(fp = fopen(path, "r"))) {
204 joinpath(path, sizeof(path), repodir, ".git/owner");
205 fp = fopen(path, "r");
209 if (!fgets(owner, sizeof(owner), fp))
211 owner[strcspn(owner, "\n")] = '\0';
219 git_repository_free(repo);
220 git_libgit2_shutdown();