stagit

opinionated personal fork of codemadness.org/git/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (7024B)


      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 #define LEN(s)	  (sizeof(s)/sizeof(*s))
     12 
     13 static git_repository *repo;
     14 
     15 static const char *relpath = "";
     16 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md", "HEAD:readme.md", "HEAD:README.org", "HEAD:readme.org" };
     17 
     18 static char description[255] = "Repositories";
     19 static char last_development[41] = "";
     20 static char *name = "";
     21 static char *readme = NULL;
     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", relpath);
    109 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
    110 	fputs("</head>\n<body>\n", fp);
    111 	fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
    112 			"<td><span class=\"desc\">", relpath);
    113 	xmlencode(fp, description, strlen(description));
    114 	fputs("</span></td></tr><tr><td></td><td>\n"
    115 		"</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
    116 		"<table id=\"index\"><thead>\n"
    117 		"<tr><td><b>Name</b></td><td><b>Description</b></td>"
    118 		"<td><b>Last development</b></td></tr>"
    119 		"</thead><tbody>\n", fp);
    120 }
    121 
    122 void
    123 writefooter(FILE *fp)
    124 {
    125 	fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
    126 }
    127 
    128 int
    129 writelog(FILE *fp)
    130 {
    131 	git_commit *commit = NULL;
    132 	const git_signature *author;
    133 	git_revwalk *w = NULL;
    134 	git_oid id;
    135 	char *stripped_name = NULL, *p;
    136 	int ret = 0;
    137 
    138 	git_revwalk_new(&w, repo);
    139 	git_revwalk_push_head(w);
    140 
    141 	if (git_oid_fromstr(&id, last_development) &&
    142 		git_revwalk_next(&id, w)) {
    143 		fprintf(stderr, "failed to get good commit id for %s", name);
    144 		ret = -1;
    145 		goto err;
    146 	}
    147 
    148 	if (git_commit_lookup(&commit, repo, &id)) {
    149 		fprintf(stderr, "failed get commit info for %s\n", name);
    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 
    166 	if (readme != NULL) {
    167 		fprintf(fp, "/file/%s.html\">", readme);
    168 	} else {
    169 		fputs("/log.html\">", fp);
    170 	}
    171 
    172 	xmlencode(fp, stripped_name, strlen(stripped_name));
    173 	fputs("</a></td><td>", fp);
    174 	xmlencode(fp, description, strlen(description));
    175 	fputs("</td><td>", fp);
    176 	if (author)
    177 		printtimeshort(fp, &(author->when));
    178 	fputs("</td></tr>", fp);
    179 
    180 	git_commit_free(commit);
    181 err:
    182 	git_revwalk_free(w);
    183 	free(stripped_name);
    184 
    185 	return ret;
    186 }
    187 
    188 int
    189 main(int argc, char *argv[])
    190 {
    191 	FILE *fp;
    192 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    193 	const char *repodir;
    194 	int i, j, ret = 0;
    195 	git_object *obj = NULL;
    196 
    197 	if (argc < 2) {
    198 		fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
    199 		return 1;
    200 	}
    201 
    202 	/* do not search outside the git repository:
    203 	   GIT_CONFIG_LEVEL_APP is the highest level currently */
    204 	git_libgit2_init();
    205 	for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
    206 		git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
    207 	/* do not require the git repository to be owned by the current user */
    208 	git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
    209 
    210 #ifdef __OpenBSD__
    211 	if (pledge("stdio rpath", NULL) == -1)
    212 		err(1, "pledge");
    213 #endif
    214 
    215 	writeheader(stdout);
    216 
    217 	for (i = 1; i < argc; i++) {
    218 		repodir = argv[i];
    219 		if (!realpath(repodir, repodirabs))
    220 			err(1, "realpath");
    221 
    222 		if (git_repository_open_ext(&repo, repodir,
    223 			GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    224 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    225 			ret = 1;
    226 			continue;
    227 		}
    228 
    229 		/* use directory name as name */
    230 		if ((name = strrchr(repodirabs, '/')))
    231 			name++;
    232 		else
    233 			name = "";
    234 
    235 		/* read description or .git/description */
    236 		joinpath(path, sizeof(path), repodir, "description");
    237 		if (!(fp = fopen(path, "r"))) {
    238 			joinpath(path, sizeof(path), repodir, ".git/description");
    239 			fp = fopen(path, "r");
    240 		}
    241 		description[0] = '\0';
    242 		if (fp) {
    243 			if (!fgets(description, sizeof(description), fp))
    244 				description[0] = '\0';
    245 			checkfileerror(fp, "description", 'r');
    246 			fclose(fp);
    247 		}
    248 
    249 		/* read last-development or .git/last-development */
    250 		joinpath(path, sizeof(path), repodir, "last-development");
    251 		if (!(fp = fopen(path, "r"))) {
    252 			joinpath(path, sizeof(path), repodir, ".git/last-development");
    253 			fp = fopen(path, "r");
    254 		}
    255 		last_development[0] = '\0';
    256 		if (fp) {
    257 			if (!fgets(last_development, sizeof(last_development), fp))
    258 				last_development[0] = '\0';
    259 			checkfileerror(fp, "last-development", 'r');
    260 			fclose(fp);
    261 		}
    262 
    263 		for (j = 0; j < LEN(readmefiles) && !readme; j++) {
    264 			if (!git_revparse_single(&obj, repo, readmefiles[j]) &&
    265 				git_object_type(obj) == GIT_OBJ_BLOB) {
    266 				readme = readmefiles[j] + strlen("HEAD:");
    267 			}
    268 			git_object_free(obj);
    269 		}
    270 
    271 		writelog(stdout);
    272 		readme = NULL;
    273 	}
    274 	writefooter(stdout);
    275 
    276 	/* cleanup */
    277 	git_repository_free(repo);
    278 	git_libgit2_shutdown();
    279 
    280 	checkfileerror(stdout, "<stdout>", 'w');
    281 
    282 	return ret;
    283 }