stagit-index.c (5898B) - 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] = "Samir Parikh's 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=\"%sstyle.css\" />\n", relpath); 74 fputs("</head>\n<body>\n", fp); 75 fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n" 76 "<td><span class=\"desc\">", relpath); 77 xmlencode(fp, description, strlen(description)); 78 fputs("</span></td></tr><tr><td></td><td>\n" 79 "</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n" 80 "<table id=\"index\"><thead>\n" 81 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>" 82 "<td><b>Last commit</b></td></tr>" 83 "</thead><tbody>\n", fp); 84 } 85 86 void 87 writefooter(FILE *fp) 88 { 89 fputs("</tbody>\n</table>\n</div>\n" 90 "<br>\n<br>\n<br>\n" 91 "<h3 id=\"about\">About</h3>\n" 92 "<p>Welcome to my Git repositories where I try to share some of the coding and programming projects and exercises I am working on.</p>\n" 93 "<p>Please feel free to <a href=\"mailto:siparikh@gmail.com\">email</a> me any comments, feedback and suggestions.</p>\n" 94 "<p>This portion of my site is generated by Hiltjo Posthuma’s <a href=\"https://codemadness.org/stagit.html\">stagit</a> with <a href=\"https://git.oscarbenedito.com/stagit/about.html\">updates</a> by Oscar Benedito. Updates I made can be found in this <a href=\"https://git.samirparikh.com/stagit/log.html\">repo</a>. See how I installed stagit <a href=\"https://samirparikh.com/blog/share-your-git-repositories-with-stagit-on-freebsd.html\">here</a>.</p>\n" 95 "</body>\n</html>\n", fp); 96 } 97 98 int 99 writelog(FILE *fp) 100 { 101 git_commit *commit = NULL; 102 const git_signature *author; 103 git_revwalk *w = NULL; 104 git_oid id; 105 char *stripped_name = NULL, *p; 106 int ret = 0; 107 108 git_revwalk_new(&w, repo); 109 git_revwalk_push_head(w); 110 111 if (git_revwalk_next(&id, w) || 112 git_commit_lookup(&commit, repo, &id)) { 113 ret = -1; 114 goto err; 115 } 116 117 author = git_commit_author(commit); 118 119 /* strip .git suffix */ 120 if (!(stripped_name = strdup(name))) 121 err(1, "strdup"); 122 if ((p = strrchr(stripped_name, '.'))) 123 if (!strcmp(p, ".git")) 124 *p = '\0'; 125 126 fputs("<tr><td><a href=\"", fp); 127 xmlencode(fp, stripped_name, strlen(stripped_name)); 128 fputs("/log.html\">", fp); 129 xmlencode(fp, stripped_name, strlen(stripped_name)); 130 fputs("</a></td><td>", fp); 131 xmlencode(fp, description, strlen(description)); 132 fputs("</td><td>", fp); 133 xmlencode(fp, owner, strlen(owner)); 134 fputs("</td><td>", fp); 135 if (author) 136 printtimeshort(fp, &(author->when)); 137 fputs("</td></tr>", fp); 138 139 git_commit_free(commit); 140 err: 141 git_revwalk_free(w); 142 free(stripped_name); 143 144 return ret; 145 } 146 147 int 148 main(int argc, char *argv[]) 149 { 150 FILE *fp; 151 char path[PATH_MAX], repodirabs[PATH_MAX + 1]; 152 const char *repodir; 153 int i, ret = 0; 154 155 if (argc < 2) { 156 fprintf(stderr, "%s [repodir...]\n", argv[0]); 157 return 1; 158 } 159 160 git_libgit2_init(); 161 162 #ifdef __OpenBSD__ 163 if (pledge("stdio rpath", NULL) == -1) 164 err(1, "pledge"); 165 #endif 166 167 writeheader(stdout); 168 169 for (i = 1; i < argc; i++) { 170 repodir = argv[i]; 171 if (!realpath(repodir, repodirabs)) 172 err(1, "realpath"); 173 174 if (git_repository_open_ext(&repo, repodir, 175 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) { 176 fprintf(stderr, "%s: cannot open repository\n", argv[0]); 177 ret = 1; 178 continue; 179 } 180 181 /* use directory name as name */ 182 if ((name = strrchr(repodirabs, '/'))) 183 name++; 184 else 185 name = ""; 186 187 /* read description or .git/description */ 188 joinpath(path, sizeof(path), repodir, "description"); 189 if (!(fp = fopen(path, "r"))) { 190 joinpath(path, sizeof(path), repodir, ".git/description"); 191 fp = fopen(path, "r"); 192 } 193 description[0] = '\0'; 194 if (fp) { 195 if (!fgets(description, sizeof(description), fp)) 196 description[0] = '\0'; 197 fclose(fp); 198 } 199 200 /* read owner or .git/owner */ 201 joinpath(path, sizeof(path), repodir, "owner"); 202 if (!(fp = fopen(path, "r"))) { 203 joinpath(path, sizeof(path), repodir, ".git/owner"); 204 fp = fopen(path, "r"); 205 } 206 owner[0] = '\0'; 207 if (fp) { 208 if (!fgets(owner, sizeof(owner), fp)) 209 owner[0] = '\0'; 210 owner[strcspn(owner, "\n")] = '\0'; 211 fclose(fp); 212 } 213 writelog(stdout); 214 } 215 writefooter(stdout); 216 217 /* cleanup */ 218 git_repository_free(repo); 219 git_libgit2_shutdown(); 220 221 return ret; 222 }