add urmoms-index, initial version
3 files changed, 342 insertions(+), 3 deletions(-) | |||
---|---|---|---|
M | Makefile | +9 | -3 |
A | urmoms-index.1 | +14 | -0 |
A | urmoms-index.c | +319 | -0 |
1@@ -3,11 +3,14 @@ include config.mk
2 NAME = urmoms
3 VERSION = 0.1
4 SRC = \
5- urmoms.c
6+ urmoms.c\
7+ urmoms-index.c
8 BIN = \
9- urmoms
10+ urmoms\
11+ urmoms-index
12 MAN1 = \
13- urmoms.1
14+ urmoms.1\
15+ urmoms-index.1
16 DOC = \
17 LICENSE\
18 README\
19@@ -42,6 +45,9 @@ config.h:
20 urmoms: urmoms.o
21 ${CC} -o $@ urmoms.o ${LDFLAGS}
22
23+urmoms-index: urmoms-index.o
24+ ${CC} -o $@ urmoms-index.o ${LDFLAGS}
25+
26 clean:
27 rm -f ${BIN} ${OBJ}
28
A · urmoms-index.1
+14, -0 1@@ -0,0 +1,14 @@
2+.Dd December 15, 2015
3+.Dt URMOMS-INDEX 1
4+.Os
5+.Sh NAME
6+.Nm urmoms-index
7+.Nd static git page generator (repo list)
8+.Sh SYNOPSIS
9+.Nm
10+.Op Ar repodir...
11+.Sh DESCRIPTION
12+.Nm
13+is undocumented
14+.Sh AUTHORS
15+.An Hiltjo Posthuma Aq Mt hiltjo@codemadness.org
A · urmoms-index.c
+319, -0 1@@ -0,0 +1,319 @@
2+#include <sys/stat.h>
3+
4+#include <err.h>
5+#include <errno.h>
6+#include <inttypes.h>
7+#include <libgen.h>
8+#include <limits.h>
9+#include <stdio.h>
10+#include <stdlib.h>
11+#include <string.h>
12+#include <unistd.h>
13+
14+#include "config.h"
15+#include "git2.h"
16+
17+struct commitinfo {
18+ const git_oid *id;
19+
20+ char oid[GIT_OID_HEXSZ + 1];
21+ char parentoid[GIT_OID_HEXSZ + 1];
22+
23+ const git_signature *author;
24+ const char *summary;
25+ const char *msg;
26+
27+ git_diff_stats *stats;
28+ git_diff *diff;
29+ git_commit *commit;
30+ git_commit *parent;
31+ git_tree *commit_tree;
32+ git_tree *parent_tree;
33+
34+ size_t addcount;
35+ size_t delcount;
36+ size_t filecount;
37+};
38+
39+static git_repository *repo;
40+
41+static const char *relpath = "";
42+static const char *repodir;
43+
44+static char description[255] = "Repositories";
45+static char name[255];
46+static char owner[255];
47+
48+void
49+commitinfo_free(struct commitinfo *ci)
50+{
51+ if (!ci)
52+ return;
53+
54+ git_diff_stats_free(ci->stats);
55+ git_diff_free(ci->diff);
56+ git_tree_free(ci->commit_tree);
57+ git_tree_free(ci->parent_tree);
58+ git_commit_free(ci->commit);
59+}
60+
61+struct commitinfo *
62+commitinfo_getbyoid(const git_oid *id)
63+{
64+ struct commitinfo *ci;
65+ git_diff_options opts;
66+ int error;
67+
68+ if (!(ci = calloc(1, sizeof(struct commitinfo))))
69+ err(1, "calloc");
70+
71+ ci->id = id;
72+ if (git_commit_lookup(&(ci->commit), repo, id))
73+ goto err;
74+
75+ /* TODO: show tags when commit has it */
76+ git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
77+ git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
78+
79+ ci->author = git_commit_author(ci->commit);
80+ ci->summary = git_commit_summary(ci->commit);
81+ ci->msg = git_commit_message(ci->commit);
82+
83+ if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
84+ goto err; /* TODO: handle error */
85+ if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
86+ if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
87+ goto err;
88+ } else {
89+ ci->parent = NULL;
90+ ci->parent_tree = NULL;
91+ }
92+
93+ git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
94+ opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
95+ if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
96+ goto err;
97+ if (git_diff_get_stats(&(ci->stats), ci->diff))
98+ goto err;
99+
100+ ci->addcount = git_diff_stats_insertions(ci->stats);
101+ ci->delcount = git_diff_stats_deletions(ci->stats);
102+ ci->filecount = git_diff_stats_files_changed(ci->stats);
103+
104+ /* TODO: show tag when commit has it */
105+
106+ return ci;
107+
108+err:
109+ commitinfo_free(ci);
110+ free(ci);
111+
112+ return NULL;
113+}
114+
115+FILE *
116+efopen(const char *name, const char *flags)
117+{
118+ FILE *fp;
119+
120+ if (!(fp = fopen(name, flags)))
121+ err(1, "fopen");
122+
123+ return fp;
124+}
125+
126+/* Escape characters below as HTML 2.0 / XML 1.0. */
127+void
128+xmlencode(FILE *fp, const char *s, size_t len)
129+{
130+ size_t i;
131+
132+ for (i = 0; *s && i < len; s++, i++) {
133+ switch(*s) {
134+ case '<': fputs("<", fp); break;
135+ case '>': fputs(">", fp); break;
136+ case '\'': fputs("'", fp); break;
137+ case '&': fputs("&", fp); break;
138+ case '"': fputs(""", fp); break;
139+ default: fputc(*s, fp);
140+ }
141+ }
142+}
143+
144+/* Some implementations of basename(3) return a pointer to a static
145+ * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
146+ * This is a wrapper function that is compatible with both versions.
147+ * The program will error out if basename(3) failed, this can only happen
148+ * with the OpenBSD version. */
149+char *
150+xbasename(const char *path)
151+{
152+ char *p, *b;
153+
154+ if (!(p = strdup(path)))
155+ err(1, "strdup");
156+ if (!(b = basename(p)))
157+ err(1, "basename");
158+ if (!(b = strdup(b)))
159+ err(1, "strdup");
160+ free(p);
161+
162+ return b;
163+}
164+
165+void
166+printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
167+{
168+ struct tm *intm;
169+ time_t t;
170+ char out[32];
171+
172+ t = (time_t) intime->time + (intime->offset * 60);
173+ intm = gmtime(&t);
174+ strftime(out, sizeof(out), fmt, intm);
175+ fputs(out, fp);
176+}
177+
178+void
179+printtimeshort(FILE *fp, const git_time *intime)
180+{
181+ printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
182+}
183+
184+int
185+writeheader(FILE *fp)
186+{
187+ fputs("<!DOCTYPE HTML>"
188+ "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
189+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
190+ "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
191+ xmlencode(fp, description, strlen(description));
192+ fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
193+ fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
194+ fputs("</head>\n<body>\n\n", fp);
195+ fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
196+ "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
197+ relpath, name, description);
198+ fputs("</td></tr>\n</table>\n<hr/><div id=\"content\">\n"
199+ "<table><thead>\n"
200+ "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
201+ "</thead><tbody>\n", fp);
202+
203+ return 0;
204+}
205+
206+int
207+writefooter(FILE *fp)
208+{
209+ return !fputs("</tbody></table></div></body>\n</html>", fp);
210+}
211+
212+int
213+writelog(FILE *fp)
214+{
215+ struct commitinfo *ci;
216+ git_revwalk *w = NULL;
217+ git_oid id;
218+ int ret = 0;
219+
220+ git_revwalk_new(&w, repo);
221+ git_revwalk_push_head(w);
222+ git_revwalk_sorting(w, GIT_SORT_TIME);
223+ git_revwalk_simplify_first_parent(w);
224+
225+ if (git_revwalk_next(&id, w) ||
226+ !(ci = commitinfo_getbyoid(&id))) {
227+ ret = -1;
228+ goto err;
229+ }
230+
231+ fputs("<tr><td><a href=\"", fp);
232+ xmlencode(fp, name, strlen(name));
233+ fputs("/log.html\">", fp);
234+ xmlencode(fp, name, strlen(name));
235+ fputs("</a></td><td>", fp);
236+ xmlencode(fp, description, strlen(description));
237+ fputs("</td><td>", fp);
238+ xmlencode(fp, owner, strlen(owner));
239+ fputs("</td><td>", fp);
240+ if (ci->author)
241+ printtimeshort(fp, &(ci->author->when));
242+ fputs("</td></tr>", fp);
243+
244+err:
245+ git_revwalk_free(w);
246+
247+ return ret;
248+}
249+
250+int
251+main(int argc, char *argv[])
252+{
253+ const git_error *e = NULL;
254+ FILE *fp;
255+ char path[PATH_MAX], *p;
256+ int status;
257+ size_t i;
258+
259+ if (argc < 2) {
260+ fprintf(stderr, "%s [repodir...]\n", argv[0]);
261+ return 1;
262+ }
263+ git_libgit2_init();
264+
265+ writeheader(stdout);
266+
267+ for (i = 1; i < argc; i++) {
268+ repodir = argv[i];
269+
270+ if ((status = git_repository_open_ext(&repo, repodir,
271+ GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
272+ e = giterr_last();
273+ fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
274+ return status;
275+ }
276+
277+ /* use directory name as name */
278+ p = xbasename(repodir);
279+ snprintf(name, sizeof(name), "%s", p);
280+ free(p);
281+
282+ /* read description or .git/description */
283+ description[0] = '\0';
284+ snprintf(path, sizeof(path), "%s%s%s",
285+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
286+ if (!(fp = fopen(path, "r"))) {
287+ snprintf(path, sizeof(path), "%s%s%s",
288+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
289+ fp = fopen(path, "r");
290+ }
291+ if (fp) {
292+ if (!fgets(description, sizeof(description), fp))
293+ description[0] = '\0';
294+ fclose(fp);
295+ }
296+
297+ /* read owner or .git/owner */
298+ owner[0] = '\0';
299+ snprintf(path, sizeof(path), "%s%s%s",
300+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
301+ if (!(fp = fopen(path, "r"))) {
302+ snprintf(path, sizeof(path), "%s%s%s",
303+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
304+ fp = fopen(path, "r");
305+ }
306+ if (fp) {
307+ if (!fgets(owner, sizeof(owner), fp))
308+ owner[0] = '\0';
309+ fclose(fp);
310+ }
311+ writelog(stdout);
312+ }
313+ writefooter(stdout);
314+
315+ /* cleanup */
316+ git_repository_free(repo);
317+ git_libgit2_shutdown();
318+
319+ return 0;
320+}