stagit

my fork of a fork of stagit
git clone https://github.com/TanguyAndreani/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (5235B)


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