]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
add OpenBSD pledge(2) support using -DUSE_PLEDGE
[stagit.git] / stagit-index.c
1 #include <sys/stat.h>
2
3 #include <err.h>
4 #include <errno.h>
5 #include <inttypes.h>
6 #include <limits.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include <git2.h>
13
14 #include "compat.h"
15 #include "config.h"
16
17 static git_repository *repo;
18
19 static const char *relpath = "";
20 static const char *repodir;
21
22 static char description[255] = "Repositories";
23 static char *name = "";
24 static char owner[255];
25
26 #ifndef USE_PLEDGE
27 int
28 pledge(const char *promises, const char *paths[])
29 {
30         return 0;
31 }
32 #endif
33
34 /* Escape characters below as HTML 2.0 / XML 1.0. */
35 void
36 xmlencode(FILE *fp, const char *s, size_t len)
37 {
38         size_t i;
39
40         for (i = 0; *s && i < len; s++, i++) {
41                 switch(*s) {
42                 case '<':  fputs("&lt;",   fp); break;
43                 case '>':  fputs("&gt;",   fp); break;
44                 case '\'': fputs("&apos;", fp); break;
45                 case '&':  fputs("&amp;",  fp); break;
46                 case '"':  fputs("&quot;", fp); break;
47                 default:   fputc(*s, fp);
48                 }
49         }
50 }
51
52 void
53 printtimeshort(FILE *fp, const git_time *intime)
54 {
55         struct tm *intm;
56         time_t t;
57         char out[32];
58
59         t = (time_t)intime->time;
60         if (!(intm = gmtime(&t)))
61                 return;
62         strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
63         fputs(out, fp);
64 }
65
66 int
67 writeheader(FILE *fp)
68 {
69         fputs("<!DOCTYPE html>\n"
70                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
71                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
72                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
73         xmlencode(fp, description, strlen(description));
74         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
75         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
76         fputs("</head>\n<body>\n", fp);
77         fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
78                 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
79                 relpath, name, description);
80         fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
81               "<table id=\"index\"><thead>\n"
82               "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
83               "</thead><tbody>\n", fp);
84
85         return 0;
86 }
87
88 int
89 writefooter(FILE *fp)
90 {
91         return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
92 }
93
94 int
95 writelog(FILE *fp)
96 {
97         git_commit *commit = NULL;
98         const git_signature *author;
99         git_revwalk *w = NULL;
100         git_oid id;
101         char *stripped_name = NULL, *p;
102         int ret = 0;
103
104         git_revwalk_new(&w, repo);
105         git_revwalk_push_head(w);
106         git_revwalk_sorting(w, GIT_SORT_TIME);
107         git_revwalk_simplify_first_parent(w);
108
109         if (git_revwalk_next(&id, w) ||
110             git_commit_lookup(&commit, repo, &id)) {
111                 ret = -1;
112                 goto err;
113         }
114
115         author = git_commit_author(commit);
116
117         /* strip .git suffix */
118         if (!(stripped_name = strdup(name)))
119                 err(1, "strdup");
120         if ((p = strrchr(stripped_name, '.')))
121                 if (!strcmp(p, ".git"))
122                         *p = '\0';
123
124         fputs("<tr><td><a href=\"", fp);
125         xmlencode(fp, stripped_name, strlen(stripped_name));
126         fputs("/log.html\">", fp);
127         xmlencode(fp, stripped_name, strlen(stripped_name));
128         fputs("</a></td><td>", fp);
129         xmlencode(fp, description, strlen(description));
130         fputs("</td><td>", fp);
131         xmlencode(fp, owner, strlen(owner));
132         fputs("</td><td>", fp);
133         if (author)
134                 printtimeshort(fp, &(author->when));
135         fputs("</td></tr>", fp);
136
137         git_commit_free(commit);
138 err:
139         git_revwalk_free(w);
140         free(stripped_name);
141
142         return ret;
143 }
144
145 void
146 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
147 {
148         int r;
149
150         r = snprintf(buf, bufsiz, "%s%s%s",
151                 repodir, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
152         if (r == -1 || (size_t)r >= bufsiz)
153                 errx(1, "path truncated: '%s%s%s'",
154                         path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
155 }
156
157 int
158 main(int argc, char *argv[])
159 {
160         const git_error *e = NULL;
161         FILE *fp;
162         char path[PATH_MAX], repodirabs[PATH_MAX + 1];
163         int i, ret = 0;
164
165         if (pledge("stdio rpath", NULL) == -1)
166                 err(1, "pledge");
167
168         if (argc < 2) {
169                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
170                 return 1;
171         }
172         git_libgit2_init();
173
174         writeheader(stdout);
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                         e = giterr_last();
184                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
185                         ret = 1;
186                         continue;
187                 }
188
189                 /* use directory name as name */
190                 if ((name = strrchr(repodirabs, '/')))
191                         name++;
192                 else
193                         name = "";
194
195                 /* read description or .git/description */
196                 joinpath(path, sizeof(path), repodir, "description");
197                 if (!(fp = fopen(path, "r"))) {
198                         joinpath(path, sizeof(path), repodir, ".git/description");
199                         fp = fopen(path, "r");
200                 }
201                 description[0] = '\0';
202                 if (fp) {
203                         if (!fgets(description, sizeof(description), fp))
204                                 description[0] = '\0';
205                         fclose(fp);
206                 }
207
208                 /* read owner or .git/owner */
209                 joinpath(path, sizeof(path), repodir, "owner");
210                 if (!(fp = fopen(path, "r"))) {
211                         joinpath(path, sizeof(path), repodir, ".git/owner");
212                         fp = fopen(path, "r");
213                 }
214                 owner[0] = '\0';
215                 if (fp) {
216                         if (!fgets(owner, sizeof(owner), fp))
217                                 owner[0] = '\0';
218                         owner[strcspn(owner, "\n")] = '\0';
219                         fclose(fp);
220                 }
221                 writelog(stdout);
222         }
223         writefooter(stdout);
224
225         /* cleanup */
226         git_repository_free(repo);
227         git_libgit2_shutdown();
228
229         return ret;
230 }