stagit-index.c (5840B)
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 static git_repository *repo;
12
13 static const char *relpath = "";
14
15 static char description[255] = "Repositories";
16 static char *name = "";
17 static char owner[255];
18
19 /* Handle read or write errors for a FILE * stream */
20 void
21 checkfileerror(FILE *fp, const char *name, int mode)
22 {
23 if (mode == 'r' && ferror(fp))
24 errx(1, "read error: %s", name);
25 else if (mode == 'w' && (fflush(fp) || ferror(fp)))
26 errx(1, "write error: %s", name);
27 }
28
29 void
30 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
31 {
32 int r;
33
34 r = snprintf(buf, bufsiz, "%s%s%s",
35 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
36 if (r < 0 || (size_t)r >= bufsiz)
37 errx(1, "path truncated: '%s%s%s'",
38 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
39 }
40
41 /* Percent-encode, see RFC3986 section 2.1. */
42 void
43 percentencode(FILE *fp, const char *s, size_t len)
44 {
45 static char tab[] = "0123456789ABCDEF";
46 unsigned char uc;
47 size_t i;
48
49 for (i = 0; *s && i < len; s++, i++) {
50 uc = *s;
51 /* NOTE: do not encode '/' for paths or ",-." */
52 if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') ||
53 uc == '[' || uc == ']') {
54 putc('%', fp);
55 putc(tab[(uc >> 4) & 0x0f], fp);
56 putc(tab[uc & 0x0f], fp);
57 } else {
58 putc(uc, fp);
59 }
60 }
61 }
62
63 /* Escape characters below as HTML 2.0 / XML 1.0. */
64 void
65 xmlencode(FILE *fp, const char *s, size_t len)
66 {
67 size_t i;
68
69 for (i = 0; *s && i < len; s++, i++) {
70 switch(*s) {
71 case '<': fputs("<", fp); break;
72 case '>': fputs(">", fp); break;
73 case '\'': fputs("'" , fp); break;
74 case '&': fputs("&", fp); break;
75 case '"': fputs(""", fp); break;
76 default: putc(*s, fp);
77 }
78 }
79 }
80
81 void
82 printtimeshort(FILE *fp, const git_time *intime)
83 {
84 struct tm *intm;
85 time_t t;
86 char out[32];
87
88 t = (time_t)intime->time;
89 if (!(intm = gmtime(&t)))
90 return;
91 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
92 fputs(out, fp);
93 }
94
95 void
96 writeheader(FILE *fp)
97 {
98 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n"
99 "<html>\n<head>\n"
100 "<title>", fp);
101 xmlencode(fp, description, strlen(description));
102 fputs("</title>\n</head>\n<body>\n", fp);
103 fprintf(fp, "<table>\n<tr><td></td>\n"
104 "<td><span class=\"desc\">");
105 xmlencode(fp, description, strlen(description));
106 fputs("</span></td></tr><tr><td></td><td>\n"
107 "</td></tr>\n</table>\n<hr/>\n"
108 "<table id=\"index\">\n"
109 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
110 "<td><b>Last commit</b></td></tr>", fp);
111 }
112
113 void
114 writefooter(FILE *fp)
115 {
116 fputs("</table>\n</body>\n</html>\n", fp);
117 }
118
119 int
120 writelog(FILE *fp)
121 {
122 git_commit *commit = NULL;
123 const git_signature *author;
124 git_revwalk *w = NULL;
125 git_oid id;
126 char *stripped_name = NULL, *p;
127 int ret = 0;
128
129 git_revwalk_new(&w, repo);
130 git_revwalk_push_head(w);
131
132 if (git_revwalk_next(&id, w) ||
133 git_commit_lookup(&commit, repo, &id)) {
134 ret = -1;
135 goto err;
136 }
137
138 author = git_commit_author(commit);
139
140 /* strip .git suffix */
141 if (!(stripped_name = strdup(name)))
142 err(1, "strdup");
143 if ((p = strrchr(stripped_name, '.')))
144 if (!strcmp(p, ".git"))
145 *p = '\0';
146
147 fputs("<tr><td><a href=\"", fp);
148 percentencode(fp, stripped_name, strlen(stripped_name));
149 fputs("/log.html\">", fp);
150 xmlencode(fp, stripped_name, strlen(stripped_name));
151 fputs("</a></td><td>", fp);
152 xmlencode(fp, description, strlen(description));
153 fputs("</td><td>", fp);
154 xmlencode(fp, owner, strlen(owner));
155 fputs("</td><td>", fp);
156 if (author)
157 printtimeshort(fp, &(author->when));
158 fputs("</td></tr>", fp);
159
160 git_commit_free(commit);
161 err:
162 git_revwalk_free(w);
163 free(stripped_name);
164
165 return ret;
166 }
167
168 int
169 main(int argc, char *argv[])
170 {
171 FILE *fp;
172 char path[PATH_MAX], repodirabs[PATH_MAX + 1];
173 const char *repodir;
174 int i, ret = 0;
175
176 if (argc < 2) {
177 fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
178 return 1;
179 }
180
181 /* do not search outside the git repository:
182 GIT_CONFIG_LEVEL_APP is the highest level currently */
183 git_libgit2_init();
184 for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
185 git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
186 /* do not require the git repository to be owned by the current user */
187 git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
188
189 #ifdef __OpenBSD__
190 if (pledge("stdio rpath", NULL) == -1)
191 err(1, "pledge");
192 #endif
193
194 writeheader(stdout);
195
196 for (i = 1; i < argc; i++) {
197 repodir = argv[i];
198 if (!realpath(repodir, repodirabs))
199 err(1, "realpath");
200
201 if (git_repository_open_ext(&repo, repodir,
202 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
203 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
204 ret = 1;
205 continue;
206 }
207
208 /* use directory name as name */
209 if ((name = strrchr(repodirabs, '/')))
210 name++;
211 else
212 name = "";
213
214 /* read description or .git/description */
215 joinpath(path, sizeof(path), repodir, "description");
216 if (!(fp = fopen(path, "r"))) {
217 joinpath(path, sizeof(path), repodir, ".git/description");
218 fp = fopen(path, "r");
219 }
220 description[0] = '\0';
221 if (fp) {
222 if (!fgets(description, sizeof(description), fp))
223 description[0] = '\0';
224 checkfileerror(fp, "description", 'r');
225 fclose(fp);
226 }
227
228 /* read owner or .git/owner */
229 joinpath(path, sizeof(path), repodir, "owner");
230 if (!(fp = fopen(path, "r"))) {
231 joinpath(path, sizeof(path), repodir, ".git/owner");
232 fp = fopen(path, "r");
233 }
234 owner[0] = '\0';
235 if (fp) {
236 if (!fgets(owner, sizeof(owner), fp))
237 owner[0] = '\0';
238 checkfileerror(fp, "owner", 'r');
239 fclose(fp);
240 owner[strcspn(owner, "\n")] = '\0';
241 }
242 writelog(stdout);
243 }
244 writefooter(stdout);
245
246 /* cleanup */
247 git_repository_free(repo);
248 git_libgit2_shutdown();
249
250 checkfileerror(stdout, "<stdout>", 'w');
251
252 return ret;
253 }