stagit

stagit fork for git.lleb.me
git clone git://git.lleb.me/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (5232B)


      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] = "git server";
     16 static char org[255] = "lleb labs";
     17 static char *name = "";
     18 static char owner[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:   fputc(*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 		"<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.svg\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
     76 	        "<td><h1>", relpath);
     77 	xmlencode(fp, org, strlen(org));
     78 	fputs("</h1><span class=\"desc\">", fp);
     79 	xmlencode(fp, description, strlen(description));
     80 	fputs("</span></td></tr><tr><td></td><td>\n"
     81 		"</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
     82 		"<table id=\"index\"><thead>\n"
     83 	      "<tr><td><b>Name</b></td><td><b>Description</b></td>" /* <td><b>Owner</b></td> */
     84 		"<td><b>Last commit</b></td></tr>"
     85 		"</thead><tbody>\n", fp);
     86 }
     87 
     88 void
     89 writefooter(FILE *fp)
     90 {
     91 	fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
     92 }
     93 
     94 int
     95 writelog(FILE *fp)
     96 {
     97 	git_commit *commit = NULL;
     98 	const git_signature *author;
     99 	git_revwalk *w = NULL;
    100 	git_oid id;
    101 	char *stripped_name = NULL, *p;
    102 	int ret = 0;
    103 
    104 	git_revwalk_new(&w, repo);
    105 	git_revwalk_push_head(w);
    106 	git_revwalk_simplify_first_parent(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("<tr><td><a href=\"", fp);
    124 	xmlencode(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 	/* xmlencode(fp, owner, strlen(owner));
    131 	fputs("</td><td>", fp); */
    132 	if (author)
    133 		printtimeshort(fp, &(author->when));
    134 	fputs("</td></tr>", fp);
    135 
    136 	git_commit_free(commit);
    137 err:
    138 	git_revwalk_free(w);
    139 	free(stripped_name);
    140 
    141 	return ret;
    142 }
    143 
    144 int
    145 main(int argc, char *argv[])
    146 {
    147 	FILE *fp;
    148 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    149 	const char *repodir;
    150 	int i, ret = 0;
    151 
    152 	if (argc < 2) {
    153 		fprintf(stderr, "%s [repodir...]\n", argv[0]);
    154 		return 1;
    155 	}
    156 
    157 	git_libgit2_init();
    158 
    159 #ifdef __OpenBSD__
    160 	for (i = 1; i < argc; i++)
    161 		if (unveil(argv[i], "r") == -1)
    162 			err(1, "unveil: %s", argv[i]);
    163 
    164 	if (pledge("stdio rpath", NULL) == -1)
    165 		err(1, "pledge");
    166 #endif
    167 
    168 	writeheader(stdout);
    169 
    170 	for (i = 1; i < argc; i++) {
    171 		repodir = argv[i];
    172 		if (!realpath(repodir, repodirabs))
    173 			err(1, "realpath");
    174 
    175 		if (git_repository_open_ext(&repo, repodir,
    176 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    177 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    178 			ret = 1;
    179 			continue;
    180 		}
    181 
    182 		/* use directory name as name */
    183 		if ((name = strrchr(repodirabs, '/')))
    184 			name++;
    185 		else
    186 			name = "";
    187 
    188 		/* read description or .git/description */
    189 		joinpath(path, sizeof(path), repodir, "description");
    190 		if (!(fp = fopen(path, "r"))) {
    191 			joinpath(path, sizeof(path), repodir, ".git/description");
    192 			fp = fopen(path, "r");
    193 		}
    194 		description[0] = '\0';
    195 		if (fp) {
    196 			if (!fgets(description, sizeof(description), fp))
    197 				description[0] = '\0';
    198 			fclose(fp);
    199 		}
    200 		
    201 		/* read owner or .git/owner
    202 		joinpath(path, sizeof(path), repodir, "owner");
    203 		if (!(fp = fopen(path, "r"))) {
    204 			joinpath(path, sizeof(path), repodir, ".git/owner");
    205 			fp = fopen(path, "r");
    206 		}
    207 		owner[0] = '\0';
    208 		if (fp) {
    209 			if (!fgets(owner, sizeof(owner), fp))
    210 				owner[0] = '\0';
    211 			owner[strcspn(owner, "\n")] = '\0';
    212 			fclose(fp);
    213 		}
    214 		*/
    215 		writelog(stdout);
    216 	}
    217 	writefooter(stdout);
    218 
    219 	/* cleanup */
    220 	git_repository_free(repo);
    221 	git_libgit2_shutdown();
    222 
    223 	return ret;
    224 }