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