]> git.armaanb.net Git - stagit.git/blob - urmoms-index.c
ff84be3947f1a2d922ce21b05ce0480f7203388f
[stagit.git] / urmoms-index.c
1 #include <sys/stat.h>
2
3 #include <err.h>
4 #include <errno.h>
5 #include <inttypes.h>
6 #include <libgen.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include <git2.h>
14
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[255];
24 static char owner[255];
25
26 FILE *
27 efopen(const char *name, const char *flags)
28 {
29         FILE *fp;
30
31         if (!(fp = fopen(name, flags)))
32                 err(1, "fopen");
33
34         return fp;
35 }
36
37 /* Escape characters below as HTML 2.0 / XML 1.0. */
38 void
39 xmlencode(FILE *fp, const char *s, size_t len)
40 {
41         size_t i;
42
43         for (i = 0; *s && i < len; s++, i++) {
44                 switch(*s) {
45                 case '<':  fputs("&lt;",   fp); break;
46                 case '>':  fputs("&gt;",   fp); break;
47                 case '\'': fputs("&apos;", fp); break;
48                 case '&':  fputs("&amp;",  fp); break;
49                 case '"':  fputs("&quot;", fp); break;
50                 default:   fputc(*s, fp);
51                 }
52         }
53 }
54
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. */
60 char *
61 xbasename(const char *path)
62 {
63         char *p, *b;
64
65         if (!(p = strdup(path)))
66                 err(1, "strdup");
67         if (!(b = basename(p)))
68                 err(1, "basename");
69         if (!(b = strdup(b)))
70                 err(1, "strdup");
71         free(p);
72
73         return b;
74 }
75
76 void
77 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
78 {
79         struct tm *intm;
80         time_t t;
81         char out[32];
82
83         t = (time_t) intime->time + (intime->offset * 60);
84         intm = gmtime(&t);
85         strftime(out, sizeof(out), fmt, intm);
86         fputs(out, fp);
87 }
88
89 void
90 printtimeshort(FILE *fp, const git_time *intime)
91 {
92         printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
93 }
94
95 int
96 writeheader(FILE *fp)
97 {
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"
110               "<table><thead>\n"
111               "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
112               "</thead><tbody>\n", fp);
113
114         return 0;
115 }
116
117 int
118 writefooter(FILE *fp)
119 {
120         return !fputs("</tbody></table></div></body>\n</html>", fp);
121 }
122
123 int
124 writelog(FILE *fp)
125 {
126         git_commit *commit = NULL;
127         const git_signature *author;
128         git_revwalk *w = NULL;
129         git_oid id;
130         int ret = 0;
131
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);
136
137         if (git_revwalk_next(&id, w) ||
138             git_commit_lookup(&commit, repo, &id)) {
139                 ret = -1;
140                 goto err;
141         }
142
143         author = git_commit_author(commit);
144
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);
154         if (author)
155                 printtimeshort(fp, &(author->when));
156         fputs("</td></tr>", fp);
157
158         git_commit_free(commit);
159 err:
160         git_revwalk_free(w);
161
162         return ret;
163 }
164
165 int
166 main(int argc, char *argv[])
167 {
168         const git_error *e = NULL;
169         FILE *fp;
170         char path[PATH_MAX], *p;
171         int i, status;
172
173         if (argc < 2) {
174                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
175                 return 1;
176         }
177         git_libgit2_init();
178
179         writeheader(stdout);
180
181         for (i = 1; i < argc; i++) {
182                 repodir = argv[i];
183
184                 if ((status = git_repository_open_ext(&repo, repodir,
185                         GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
186                         e = giterr_last();
187                         fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
188                         return status;
189                 }
190
191                 /* use directory name as name */
192                 p = xbasename(repodir);
193                 snprintf(name, sizeof(name), "%s", p);
194                 free(p);
195
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");
204                 }
205                 if (fp) {
206                         if (!fgets(description, sizeof(description), fp))
207                                 description[0] = '\0';
208                         fclose(fp);
209                 }
210
211                 /* read owner or .git/owner */
212                 owner[0] = '\0';
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");
219                 }
220                 if (fp) {
221                         if (!fgets(owner, sizeof(owner), fp))
222                                 owner[0] = '\0';
223                         fclose(fp);
224                 }
225                 writelog(stdout);
226         }
227         writefooter(stdout);
228
229         /* cleanup */
230         git_repository_free(repo);
231         git_libgit2_shutdown();
232
233         return 0;
234 }