stagit-index.c (6205B) - 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 void
     21 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
     22 {
     23 	int r;
     24 
     25 	r = snprintf(buf, bufsiz, "%s%s%s",
     26 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     27 	if (r < 0 || (size_t)r >= bufsiz)
     28 		errx(1, "path truncated: '%s%s%s'",
     29 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     30 }
     31 
     32 /* Escape characters below as HTML 2.0 / XML 1.0. */
     33 void
     34 xmlencode(FILE *fp, const char *s, size_t len)
     35 {
     36 	size_t i;
     37 
     38 	for (i = 0; *s && i < len; s++, i++) {
     39 		switch(*s) {
     40 		case '<':  fputs("&lt;",   fp); break;
     41 		case '>':  fputs("&gt;",   fp); break;
     42 		case '\'': fputs("&#39;" , fp); break;
     43 		case '&':  fputs("&amp;",  fp); break;
     44 		case '"':  fputs("&quot;", fp); break;
     45 		default:   putc(*s, fp);
     46 		}
     47 	}
     48 }
     49 
     50 void
     51 printtimeshort(FILE *fp, const git_time *intime)
     52 {
     53 	struct tm *intm;
     54 	time_t t;
     55 	char out[32];
     56 
     57 	t = (time_t)intime->time;
     58 	if (!(intm = gmtime(&t)))
     59 		return;
     60 	strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
     61 	fputs(out, fp);
     62 }
     63 
     64 void
     65 writeheader(FILE *fp)
     66 {
     67 	fputs("<!DOCTYPE html>\n"
     68 		"<html>\n<head>\n"
     69 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
     70 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
     71 		"<title>", fp);
     72 	xmlencode(fp, description, strlen(description));
     73 	fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/svg+xml\" href=\"../%slogo.svg\" />\n", relpath);
     74 	fprintf(fp, "<link rel=\"alternate icon\" href=\"%sfavicon.ico\" />\n", relpath);
     75 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
     76 	fputs("</head>\n<body id=\"home\">\n<h1>", fp);
     77 	xmlencode(fp, description, strlen(description));
     78 	fputs("</h1>\n<div id=\"content\">\n"
     79 		"<h2 id=\"repositories\">Repositories</h2>\n"
     80 		"<div class=\"table-container\">\n<table id=\"index\"><thead>\n"
     81 		"<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Last commit</b></td></tr>"
     82 		"</thead><tbody>\n", fp);
     83 }
     84 
     85 void
     86 writefooter(FILE *fp)
     87 {
     88 	fputs("</tbody>\n</table>\n</div>\n"
     89 		"<h2 id=\"contribute\">Contribute</h2>\n"
     90 		"<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"
     91 		"<pre><code>git config format.subjectPrefix \"PATCH &lt;name-of-repository&gt;\"</code></pre>\n"
     92 		"<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"
     93 		"</div>\n</body>\n</html>\n", fp);
     94 }
     95 
     96 int
     97 writelog(FILE *fp)
     98 {
     99 	git_commit *commit = NULL;
    100 	const git_signature *author;
    101 	git_revwalk *w = NULL;
    102 	git_oid id;
    103 	char *stripped_name = NULL, *p;
    104 	int ret = 0;
    105 
    106 	git_revwalk_new(&w, repo);
    107 	git_revwalk_push_head(w);
    108 
    109 	if (git_revwalk_next(&id, w) ||
    110 	    git_commit_lookup(&commit, repo, &id)) {
    111 		ret = -1;
    112 		goto err;
    113 	}
    114 
    115 	author = git_commit_author(commit);
    116 
    117 	/* strip .git suffix */
    118 	if (!(stripped_name = strdup(name)))
    119 		err(1, "strdup");
    120 	if ((p = strrchr(stripped_name, '.')))
    121 		if (!strcmp(p, ".git"))
    122 			*p = '\0';
    123 
    124 	fputs("<tr class=\"repo\"><td><a href=\"", fp);
    125 	xmlencode(fp, stripped_name, strlen(stripped_name));
    126 	fputs("/\">", fp);
    127 	xmlencode(fp, stripped_name, strlen(stripped_name));
    128 	fputs("</a></td><td>", fp);
    129 	xmlencode(fp, description, strlen(description));
    130 	fputs("</td><td>", fp);
    131 	if (author)
    132 		printtimeshort(fp, &(author->when));
    133 	fputs("</td></tr>\n", fp);
    134 
    135 	git_commit_free(commit);
    136 err:
    137 	git_revwalk_free(w);
    138 	free(stripped_name);
    139 
    140 	return ret;
    141 }
    142 
    143 int
    144 main(int argc, char *argv[])
    145 {
    146 	FILE *fp;
    147 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    148 	const char *repodir;
    149 	int i, ret = 0, tmp;
    150 
    151 	if (argc < 2) {
    152 		fprintf(stderr, "%s [repodir...]\n", argv[0]);
    153 		return 1;
    154 	}
    155 
    156 	git_libgit2_init();
    157 
    158 #ifdef __OpenBSD__
    159 	if (pledge("stdio rpath", NULL) == -1)
    160 		err(1, "pledge");
    161 #endif
    162 
    163 	writeheader(stdout);
    164 
    165 	for (i = 1; i < argc; i++) {
    166 		if (!strcmp(argv[i], "-c")) {
    167 			i++;
    168 			if (i == argc)
    169 				err(1, "missing argument");
    170 			repodir = argv[i];
    171 			fputs("<tr class=\"cat\"><td>", stdout);
    172 			xmlencode(stdout, repodir, strlen(repodir));
    173 			fputs("</td><td></td><td></td></tr>\n", stdout);
    174 			continue;
    175 		}
    176 
    177 		repodir = argv[i];
    178 		if (!realpath(repodir, repodirabs))
    179 			err(1, "realpath");
    180 
    181 		if (git_repository_open_ext(&repo, repodir,
    182 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    183 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    184 			ret = 1;
    185 			continue;
    186 		}
    187 
    188 		/* use directory name as name */
    189 		if ((name = strrchr(repodirabs, '/')))
    190 			name++;
    191 		else
    192 			name = "";
    193 
    194 		/* read description or .git/description */
    195 		joinpath(path, sizeof(path), repodir, "description");
    196 		if (!(fp = fopen(path, "r"))) {
    197 			joinpath(path, sizeof(path), repodir, ".git/description");
    198 			fp = fopen(path, "r");
    199 		}
    200 		description[0] = '\0';
    201 		if (fp) {
    202 			if (!fgets(description, sizeof(description), fp))
    203 				description[0] = '\0';
    204 			tmp = strlen(description);
    205 			if (tmp > 0 && description[tmp-1] == '\n')
    206 				description[tmp-1] = '\0';
    207 			fclose(fp);
    208 		}
    209 
    210 		/* read owner or .git/owner */
    211 		joinpath(path, sizeof(path), repodir, "owner");
    212 		if (!(fp = fopen(path, "r"))) {
    213 			joinpath(path, sizeof(path), repodir, ".git/owner");
    214 			fp = fopen(path, "r");
    215 		}
    216 		owner[0] = '\0';
    217 		if (fp) {
    218 			if (!fgets(owner, sizeof(owner), fp))
    219 				owner[0] = '\0';
    220 			owner[strcspn(owner, "\n")] = '\0';
    221 			fclose(fp);
    222 		}
    223 		writelog(stdout);
    224 	}
    225 	writefooter(stdout);
    226 
    227 	/* cleanup */
    228 	git_repository_free(repo);
    229 	git_libgit2_shutdown();
    230 
    231 	return ret;
    232 }