index.c
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 #include "config.h" 12 13 // TODO extract these to a shared location (main.h?) 14 #define SPUTF(f, s) fputs((s), (f)) 15 #define put_xml(fp, s) put_xml_len((fp), (s), strlen(s)) 16 17 static char owner[255]; 18 19 FILE * git_fopen(char *path, size_t path_size, const char *name); 20 void put_xml_len(FILE *fp, const char *data, size_t len); 21 void put_percent_encoded(FILE *fp, const char *data); 22 void printtimeshort(FILE *fp, const git_time *intime); 23 void check_file_error(FILE *fp, const char *path, int mode); 24 void put_absolute_path(FILE *fp, char *name); 25 26 void 27 put_index_header(FILE *fp) 28 { 29 // TODO replace with option or config 30 const char *description = "Repositories"; 31 32 SPUTF( 33 fp, 34 "<!DOCTYPE html>" "\n" "<html>" "\n" "<head>" "\n" 35 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" "\n" 36 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />" "\n" 37 "<title>" 38 ); 39 put_xml(fp, description); 40 // TODO make links relative to base_url or ? 41 SPUTF( 42 fp, 43 "</title>" "\n" "<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\" />" "\n" 44 "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" "\n" 45 "</head>" "\n" "<body>" "\n" 46 "<table>" "\n" "<tr><td><a href=\"" 47 ); 48 fprintf(fp, INDEX_LOGO_HREF); 49 SPUTF(fp, "\"><img alt=\"logo\" src=\""); 50 fprintf(fp, INDEX_LOGO_SRC); 51 SPUTF(fp, "\" alt=\"\" width=\"32\" height=\"32\" /></img></td>" "\n"); 52 SPUTF(fp, "<td><span class=\"desc\">"); 53 put_xml(fp, description); 54 SPUTF( 55 fp, 56 "</span></td></tr><tr><td></td><td>" "\n" 57 "</td></tr>" "\n" "</table>" "\n" "<hr/>" "\n" 58 "<div id=\"content\">" "\n" "<table id=\"index\"><thead>" "\n" 59 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>" 60 "<td><b>Last commit</b></td></tr>" 61 "</thead><tbody>" "\n" 62 ); 63 } 64 65 void 66 put_index_footer(FILE *fp) 67 { 68 SPUTF( 69 fp, 70 "</tbody>" "\n" "</table>" "\n" "</div>" "\n" 71 "</body>" "\n" "</html>" "\n" 72 ); 73 } 74 75 int 76 put_index_log( 77 FILE *fp, 78 git_repository *repo, 79 const char *description, 80 const char *repo_name, 81 const char *readme 82 ) { 83 git_commit *commit = NULL; 84 const git_signature *author; 85 git_revwalk *w = NULL; 86 git_oid id; 87 int ret = 0; 88 89 git_revwalk_new(&w, repo); 90 git_revwalk_push_head(w); 91 92 if (git_revwalk_next(&id, w) || 93 git_commit_lookup(&commit, repo, &id)) { 94 ret = -1; 95 goto err; 96 } 97 98 author = git_commit_author(commit); 99 100 // TODO make this link reflect the repo page configuration 101 SPUTF(fp, "<tr><td><a href=\""); 102 put_absolute_path(fp, ""); 103 fprintf(fp, INDEX_LINK); 104 SPUTF(fp, "\">"); 105 put_xml(fp, repo_name); 106 SPUTF(fp, "</a></td><td>"); 107 put_xml(fp, description); 108 SPUTF(fp, "</td><td>"); 109 put_xml(fp, owner); 110 SPUTF(fp, "</td><td>"); 111 if (author) printtimeshort(fp, &(author->when)); 112 SPUTF(fp, "</td></tr>"); 113 114 git_commit_free(commit); 115 116 err: 117 git_revwalk_free(w); 118 119 return ret; 120 } 121 122 void 123 find_owner() 124 { 125 FILE *fp; 126 char path[PATH_MAX]; 127 fp = git_fopen(path, sizeof(path), ".git/owner"); 128 129 owner[0] = '\0'; 130 if (fp) { 131 if (!fgets(owner, sizeof(owner), fp)) 132 owner[0] = '\0'; 133 check_file_error(fp, path, 'r'); 134 fclose(fp); 135 owner[strcspn(owner, "\n")] = '\0'; 136 } 137 }