stagit-index.c (7423B) - raw


      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] = "Oscar Benedito's Git repositories";
     16 static char *name = "";
     17 static char owner[255];
     18 static char category[255];
     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 	xmlencode(fp, description, strlen(description));
    105 	fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/svg+xml\" href=\"../%slogo.svg\" />\n", relpath);
    106 	fprintf(fp, "<link rel=\"alternate icon\" href=\"%sfavicon.ico\" />\n", relpath);
    107 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
    108 	fputs("</head>\n<body id=\"home\">\n<h1>", fp);
    109 	xmlencode(fp, description, strlen(description));
    110 	fputs("</h1>\n<div id=\"content\">\n"
    111 		"<h2 id=\"repositories\">Repositories</h2>\n"
    112 		"<div class=\"table-container\">\n<table id=\"index\"><thead>\n"
    113 		"<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Last commit</b></td></tr>"
    114 		"</thead><tbody>\n", fp);
    115 }
    116 
    117 void
    118 writefooter(FILE *fp)
    119 {
    120 	fputs("</tbody>\n</table>\n</div>\n"
    121 		"<h2 id=\"contribute\">Contribute</h2>\n"
    122 		"<p>The best way to contribute to my repositories is through e-mail, check out <a href=\"https://git-send-email.io\">git-send-email.io</a> if you don’t know how to do that. Send your patches to <a href=\"mailto:patches@oscarbenedito.com\">patches@oscarbenedito.com</a> and change the subject prefix to specify the repository you are sending the patch for. You can do that running the following command from the git repository:</p>\n"
    123 		"<pre><code>git config format.subjectPrefix \"PATCH &lt;name-of-repository&gt;\"</code></pre>\n"
    124 		"<p>You can also contribute on <a href=\"https://github.com/oscarbenedito\">GitHub</a> doing pull requests (all my public repositories are mirrored there, as well as <a href=\"https://git.sr.ht/~ob\">SourceHut</a>).</p>\n"
    125 		"</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_revwalk_next(&id, w) ||
    142 	    git_commit_lookup(&commit, repo, &id)) {
    143 		ret = -1;
    144 		goto err;
    145 	}
    146 
    147 	author = git_commit_author(commit);
    148 
    149 	/* strip .git suffix */
    150 	if (!(stripped_name = strdup(name)))
    151 		err(1, "strdup");
    152 	if ((p = strrchr(stripped_name, '.')))
    153 		if (!strcmp(p, ".git"))
    154 			*p = '\0';
    155 
    156 	fputs("<tr class=\"repo\"><td><a href=\"", fp);
    157 	percentencode(fp, stripped_name, strlen(stripped_name));
    158 	fputs("/\">", fp);
    159 	xmlencode(fp, stripped_name, strlen(stripped_name));
    160 	fputs("</a></td><td>", fp);
    161 	xmlencode(fp, description, strlen(description));
    162 	fputs("</td><td>", fp);
    163 	if (author)
    164 		printtimeshort(fp, &(author->when));
    165 	fputs("</td></tr>\n", fp);
    166 
    167 	git_commit_free(commit);
    168 err:
    169 	git_revwalk_free(w);
    170 	free(stripped_name);
    171 
    172 	return ret;
    173 }
    174 
    175 int
    176 main(int argc, char *argv[])
    177 {
    178 	FILE *fp;
    179 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    180 	const char *repodir;
    181 	int i, ret = 0, tmp;
    182 
    183 	if (argc < 2) {
    184 		fprintf(stderr, "%s [repodir...]\n", argv[0]);
    185 		return 1;
    186 	}
    187 
    188 	/* do not search outside the git repository:
    189 	   GIT_CONFIG_LEVEL_APP is the highest level currently */
    190 	git_libgit2_init();
    191 	for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
    192 		git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
    193 	/* do not require the git repository to be owned by the current user */
    194 	git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
    195 
    196 #ifdef __OpenBSD__
    197 	if (pledge("stdio rpath", NULL) == -1)
    198 		err(1, "pledge");
    199 #endif
    200 
    201 	writeheader(stdout);
    202 
    203 	for (i = 1; i < argc; i++) {
    204 		if (!strcmp(argv[i], "-c")) {
    205 			i++;
    206 			if (i == argc)
    207 				err(1, "missing argument");
    208 			repodir = argv[i];
    209 			fputs("<tr class=\"cat\"><td>", stdout);
    210 			xmlencode(stdout, repodir, strlen(repodir));
    211 			fputs("</td><td></td><td></td></tr>\n", stdout);
    212 			continue;
    213 		}
    214 
    215 		repodir = argv[i];
    216 		if (!realpath(repodir, repodirabs))
    217 			err(1, "realpath");
    218 
    219 		if (git_repository_open_ext(&repo, repodir,
    220 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    221 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    222 			ret = 1;
    223 			continue;
    224 		}
    225 
    226 		/* use directory name as name */
    227 		if ((name = strrchr(repodirabs, '/')))
    228 			name++;
    229 		else
    230 			name = "";
    231 
    232 		/* read description or .git/description */
    233 		joinpath(path, sizeof(path), repodir, "description");
    234 		if (!(fp = fopen(path, "r"))) {
    235 			joinpath(path, sizeof(path), repodir, ".git/description");
    236 			fp = fopen(path, "r");
    237 		}
    238 		description[0] = '\0';
    239 		if (fp) {
    240 			if (!fgets(description, sizeof(description), fp))
    241 				description[0] = '\0';
    242 			tmp = strlen(description);
    243 			if (tmp > 0 && description[tmp-1] == '\n')
    244 				description[tmp-1] = '\0';
    245 			checkfileerror(fp, "description", 'r');
    246 			fclose(fp);
    247 		}
    248 
    249 		/* read owner or .git/owner */
    250 		joinpath(path, sizeof(path), repodir, "owner");
    251 		if (!(fp = fopen(path, "r"))) {
    252 			joinpath(path, sizeof(path), repodir, ".git/owner");
    253 			fp = fopen(path, "r");
    254 		}
    255 		owner[0] = '\0';
    256 		if (fp) {
    257 			if (!fgets(owner, sizeof(owner), fp))
    258 				owner[0] = '\0';
    259 			checkfileerror(fp, "owner", 'r');
    260 			fclose(fp);
    261 			owner[strcspn(owner, "\n")] = '\0';
    262 		}
    263 		writelog(stdout);
    264 	}
    265 	writefooter(stdout);
    266 
    267 	/* cleanup */
    268 	git_repository_free(repo);
    269 	git_libgit2_shutdown();
    270 
    271 	checkfileerror(stdout, "<stdout>", 'w');
    272 
    273 	return ret;
    274 }