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