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 (index.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, "<p id=\"content\">" "\n");
     46 }
     47 
     48 void
     49 put_index_footer(FILE *fp)
     50 {
     51 	SPUTF(
     52 		fp,
     53 		"</tbody></table>" "\n" "</p>" "\n"
     54 		"</body>" "\n" "</html>" "\n"
     55 	);
     56 }
     57 
     58 void
     59 put_index_category(FILE *fp, const char *category, int first)
     60 {
     61 	if (!first && category == NULL) return;
     62 
     63 	if (!first) SPUTF(fp, "</tbody></table><br>" "\n");
     64 
     65 	SPUTF(fp, "<table class=\"index\"><tbody>" "\n");
     66 
     67 	if (category != NULL) {
     68 		SPUTF(fp, "<tr></tr>" "<tr><td colspan=3><b>");
     69 		put_xml(fp, category);
     70 		SPUTF(fp, "</b></td></tr>" "\n");
     71 	}
     72 }
     73 
     74 int
     75 put_index_log(
     76 	FILE *fp,
     77 	RepoData *data
     78 ) {
     79 	git_commit *commit = NULL;
     80 	const git_signature *author;
     81 	git_revwalk *w = NULL;
     82 	git_oid id;
     83 	int ret = 0;
     84 
     85 	git_revwalk_new(&w, data->repo);
     86 	git_revwalk_push_head(w);
     87 
     88 	if (
     89 		git_revwalk_next(&id, w) ||
     90 		git_commit_lookup(&commit, data->repo, &id)
     91 	) {
     92 		ret = -1;
     93 		goto err;
     94 	}
     95 
     96 	author = git_commit_author(commit);
     97 
     98 	// TODO make this link reflect the repo page configuration
     99 	SPUTF(fp, "<tr><td><a href=\"");
    100 	put_absolute_path(fp, "");
    101 	fprintf(fp, INDEX_REPO_LINK);
    102 	SPUTF(fp, "\">");
    103 	put_xml(fp, data->repo_name);
    104 	SPUTF(fp, "</a></td><td>");
    105 	put_xml(fp, data->description);
    106 	SPUTF(fp, "</td><td>");
    107 	if (author) printtimeshort(fp, &(author->when));
    108 	SPUTF(fp, "</td></tr>"); 
    109 
    110 	git_commit_free(commit);
    111 
    112 err:
    113 	git_revwalk_free(w);
    114 	return ret;
    115 }

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