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