stagit

- static git page generator
git clone git://git.acid.vegas/stagit.git
Log | Files | Refs | Archive | README | LICENSE

stagit-index.c (6722B)

      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 #include <git2.h>
      9 
     10 static git_repository *repo;
     11 static const char *relpath = "";
     12 static char description[255] = "Acidvegas Repositories";
     13 static char *name = "";
     14 static char category[255];
     15 
     16 /* Handle read or write errors for a FILE * stream */
     17 void checkfileerror(FILE *fp, const char *name, int mode) {
     18 	if (mode == 'r' && ferror(fp))
     19 		errx(1, "read error: %s", name);
     20 	else if (mode == 'w' && (fflush(fp) || ferror(fp)))
     21 		errx(1, "write error: %s", name);
     22 }
     23 
     24 void joinpath(char *buf, size_t bufsiz, const char *path, const char *path2) {
     25 	int r;
     26 	r = snprintf(buf, bufsiz, "%s%s%s", path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     27 	if (r < 0 || (size_t)r >= bufsiz)
     28 		errx(1, "path truncated: '%s%s%s'", path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     29 }
     30 
     31 /* Percent-encode, see RFC3986 section 2.1. */
     32 void percentencode(FILE *fp, const char *s, size_t len) {
     33 	static char tab[] = "0123456789ABCDEF";
     34 	unsigned char uc;
     35 	size_t i;
     36 	for (i = 0; *s && i < len; s++, i++) {
     37 		uc = *s;
     38 		/* NOTE: do not encode '/' for paths or ",-." */
     39 		if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') || uc == '[' || uc == ']') {
     40 			putc('%', fp);
     41 			putc(tab[(uc >> 4) & 0x0f], fp);
     42 			putc(tab[uc & 0x0f], fp);
     43 		} else {
     44 			putc(uc, fp);
     45 		}
     46 	}
     47 }
     48 
     49 /* Escape characters below as HTML 2.0 / XML 1.0. */
     50 void xmlencode(FILE *fp, const char *s, size_t len) {
     51 	size_t i;
     52 	for (i = 0; *s && i < len; s++, i++) {
     53 		switch(*s) {
     54 			case '<':  fputs("&lt;",   fp); break;
     55 			case '>':  fputs("&gt;",   fp); break;
     56 			case '\'': fputs("&#39;" , fp); break;
     57 			case '&':  fputs("&amp;",  fp); break;
     58 			case '"':  fputs("&quot;", fp); break;
     59 			default:   putc(*s, fp);
     60 		}
     61 	}
     62 }
     63 
     64 void printtimeshort(FILE *fp, const git_time *intime) {
     65 	struct tm *intm;
     66 	time_t t;
     67 	char out[32];
     68 	t = (time_t)intime->time;
     69 	if (!(intm = gmtime(&t)))
     70 		return;
     71 	strftime(out, sizeof(out), "%Y-%m-%d", intm);
     72 	fputs(out, fp);
     73 }
     74 
     75 void writeheader(FILE *fp) {
     76 	fputs("<!DOCTYPE html>\n<meta charset=\"UTF-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<title>", fp);
     77 	xmlencode(fp, description, strlen(description));
     78 	fputs("</title>\n<meta name=\"description\" content=\"acidvegas repositories\">\n"
     79 		"<meta name=\"keywords\" content=\"git, repositories, supernets, irc, python, stagit\">\n"
     80 		"<meta name=\"author\" content=\"acidvegas\">\n", fp);
     81 	fputs("<link rel=\"icon\" type=\"image/png\" href=\"/assets/favicon.png\">\n"
     82 		"<link rel=\"stylesheet\" type=\"text/css\" href=\"/assets/style.css\">\n", fp);
     83 	fputs("<center>\n<img src=\"/assets/acidvegas.png\"><br>\n<img src=\"/assets/mostdangerous.png\"><br><br>\n", fp);
     84 	fputs("<div class=\"container\">\n\t<center>\n\t<table>\n\t\t<tr><td>\n"
     85 		"<b>contact</b> : <a href=\"https://discord.gg/BCqRZZR\">discord</a> &bull; <a href=\"ircs://irc.supernets.org/superbowl\">irc</a> &bull; <a href=\"mailto://acid.vegas@acid.vegas\">mail</a> &bull; <a href=\"https://twitter.com/acidvegas\">twitter</a>\n"
     86 		"<br><b>mirrors</b> : <a href=\"https://github.com/acidvegas\">github</a> &bull; <a href=\"https://gitlab.com/acidvegas\">gitlab</a> &bull; <a href=\"https://git.supernets.org/acidvegas\">supernets</a>\n"
     87         "\t\t</td></tr>\n\t</table>\n\t</center>\n</div>\n<br>\n", fp);
     88 	fputs("<div id=\"content\">\n\t<table id=\"index\">\n\t\t<thead>\n\t\t\t<tr><td>Name</td><td>Description</td><td>Last commit</td></tr>\n\t\t</thead>\n\t\t<tbody>", fp);
     89 }
     90 
     91 void writefooter(FILE *fp) {
     92 	fputs("\n\t\t</tbody>\n\t</table>\n</div>\n<div id=\"footer\">\n"
     93 		"\t&copy; 2023 acidvegas, inc &bull; generated with stagit\n"
     94 		"</div>\n</center>", fp);
     95 }
     96 
     97 int writelog(FILE *fp) {
     98 	git_commit *commit = NULL;
     99 	const git_signature *author;
    100 	git_revwalk *w = NULL;
    101 	git_oid id;
    102 	char *stripped_name = NULL, *p;
    103 	int ret = 0;
    104 
    105 	git_revwalk_new(&w, repo);
    106 	git_revwalk_push_head(w);
    107 
    108 	if (git_revwalk_next(&id, w) ||
    109 	    git_commit_lookup(&commit, repo, &id)) {
    110 		ret = -1;
    111 		goto err;
    112 	}
    113 
    114 	author = git_commit_author(commit);
    115 
    116 	/* strip .git suffix */
    117 	if (!(stripped_name = strdup(name)))
    118 		err(1, "strdup");
    119 	if ((p = strrchr(stripped_name, '.')))
    120 		if (!strcmp(p, ".git"))
    121 			*p = '\0';
    122 
    123 	fputs("\n\t\t\t<tr class=\"item-repo\"><td><a href=\"", fp);
    124 	percentencode(fp, stripped_name, strlen(stripped_name));
    125 	fputs("/log.html\">", fp);
    126 	xmlencode(fp, stripped_name, strlen(stripped_name));
    127 	fputs("</a></td><td>", fp);
    128 	xmlencode(fp, description, strlen(description));
    129 	fputs("</td><td>", fp);
    130 	if (author)
    131 		printtimeshort(fp, &(author->when));
    132 	fputs("</td></tr>", fp);
    133 
    134 	git_commit_free(commit);
    135 err:
    136 	git_revwalk_free(w);
    137 	free(stripped_name);
    138 
    139 	return ret;
    140 }
    141 
    142 int main(int argc, char *argv[]) {
    143 	FILE *fp;
    144 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    145 	const char *repodir;
    146 	int i, ret = 0;
    147 
    148 	if (argc < 2) {
    149 		fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
    150 		return 1;
    151 	}
    152 
    153 	/* do not search outside the git repository:
    154 	   GIT_CONFIG_LEVEL_APP is the highest level currently */
    155 	git_libgit2_init();
    156 	for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
    157 		git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
    158 	/* do not require the git repository to be owned by the current user */
    159 	git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
    160 
    161 #ifdef __OpenBSD__
    162 	if (pledge("stdio rpath", NULL) == -1)
    163 		err(1, "pledge");
    164 #endif
    165 
    166 	writeheader(stdout);
    167 
    168 	for (i = 1; i < argc; i++) {
    169 		if (!strcmp(argv[i], "-c")) {
    170 			i++;
    171 			if (i == argc)
    172 				err(1, "missing argument");
    173 			repodir = argv[i];
    174 			fputs("\n\t\t\t<tr class=\"category\"><td colspan=\"3\">", stdout);
    175 			xmlencode(stdout, repodir, strlen(repodir));
    176 			fputs("</td></tr>", stdout);
    177 			continue;
    178 		}
    179 
    180 		repodir = argv[i];
    181 		if (!realpath(repodir, repodirabs))
    182 			err(1, "realpath");
    183 
    184 		if (git_repository_open_ext(&repo, repodir, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    185 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    186 			ret = 1;
    187 			continue;
    188 		}
    189 
    190 		/* use directory name as name */
    191 		if ((name = strrchr(repodirabs, '/')))
    192 			name++;
    193 		else
    194 			name = "";
    195 
    196 		/* read description or .git/description */
    197 		joinpath(path, sizeof(path), repodir, "description");
    198 		if (!(fp = fopen(path, "r"))) {
    199 			joinpath(path, sizeof(path), repodir, ".git/description");
    200 			fp = fopen(path, "r");
    201 		}
    202 		description[0] = '\0';
    203 		if (fp) {
    204 			if (!fgets(description, sizeof(description), fp))
    205 				description[0] = '\0';
    206 			checkfileerror(fp, "description", 'r');
    207 			fclose(fp);
    208 		}
    209 		writelog(stdout);
    210 	}
    211 	writefooter(stdout);
    212 
    213 	/* cleanup */
    214 	git_repository_free(repo);
    215 	git_libgit2_shutdown();
    216 
    217 	checkfileerror(stdout, "<stdout>", 'w');
    218 
    219 	return ret;
    220 }