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