stagit-index.c (6893B) - 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] = "hwc's git repositories"; 16 static char *name = ""; 17 static char owner[255]; 18 static char category[255]; 19 20 /* Handle read or write errors for a FILE * stream */ 21 void 22 checkfileerror(FILE *fp, const char *name, int mode) 23 { 24 if (mode == 'r' && ferror(fp)) 25 errx(1, "read error: %s", name); 26 else if (mode == 'w' && (fflush(fp) || ferror(fp))) 27 errx(1, "write error: %s", name); 28 } 29 30 void 31 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2) 32 { 33 int r; 34 35 r = snprintf(buf, bufsiz, "%s%s%s", 36 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2); 37 if (r < 0 || (size_t)r >= bufsiz) 38 errx(1, "path truncated: '%s%s%s'", 39 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2); 40 } 41 42 /* Percent-encode, see RFC3986 section 2.1. */ 43 void 44 percentencode(FILE *fp, const char *s, size_t len) 45 { 46 static char tab[] = "0123456789ABCDEF"; 47 unsigned char uc; 48 size_t i; 49 50 for (i = 0; *s && i < len; s++, i++) { 51 uc = *s; 52 /* NOTE: do not encode '/' for paths or ",-." */ 53 if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') || 54 uc == '[' || uc == ']') { 55 putc('%', fp); 56 putc(tab[(uc >> 4) & 0x0f], fp); 57 putc(tab[uc & 0x0f], fp); 58 } else { 59 putc(uc, fp); 60 } 61 } 62 } 63 64 /* Escape characters below as HTML 2.0 / XML 1.0. */ 65 void 66 xmlencode(FILE *fp, const char *s, size_t len) 67 { 68 size_t i; 69 70 for (i = 0; *s && i < len; s++, i++) { 71 switch(*s) { 72 case '<': fputs("<", fp); break; 73 case '>': fputs(">", fp); break; 74 case '\'': fputs("'" , fp); break; 75 case '&': fputs("&", fp); break; 76 case '"': fputs(""", fp); break; 77 default: putc(*s, fp); 78 } 79 } 80 } 81 82 void 83 printtimeshort(FILE *fp, const git_time *intime) 84 { 85 struct tm *intm; 86 time_t t; 87 char out[32]; 88 89 t = (time_t)intime->time; 90 if (!(intm = gmtime(&t))) 91 return; 92 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm); 93 fputs(out, fp); 94 } 95 96 void 97 writeheader(FILE *fp) 98 { 99 fputs("<!DOCTYPE html>\n" 100 "<html>\n<head>\n" 101 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" 102 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" 103 "<title>", fp); 104 xmlencode(fp, description, strlen(description)); 105 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"../%slogo.png\" />\n", relpath); 106 fprintf(fp, "<link rel=\"alternate icon\" href=\"../%sfavicon.png\" />\n", relpath); 107 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath); 108 fputs("</head>\n<body id=\"home\">\n<h1>", fp); 109 xmlencode(fp, description, strlen(description)); 110 fputs("</h1>\n<div id=\"content\">\n" 111 "<h2 id=\"repositories\">Repositories</h2>\n" 112 "<div class=\"table-container\">\n<table id=\"index\"><thead>\n" 113 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Last commit</b></td></tr>" 114 "</thead><tbody>\n", fp); 115 } 116 117 void 118 writefooter(FILE *fp) 119 { 120 fputs("</tbody>\n</table>\n</div>\n" 121 "<h2 id=\"contribute\">Contribute</h2>\n" 122 "<p>Open source repos are currently hosted on <a href=\"https://github.com/hwchen\">Github</a>. Projects hosted at <a href=\"https://git.hwc.io\">git.hwc.io</a> are not intended for collaboration, although you may try to send a patch.</p>\n" 123 "</div>\n</body>\n</html>\n", fp); 124 } 125 126 int 127 writelog(FILE *fp) 128 { 129 git_commit *commit = NULL; 130 const git_signature *author; 131 git_revwalk *w = NULL; 132 git_oid id; 133 char *stripped_name = NULL, *p; 134 int ret = 0; 135 136 git_revwalk_new(&w, repo); 137 git_revwalk_push_head(w); 138 139 if (git_revwalk_next(&id, w) || 140 git_commit_lookup(&commit, repo, &id)) { 141 ret = -1; 142 goto err; 143 } 144 145 author = git_commit_author(commit); 146 147 /* strip .git suffix */ 148 if (!(stripped_name = strdup(name))) 149 err(1, "strdup"); 150 if ((p = strrchr(stripped_name, '.'))) 151 if (!strcmp(p, ".git")) 152 *p = '\0'; 153 154 fputs("<tr class=\"repo\"><td><a href=\"", fp); 155 percentencode(fp, stripped_name, strlen(stripped_name)); 156 fputs("/\">", fp); 157 xmlencode(fp, stripped_name, strlen(stripped_name)); 158 fputs("</a></td><td>", fp); 159 xmlencode(fp, description, strlen(description)); 160 fputs("</td><td>", fp); 161 if (author) 162 printtimeshort(fp, &(author->when)); 163 fputs("</td></tr>\n", fp); 164 165 git_commit_free(commit); 166 err: 167 git_revwalk_free(w); 168 free(stripped_name); 169 170 return ret; 171 } 172 173 int 174 main(int argc, char *argv[]) 175 { 176 FILE *fp; 177 char path[PATH_MAX], repodirabs[PATH_MAX + 1]; 178 const char *repodir; 179 int i, ret = 0, tmp; 180 181 if (argc < 2) { 182 fprintf(stderr, "%s [repodir...]\n", argv[0]); 183 return 1; 184 } 185 186 /* do not search outside the git repository: 187 GIT_CONFIG_LEVEL_APP is the highest level currently */ 188 git_libgit2_init(); 189 for (i = 1; i <= GIT_CONFIG_LEVEL_APP; i++) 190 git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, i, ""); 191 /* do not require the git repository to be owned by the current user */ 192 git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0); 193 194 #ifdef __OpenBSD__ 195 if (pledge("stdio rpath", NULL) == -1) 196 err(1, "pledge"); 197 #endif 198 199 writeheader(stdout); 200 201 for (i = 1; i < argc; i++) { 202 if (!strcmp(argv[i], "-c")) { 203 i++; 204 if (i == argc) 205 err(1, "missing argument"); 206 repodir = argv[i]; 207 fputs("<tr class=\"cat\"><td>", stdout); 208 xmlencode(stdout, repodir, strlen(repodir)); 209 fputs("</td><td></td><td></td></tr>\n", stdout); 210 continue; 211 } 212 213 repodir = argv[i]; 214 if (!realpath(repodir, repodirabs)) 215 err(1, "realpath"); 216 217 if (git_repository_open_ext(&repo, repodir, 218 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) { 219 fprintf(stderr, "%s: cannot open repository\n", argv[0]); 220 ret = 1; 221 continue; 222 } 223 224 /* use directory name as name */ 225 if ((name = strrchr(repodirabs, '/'))) 226 name++; 227 else 228 name = ""; 229 230 /* read description or .git/description */ 231 joinpath(path, sizeof(path), repodir, "description"); 232 if (!(fp = fopen(path, "r"))) { 233 joinpath(path, sizeof(path), repodir, ".git/description"); 234 fp = fopen(path, "r"); 235 } 236 description[0] = '\0'; 237 if (fp) { 238 if (!fgets(description, sizeof(description), fp)) 239 description[0] = '\0'; 240 tmp = strlen(description); 241 if (tmp > 0 && description[tmp-1] == '\n') 242 description[tmp-1] = '\0'; 243 checkfileerror(fp, "description", 'r'); 244 fclose(fp); 245 } 246 247 /* read owner or .git/owner */ 248 joinpath(path, sizeof(path), repodir, "owner"); 249 if (!(fp = fopen(path, "r"))) { 250 joinpath(path, sizeof(path), repodir, ".git/owner"); 251 fp = fopen(path, "r"); 252 } 253 owner[0] = '\0'; 254 if (fp) { 255 if (!fgets(owner, sizeof(owner), fp)) 256 owner[0] = '\0'; 257 checkfileerror(fp, "owner", 'r'); 258 fclose(fp); 259 owner[strcspn(owner, "\n")] = '\0'; 260 } 261 writelog(stdout); 262 } 263 writefooter(stdout); 264 265 /* cleanup */ 266 git_repository_free(repo); 267 git_libgit2_shutdown(); 268 269 checkfileerror(stdout, "<stdout>", 'w'); 270 271 return ret; 272 }