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