garbash-stagit

garbash fork of stagit for git hosting with subdirs
git clone git://git.alexkarle.com.com/garbash-stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (5382B) [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] = "Repositories";
     16 static const 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 	int rflag = 0;
    149 	char opt;
    150 
    151 	while ((opt = getopt(argc, argv, "r")) != -1) {
    152 		switch (opt) {
    153 		case 'r':
    154 			rflag = 1;
    155 			break;
    156 		default:
    157 			fprintf(stderr, "%s [-r] repodir...\n", argv[0]);
    158 			return 1;
    159 		}
    160 	}
    161 
    162 	if (optind >= argc) {
    163 		fprintf(stderr, "%s [-r] repodir...\n", argv[0]);
    164 		return 1;
    165 	}
    166 
    167 	git_libgit2_init();
    168 
    169 #ifdef __OpenBSD__
    170 	if (pledge("stdio rpath", NULL) == -1)
    171 		err(1, "pledge");
    172 #endif
    173 
    174 	writeheader(stdout);
    175 
    176 	for (i = optind; i < argc; i++) {
    177 		repodir = argv[i];
    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 		if (rflag) {
    186 			/* use full path as name */
    187 			name = repodir;
    188 		} else {
    189 			/* use directory name as name */
    190 			if (!realpath(repodir, repodirabs))
    191 				err(1, "realpath");
    192 
    193 			if ((name = strrchr(repodirabs, '/')))
    194 				name++;
    195 			else
    196 				name = "";
    197 		}
    198 
    199 		/* read description or .git/description */
    200 		joinpath(path, sizeof(path), repodir, "description");
    201 		if (!(fp = fopen(path, "r"))) {
    202 			joinpath(path, sizeof(path), repodir, ".git/description");
    203 			fp = fopen(path, "r");
    204 		}
    205 		description[0] = '\0';
    206 		if (fp) {
    207 			if (!fgets(description, sizeof(description), fp))
    208 				description[0] = '\0';
    209 			fclose(fp);
    210 		}
    211 
    212 		/* read owner or .git/owner */
    213 		joinpath(path, sizeof(path), repodir, "owner");
    214 		if (!(fp = fopen(path, "r"))) {
    215 			joinpath(path, sizeof(path), repodir, ".git/owner");
    216 			fp = fopen(path, "r");
    217 		}
    218 		owner[0] = '\0';
    219 		if (fp) {
    220 			if (!fgets(owner, sizeof(owner), fp))
    221 				owner[0] = '\0';
    222 			owner[strcspn(owner, "\n")] = '\0';
    223 			fclose(fp);
    224 		}
    225 		writelog(stdout);
    226 	}
    227 	writefooter(stdout);
    228 
    229 	/* cleanup */
    230 	git_repository_free(repo);
    231 	git_libgit2_shutdown();
    232 
    233 	return ret;
    234 }