]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
stagit-index: Strip per repo .git suffix from generated index
[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 <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 "compat.h"
16 #include "config.h"
17
18 static git_repository *repo;
19
20 static const char *relpath = "";
21 static const char *repodir;
22
23 static char description[255] = "Repositories";
24 static char name[255];
25 static char owner[255];
26
27 FILE *
28 efopen(const char *name, const char *flags)
29 {
30         FILE *fp;
31
32         if (!(fp = fopen(name, flags)))
33                 err(1, "fopen");
34
35         return fp;
36 }
37
38 /* Escape characters below as HTML 2.0 / XML 1.0. */
39 void
40 xmlencode(FILE *fp, const char *s, size_t len)
41 {
42         size_t i;
43
44         for (i = 0; *s && i < len; s++, i++) {
45                 switch(*s) {
46                 case '<':  fputs("&lt;",   fp); break;
47                 case '>':  fputs("&gt;",   fp); break;
48                 case '\'': fputs("&apos;", fp); break;
49                 case '&':  fputs("&amp;",  fp); break;
50                 case '"':  fputs("&quot;", fp); break;
51                 default:   fputc(*s, fp);
52                 }
53         }
54 }
55
56 /* Some implementations of basename(3) return a pointer to a static
57  * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
58  * This is a wrapper function that is compatible with both versions.
59  * The program will error out if basename(3) failed, this can only happen
60  * with the OpenBSD version. */
61 char *
62 xbasename(const char *path)
63 {
64         char *p, *b;
65
66         if (!(p = strdup(path)))
67                 err(1, "strdup");
68         if (!(b = basename(p)))
69                 err(1, "basename");
70         if (!(b = strdup(b)))
71                 err(1, "strdup");
72         free(p);
73
74         return b;
75 }
76
77 void
78 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
79 {
80         struct tm *intm;
81         time_t t;
82         char out[32];
83
84         t = (time_t) intime->time + (intime->offset * 60);
85         intm = gmtime(&t);
86         strftime(out, sizeof(out), fmt, intm);
87         fputs(out, fp);
88 }
89
90 void
91 printtimeshort(FILE *fp, const git_time *intime)
92 {
93         printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
94 }
95
96 int
97 writeheader(FILE *fp)
98 {
99         fputs("<!DOCTYPE HTML>"
100                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
101                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
102                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
103         xmlencode(fp, description, strlen(description));
104         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
105         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
106         fputs("</head>\n<body>\n", fp);
107         fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
108                 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
109                 relpath, name, description);
110         fputs("</td></tr>\n</table>\n<hr/><div id=\"content\">\n"
111               "<table id=\"index\"><thead>\n"
112               "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
113               "</thead><tbody>\n", fp);
114
115         return 0;
116 }
117
118 int
119 writefooter(FILE *fp)
120 {
121         return !fputs("</tbody></table></div></body>\n</html>", fp);
122 }
123
124 int
125 writelog(FILE *fp)
126 {
127         char *stripped_name, *p;
128         git_commit *commit = NULL;
129         const git_signature *author;
130         git_revwalk *w = NULL;
131         git_oid id;
132         int ret = 0;
133
134         git_revwalk_new(&w, repo);
135         git_revwalk_push_head(w);
136         git_revwalk_sorting(w, GIT_SORT_TIME);
137         git_revwalk_simplify_first_parent(w);
138
139         if (git_revwalk_next(&id, w) ||
140             git_commit_lookup(&commit, repo, &id)) {
141                 ret = -1;
142                 goto err;
143         }
144
145         author = git_commit_author(commit);
146
147         fputs("<tr><td><a href=\"", fp);
148         xmlencode(fp, name, strlen(name));
149         fputs("/log.html\">", fp);
150
151         /* strip .git suffix */
152         if (!(stripped_name = strdup(name)))
153                 err(1, "strdup");
154         if ((p = strrchr(stripped_name, '.')))
155                 *p = '\0';
156         xmlencode(fp, stripped_name, strlen(stripped_name));
157
158         fputs("</a></td><td>", fp);
159         xmlencode(fp, description, strlen(description));
160         fputs("</td><td>", fp);
161         xmlencode(fp, owner, strlen(owner));
162         fputs("</td><td>", fp);
163         if (author)
164                 printtimeshort(fp, &(author->when));
165         fputs("</td></tr>", fp);
166
167         git_commit_free(commit);
168 err:
169         git_revwalk_free(w);
170
171         return ret;
172 }
173
174 int
175 main(int argc, char *argv[])
176 {
177         const git_error *e = NULL;
178         FILE *fp;
179         char path[PATH_MAX], *p;
180         int i, ret = 0;
181
182         if (argc < 2) {
183                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
184                 return 1;
185         }
186         git_libgit2_init();
187
188         writeheader(stdout);
189
190         for (i = 1; i < argc; i++) {
191                 repodir = argv[i];
192
193                 if (git_repository_open_ext(&repo, repodir,
194                     GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
195                         e = giterr_last();
196                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
197                         ret = 1;
198                         continue;
199                 }
200
201                 /* use directory name as name */
202                 p = xbasename(repodir);
203                 snprintf(name, sizeof(name), "%s", p);
204                 free(p);
205
206                 /* read description or .git/description */
207                 description[0] = '\0';
208                 snprintf(path, sizeof(path), "%s%s%s",
209                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
210                 if (!(fp = fopen(path, "r"))) {
211                         snprintf(path, sizeof(path), "%s%s%s",
212                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
213                         fp = fopen(path, "r");
214                 }
215                 if (fp) {
216                         if (!fgets(description, sizeof(description), fp))
217                                 description[0] = '\0';
218                         fclose(fp);
219                 }
220
221                 /* read owner or .git/owner */
222                 owner[0] = '\0';
223                 snprintf(path, sizeof(path), "%s%s%s",
224                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
225                 if (!(fp = fopen(path, "r"))) {
226                         snprintf(path, sizeof(path), "%s%s%s",
227                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
228                         fp = fopen(path, "r");
229                 }
230                 if (fp) {
231                         if (!fgets(owner, sizeof(owner), fp))
232                                 owner[0] = '\0';
233                         owner[strcspn(owner, "\n")] = '\0';
234                         fclose(fp);
235                 }
236                 writelog(stdout);
237         }
238         writefooter(stdout);
239
240         /* cleanup */
241         git_repository_free(repo);
242         git_libgit2_shutdown();
243
244         return ret;
245 }