stagit

stagit fork
git clone git://git.mdnr.space/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (6930B)


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