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