]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
code cleanup
[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 void
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
85 void
86 writefooter(FILE *fp)
87 {
88         fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
89 }
90
91 int
92 writelog(FILE *fp)
93 {
94         git_commit *commit = NULL;
95         const git_signature *author;
96         git_revwalk *w = NULL;
97         git_oid id;
98         char *stripped_name = NULL, *p;
99         int ret = 0;
100
101         git_revwalk_new(&w, repo);
102         git_revwalk_push_head(w);
103         git_revwalk_sorting(w, GIT_SORT_TIME);
104         git_revwalk_simplify_first_parent(w);
105
106         if (git_revwalk_next(&id, w) ||
107             git_commit_lookup(&commit, repo, &id)) {
108                 ret = -1;
109                 goto err;
110         }
111
112         author = git_commit_author(commit);
113
114         /* strip .git suffix */
115         if (!(stripped_name = strdup(name)))
116                 err(1, "strdup");
117         if ((p = strrchr(stripped_name, '.')))
118                 if (!strcmp(p, ".git"))
119                         *p = '\0';
120
121         fputs("<tr><td><a href=\"", fp);
122         xmlencode(fp, stripped_name, strlen(stripped_name));
123         fputs("/log.html\">", fp);
124         xmlencode(fp, stripped_name, strlen(stripped_name));
125         fputs("</a></td><td>", fp);
126         xmlencode(fp, description, strlen(description));
127         fputs("</td><td>", fp);
128         xmlencode(fp, owner, strlen(owner));
129         fputs("</td><td>", fp);
130         if (author)
131                 printtimeshort(fp, &(author->when));
132         fputs("</td></tr>", fp);
133
134         git_commit_free(commit);
135 err:
136         git_revwalk_free(w);
137         free(stripped_name);
138
139         return ret;
140 }
141
142 void
143 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
144 {
145         int r;
146
147         r = snprintf(buf, bufsiz, "%s%s%s",
148                 repodir, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
149         if (r == -1 || (size_t)r >= bufsiz)
150                 errx(1, "path truncated: '%s%s%s'",
151                         path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
152 }
153
154 int
155 main(int argc, char *argv[])
156 {
157         const git_error *e = NULL;
158         FILE *fp;
159         char path[PATH_MAX], repodirabs[PATH_MAX + 1];
160         int i, ret = 0;
161
162         if (pledge("stdio rpath", NULL) == -1)
163                 err(1, "pledge");
164
165         if (argc < 2) {
166                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
167                 return 1;
168         }
169         git_libgit2_init();
170
171         writeheader(stdout);
172
173         for (i = 1; i < argc; i++) {
174                 repodir = argv[i];
175                 if (!realpath(repodir, repodirabs))
176                         err(1, "realpath");
177
178                 if (git_repository_open_ext(&repo, repodir,
179                     GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
180                         e = giterr_last();
181                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
182                         ret = 1;
183                         continue;
184                 }
185
186                 /* use directory name as name */
187                 if ((name = strrchr(repodirabs, '/')))
188                         name++;
189                 else
190                         name = "";
191
192                 /* read description or .git/description */
193                 joinpath(path, sizeof(path), repodir, "description");
194                 if (!(fp = fopen(path, "r"))) {
195                         joinpath(path, sizeof(path), repodir, ".git/description");
196                         fp = fopen(path, "r");
197                 }
198                 description[0] = '\0';
199                 if (fp) {
200                         if (!fgets(description, sizeof(description), fp))
201                                 description[0] = '\0';
202                         fclose(fp);
203                 }
204
205                 /* read owner or .git/owner */
206                 joinpath(path, sizeof(path), repodir, "owner");
207                 if (!(fp = fopen(path, "r"))) {
208                         joinpath(path, sizeof(path), repodir, ".git/owner");
209                         fp = fopen(path, "r");
210                 }
211                 owner[0] = '\0';
212                 if (fp) {
213                         if (!fgets(owner, sizeof(owner), fp))
214                                 owner[0] = '\0';
215                         owner[strcspn(owner, "\n")] = '\0';
216                         fclose(fp);
217                 }
218                 writelog(stdout);
219         }
220         writefooter(stdout);
221
222         /* cleanup */
223         git_repository_free(repo);
224         git_libgit2_shutdown();
225
226         return ret;
227 }