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