]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
stagit-index: continue making list, but return error code
[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         git_commit *commit = NULL;
128         const git_signature *author;
129         git_revwalk *w = NULL;
130         git_oid id;
131         int ret = 0;
132
133         git_revwalk_new(&w, repo);
134         git_revwalk_push_head(w);
135         git_revwalk_sorting(w, GIT_SORT_TIME);
136         git_revwalk_simplify_first_parent(w);
137
138         if (git_revwalk_next(&id, w) ||
139             git_commit_lookup(&commit, repo, &id)) {
140                 ret = -1;
141                 goto err;
142         }
143
144         author = git_commit_author(commit);
145
146         fputs("<tr><td><a href=\"", fp);
147         xmlencode(fp, name, strlen(name));
148         fputs("/log.html\">", fp);
149         xmlencode(fp, name, strlen(name));
150         fputs("</a></td><td>", fp);
151         xmlencode(fp, description, strlen(description));
152         fputs("</td><td>", fp);
153         xmlencode(fp, owner, strlen(owner));
154         fputs("</td><td>", fp);
155         if (author)
156                 printtimeshort(fp, &(author->when));
157         fputs("</td></tr>", fp);
158
159         git_commit_free(commit);
160 err:
161         git_revwalk_free(w);
162
163         return ret;
164 }
165
166 int
167 main(int argc, char *argv[])
168 {
169         const git_error *e = NULL;
170         FILE *fp;
171         char path[PATH_MAX], *p;
172         int i, ret = 0;
173
174         if (argc < 2) {
175                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
176                 return 1;
177         }
178         git_libgit2_init();
179
180         writeheader(stdout);
181
182         for (i = 1; i < argc; i++) {
183                 repodir = argv[i];
184
185                 if (git_repository_open_ext(&repo, repodir,
186                     GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
187                         e = giterr_last();
188                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
189                         ret = 1;
190                         continue;
191                 }
192
193                 /* use directory name as name */
194                 p = xbasename(repodir);
195                 snprintf(name, sizeof(name), "%s", p);
196                 free(p);
197
198                 /* read description or .git/description */
199                 description[0] = '\0';
200                 snprintf(path, sizeof(path), "%s%s%s",
201                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
202                 if (!(fp = fopen(path, "r"))) {
203                         snprintf(path, sizeof(path), "%s%s%s",
204                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
205                         fp = fopen(path, "r");
206                 }
207                 if (fp) {
208                         if (!fgets(description, sizeof(description), fp))
209                                 description[0] = '\0';
210                         fclose(fp);
211                 }
212
213                 /* read owner or .git/owner */
214                 owner[0] = '\0';
215                 snprintf(path, sizeof(path), "%s%s%s",
216                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
217                 if (!(fp = fopen(path, "r"))) {
218                         snprintf(path, sizeof(path), "%s%s%s",
219                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
220                         fp = fopen(path, "r");
221                 }
222                 if (fp) {
223                         if (!fgets(owner, sizeof(owner), fp))
224                                 owner[0] = '\0';
225                         owner[strcspn(owner, "\n")] = '\0';
226                         fclose(fp);
227                 }
228                 writelog(stdout);
229         }
230         writefooter(stdout);
231
232         /* cleanup */
233         git_repository_free(repo);
234         git_libgit2_shutdown();
235
236         return ret;
237 }