stagit

Fork of stagit (git://git.codemadness.org/stagit) with personal tweaks.
git clone https://git.philomathiclife.com/repos/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (6636B)


      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 
     12 static const char *relpath = "";
     13 
     14 static char title[255] = "Repos | Philomathic Life";
     15 static char description[255] = "SSH signing key:";
     16 static char description2[255] = "sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAILd+jZn2ddw1I5yJdTF6ZpB4pBmPZ8EK6y5bKob40kRkAAAADHNzaDpwZXJzb25hbA==";
     17 static char *name = "";
     18 static char owner[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 lang=\"en\">\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, title, strlen(title));
    105 	fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/x-icon\" href=\"%sfavicon.ico\" />\n", relpath);
    106 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
    107 	fputs("</head>\n<body>\n", fp);
    108 	fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.svg\" alt=\"Philomathic Life\" width=\"73\" height=\"32\" /></td>\n"
    109 	        "<td><span class=\"desc\">", relpath);
    110 	xmlencode(fp, description, strlen(description));
    111 	fputs("</span><br><span class=\"desc\">", fp);
    112 	xmlencode(fp, description2, strlen(description2));
    113 	fputs("</span></td></tr><tr><td></td><td>\n"
    114 		"</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
    115 		"<table id=\"index\"><thead>\n"
    116 		"<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>"
    117 		"<td><b>Last commit</b></td></tr>"
    118 		"</thead><tbody>\n", fp);
    119 }
    120 
    121 void
    122 writefooter(FILE *fp)
    123 {
    124 	fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
    125 }
    126 
    127 int
    128 writelog(FILE *fp)
    129 {
    130 	git_commit *commit = NULL;
    131 	const git_signature *author;
    132 	git_revwalk *w = NULL;
    133 	git_oid id;
    134 	char *stripped_name = NULL, *p;
    135 	int ret = 0;
    136 
    137 	git_revwalk_new(&w, repo);
    138 	git_revwalk_push_head(w);
    139 
    140 	if (git_revwalk_next(&id, w) ||
    141 	    git_commit_lookup(&commit, repo, &id)) {
    142 		ret = -1;
    143 		goto err;
    144 	}
    145 
    146 	author = git_commit_author(commit);
    147 
    148 	/* strip .git suffix */
    149 	if (!(stripped_name = strdup(name)))
    150 		err(1, "strdup");
    151 	if ((p = strrchr(stripped_name, '.')))
    152 		if (!strcmp(p, ".git"))
    153 			*p = '\0';
    154 
    155 	fputs("<tr><td><a href=\"", fp);
    156 	percentencode(fp, stripped_name, strlen(stripped_name));
    157 	fputs("/log.html\">", fp);
    158 	xmlencode(fp, stripped_name, strlen(stripped_name));
    159 	fputs("</a></td><td>", fp);
    160 	xmlencode(fp, description, strlen(description));
    161 	fputs("</td><td>", fp);
    162 	xmlencode(fp, owner, strlen(owner));
    163 	fputs("</td><td>", fp);
    164 	if (author)
    165 		printtimeshort(fp, &(author->when));
    166 	fputs("</td></tr>", fp);
    167 
    168 	git_commit_free(commit);
    169 err:
    170 	git_revwalk_free(w);
    171 	free(stripped_name);
    172 
    173 	return ret;
    174 }
    175 
    176 int
    177 main(int argc, char *argv[])
    178 {
    179 	FILE *fp;
    180 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    181 	const char *repodir;
    182 	int i, ret = 0;
    183 
    184 	if (argc < 2) {
    185 		fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
    186 		return 1;
    187 	}
    188 
    189 	/* do not search outside the git repository:
    190 	   GIT_CONFIG_LEVEL_APP is the highest level currently */
    191 	git_libgit2_init();
    192 	for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++)
    193 		git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, "");
    194 	/* do not require the git repository to be owned by the current user */
    195 	git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
    196 
    197 #ifdef __OpenBSD__
    198 	if (pledge("stdio rpath", NULL) == -1)
    199 		err(1, "pledge");
    200 #endif
    201 
    202 	writeheader(stdout);
    203 
    204 	for (i = 1; i < argc; i++) {
    205 		repodir = argv[i];
    206 		if (!realpath(repodir, repodirabs))
    207 			err(1, "realpath");
    208 
    209 		if (git_repository_open_ext(&repo, repodir,
    210 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    211 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    212 			ret = 1;
    213 			continue;
    214 		}
    215 
    216 		/* use directory name as name */
    217 		if ((name = strrchr(repodirabs, '/')))
    218 			name++;
    219 		else
    220 			name = "";
    221 
    222 		/* read description or .git/description */
    223 		joinpath(path, sizeof(path), repodir, "description");
    224 		if (!(fp = fopen(path, "r"))) {
    225 			joinpath(path, sizeof(path), repodir, ".git/description");
    226 			fp = fopen(path, "r");
    227 		}
    228 		description[0] = '\0';
    229 		if (fp) {
    230 			if (!fgets(description, sizeof(description), fp))
    231 				description[0] = '\0';
    232 			checkfileerror(fp, "description", 'r');
    233 			fclose(fp);
    234 		}
    235 
    236 		/* read owner or .git/owner */
    237 		joinpath(path, sizeof(path), repodir, "owner");
    238 		if (!(fp = fopen(path, "r"))) {
    239 			joinpath(path, sizeof(path), repodir, ".git/owner");
    240 			fp = fopen(path, "r");
    241 		}
    242 		owner[0] = '\0';
    243 		if (fp) {
    244 			if (!fgets(owner, sizeof(owner), fp))
    245 				owner[0] = '\0';
    246 			checkfileerror(fp, "owner", 'r');
    247 			fclose(fp);
    248 			owner[strcspn(owner, "\n")] = '\0';
    249 		}
    250 		writelog(stdout);
    251 	}
    252 	writefooter(stdout);
    253 
    254 	/* cleanup */
    255 	git_repository_free(repo);
    256 	git_libgit2_shutdown();
    257 
    258 	checkfileerror(stdout, "<stdout>", 'w');
    259 
    260 	return ret;
    261 }