stagit

static git page - forked from git.codemadness.org/stagit
git clone git://src.gearsix.net/stagit
Log | Files | Refs | Atom | README | LICENSE

stagit-index.c (7405B)


      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 char *binary;
     12 
     13 static git_repository *repo;
     14 
     15 static const char *rootpath = "/";
     16 static const char *relpath = "";
     17 
     18 static char description[255] = "gearsix source code";
     19 static char *name = "gearsix";
     20 static char *contact = "gearsix@tuta.io";
     21 static char owner[255];
     22 
     23 /* Handle read or write errors for a FILE * stream */
     24 void
     25 checkfileerror(FILE *fp, const char *name, int mode)
     26 {
     27 	if (mode == 'r' && ferror(fp))
     28 		errx(1, "read error: %s", name);
     29 	else if (mode == 'w' && (fflush(fp) || ferror(fp)))
     30 		errx(1, "write error: %s", name);
     31 }
     32 
     33 void
     34 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
     35 {
     36 	int r;
     37 
     38 	r = snprintf(buf, bufsiz, "%s%s%s",
     39 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     40 	if (r < 0 || (size_t)r >= bufsiz)
     41 		errx(1, "path truncated: '%s%s%s'",
     42 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     43 }
     44 
     45 /* Percent-encode, see RFC3986 section 2.1. */
     46 void
     47 percentencode(FILE *fp, const char *s, size_t len)
     48 {
     49 	static char tab[] = "0123456789ABCDEF";
     50 	unsigned char uc;
     51 	size_t i;
     52 
     53 	for (i = 0; *s && i < len; s++, i++) {
     54 		uc = *s;
     55 		/* NOTE: do not encode '/' for paths or ",-." */
     56 		if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') ||
     57 		    uc == '[' || uc == ']') {
     58 			putc('%', fp);
     59 			putc(tab[(uc >> 4) & 0x0f], fp);
     60 			putc(tab[uc & 0x0f], fp);
     61 		} else {
     62 			putc(uc, fp);
     63 		}
     64 	}
     65 }
     66 
     67 /* Escape characters below as HTML 2.0 / XML 1.0. */
     68 void
     69 xmlencode(FILE *fp, const char *s, size_t len)
     70 {
     71 	size_t i;
     72 
     73 	for (i = 0; *s && i < len; s++, i++) {
     74 		switch(*s) {
     75 		case '<':  fputs("&lt;",   fp); break;
     76 		case '>':  fputs("&gt;",   fp); break;
     77 		case '\'': fputs("&#39;" , fp); break;
     78 		case '&':  fputs("&amp;",  fp); break;
     79 		case '"':  fputs("&quot;", fp); break;
     80 		default:   putc(*s, fp);
     81 		}
     82 	}
     83 }
     84 
     85 void
     86 printtimeshort(FILE *fp, const git_time *intime)
     87 {
     88 	struct tm *intm;
     89 	time_t t;
     90 	char out[32];
     91 
     92 	t = (time_t)intime->time;
     93 	if (!(intm = gmtime(&t)))
     94 		return;
     95 	strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
     96 	fputs(out, fp);
     97 }
     98 
     99 void
    100 writeheader(FILE *fp)
    101 {
    102 	fputs("<!DOCTYPE html>\n"
    103 		"<html>\n<head>\n"
    104 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
    105 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
    106 		"<title>", fp);
    107 	xmlencode(fp, description, strlen(description));
    108 	fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", rootpath);
    109 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", rootpath);
    110 	fputs("</head><body>\n", fp);
    111 	fprintf(fp, "<table>\n<tr><td id=\"logo\"><img src=\"%slogo.png\" alt=\"\" width=\"50\" height=\"50\" /></td>\n"
    112 	        "<td><h2>%s</h2>", rootpath, description);
    113 	if (contact && strlen(contact) > 0) {
    114 		int c = strlen(contact)-1;
    115 		fprintf(fp, "<span class=\"contact\"><b>contact:</b> ");
    116 		fprintf(fp, "<author style='white-space: nowrap; unicode-bidi:bidi-override; direction: rtl;'>");
    117 		for (; c > -1; --c) fprintf(fp, "%c", contact[c]);
    118 		fprintf(fp, "</author></span>\n");
    119 	}
    120 	fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n", fp);
    121 }
    122 
    123 void
    124 writebodyhead(FILE *fp) {
    125 	fputs("<table class=\"index\"><thead>\n"
    126 		"<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
    127 		"<td><b>Last commit</b></td></tr>"
    128 		"</thead><tbody>\n", fp);
    129 }
    130 
    131 int
    132 writelog(FILE *fp)
    133 {
    134 	git_commit *commit = NULL;
    135 	const git_signature *author;
    136 	git_revwalk *w = NULL;
    137 	git_oid id;
    138 	char *stripped_name = NULL, *p;
    139 	int ret = 0;
    140 
    141 	git_revwalk_new(&w, repo);
    142 	git_revwalk_push_head(w);
    143 
    144 	if (git_revwalk_next(&id, w) ||
    145 	    git_commit_lookup(&commit, repo, &id)) {
    146 		ret = -1;
    147 		goto err;
    148 	}
    149 
    150 	author = git_commit_author(commit);
    151 
    152 	/* strip .git suffix */
    153 	if (!(stripped_name = strdup(name)))
    154 		err(1, "strdup");
    155 	if ((p = strrchr(stripped_name, '.')))
    156 		if (!strcmp(p, ".git"))
    157 			*p = '\0';
    158 
    159 	fputs("<tr><td><a href=\"", fp);
    160 	percentencode(fp, stripped_name, strlen(stripped_name));
    161 	fputs("/log.html\">", fp);
    162 	xmlencode(fp, stripped_name, strlen(stripped_name));
    163 	fputs("</a></td><td>", fp);
    164 	xmlencode(fp, description, strlen(description));
    165 	fputs("</td><td>", fp);
    166 	xmlencode(fp, owner, strlen(owner));
    167 	fputs("</td><td>", fp);
    168 	if (author)
    169 		printtimeshort(fp, &(author->when));
    170 	fputs("</td></tr>", fp);
    171 
    172 	git_commit_free(commit);
    173 err:
    174 	git_revwalk_free(w);
    175 	free(stripped_name);
    176 
    177 	return ret;
    178 }
    179 
    180 /* forks:
    181  *   0 = only add repo if .git/fork is not present
    182  *   1 = only add repo if .git/fork is present
    183  *   2 = add repo regardless of .git/fork file presence
    184  */
    185 int
    186 writebody(FILE *fp, char *repodir, int forks) {
    187 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    188 
    189 	if (!realpath(repodir, repodirabs))
    190 		err(1, "realpath");
    191 
    192 	if (git_repository_open_ext(&repo, repodir,
    193 		GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    194 		fprintf(stderr, "%s: cannot open repository '%s'\n", binary, repodir);
    195 		return 1;
    196 	}
    197 
    198 	// check .git/fork
    199 	if (forks != 2) {
    200 		joinpath(path, sizeof(path), repodir, ".git/fork");
    201 		fp = fopen(path, "r");
    202 		if ((fp && forks == 0) || (!fp && forks == 1))
    203 			return 0;
    204 	}
    205 
    206 	/* use directory name as name */
    207 	if ((name = strrchr(repodirabs, '/')))
    208 		name++;
    209 	else
    210 		name = "";
    211 
    212 	/* read description or .git/description */
    213 	joinpath(path, sizeof(path), repodir, "description");
    214 	if (!(fp = fopen(path, "r"))) {
    215 		joinpath(path, sizeof(path), repodir, ".git/description");
    216 		fp = fopen(path, "r");
    217 	}
    218 	description[0] = '\0';
    219 	if (fp) {
    220 		if (!fgets(description, sizeof(description), fp))
    221 			description[0] = '\0';
    222 		fclose(fp);
    223 	}
    224 
    225 	/* read owner or .git/owner */
    226 	joinpath(path, sizeof(path), repodir, "owner");
    227 	if (!(fp = fopen(path, "r"))) {
    228 		joinpath(path, sizeof(path), repodir, ".git/owner");
    229 		fp = fopen(path, "r");
    230 	}
    231 	owner[0] = '\0';
    232 	if (fp) {
    233 		if (!fgets(owner, sizeof(owner), fp))
    234 			owner[0] = '\0';
    235 		owner[strcspn(owner, "\n")] = '\0';
    236 		fclose(fp);
    237 	}
    238 	writelog(stdout);
    239 }
    240 
    241 void
    242 writebodyfoot(FILE *fp)
    243 {
    244 	fputs("</tbody>\n</table></details>\n", fp);
    245 }
    246 
    247 void
    248 writefooter(FILE *fp)
    249 {
    250 	fputs("</div>\n</body>\n</html>\n", fp);
    251 }
    252 
    253 int
    254 main(int argc, char *argv[])
    255 {
    256 	FILE *fp;
    257 	const char *repodir;
    258 	int i, ret = 0;
    259 
    260 	binary = argv[0];
    261 	if (argc < 2) {
    262 		fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
    263 		return 1;
    264 	}
    265 
    266 	/* do not search outside the git repository:
    267 	   GIT_CONFIG_LEVEL_APP is the highest level currently */
    268 	git_libgit2_init();
    269 	for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
    270 		git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
    271 	/* do not require the git repository to be owned by the current user */
    272 	git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
    273 
    274 #ifdef __OpenBSD__
    275 	if (pledge("stdio rpath", NULL) == -1)
    276 		err(1, "pledge");
    277 #endif
    278 
    279 	writeheader(stdout);
    280 
    281 	writebodyhead(stdout);
    282 	for (i = 1; i < argc; i++) {
    283 		ret = writebody(stdout, argv[i], 0); 
    284 		if (ret != 0)
    285 			return ret;
    286 	}
    287 	writebodyfoot(stdout);
    288 
    289 	fputs("<details open><summary><b>forks</b></summary>", stdout);
    290 	writebodyhead(stdout);
    291 	for (i = 1; i < argc; i++) {
    292 		ret = writebody(stdout, argv[i], 1);
    293 		if (ret != 0)
    294 			return ret;
    295 	}
    296 	writebodyfoot(stdout);
    297 	fputs("</details>", stdout);
    298 
    299 	writefooter(stdout);
    300 
    301 	/* cleanup */
    302 	git_repository_free(repo);
    303 	git_libgit2_shutdown();
    304 
    305 	checkfileerror(stdout, "<stdout>", 'w');
    306 
    307 	return ret;
    308 }