]> git.armaanb.net Git - stagit.git/blob - src/stagit-index.c
Implement lighthouse suggestions
[stagit.git] / src / stagit-index.c
1 #include <err.h>
2 #include <limits.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7 #include <unistd.h>
8
9 #include <git2.h>
10
11 #include "cp.h"
12
13 static git_repository *repo;
14
15 static const char *relpath = "";
16
17 static char description[255] = "Repositories";
18 static char *name = "";
19 static char owner[255];
20
21 void
22 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
23 {
24         int r;
25
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);
31 }
32
33 /* Escape characters below as HTML 2.0 / XML 1.0. */
34 void
35 xmlencode(FILE *fp, const char *s, size_t len)
36 {
37         size_t i;
38
39         for (i = 0; *s && i < len; s++, i++) {
40                 switch(*s) {
41                 case '<':  fputs("&lt;",   fp); break;
42                 case '>':  fputs("&gt;",   fp); break;
43                 case '\'': fputs("&#39;" , fp); break;
44                 case '&':  fputs("&amp;",  fp); break;
45                 case '"':  fputs("&quot;", fp); break;
46                 default:   fputc(*s, fp);
47                 }
48         }
49 }
50
51 void
52 printtimeshort(FILE *fp, const git_time *intime)
53 {
54         struct tm *intm;
55         time_t t;
56         char out[32];
57
58         t = (time_t)intime->time;
59         if (!(intm = gmtime(&t)))
60                 return;
61         strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
62         fputs(out, fp);
63 }
64
65 void
66 writeheader(char *path)
67 {
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"
73                 "<title>", fp);
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"
79                 "<td><h1>", relpath);
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);
87         fclose(fp);
88 }
89
90 void
91 writefooter(char *path)
92 {
93         FILE *fp = fopen(path, "a");
94         fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
95         fclose(fp);
96 }
97
98 int
99 writelog(char *path)
100 {
101         FILE *fp = fopen(path, "a");
102         git_commit *commit = NULL;
103         const git_signature *author;
104         git_revwalk *w = NULL;
105         git_oid id;
106         char *stripped_name = NULL, *p;
107         int ret = 0;
108
109         git_revwalk_new(&w, repo);
110         git_revwalk_push_head(w);
111         git_revwalk_simplify_first_parent(w);
112
113         if (git_revwalk_next(&id, w) ||
114             git_commit_lookup(&commit, repo, &id)) {
115                 ret = -1;
116                 goto err;
117         }
118
119         author = git_commit_author(commit);
120
121         /* strip .git suffix */
122         if (!(stripped_name = strdup(name)))
123                 err(1, "strdup");
124         if ((p = strrchr(stripped_name, '.')))
125                 if (!strcmp(p, ".git"))
126                         *p = '\0';
127
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);
137         if (author)
138                 printtimeshort(fp, &(author->when));
139         fputs("</td></tr>", fp);
140
141         git_commit_free(commit);
142 err:
143         git_revwalk_free(w);
144         free(stripped_name);
145         fclose(fp);
146
147         return ret;
148 }
149
150 int
151 main(int argc, char *argv[])
152 {
153         FILE *fp;
154         char path[PATH_MAX], repodirabs[PATH_MAX + 1];
155         const char *repodir;
156         int i, ret = 0;
157
158         if (argc < 2) {
159                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
160                 return 1;
161         }
162
163         git_libgit2_init();
164
165 #ifdef __OpenBSD__
166         for (i = 1; i < argc; i++)
167                 if (unveil(argv[i], "r") == -1)
168                         err(1, "unveil: %s", argv[i]);
169
170         if (pledge("stdio rpath", NULL) == -1)
171                 err(1, "pledge");
172 #endif
173
174         writeheader("index.html");
175
176         for (i = 1; i < argc; i++) {
177                 repodir = argv[i];
178                 if (!realpath(repodir, repodirabs))
179                         err(1, "realpath");
180
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]);
184                         ret = 1;
185                         continue;
186                 }
187
188                 /* use directory name as name */
189                 if ((name = strrchr(repodirabs, '/')))
190                         name++;
191                 else
192                         name = "";
193
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");
199                 }
200                 description[0] = '\0';
201                 if (fp) {
202                         if (!fgets(description, sizeof(description), fp))
203                                 description[0] = '\0';
204                         fclose(fp);
205                 }
206
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");
212                 }
213                 owner[0] = '\0';
214                 if (fp) {
215                         if (!fgets(owner, sizeof(owner), fp))
216                                 owner[0] = '\0';
217                         owner[strcspn(owner, "\n")] = '\0';
218                         fclose(fp);
219                 }
220                 writelog("index.html");
221         }
222         writefooter("index.html");
223
224         /* copy css */
225         char cwd[PATH_MAX];
226         strcpy(cwd, getcwd(cwd, sizeof(cwd)));
227         cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
228
229         /* cleanup */
230         git_repository_free(repo);
231         git_libgit2_shutdown();
232
233         return ret;
234 }