]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
remove config.h, add options to stagit.c
[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 static const char *repodir;
20
21 static char description[255] = "Repositories";
22 static char *name = "";
23 static char owner[255];
24
25 #ifndef USE_PLEDGE
26 int
27 pledge(const char *promises, const char *paths[])
28 {
29         return 0;
30 }
31 #endif
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("&apos;", 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 int
66 writeheader(FILE *fp)
67 {
68         fputs("<!DOCTYPE html>\n"
69                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
70                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
71                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
72         xmlencode(fp, description, strlen(description));
73         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
74         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
75         fputs("</head>\n<body>\n", fp);
76         fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
77                 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
78                 relpath, name, description);
79         fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
80               "<table id=\"index\"><thead>\n"
81               "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
82               "</thead><tbody>\n", fp);
83
84         return 0;
85 }
86
87 int
88 writefooter(FILE *fp)
89 {
90         return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
91 }
92
93 int
94 writelog(FILE *fp)
95 {
96         git_commit *commit = NULL;
97         const git_signature *author;
98         git_revwalk *w = NULL;
99         git_oid id;
100         char *stripped_name = NULL, *p;
101         int ret = 0;
102
103         git_revwalk_new(&w, repo);
104         git_revwalk_push_head(w);
105         git_revwalk_sorting(w, GIT_SORT_TIME);
106         git_revwalk_simplify_first_parent(w);
107
108         if (git_revwalk_next(&id, w) ||
109             git_commit_lookup(&commit, repo, &id)) {
110                 ret = -1;
111                 goto err;
112         }
113
114         author = git_commit_author(commit);
115
116         /* strip .git suffix */
117         if (!(stripped_name = strdup(name)))
118                 err(1, "strdup");
119         if ((p = strrchr(stripped_name, '.')))
120                 if (!strcmp(p, ".git"))
121                         *p = '\0';
122
123         fputs("<tr><td><a href=\"", fp);
124         xmlencode(fp, stripped_name, strlen(stripped_name));
125         fputs("/log.html\">", fp);
126         xmlencode(fp, stripped_name, strlen(stripped_name));
127         fputs("</a></td><td>", fp);
128         xmlencode(fp, description, strlen(description));
129         fputs("</td><td>", fp);
130         xmlencode(fp, owner, strlen(owner));
131         fputs("</td><td>", fp);
132         if (author)
133                 printtimeshort(fp, &(author->when));
134         fputs("</td></tr>", fp);
135
136         git_commit_free(commit);
137 err:
138         git_revwalk_free(w);
139         free(stripped_name);
140
141         return ret;
142 }
143
144 void
145 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
146 {
147         int r;
148
149         r = snprintf(buf, bufsiz, "%s%s%s",
150                 repodir, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
151         if (r == -1 || (size_t)r >= bufsiz)
152                 errx(1, "path truncated: '%s%s%s'",
153                         path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
154 }
155
156 int
157 main(int argc, char *argv[])
158 {
159         const git_error *e = NULL;
160         FILE *fp;
161         char path[PATH_MAX], repodirabs[PATH_MAX + 1];
162         int i, ret = 0;
163
164         if (pledge("stdio rpath", NULL) == -1)
165                 err(1, "pledge");
166
167         if (argc < 2) {
168                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
169                 return 1;
170         }
171         git_libgit2_init();
172
173         writeheader(stdout);
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                         e = giterr_last();
183                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
184                         ret = 1;
185                         continue;
186                 }
187
188                 /* use directory name as name */
189                 if ((name = strrchr(repodirabs, '/')))
190                         name++;
191                 else
192                         name = "";
193
194                 /* read description or .git/description */
195                 joinpath(path, sizeof(path), repodir, "description");
196                 if (!(fp = fopen(path, "r"))) {
197                         joinpath(path, sizeof(path), repodir, ".git/description");
198                         fp = fopen(path, "r");
199                 }
200                 description[0] = '\0';
201                 if (fp) {
202                         if (!fgets(description, sizeof(description), fp))
203                                 description[0] = '\0';
204                         fclose(fp);
205                 }
206
207                 /* read owner or .git/owner */
208                 joinpath(path, sizeof(path), repodir, "owner");
209                 if (!(fp = fopen(path, "r"))) {
210                         joinpath(path, sizeof(path), repodir, ".git/owner");
211                         fp = fopen(path, "r");
212                 }
213                 owner[0] = '\0';
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 }