summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile4
-rw-r--r--cgit.c5
-rw-r--r--html.c2
-rw-r--r--ui-shared.c2
4 files changed, 7 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index a988751..14b4df4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,244 +1,244 @@
-CGIT_VERSION = v0.8.3.4
+CGIT_VERSION = v0.8.3.5
CGIT_SCRIPT_NAME = cgit.cgi
CGIT_SCRIPT_PATH = /var/www/htdocs/cgit
CGIT_DATA_PATH = $(CGIT_SCRIPT_PATH)
CGIT_CONFIG = /etc/cgitrc
CACHE_ROOT = /var/cache/cgit
prefix = /usr
libdir = $(prefix)/lib
filterdir = $(libdir)/cgit/filters
docdir = $(prefix)/share/doc/cgit
htmldir = $(docdir)
pdfdir = $(docdir)
mandir = $(prefix)/share/man
SHA1_HEADER = <openssl/sha.h>
GIT_VER = 1.7.4
GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2
INSTALL = install
MAN5_TXT = $(wildcard *.5.txt)
MAN_TXT = $(MAN5_TXT)
DOC_MAN5 = $(patsubst %.txt,%,$(MAN5_TXT))
DOC_HTML = $(patsubst %.txt,%.html,$(MAN_TXT))
DOC_PDF = $(patsubst %.txt,%.pdf,$(MAN_TXT))
# Define NO_STRCASESTR if you don't have strcasestr.
#
# Define NO_OPENSSL to disable linking with OpenSSL and use bundled SHA1
# implementation (slower).
#
# Define NEEDS_LIBICONV if linking with libc is not enough (eg. Darwin).
#
# Define NO_C99_FORMAT if your formatted IO functions (printf/scanf et.al.)
# do not support the 'size specifiers' introduced by C99, namely ll, hh,
# j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t).
# some C compilers supported these specifiers prior to C99 as an extension.
#
#-include config.mak
#
# Platform specific tweaks
#
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
uname_O := $(shell sh -c 'uname -o 2>/dev/null || echo not')
uname_R := $(shell sh -c 'uname -r 2>/dev/null || echo not')
ifeq ($(uname_O),Cygwin)
NO_STRCASESTR = YesPlease
NEEDS_LIBICONV = YesPlease
endif
#
# Let the user override the above settings.
#
-include cgit.conf
#
# Define a way to invoke make in subdirs quietly, shamelessly ripped
# from git.git
#
QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
QUIET_SUBDIR1 =
ifneq ($(findstring $(MAKEFLAGS),w),w)
PRINT_DIR = --no-print-directory
else # "make -w"
NO_SUBDIR = :
endif
ifndef V
QUIET_CC = @echo ' ' CC $@;
QUIET_MM = @echo ' ' MM $@;
QUIET_SUBDIR0 = +@subdir=
QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \
$(MAKE) $(PRINT_DIR) -C $$subdir
endif
#
# Define a pattern rule for automatic dependency building
#
%.d: %.c
$(QUIET_MM)$(CC) $(CFLAGS) -MM -MP $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@
#
# Define a pattern rule for silent object building
#
%.o: %.c
$(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $<
EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lpthread
OBJECTS =
OBJECTS += cache.o
OBJECTS += cgit.o
OBJECTS += cmd.o
OBJECTS += configfile.o
OBJECTS += html.o
OBJECTS += parsing.o
OBJECTS += scan-tree.o
OBJECTS += shared.o
OBJECTS += ui-atom.o
OBJECTS += ui-blob.o
OBJECTS += ui-clone.o
OBJECTS += ui-commit.o
OBJECTS += ui-diff.o
OBJECTS += ui-log.o
OBJECTS += ui-patch.o
OBJECTS += ui-plain.o
OBJECTS += ui-refs.o
OBJECTS += ui-repolist.o
OBJECTS += ui-shared.o
OBJECTS += ui-snapshot.o
OBJECTS += ui-ssdiff.o
OBJECTS += ui-stats.o
OBJECTS += ui-summary.o
OBJECTS += ui-tag.o
OBJECTS += ui-tree.o
OBJECTS += vector.o
ifdef NEEDS_LIBICONV
EXTLIBS += -liconv
endif
.PHONY: all libgit test install uninstall clean force-version get-git \
doc clean-doc install-doc install-man install-html install-pdf \
uninstall-doc uninstall-man uninstall-html uninstall-pdf
all: cgit
VERSION: force-version
@./gen-version.sh "$(CGIT_VERSION)"
-include VERSION
CFLAGS += -g -Wall -Igit
CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)'
CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"'
CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
GIT_OPTIONS = prefix=/usr
ifdef NO_ICONV
CFLAGS += -DNO_ICONV
endif
ifdef NO_STRCASESTR
CFLAGS += -DNO_STRCASESTR
endif
ifdef NO_C99_FORMAT
CFLAGS += -DNO_C99_FORMAT
endif
ifdef NO_OPENSSL
CFLAGS += -DNO_OPENSSL
GIT_OPTIONS += NO_OPENSSL=1
else
EXTLIBS += -lcrypto
endif
cgit: $(OBJECTS) libgit
$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
cgit.o: VERSION
ifneq "$(MAKECMDGOALS)" "clean"
-include $(OBJECTS:.o=.d)
endif
libgit:
$(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) NO_CURL=1 $(GIT_OPTIONS) libgit.a
$(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) NO_CURL=1 $(GIT_OPTIONS) xdiff/lib.a
test: all
$(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all
install: all
$(INSTALL) -m 0755 -d $(DESTDIR)$(CGIT_SCRIPT_PATH)
$(INSTALL) -m 0755 cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME)
$(INSTALL) -m 0755 -d $(DESTDIR)$(CGIT_DATA_PATH)
$(INSTALL) -m 0644 cgit.css $(DESTDIR)$(CGIT_DATA_PATH)/cgit.css
$(INSTALL) -m 0644 cgit.png $(DESTDIR)$(CGIT_DATA_PATH)/cgit.png
$(INSTALL) -m 0755 -d $(DESTDIR)$(filterdir)
$(INSTALL) -m 0755 filters/* $(DESTDIR)$(filterdir)
install-doc: install-man install-html install-pdf
install-man: doc-man
$(INSTALL) -m 0755 -d $(DESTDIR)$(mandir)/man5
$(INSTALL) -m 0644 $(DOC_MAN5) $(DESTDIR)$(mandir)/man5
install-html: doc-html
$(INSTALL) -m 0755 -d $(DESTDIR)$(htmldir)
$(INSTALL) -m 0644 $(DOC_HTML) $(DESTDIR)$(htmldir)
install-pdf: doc-pdf
$(INSTALL) -m 0755 -d $(DESTDIR)$(pdfdir)
$(INSTALL) -m 0644 $(DOC_PDF) $(DESTDIR)$(pdfdir)
uninstall:
rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME)
rm -f $(CGIT_DATA_PATH)/cgit.css
rm -f $(CGIT_DATA_PATH)/cgit.png
uninstall-doc: uninstall-man uninstall-html uninstall-pdf
uninstall-man:
@for i in $(DOC_MAN5); do \
rm -fv $(DESTDIR)$(mandir)/man5/$$i; \
done
uninstall-html:
@for i in $(DOC_HTML); do \
rm -fv $(DESTDIR)$(htmldir)/$$i; \
done
uninstall-pdf:
@for i in $(DOC_PDF); do \
rm -fv $(DESTDIR)$(pdfdir)/$$i; \
done
doc: doc-man doc-html doc-pdf
doc-man: doc-man5
doc-man5: $(DOC_MAN5)
doc-html: $(DOC_HTML)
doc-pdf: $(DOC_PDF)
%.5 : %.5.txt
a2x -f manpage $<
$(DOC_HTML): %.html : %.txt
a2x -f xhtml --stylesheet=cgit-doc.css $<
$(DOC_PDF): %.pdf : %.txt
a2x -f pdf cgitrc.5.txt
clean: clean-doc
rm -f cgit VERSION *.o *.d
clean-doc:
rm -f cgitrc.5 cgitrc.5.html cgitrc.5.pdf cgitrc.5.xml cgitrc.5.fo
get-git:
- curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git
+ curl $(GIT_URL) | tar -xjf - && rm -rf git && mv git-$(GIT_VER) git
diff --git a/cgit.c b/cgit.c
index 916feb4..f4dd6ef 100644
--- a/cgit.c
+++ b/cgit.c
@@ -563,229 +563,230 @@ void print_repo(FILE *f, struct cgit_repo *repo)
fprintf(f, "repo.commit-filter=%s\n", repo->commit_filter->cmd);
if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter)
fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd);
if (repo->snapshots != ctx.cfg.snapshots) {
char *tmp = build_snapshot_setting(repo->snapshots);
fprintf(f, "repo.snapshots=%s\n", tmp);
free(tmp);
}
if (repo->max_stats != ctx.cfg.max_stats)
fprintf(f, "repo.max-stats=%s\n",
cgit_find_stats_periodname(repo->max_stats));
fprintf(f, "\n");
}
void print_repolist(FILE *f, struct cgit_repolist *list, int start)
{
int i;
for(i = start; i < list->count; i++)
print_repo(f, &list->repos[i]);
}
/* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc'
* and return 0 on success.
*/
static int generate_cached_repolist(const char *path, const char *cached_rc)
{
char *locked_rc;
int idx;
FILE *f;
locked_rc = xstrdup(fmt("%s.lock", cached_rc));
f = fopen(locked_rc, "wx");
if (!f) {
/* Inform about the error unless the lockfile already existed,
* since that only means we've got concurrent requests.
*/
if (errno != EEXIST)
fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
locked_rc, strerror(errno), errno);
return errno;
}
idx = cgit_repolist.count;
if (ctx.cfg.project_list)
scan_projects(path, ctx.cfg.project_list, repo_config);
else
scan_tree(path, repo_config);
print_repolist(f, &cgit_repolist, idx);
if (rename(locked_rc, cached_rc))
fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
locked_rc, cached_rc, strerror(errno), errno);
fclose(f);
return 0;
}
static void process_cached_repolist(const char *path)
{
struct stat st;
char *cached_rc;
time_t age;
unsigned long hash;
hash = hash_str(path);
if (ctx.cfg.project_list)
hash += hash_str(ctx.cfg.project_list);
cached_rc = xstrdup(fmt("%s/rc-%8lx", ctx.cfg.cache_root, hash));
if (stat(cached_rc, &st)) {
/* Nothing is cached, we need to scan without forking. And
* if we fail to generate a cached repolist, we need to
* invoke scan_tree manually.
*/
if (generate_cached_repolist(path, cached_rc)) {
if (ctx.cfg.project_list)
scan_projects(path, ctx.cfg.project_list,
repo_config);
else
scan_tree(path, repo_config);
}
return;
}
parse_configfile(cached_rc, config_cb);
/* If the cached configfile hasn't expired, lets exit now */
age = time(NULL) - st.st_mtime;
if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
return;
/* The cached repolist has been parsed, but it was old. So lets
* rescan the specified path and generate a new cached repolist
* in a child-process to avoid latency for the current request.
*/
if (fork())
return;
exit(generate_cached_repolist(path, cached_rc));
}
static void cgit_parse_args(int argc, const char **argv)
{
int i;
int scan = 0;
for (i = 1; i < argc; i++) {
if (!strncmp(argv[i], "--cache=", 8)) {
ctx.cfg.cache_root = xstrdup(argv[i]+8);
}
if (!strcmp(argv[i], "--nocache")) {
ctx.cfg.nocache = 1;
}
if (!strcmp(argv[i], "--nohttp")) {
ctx.env.no_http = "1";
}
if (!strncmp(argv[i], "--query=", 8)) {
ctx.qry.raw = xstrdup(argv[i]+8);
}
if (!strncmp(argv[i], "--repo=", 7)) {
ctx.qry.repo = xstrdup(argv[i]+7);
}
if (!strncmp(argv[i], "--page=", 7)) {
ctx.qry.page = xstrdup(argv[i]+7);
}
if (!strncmp(argv[i], "--head=", 7)) {
ctx.qry.head = xstrdup(argv[i]+7);
ctx.qry.has_symref = 1;
}
if (!strncmp(argv[i], "--sha1=", 7)) {
ctx.qry.sha1 = xstrdup(argv[i]+7);
ctx.qry.has_sha1 = 1;
}
if (!strncmp(argv[i], "--ofs=", 6)) {
ctx.qry.ofs = atoi(argv[i]+6);
}
if (!strncmp(argv[i], "--scan-tree=", 12) ||
!strncmp(argv[i], "--scan-path=", 12)) {
/* HACK: the global snapshot bitmask defines the
* set of allowed snapshot formats, but the config
* file hasn't been parsed yet so the mask is
* currently 0. By setting all bits high before
* scanning we make sure that any in-repo cgitrc
* snapshot setting is respected by scan_tree().
* BTW: we assume that there'll never be more than
* 255 different snapshot formats supported by cgit...
*/
ctx.cfg.snapshots = 0xFF;
scan++;
scan_tree(argv[i] + 12, repo_config);
}
}
if (scan) {
qsort(cgit_repolist.repos, cgit_repolist.count,
sizeof(struct cgit_repo), cmp_repos);
print_repolist(stdout, &cgit_repolist, 0);
exit(0);
}
}
static int calc_ttl()
{
if (!ctx.repo)
return ctx.cfg.cache_root_ttl;
if (!ctx.qry.page)
return ctx.cfg.cache_repo_ttl;
if (ctx.qry.has_symref)
return ctx.cfg.cache_dynamic_ttl;
if (ctx.qry.has_sha1)
return ctx.cfg.cache_static_ttl;
return ctx.cfg.cache_repo_ttl;
}
int main(int argc, const char **argv)
{
const char *path;
char *qry;
int err, ttl;
prepare_context(&ctx);
cgit_repolist.length = 0;
cgit_repolist.count = 0;
cgit_repolist.repos = NULL;
cgit_parse_args(argc, argv);
parse_configfile(expand_macros(ctx.env.cgit_config), config_cb);
ctx.repo = NULL;
http_parse_querystring(ctx.qry.raw, querystring_cb);
/* If virtual-root isn't specified in cgitrc, lets pretend
- * that virtual-root equals SCRIPT_NAME.
+ * that virtual-root equals SCRIPT_NAME, minus any possibly
+ * trailing slashes.
*/
if (!ctx.cfg.virtual_root)
- ctx.cfg.virtual_root = ctx.cfg.script_name;
+ ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/');
/* If no url parameter is specified on the querystring, lets
* use PATH_INFO as url. This allows cgit to work with virtual
* urls without the need for rewriterules in the webserver (as
* long as PATH_INFO is included in the cache lookup key).
*/
path = ctx.env.path_info;
if (!ctx.qry.url && path) {
if (path[0] == '/')
path++;
ctx.qry.url = xstrdup(path);
if (ctx.qry.raw) {
qry = ctx.qry.raw;
ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
free(qry);
} else
ctx.qry.raw = xstrdup(ctx.qry.url);
cgit_parse_url(ctx.qry.url);
}
ttl = calc_ttl();
ctx.page.expires += ttl*60;
if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
ctx.cfg.nocache = 1;
if (ctx.cfg.nocache)
ctx.cfg.cache_size = 0;
err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
ctx.qry.raw, ttl, process_request, &ctx);
if (err)
cgit_print_error(fmt("Error processing page: %s (%d)",
strerror(err), err));
return err;
}
diff --git a/html.c b/html.c
index a1fe87d..a60bc13 100644
--- a/html.c
+++ b/html.c
@@ -88,233 +88,233 @@ void html_status(int code, const char *msg, int more_headers)
if (!more_headers)
html("\n");
}
void html_txt(const char *txt)
{
const char *t = txt;
while(t && *t){
int c = *t;
if (c=='<' || c=='>' || c=='&') {
html_raw(txt, t - txt);
if (c=='>')
html("&gt;");
else if (c=='<')
html("&lt;");
else if (c=='&')
html("&amp;");
txt = t+1;
}
t++;
}
if (t!=txt)
html(txt);
}
void html_ntxt(int len, const char *txt)
{
const char *t = txt;
while(t && *t && len--){
int c = *t;
if (c=='<' || c=='>' || c=='&') {
html_raw(txt, t - txt);
if (c=='>')
html("&gt;");
else if (c=='<')
html("&lt;");
else if (c=='&')
html("&amp;");
txt = t+1;
}
t++;
}
if (t!=txt)
html_raw(txt, t - txt);
if (len<0)
html("...");
}
void html_attr(const char *txt)
{
const char *t = txt;
while(t && *t){
int c = *t;
if (c=='<' || c=='>' || c=='\'' || c=='\"') {
html_raw(txt, t - txt);
if (c=='>')
html("&gt;");
else if (c=='<')
html("&lt;");
else if (c=='\'')
html("&#x27;");
else if (c=='"')
html("&quot;");
txt = t+1;
}
t++;
}
if (t!=txt)
html(txt);
}
void html_url_path(const char *txt)
{
const char *t = txt;
while(t && *t){
int c = *t;
const char *e = url_escape_table[c];
if (e && c!='+' && c!='&' && c!='+') {
html_raw(txt, t - txt);
html_raw(e, 3);
txt = t+1;
}
t++;
}
if (t!=txt)
html(txt);
}
void html_url_arg(const char *txt)
{
const char *t = txt;
while(t && *t){
int c = *t;
const char *e = url_escape_table[c];
if (e) {
html_raw(txt, t - txt);
html_raw(e, strlen(e));
txt = t+1;
}
t++;
}
if (t!=txt)
html(txt);
}
void html_hidden(const char *name, const char *value)
{
html("<input type='hidden' name='");
html_attr(name);
html("' value='");
html_attr(value);
html("'/>");
}
void html_option(const char *value, const char *text, const char *selected_value)
{
html("<option value='");
html_attr(value);
html("'");
if (selected_value && !strcmp(selected_value, value))
html(" selected='selected'");
html(">");
html_txt(text);
html("</option>\n");
}
void html_link_open(const char *url, const char *title, const char *class)
{
html("<a href='");
html_attr(url);
if (title) {
html("' title='");
html_attr(title);
}
if (class) {
html("' class='");
html_attr(class);
}
html("'>");
}
void html_link_close(void)
{
html("</a>");
}
void html_fileperm(unsigned short mode)
{
htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
(mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
}
int html_include(const char *filename)
{
FILE *f;
char buf[4096];
size_t len;
if (!(f = fopen(filename, "r"))) {
fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
filename, strerror(errno), errno);
return -1;
}
while((len = fread(buf, 1, 4096, f)) > 0)
html_raw(buf, len);
fclose(f);
return 0;
}
int hextoint(char c)
{
if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
else if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
else if (c >= '0' && c <= '9')
return c - '0';
else
return -1;
}
char *convert_query_hexchar(char *txt)
{
int d1, d2, n;
n = strlen(txt);
if (n < 3) {
*txt = '\0';
return txt-1;
}
d1 = hextoint(*(txt+1));
d2 = hextoint(*(txt+2));
if (d1<0 || d2<0) {
- memmove(txt, txt+3, n-3);
+ memmove(txt, txt+3, n-2);
return txt-1;
} else {
*txt = d1 * 16 + d2;
memmove(txt+1, txt+3, n-2);
return txt;
}
}
int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
{
char *t, *txt, *value = NULL, c;
if (!txt_)
return 0;
t = txt = strdup(txt_);
if (t == NULL) {
printf("Out of memory\n");
exit(1);
}
while((c=*t) != '\0') {
if (c=='=') {
*t = '\0';
value = t+1;
} else if (c=='+') {
*t = ' ';
} else if (c=='%') {
t = convert_query_hexchar(t);
} else if (c=='&') {
*t = '\0';
(*fn)(txt, value);
txt = t+1;
value = NULL;
}
t++;
}
if (t!=txt)
(*fn)(txt, value);
return 0;
}
diff --git a/ui-shared.c b/ui-shared.c
index 7efae7a..5aa9119 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -385,385 +385,385 @@ void cgit_diff_link(const char *name, const char *title, const char *class,
html_url_arg(new_rev);
delim = "&amp;";
}
if (old_rev) {
html(delim);
html("id2=");
html_url_arg(old_rev);
delim = "&amp;";
}
if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
html(delim);
html("ss=1");
delim = "&amp;";
}
if (ctx.qry.context > 0 && ctx.qry.context != 3) {
html(delim);
html("context=");
htmlf("%d", ctx.qry.context);
delim = "&amp;";
}
if (ctx.qry.ignorews) {
html(delim);
html("ignorews=1");
delim = "&amp;";
}
html("'>");
html_txt(name);
html("</a>");
}
void cgit_patch_link(const char *name, const char *title, const char *class,
const char *head, const char *rev, const char *path)
{
reporevlink("patch", name, title, class, head, rev, path);
}
void cgit_stats_link(const char *name, const char *title, const char *class,
const char *head, const char *path)
{
reporevlink("stats", name, title, class, head, NULL, path);
}
void cgit_self_link(char *name, const char *title, const char *class,
struct cgit_context *ctx)
{
if (!strcmp(ctx->qry.page, "repolist"))
return cgit_index_link(name, title, class, ctx->qry.search,
ctx->qry.ofs);
else if (!strcmp(ctx->qry.page, "summary"))
return cgit_summary_link(name, title, class, ctx->qry.head);
else if (!strcmp(ctx->qry.page, "tag"))
return cgit_tag_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL);
else if (!strcmp(ctx->qry.page, "tree"))
return cgit_tree_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path);
else if (!strcmp(ctx->qry.page, "plain"))
return cgit_plain_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path);
else if (!strcmp(ctx->qry.page, "log"))
return cgit_log_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path, ctx->qry.ofs,
ctx->qry.grep, ctx->qry.search,
ctx->qry.showmsg);
else if (!strcmp(ctx->qry.page, "commit"))
return cgit_commit_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path, 0);
else if (!strcmp(ctx->qry.page, "patch"))
return cgit_patch_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path);
else if (!strcmp(ctx->qry.page, "refs"))
return cgit_refs_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path);
else if (!strcmp(ctx->qry.page, "snapshot"))
return cgit_snapshot_link(name, title, class, ctx->qry.head,
ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
ctx->qry.path);
else if (!strcmp(ctx->qry.page, "diff"))
return cgit_diff_link(name, title, class, ctx->qry.head,
ctx->qry.sha1, ctx->qry.sha2,
ctx->qry.path, 0);
else if (!strcmp(ctx->qry.page, "stats"))
return cgit_stats_link(name, title, class, ctx->qry.head,
ctx->qry.path);
/* Don't known how to make link for this page */
repolink(title, class, ctx->qry.page, ctx->qry.head, ctx->qry.path);
html("><!-- cgit_self_link() doesn't know how to make link for page '");
html_txt(ctx->qry.page);
html("' -->");
html_txt(name);
html("</a>");
}
void cgit_object_link(struct object *obj)
{
char *page, *shortrev, *fullrev, *name;
fullrev = sha1_to_hex(obj->sha1);
shortrev = xstrdup(fullrev);
shortrev[10] = '\0';
if (obj->type == OBJ_COMMIT) {
cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
ctx.qry.head, fullrev, NULL, 0);
return;
} else if (obj->type == OBJ_TREE)
page = "tree";
else if (obj->type == OBJ_TAG)
page = "tag";
else
page = "blob";
name = fmt("%s %s...", typename(obj->type), shortrev);
reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
}
void cgit_print_date(time_t secs, const char *format, int local_time)
{
char buf[64];
struct tm *time;
if (!secs)
return;
if(local_time)
time = localtime(&secs);
else
time = gmtime(&secs);
strftime(buf, sizeof(buf)-1, format, time);
html_txt(buf);
}
void cgit_print_age(time_t t, time_t max_relative, const char *format)
{
time_t now, secs;
if (!t)
return;
time(&now);
secs = now - t;
if (secs > max_relative && max_relative >= 0) {
cgit_print_date(t, format, ctx.cfg.local_time);
return;
}
if (secs < TM_HOUR * 2) {
htmlf("<span class='age-mins'>%.0f min.</span>",
secs * 1.0 / TM_MIN);
return;
}
if (secs < TM_DAY * 2) {
htmlf("<span class='age-hours'>%.0f hours</span>",
secs * 1.0 / TM_HOUR);
return;
}
if (secs < TM_WEEK * 2) {
htmlf("<span class='age-days'>%.0f days</span>",
secs * 1.0 / TM_DAY);
return;
}
if (secs < TM_MONTH * 2) {
htmlf("<span class='age-weeks'>%.0f weeks</span>",
secs * 1.0 / TM_WEEK);
return;
}
if (secs < TM_YEAR * 2) {
htmlf("<span class='age-months'>%.0f months</span>",
secs * 1.0 / TM_MONTH);
return;
}
htmlf("<span class='age-years'>%.0f years</span>",
secs * 1.0 / TM_YEAR);
}
void cgit_print_http_headers(struct cgit_context *ctx)
{
if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1"))
return;
if (ctx->page.status)
htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
if (ctx->page.mimetype && ctx->page.charset)
htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
ctx->page.charset);
else if (ctx->page.mimetype)
htmlf("Content-Type: %s\n", ctx->page.mimetype);
if (ctx->page.size)
- htmlf("Content-Length: %ld\n", ctx->page.size);
+ htmlf("Content-Length: %zd\n", ctx->page.size);
if (ctx->page.filename)
htmlf("Content-Disposition: inline; filename=\"%s\"\n",
ctx->page.filename);
htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
htmlf("Expires: %s\n", http_date(ctx->page.expires));
if (ctx->page.etag)
htmlf("ETag: \"%s\"\n", ctx->page.etag);
html("\n");
if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD"))
exit(0);
}
void cgit_print_docstart(struct cgit_context *ctx)
{
if (ctx->cfg.embedded) {
if (ctx->cfg.header)
html_include(ctx->cfg.header);
return;
}
char *host = cgit_hosturl();
html(cgit_doctype);
html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
html("<head>\n");
html("<title>");
html_txt(ctx->page.title);
html("</title>\n");
htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
if (ctx->cfg.robots && *ctx->cfg.robots)
htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
html("<link rel='stylesheet' type='text/css' href='");
html_attr(ctx->cfg.css);
html("'/>\n");
if (ctx->cfg.favicon) {
html("<link rel='shortcut icon' href='");
html_attr(ctx->cfg.favicon);
html("'/>\n");
}
if (host && ctx->repo) {
html("<link rel='alternate' title='Atom feed' href='");
html(cgit_httpscheme());
html_attr(cgit_hosturl());
html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
fmt("h=%s", ctx->qry.head)));
html("' type='application/atom+xml'/>\n");
}
if (ctx->cfg.head_include)
html_include(ctx->cfg.head_include);
html("</head>\n");
html("<body>\n");
if (ctx->cfg.header)
html_include(ctx->cfg.header);
}
void cgit_print_docend()
{
html("</div> <!-- class=content -->\n");
if (ctx.cfg.embedded) {
html("</div> <!-- id=cgit -->\n");
if (ctx.cfg.footer)
html_include(ctx.cfg.footer);
return;
}
if (ctx.cfg.footer)
html_include(ctx.cfg.footer);
else {
htmlf("<div class='footer'>generated by cgit %s at ",
cgit_version);
cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
html("</div>\n");
}
html("</div> <!-- id=cgit -->\n");
html("</body>\n</html>\n");
}
int print_branch_option(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
{
char *name = (char *)refname;
html_option(name, name, ctx.qry.head);
return 0;
}
int print_archive_ref(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
{
struct tag *tag;
struct taginfo *info;
struct object *obj;
char buf[256], *url;
unsigned char fileid[20];
int *header = (int *)cb_data;
if (prefixcmp(refname, "refs/archives"))
return 0;
strncpy(buf, refname+14, sizeof(buf));
obj = parse_object(sha1);
if (!obj)
return 1;
if (obj->type == OBJ_TAG) {
tag = lookup_tag(sha1);
if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
return 0;
hashcpy(fileid, tag->tagged->sha1);
} else if (obj->type != OBJ_BLOB) {
return 0;
} else {
hashcpy(fileid, sha1);
}
if (!*header) {
html("<h1>download</h1>\n");
*header = 1;
}
url = cgit_pageurl(ctx.qry.repo, "blob",
fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
buf));
html_link_open(url, NULL, "menu");
html_txt(strlpart(buf, 20));
html_link_close();
return 0;
}
void cgit_add_hidden_formfields(int incl_head, int incl_search,
const char *page)
{
char *url;
if (!ctx.cfg.virtual_root) {
url = fmt("%s/%s", ctx.qry.repo, page);
if (ctx.qry.vpath)
url = fmt("%s/%s", url, ctx.qry.vpath);
html_hidden("url", url);
}
if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
strcmp(ctx.qry.head, ctx.repo->defbranch))
html_hidden("h", ctx.qry.head);
if (ctx.qry.sha1)
html_hidden("id", ctx.qry.sha1);
if (ctx.qry.sha2)
html_hidden("id2", ctx.qry.sha2);
if (ctx.qry.showmsg)
html_hidden("showmsg", "1");
if (incl_search) {
if (ctx.qry.grep)
html_hidden("qt", ctx.qry.grep);
if (ctx.qry.search)
html_hidden("q", ctx.qry.search);
}
}
static const char *hc(struct cgit_context *ctx, const char *page)
{
return strcmp(ctx->qry.page, page) ? NULL : "active";
}
static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
{
char *old_path = ctx->qry.path;
char *p = path, *q, *end = path + strlen(path);
ctx->qry.path = NULL;
cgit_self_link("root", NULL, NULL, ctx);
ctx->qry.path = p = path;
while (p < end) {
if (!(q = strchr(p, '/')))
q = end;
*q = '\0';
html_txt("/");
cgit_self_link(p, NULL, NULL, ctx);
if (q < end)
*q = '/';
p = q + 1;
}
ctx->qry.path = old_path;
}
static void print_header(struct cgit_context *ctx)
{
char *logo = NULL, *logo_link = NULL;
html("<table id='header'>\n");
html("<tr>\n");
if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
logo = ctx->repo->logo;
else
logo = ctx->cfg.logo;
if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
logo_link = ctx->repo->logo_link;