]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
resolve absolute paths to repodir, remove basename just use strrchr.
[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 #include "config.h"
16
17 static git_repository *repo;
18
19 static const char *relpath = "";
20 static const char *repodir;
21
22 static char description[255] = "Repositories";
23 static char *name = "";
24 static char owner[255];
25
26 /* Escape characters below as HTML 2.0 / XML 1.0. */
27 void
28 xmlencode(FILE *fp, const char *s, size_t len)
29 {
30         size_t i;
31
32         for (i = 0; *s && i < len; s++, i++) {
33                 switch(*s) {
34                 case '<':  fputs("&lt;",   fp); break;
35                 case '>':  fputs("&gt;",   fp); break;
36                 case '\'': fputs("&apos;", fp); break;
37                 case '&':  fputs("&amp;",  fp); break;
38                 case '"':  fputs("&quot;", fp); break;
39                 default:   fputc(*s, fp);
40                 }
41         }
42 }
43
44 void
45 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
46 {
47         struct tm *intm;
48         time_t t;
49         char out[32];
50
51         t = (time_t) intime->time + (intime->offset * 60);
52         intm = gmtime(&t);
53         strftime(out, sizeof(out), fmt, intm);
54         fputs(out, fp);
55 }
56
57 void
58 printtimeshort(FILE *fp, const git_time *intime)
59 {
60         printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
61 }
62
63 int
64 writeheader(FILE *fp)
65 {
66         fputs("<!DOCTYPE html>\n"
67                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
68                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
69                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
70         xmlencode(fp, description, strlen(description));
71         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
72         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
73         fputs("</head>\n<body>\n", fp);
74         fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
75                 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
76                 relpath, name, description);
77         fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
78               "<table id=\"index\"><thead>\n"
79               "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
80               "</thead><tbody>\n", fp);
81
82         return 0;
83 }
84
85 int
86 writefooter(FILE *fp)
87 {
88         return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
89 }
90
91 int
92 writelog(FILE *fp)
93 {
94         char *stripped_name = NULL, *p;
95         git_commit *commit = NULL;
96         const git_signature *author;
97         git_revwalk *w = NULL;
98         git_oid id;
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 int
143 main(int argc, char *argv[])
144 {
145         const git_error *e = NULL;
146         FILE *fp;
147         char path[PATH_MAX], repodirabs[PATH_MAX + 1];
148         int i, r, ret = 0;
149
150         if (argc < 2) {
151                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
152                 return 1;
153         }
154         git_libgit2_init();
155
156         writeheader(stdout);
157
158         for (i = 1; i < argc; i++) {
159                 repodir = argv[i];
160                 if (!realpath(repodir, repodirabs))
161                         err(1, "realpath");
162
163                 if (git_repository_open_ext(&repo, repodir,
164                     GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
165                         e = giterr_last();
166                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
167                         ret = 1;
168                         continue;
169                 }
170
171                 /* use directory name as name */
172                 if ((name = strrchr(repodirabs, '/')))
173                         name++;
174                 else
175                         name = "";
176
177                 /* read description or .git/description */
178                 description[0] = '\0';
179                 r = snprintf(path, sizeof(path), "%s%s%s",
180                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
181                 if (r == -1 || (size_t)r >= sizeof(path))
182                         errx(1, "path truncated: '%s%s%s'",
183                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
184                 if (!(fp = fopen(path, "r"))) {
185                         r = snprintf(path, sizeof(path), "%s%s%s",
186                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
187                         if (r == -1 || (size_t)r >= sizeof(path))
188                                 errx(1, "path truncated: '%s%s%s'",
189                                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
190                         fp = fopen(path, "r");
191                 }
192                 if (fp) {
193                         if (!fgets(description, sizeof(description), fp))
194                                 description[0] = '\0';
195                         fclose(fp);
196                 }
197
198                 /* read owner or .git/owner */
199                 owner[0] = '\0';
200                 r = snprintf(path, sizeof(path), "%s%s%s",
201                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
202                 if (r == -1 || (size_t)r >= sizeof(path))
203                         errx(1, "path truncated: '%s%s%s'",
204                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
205                 if (!(fp = fopen(path, "r"))) {
206                         r = snprintf(path, sizeof(path), "%s%s%s",
207                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
208                         if (r == -1 || (size_t)r >= sizeof(path))
209                                 errx(1, "path truncated: '%s%s%s'",
210                                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
211                         fp = fopen(path, "r");
212                 }
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(stdout);
220         }
221         writefooter(stdout);
222
223         /* cleanup */
224         git_repository_free(repo);
225         git_libgit2_shutdown();
226
227         return ret;
228 }