stagit

stagit (https://www.codemadness.org/stagit.html) fork
git clone git://bsandro.tech/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (5061B)


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