stagit

personal fork of stagit (rnpnr branch)
git clone anongit@rnpnr.xyz:stagit.git
Log | Files | Refs | Feed | README | LICENSE

stagit-index.c (6354B)


      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 "config.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 
     20 /* Handle read or write errors for a FILE * stream */
     21 void
     22 checkfileerror(FILE *fp, const char *name, int mode)
     23 {
     24 	if (mode == 'r' && ferror(fp))
     25 		errx(1, "read error: %s", name);
     26 	else if (mode == 'w' && (fflush(fp) || ferror(fp)))
     27 		errx(1, "write error: %s", name);
     28 }
     29 
     30 void
     31 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
     32 {
     33 	int r;
     34 
     35 	r = snprintf(buf, bufsiz, "%s%s%s",
     36 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     37 	if (r < 0 || (size_t)r >= bufsiz)
     38 		errx(1, "path truncated: '%s%s%s'",
     39 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     40 }
     41 
     42 /* Percent-encode, see RFC3986 section 2.1. */
     43 void
     44 percentencode(FILE *fp, const char *s, size_t len)
     45 {
     46 	static char tab[] = "0123456789ABCDEF";
     47 	unsigned char uc;
     48 	size_t i;
     49 
     50 	for (i = 0; *s && i < len; s++, i++) {
     51 		uc = *s;
     52 		/* NOTE: do not encode '/' for paths or ",-." */
     53 		if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') ||
     54 		    uc == '[' || uc == ']') {
     55 			putc('%', fp);
     56 			putc(tab[(uc >> 4) & 0x0f], fp);
     57 			putc(tab[uc & 0x0f], fp);
     58 		} else {
     59 			putc(uc, fp);
     60 		}
     61 	}
     62 }
     63 
     64 /* Escape characters below as HTML 2.0 / XML 1.0. */
     65 void
     66 xmlencode(FILE *fp, const char *s, size_t len)
     67 {
     68 	size_t i;
     69 
     70 	for (i = 0; *s && i < len; s++, i++) {
     71 		switch(*s) {
     72 		case '<':  fputs("&lt;",   fp); break;
     73 		case '>':  fputs("&gt;",   fp); break;
     74 		case '\'': fputs("&#39;" , fp); break;
     75 		case '&':  fputs("&amp;",  fp); break;
     76 		case '"':  fputs("&quot;", fp); break;
     77 		default:   putc(*s, fp);
     78 		}
     79 	}
     80 }
     81 
     82 void
     83 printtimeshort(FILE *fp, const git_time *intime)
     84 {
     85 	struct tm *intm;
     86 	time_t t;
     87 	char out[32];
     88 
     89 	t = (time_t)intime->time;
     90 	if (!(intm = gmtime(&t)))
     91 		return;
     92 	strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
     93 	fputs(out, fp);
     94 }
     95 
     96 void
     97 writeheader(FILE *fp)
     98 {
     99 	fputs("<!DOCTYPE html>\n"
    100 		"<html>\n<head>\n"
    101 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
    102 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
    103 		"<title>", fp);
    104 	fputs(indextitle, fp);
    105 	fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
    106 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
    107 	fputs("</head>\n<body class=\"git\">\n", fp);
    108 	fprintf(fp, "<nav>\n\t<h4><a href=\"/\">%s</a></h4>\n", navtitle);
    109 	fputs("\t<div class=\"navbar\">\n", fp);
    110 	for (int i = 0; i < (sizeof(navbar)/sizeof(*navbar)); i++)
    111 		fprintf(fp, "\t\t<a href=\"/%s\">%s</a>\n", navbar[i][1], navbar[i][0]);
    112 	fputs("\t</div><hr>\n</nav>\n<h1>", fp);
    113 	xmlencode(fp, description, strlen(description));
    114 	fputs("</h1>\n<div id=\"content\">\n"
    115 		"<table id=\"index\"><thead>\n"
    116 		"<tr><td><b>Name</b></td><td><b>Description</b></td>"
    117 		"<td><b>Last commit</b></td></tr>"
    118 		"</thead><tbody>\n", fp);
    119 }
    120 
    121 void
    122 writefooter(FILE *fp)
    123 {
    124 	fputs("</tbody>\n</table>\n</div>\n", fp);
    125 	fputs("<footer>\n\t<hr>\n\t<p><a href=\"/\">", fp);
    126 	fputs(siteurl, fp);
    127 	fputs("</a></p>\n\t<div>\n\t<img src=\"/assets/xmr.svg\" "
    128 	            "style=\"max-height:1.5em;border-radius: 10px\" alt=\"XMR\"> "
    129 	            "Monero (XMR): <div class=\"crypto\" style=\"display: inline-block;\"><code>"
    130 	            "866mNUnXQr32FhZ9SFgfmedr2KjYQnhCwbmAxzqJ1nteCVsv8eifyn6E6cuQvyhRAVY5arYznUiUtBBbzBpiGqKCSrEqVxS"
    131 	            "</code></div>\n\t</div>\n</footer>", fp);
    132 	fputs("</body>\n</html>\n", fp);
    133 }
    134 
    135 int
    136 writelog(FILE *fp)
    137 {
    138 	git_commit *commit = NULL;
    139 	const git_signature *author;
    140 	git_revwalk *w = NULL;
    141 	git_oid id;
    142 	char *stripped_name = NULL, *p;
    143 	int ret = 0;
    144 
    145 	git_revwalk_new(&w, repo);
    146 	git_revwalk_push_head(w);
    147 
    148 	if (git_revwalk_next(&id, w) ||
    149 	    git_commit_lookup(&commit, repo, &id)) {
    150 		ret = -1;
    151 		goto err;
    152 	}
    153 
    154 	author = git_commit_author(commit);
    155 
    156 	/* strip .git suffix */
    157 	if (!(stripped_name = strdup(name)))
    158 		err(1, "strdup");
    159 	if ((p = strrchr(stripped_name, '.')))
    160 		if (!strcmp(p, ".git"))
    161 			*p = '\0';
    162 
    163 	fputs("<tr><td><a href=\"", fp);
    164 	percentencode(fp, stripped_name, strlen(stripped_name));
    165 	fputs("/log.html\">", fp);
    166 	xmlencode(fp, stripped_name, strlen(stripped_name));
    167 	fputs("</a></td><td>", fp);
    168 	xmlencode(fp, description, strlen(description));
    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 		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 }