git.y1.nz

sgw

Static git web (fork of stagit)
download: https://git.y1.nz/archives/sgw.tar.gz
README | Files | Log | Refs | LICENSE

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 #include "repo.h"
     13 
     14 // TODO extract these to a shared location (main.h?)
     15 #define SPUTF(f, s)  fputs((s), (f))
     16 #define put_xml(fp, s)  put_xml_len((fp), (s), strlen(s))
     17 
     18 // TODO improve this... yuck.....
     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 	SPUTF(
     30 		fp,
     31 		"<!DOCTYPE html>" "\n" "<html>" "\n" "<head>" "\n"
     32 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" "\n"
     33 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />" "\n"
     34 		"<title>"
     35 	);
     36 	put_xml(fp, INDEX_TITLE); 
     37 	// TODO make links relative to base_url or ?
     38 	SPUTF(
     39 		fp,
     40 		"</title>" "\n" "<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\" />" "\n"
     41 		"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" "\n"
     42 		"</head>" "\n" "<body>" "\n"
     43 	);
     44 	fprintf(fp, INDEX_HEADER);
     45 	SPUTF(fp, "<table><tbody>" "<tr><td>" "<h1>");
     46 	put_xml(fp, INDEX_TITLE);
     47 	SPUTF(
     48 		fp,
     49 		"</h1>" "</td></tr>" "</tbody>" "</table>" "\n" "<hr/>" "\n"
     50 		"<div id=\"content\">" "\n" "<table id=\"index\"><thead>" "\n"
     51 		"<tr>" "<td><b>Name</b></td>" "<td><b>Description</b></td>"
     52 		"<td><b>Last commit</b></td></tr>"
     53 		"</thead><tbody>" "\n"
     54 	);
     55 }
     56 
     57 void
     58 put_index_footer(FILE *fp)
     59 {
     60 	SPUTF(
     61 		fp,
     62 		"</tbody>" "\n" "</table>" "\n" "</div>" "\n"
     63 		"</body>" "\n" "</html>" "\n"
     64 	);
     65 }
     66 
     67 int
     68 put_index_log(
     69 	FILE *fp,
     70 	RepoData *data
     71 ) {
     72 	git_commit *commit = NULL;
     73 	const git_signature *author;
     74 	git_revwalk *w = NULL;
     75 	git_oid id;
     76 	int ret = 0;
     77 
     78 	git_revwalk_new(&w, data->repo);
     79 	git_revwalk_push_head(w);
     80 
     81 	if (
     82 		git_revwalk_next(&id, w) ||
     83 		git_commit_lookup(&commit, data->repo, &id)
     84 	) {
     85 		ret = -1;
     86 		goto err;
     87 	}
     88 
     89 	author = git_commit_author(commit);
     90 
     91 	SPUTF(fp, "<tr><td><a href=\"");
     92 	put_absolute_path(fp, "index.html");
     93 	SPUTF(fp, "\">");
     94 	put_xml(fp, data->repo_name);
     95 	SPUTF(fp, "</a></td><td>");
     96 	put_xml(fp, data->description);
     97 	SPUTF(fp, "</td><td>");
     98 	if (author) printtimeshort(fp, &(author->when));
     99 	SPUTF(fp, "</td></tr>"); 
    100 
    101 	git_commit_free(commit);
    102 
    103 err:
    104 	git_revwalk_free(w);
    105 
    106 	return ret;
    107 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.