stagit

[fork] HTML git frontend
git clone https://hhvn.uk/stagit
git clone git://hhvn.uk/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (6927B)


      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("&lt;",   fp); break;
     72 		case '>':  fputs("&gt;",   fp); break;
     73 		case '\'': fputs("&#39;" , fp); break;
     74 		case '&':  fputs("&amp;",  fp); break;
     75 		case '"':  fputs("&quot;", 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 	fprintf(fp, "<!DOCTYPE html>\n"
     99 		"<html>\n<head>\n"
    100 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
    101 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
    102 		"<title>hhvn's website</title>\n"
    103 		"<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
    104 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sindex.css\" />\n", relpath);
    105 	fputs("</head>\n<body>\n", fp);
    106 	fprintf(fp, "<h1>hhvn's website</h1>\n"
    107 		"<hr />\n"
    108 		"Mail me if you can: hayden<span><b><!---</b>--->@</b></span>hhvn<i><!---span <div>--->.</i>uk<br />\n"
    109 		"Spam me if you prefer: spamtrap@hhvn.uk<br />\n"
    110 		"<a href=\"./pgp.asc\">PGP key</a> &amp; <a href=\"./ssh.pub\">SSH key</a>"
    111 		"<hr />\n"
    112 		"<a href=\"./blog\">Blog ---></a><hr />\n"
    113 		"Clearnet: <a href=\"//hhvn.uk\">hhvn.uk</a>\n"
    114 		"<span class=\"tor\">Tor: <a href=\"//zl7kawb6nd3aqzrwo5cz57id7rnp3k7rvsme5seopv65lcgm56ey2oyd.onion\">zl7kawb6nd3aqzrwo5cz57id7rnp3k7rvsme5seopv65lcgm56ey2oyd.onion</a></span><br />\n"
    115 		"Gopher: <a href=\"gopher://hhvn.uk\">gopher://hhvn.uk</a>\n"
    116 		"<span class=\"tor\">Gopher (tor): <a href=\"gopher://zl7kawb6nd3aqzrwo5cz57id7rnp3k7rvsme5seopv65lcgm56ey2oyd.onion\">gopher://zl7kawb6nd3aqzrwo5cz57id7rnp3k7rvsme5seopv65lcgm56ey2oyd.onion</a></span>\n"
    117 		"<hr />\n"
    118 		"<p>Anyway, here's some code:\n");
    119 	fputs("</span></td></tr><tr><td></td><td>\n"
    120 		"</td></tr>\n</table>\n<div id=\"content\">\n"
    121 		"<table id=\"index\"><thead>\n"
    122 		"<tr><td><b>Name</b></td><td><b>Description</b></td><td></td>"
    123 		"<td><b>Last commit</b></td></tr>"
    124 		"</thead><tbody>\n", fp);
    125 }
    126 
    127 void
    128 writefooter(FILE *fp)
    129 {
    130 	fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
    131 }
    132 
    133 int
    134 writelog(FILE *fp)
    135 {
    136 	git_commit *commit = NULL;
    137 	const git_signature *author;
    138 	git_revwalk *w = NULL;
    139 	git_oid id;
    140 	char *stripped_name = NULL, *p;
    141 	int ret = 0;
    142 
    143 	git_revwalk_new(&w, repo);
    144 	git_revwalk_push_head(w);
    145 
    146 	if (git_revwalk_next(&id, w) ||
    147 	    git_commit_lookup(&commit, repo, &id)) {
    148 		ret = -1;
    149 		goto err;
    150 	}
    151 
    152 	author = git_commit_author(commit);
    153 
    154 	/* strip .git suffix */
    155 	if (!(stripped_name = strdup(name)))
    156 		err(1, "strdup");
    157 	if ((p = strrchr(stripped_name, '.')))
    158 		if (!strcmp(p, ".git"))
    159 			*p = '\0';
    160 
    161 	fputs("<tr><td><a href=\"", fp);
    162 	percentencode(fp, stripped_name, strlen(stripped_name));
    163 	fputs("/log.html\">", fp);
    164 	xmlencode(fp, stripped_name, strlen(stripped_name));
    165 	fputs("</a></td><td>", fp);
    166 	xmlencode(fp, description, strlen(description));
    167 	fputs("</td><td>", fp);
    168 	xmlencode(fp, owner, strlen(owner));
    169 	fputs("</td><td>", fp);
    170 	if (author)
    171 		printtimeshort(fp, &(author->when));
    172 	fputs("</td></tr>", fp);
    173 
    174 	git_commit_free(commit);
    175 err:
    176 	git_revwalk_free(w);
    177 	free(stripped_name);
    178 
    179 	return ret;
    180 }
    181 
    182 int
    183 main(int argc, char *argv[])
    184 {
    185 	FILE *fp;
    186 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    187 	const char *repodir;
    188 	int i, ret = 0;
    189 
    190 	if (argc < 2) {
    191 		fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
    192 		return 1;
    193 	}
    194 
    195 	/* do not search outside the git repository:
    196 	   GIT_CONFIG_LEVEL_APP is the highest level currently */
    197 	git_libgit2_init();
    198 	for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
    199 		git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
    200 	/* do not require the git repository to be owned by the current user */
    201 	git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
    202 
    203 #ifdef __OpenBSD__
    204 	if (pledge("stdio rpath", NULL) == -1)
    205 		err(1, "pledge");
    206 #endif
    207 
    208 	writeheader(stdout);
    209 
    210 	for (i = 1; i < argc; i++) {
    211 		repodir = argv[i];
    212 		if (!realpath(repodir, repodirabs))
    213 			err(1, "realpath");
    214 
    215 		if (git_repository_open_ext(&repo, repodir,
    216 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    217 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    218 			ret = 1;
    219 			continue;
    220 		}
    221 
    222 		/* use directory name as name */
    223 		if ((name = strrchr(repodirabs, '/')))
    224 			name++;
    225 		else
    226 			name = "";
    227 
    228 		/* read description or .git/description */
    229 		joinpath(path, sizeof(path), repodir, "description");
    230 		if (!(fp = fopen(path, "r"))) {
    231 			joinpath(path, sizeof(path), repodir, ".git/description");
    232 			fp = fopen(path, "r");
    233 		}
    234 		description[0] = '\0';
    235 		if (fp) {
    236 			if (!fgets(description, sizeof(description), fp))
    237 				description[0] = '\0';
    238 			checkfileerror(fp, "description", 'r');
    239 			fclose(fp);
    240 		}
    241 
    242 		/* read owner or .git/owner */
    243 		joinpath(path, sizeof(path), repodir, "owner");
    244 		if (!(fp = fopen(path, "r"))) {
    245 			joinpath(path, sizeof(path), repodir, ".git/owner");
    246 			fp = fopen(path, "r");
    247 		}
    248 		owner[0] = '\0';
    249 		if (fp) {
    250 			if (!fgets(owner, sizeof(owner), fp))
    251 				owner[0] = '\0';
    252 			checkfileerror(fp, "owner", 'r');
    253 			fclose(fp);
    254 			owner[strcspn(owner, "\n")] = '\0';
    255 		}
    256 		writelog(stdout);
    257 	}
    258 	writefooter(stdout);
    259 
    260 	/* cleanup */
    261 	git_repository_free(repo);
    262 	git_libgit2_shutdown();
    263 
    264 	checkfileerror(stdout, "<stdout>", 'w');
    265 
    266 	return ret;
    267 }