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