13 static git_repository *repo;
15 static const char *relpath = "";
17 static char description[255] = "Repositories";
18 static char *name = "";
19 static char owner[255];
22 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
26 r = snprintf(buf, bufsiz, "%s%s%s",
27 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
28 if (r < 0 || (size_t)r >= bufsiz)
29 errx(1, "path truncated: '%s%s%s'",
30 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
33 /* Escape characters below as HTML 2.0 / XML 1.0. */
35 xmlencode(FILE *fp, const char *s, size_t len)
39 for (i = 0; *s && i < len; s++, i++) {
41 case '<': fputs("<", fp); break;
42 case '>': fputs(">", fp); break;
43 case '\'': fputs("'" , fp); break;
44 case '&': fputs("&", fp); break;
45 case '"': fputs(""", fp); break;
46 default: putc(*s, fp);
52 printtimeshort(FILE *fp, const git_time *intime)
58 t = (time_t)intime->time;
59 if (!(intm = gmtime(&t)))
61 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
66 writeheader(char *path)
68 FILE *fp = fopen(path, "w");
69 fputs("<!DOCTYPE html>\n"
70 "<html lang=\"en\">\n<head>\n"
71 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
72 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
74 xmlencode(fp, description, strlen(description));
75 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
76 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
77 fputs("</head>\n<body>\n", fp);
78 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
80 xmlencode(fp, description, strlen(description));
81 fputs("</h1></td></tr><tr><td></td><td>\n"
82 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
83 "<table id=\"index\"><thead>\n"
84 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
85 "<td><b>Last commit</b></td></tr>"
86 "</thead><tbody>\n", fp);
91 writefooter(char *path)
93 FILE *fp = fopen(path, "a");
94 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
101 FILE *fp = fopen(path, "a");
102 git_commit *commit = NULL;
103 const git_signature *author;
104 git_revwalk *w = NULL;
106 char *stripped_name = NULL, *p;
109 git_revwalk_new(&w, repo);
110 git_revwalk_push_head(w);
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);
151 main(int argc, char *argv[])
154 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
159 fprintf(stderr, "%s [repodir...]\n", argv[0]);
166 for (i = 1; i < argc; i++)
167 if (unveil(argv[i], "r") == -1)
168 err(1, "unveil: %s", argv[i]);
170 if (pledge("stdio rpath", NULL) == -1)
174 writeheader("index.html");
176 for (i = 1; i < argc; i++) {
178 if (!realpath(repodir, repodirabs))
181 if (git_repository_open_ext(&repo, repodir,
182 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
183 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
188 /* use directory name as name */
189 if ((name = strrchr(repodirabs, '/')))
194 /* read description or .git/description */
195 joinpath(path, sizeof(path), repodir, "description");
196 if (!(fp = fopen(path, "r"))) {
197 joinpath(path, sizeof(path), repodir, ".git/description");
198 fp = fopen(path, "r");
200 description[0] = '\0';
202 if (!fgets(description, sizeof(description), fp))
203 description[0] = '\0';
207 /* read owner or .git/owner */
208 joinpath(path, sizeof(path), repodir, "owner");
209 if (!(fp = fopen(path, "r"))) {
210 joinpath(path, sizeof(path), repodir, ".git/owner");
211 fp = fopen(path, "r");
215 if (!fgets(owner, sizeof(owner), fp))
217 owner[strcspn(owner, "\n")] = '\0';
220 writelog("index.html");
222 writefooter("index.html");
226 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
227 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
230 git_repository_free(repo);
231 git_libgit2_shutdown();