stagit-index.c (5117B)
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: fputc(*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 "<title>", fp); 70 xmlencode(fp, description, strlen(description)); 71 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath); 72 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath); 73 fputs("</head>\n<body>\n", fp); 74 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n" 75 "<td><span class=\"desc\">", relpath); 76 xmlencode(fp, description, strlen(description)); 77 fputs("</span></td></tr><tr><td></td><td>\n" 78 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n" 79 "<table id=\"index\"><thead>\n" 80 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>" 81 "<td><b>Last commit</b></td></tr>" 82 "</thead><tbody>\n", fp); 83 } 84 85 void 86 writefooter(FILE *fp) 87 { 88 fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp); 89 } 90 91 int 92 writelog(FILE *fp) 93 { 94 git_commit *commit = NULL; 95 const git_signature *author; 96 git_revwalk *w = NULL; 97 git_oid id; 98 char *stripped_name = NULL, *p; 99 int ret = 0; 100 101 git_revwalk_new(&w, repo); 102 git_revwalk_push_head(w); 103 git_revwalk_simplify_first_parent(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 149 if (argc < 2) { 150 fprintf(stderr, "%s [repodir...]\n", argv[0]); 151 return 1; 152 } 153 154 git_libgit2_init(); 155 156 #ifdef __OpenBSD__ 157 for (i = 1; i < argc; i++) 158 if (unveil(argv[i], "r") == -1) 159 err(1, "unveil: %s", argv[i]); 160 161 if (pledge("stdio rpath", NULL) == -1) 162 err(1, "pledge"); 163 #endif 164 165 writeheader(stdout); 166 167 for (i = 1; i < argc; i++) { 168 repodir = argv[i]; 169 if (!realpath(repodir, repodirabs)) 170 err(1, "realpath"); 171 172 if (git_repository_open_ext(&repo, repodir, 173 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) { 174 fprintf(stderr, "%s: cannot open repository\n", argv[0]); 175 ret = 1; 176 continue; 177 } 178 179 /* use directory name as name */ 180 if ((name = strrchr(repodirabs, '/'))) 181 name++; 182 else 183 name = ""; 184 185 /* read description or .git/description */ 186 joinpath(path, sizeof(path), repodir, "description"); 187 if (!(fp = fopen(path, "r"))) { 188 joinpath(path, sizeof(path), repodir, ".git/description"); 189 fp = fopen(path, "r"); 190 } 191 description[0] = '\0'; 192 if (fp) { 193 if (!fgets(description, sizeof(description), fp)) 194 description[0] = '\0'; 195 fclose(fp); 196 } 197 198 /* read owner or .git/owner */ 199 joinpath(path, sizeof(path), repodir, "owner"); 200 if (!(fp = fopen(path, "r"))) { 201 joinpath(path, sizeof(path), repodir, ".git/owner"); 202 fp = fopen(path, "r"); 203 } 204 owner[0] = '\0'; 205 if (fp) { 206 if (!fgets(owner, sizeof(owner), fp)) 207 owner[0] = '\0'; 208 owner[strcspn(owner, "\n")] = '\0'; 209 fclose(fp); 210 } 211 writelog(stdout); 212 } 213 writefooter(stdout); 214 215 /* cleanup */ 216 git_repository_free(repo); 217 git_libgit2_shutdown(); 218 219 return ret; 220 }