stagit-index.c (5363B)
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 /* 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("<", fp); break; 40 case '>': fputs(">", fp); break; 41 case '\'': fputs("'" , fp); break; 42 case '&': fputs("&", fp); break; 43 case '"': fputs(""", 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=\"%scolors.css\" />\n", relpath); 74 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath); 75 fputs("</head>\n<body>\n<input type=\"checkbox\" id=\"theme-switch\" class=\"theme-switch\">\n<div id=\"page\">\n<label for=\"theme-switch\" class=\"switch-label\"></label>\n", fp); 76 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n" 77 "<td><span class=\"desc\">", relpath); 78 xmlencode(fp, description, strlen(description)); 79 fputs("</span></td></tr><tr><td></td><td>\n" 80 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n" 81 "<table id=\"index\"><thead>\n" 82 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>" 83 "<td><b>Last commit</b></td></tr>" 84 "</thead><tbody>\n", fp); 85 } 86 87 void 88 writefooter(FILE *fp) 89 { 90 fprintf(fp, "</tbody>\n</table>\n</div>\n</div>\n<script src=\"%smain.js\"></script>\n</body>\n</html>\n", relpath); 91 } 92 93 int 94 writelog(FILE *fp) 95 { 96 git_commit *commit = NULL; 97 const git_signature *author; 98 git_revwalk *w = NULL; 99 git_oid id; 100 char *stripped_name = NULL, *p; 101 int ret = 0; 102 103 git_revwalk_new(&w, repo); 104 git_revwalk_push_head(w); 105 106 if (git_revwalk_next(&id, w) || 107 git_commit_lookup(&commit, repo, &id)) { 108 ret = -1; 109 goto err; 110 } 111 112 author = git_commit_author(commit); 113 114 /* strip .git suffix */ 115 if (!(stripped_name = strdup(name))) 116 err(1, "strdup"); 117 if ((p = strrchr(stripped_name, '.'))) 118 if (!strcmp(p, ".git")) 119 *p = '\0'; 120 121 fputs("<tr><td><a href=\"", fp); 122 xmlencode(fp, stripped_name, strlen(stripped_name)); 123 fputs("/log.html\">", fp); 124 xmlencode(fp, stripped_name, strlen(stripped_name)); 125 fputs("</a></td><td>", fp); 126 xmlencode(fp, description, strlen(description)); 127 fputs("</td><td>", fp); 128 xmlencode(fp, owner, strlen(owner)); 129 fputs("</td><td>", fp); 130 if (author) 131 printtimeshort(fp, &(author->when)); 132 fputs("</td></tr>", fp); 133 134 git_commit_free(commit); 135 err: 136 git_revwalk_free(w); 137 free(stripped_name); 138 139 return ret; 140 } 141 142 int 143 main(int argc, char *argv[]) 144 { 145 FILE *fp; 146 char path[PATH_MAX], repodirabs[PATH_MAX + 1]; 147 const char *repodir; 148 int i, ret = 0; 149 150 if (argc < 2) { 151 fprintf(stderr, "%s [repodir...]\n", argv[0]); 152 return 1; 153 } 154 155 git_libgit2_init(); 156 157 #ifdef __OpenBSD__ 158 if (pledge("stdio rpath", NULL) == -1) 159 err(1, "pledge"); 160 #endif 161 162 writeheader(stdout); 163 164 for (i = 1; i < argc; i++) { 165 repodir = argv[i]; 166 if (!realpath(repodir, repodirabs)) 167 err(1, "realpath"); 168 169 if (git_repository_open_ext(&repo, repodir, 170 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) { 171 fprintf(stderr, "%s: cannot open repository\n", argv[0]); 172 ret = 1; 173 continue; 174 } 175 176 /* use directory name as name */ 177 if ((name = strrchr(repodirabs, '/'))) 178 name++; 179 else 180 name = ""; 181 182 /* read description or .git/description */ 183 joinpath(path, sizeof(path), repodir, "description"); 184 if (!(fp = fopen(path, "r"))) { 185 joinpath(path, sizeof(path), repodir, ".git/description"); 186 fp = fopen(path, "r"); 187 } 188 description[0] = '\0'; 189 if (fp) { 190 if (!fgets(description, sizeof(description), fp)) 191 description[0] = '\0'; 192 fclose(fp); 193 } 194 195 /* read owner or .git/owner */ 196 joinpath(path, sizeof(path), repodir, "owner"); 197 if (!(fp = fopen(path, "r"))) { 198 joinpath(path, sizeof(path), repodir, ".git/owner"); 199 fp = fopen(path, "r"); 200 } 201 owner[0] = '\0'; 202 if (fp) { 203 if (!fgets(owner, sizeof(owner), fp)) 204 owner[0] = '\0'; 205 owner[strcspn(owner, "\n")] = '\0'; 206 fclose(fp); 207 } 208 writelog(stdout); 209 } 210 writefooter(stdout); 211 212 /* cleanup */ 213 git_repository_free(repo); 214 git_libgit2_shutdown(); 215 216 return ret; 217 }