-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | cgit.c | 5 | ||||
-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | configfile.c | 87 | ||||
-rw-r--r-- | configfile.h | 8 | ||||
-rw-r--r-- | parsing.c | 75 |
6 files changed, 99 insertions, 78 deletions
@@ -1,135 +1,136 @@ | |||
1 | CGIT_VERSION = v0.7.2 | 1 | CGIT_VERSION = v0.7.2 |
2 | CGIT_SCRIPT_NAME = cgit.cgi | 2 | CGIT_SCRIPT_NAME = cgit.cgi |
3 | CGIT_SCRIPT_PATH = /var/www/htdocs/cgit | 3 | CGIT_SCRIPT_PATH = /var/www/htdocs/cgit |
4 | CGIT_CONFIG = /etc/cgitrc | 4 | CGIT_CONFIG = /etc/cgitrc |
5 | CACHE_ROOT = /var/cache/cgit | 5 | CACHE_ROOT = /var/cache/cgit |
6 | SHA1_HEADER = <openssl/sha.h> | 6 | SHA1_HEADER = <openssl/sha.h> |
7 | GIT_VER = 1.5.4.1 | 7 | GIT_VER = 1.5.4.1 |
8 | GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 | 8 | GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 |
9 | 9 | ||
10 | # | 10 | # |
11 | # Let the user override the above settings. | 11 | # Let the user override the above settings. |
12 | # | 12 | # |
13 | -include cgit.conf | 13 | -include cgit.conf |
14 | 14 | ||
15 | # | 15 | # |
16 | # Define a way to invoke make in subdirs quietly, shamelessly ripped | 16 | # Define a way to invoke make in subdirs quietly, shamelessly ripped |
17 | # from git.git | 17 | # from git.git |
18 | # | 18 | # |
19 | QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir | 19 | QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir |
20 | QUIET_SUBDIR1 = | 20 | QUIET_SUBDIR1 = |
21 | 21 | ||
22 | ifneq ($(findstring $(MAKEFLAGS),w),w) | 22 | ifneq ($(findstring $(MAKEFLAGS),w),w) |
23 | PRINT_DIR = --no-print-directory | 23 | PRINT_DIR = --no-print-directory |
24 | else # "make -w" | 24 | else # "make -w" |
25 | NO_SUBDIR = : | 25 | NO_SUBDIR = : |
26 | endif | 26 | endif |
27 | 27 | ||
28 | ifndef V | 28 | ifndef V |
29 | QUIET_CC = @echo ' ' CC $@; | 29 | QUIET_CC = @echo ' ' CC $@; |
30 | QUIET_MM = @echo ' ' MM $@; | 30 | QUIET_MM = @echo ' ' MM $@; |
31 | QUIET_SUBDIR0 = +@subdir= | 31 | QUIET_SUBDIR0 = +@subdir= |
32 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ | 32 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ |
33 | $(MAKE) $(PRINT_DIR) -C $$subdir | 33 | $(MAKE) $(PRINT_DIR) -C $$subdir |
34 | endif | 34 | endif |
35 | 35 | ||
36 | # | 36 | # |
37 | # Define a pattern rule for automatic dependency building | 37 | # Define a pattern rule for automatic dependency building |
38 | # | 38 | # |
39 | %.d: %.c | 39 | %.d: %.c |
40 | $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ | 40 | $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ |
41 | 41 | ||
42 | # | 42 | # |
43 | # Define a pattern rule for silent object building | 43 | # Define a pattern rule for silent object building |
44 | # | 44 | # |
45 | %.o: %.c | 45 | %.o: %.c |
46 | $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< | 46 | $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< |
47 | 47 | ||
48 | 48 | ||
49 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto | 49 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto |
50 | OBJECTS = | 50 | OBJECTS = |
51 | OBJECTS += cache.o | 51 | OBJECTS += cache.o |
52 | OBJECTS += cgit.o | 52 | OBJECTS += cgit.o |
53 | OBJECTS += cmd.o | 53 | OBJECTS += cmd.o |
54 | OBJECTS += configfile.o | ||
54 | OBJECTS += html.o | 55 | OBJECTS += html.o |
55 | OBJECTS += parsing.o | 56 | OBJECTS += parsing.o |
56 | OBJECTS += shared.o | 57 | OBJECTS += shared.o |
57 | OBJECTS += ui-blob.o | 58 | OBJECTS += ui-blob.o |
58 | OBJECTS += ui-commit.o | 59 | OBJECTS += ui-commit.o |
59 | OBJECTS += ui-diff.o | 60 | OBJECTS += ui-diff.o |
60 | OBJECTS += ui-log.o | 61 | OBJECTS += ui-log.o |
61 | OBJECTS += ui-patch.o | 62 | OBJECTS += ui-patch.o |
62 | OBJECTS += ui-refs.o | 63 | OBJECTS += ui-refs.o |
63 | OBJECTS += ui-repolist.o | 64 | OBJECTS += ui-repolist.o |
64 | OBJECTS += ui-shared.o | 65 | OBJECTS += ui-shared.o |
65 | OBJECTS += ui-snapshot.o | 66 | OBJECTS += ui-snapshot.o |
66 | OBJECTS += ui-summary.o | 67 | OBJECTS += ui-summary.o |
67 | OBJECTS += ui-tag.o | 68 | OBJECTS += ui-tag.o |
68 | OBJECTS += ui-tree.o | 69 | OBJECTS += ui-tree.o |
69 | 70 | ||
70 | ifdef NEEDS_LIBICONV | 71 | ifdef NEEDS_LIBICONV |
71 | EXTLIBS += -liconv | 72 | EXTLIBS += -liconv |
72 | endif | 73 | endif |
73 | 74 | ||
74 | 75 | ||
75 | .PHONY: all git test install clean distclean emptycache force-version get-git | 76 | .PHONY: all git test install clean distclean emptycache force-version get-git |
76 | 77 | ||
77 | all: cgit git | 78 | all: cgit git |
78 | 79 | ||
79 | VERSION: force-version | 80 | VERSION: force-version |
80 | @./gen-version.sh "$(CGIT_VERSION)" | 81 | @./gen-version.sh "$(CGIT_VERSION)" |
81 | -include VERSION | 82 | -include VERSION |
82 | 83 | ||
83 | 84 | ||
84 | CFLAGS += -g -Wall -Igit | 85 | CFLAGS += -g -Wall -Igit |
85 | CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' | 86 | CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' |
86 | CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' | 87 | CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' |
87 | CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' | 88 | CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' |
88 | CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' | 89 | CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' |
89 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' | 90 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' |
90 | 91 | ||
91 | 92 | ||
92 | cgit: $(OBJECTS) | 93 | cgit: $(OBJECTS) |
93 | $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) | 94 | $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) |
94 | 95 | ||
95 | $(OBJECTS): git/xdiff/lib.a git/libgit.a | 96 | $(OBJECTS): git/xdiff/lib.a git/libgit.a |
96 | 97 | ||
97 | cgit.o: VERSION | 98 | cgit.o: VERSION |
98 | 99 | ||
99 | -include $(OBJECTS:.o=.d) | 100 | -include $(OBJECTS:.o=.d) |
100 | 101 | ||
101 | git/xdiff/lib.a: | git | 102 | git/xdiff/lib.a: | git |
102 | 103 | ||
103 | git/libgit.a: | git | 104 | git/libgit.a: | git |
104 | 105 | ||
105 | git: | 106 | git: |
106 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a | 107 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a |
107 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a | 108 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a |
108 | 109 | ||
109 | test: all | 110 | test: all |
110 | $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all | 111 | $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all |
111 | 112 | ||
112 | install: all | 113 | install: all |
113 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) | 114 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) |
114 | install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) | 115 | install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) |
115 | install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css | 116 | install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css |
116 | install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png | 117 | install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png |
117 | 118 | ||
118 | uninstall: | 119 | uninstall: |
119 | rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) | 120 | rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) |
120 | rm -f $(CGIT_SCRIPT_PATH)/cgit.css | 121 | rm -f $(CGIT_SCRIPT_PATH)/cgit.css |
121 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png | 122 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png |
122 | 123 | ||
123 | clean: | 124 | clean: |
124 | rm -f cgit VERSION *.o *.d | 125 | rm -f cgit VERSION *.o *.d |
125 | cd git && $(MAKE) clean | 126 | cd git && $(MAKE) clean |
126 | 127 | ||
127 | distclean: clean | 128 | distclean: clean |
128 | git clean -d -x | 129 | git clean -d -x |
129 | cd git && git clean -d -x | 130 | cd git && git clean -d -x |
130 | 131 | ||
131 | emptycache: | 132 | emptycache: |
132 | rm -rf $(DESTDIR)$(CACHE_ROOT)/* | 133 | rm -rf $(DESTDIR)$(CACHE_ROOT)/* |
133 | 134 | ||
134 | get-git: | 135 | get-git: |
135 | curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git | 136 | curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git |
@@ -1,456 +1,457 @@ | |||
1 | /* cgit.c: cgi for the git scm | 1 | /* cgit.c: cgi for the git scm |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cache.h" | 10 | #include "cache.h" |
11 | #include "cmd.h" | 11 | #include "cmd.h" |
12 | #include "configfile.h" | ||
12 | #include "ui-shared.h" | 13 | #include "ui-shared.h" |
13 | 14 | ||
14 | const char *cgit_version = CGIT_VERSION; | 15 | const char *cgit_version = CGIT_VERSION; |
15 | 16 | ||
16 | void config_cb(const char *name, const char *value) | 17 | void config_cb(const char *name, const char *value) |
17 | { | 18 | { |
18 | if (!strcmp(name, "root-title")) | 19 | if (!strcmp(name, "root-title")) |
19 | ctx.cfg.root_title = xstrdup(value); | 20 | ctx.cfg.root_title = xstrdup(value); |
20 | else if (!strcmp(name, "css")) | 21 | else if (!strcmp(name, "css")) |
21 | ctx.cfg.css = xstrdup(value); | 22 | ctx.cfg.css = xstrdup(value); |
22 | else if (!strcmp(name, "logo")) | 23 | else if (!strcmp(name, "logo")) |
23 | ctx.cfg.logo = xstrdup(value); | 24 | ctx.cfg.logo = xstrdup(value); |
24 | else if (!strcmp(name, "index-header")) | 25 | else if (!strcmp(name, "index-header")) |
25 | ctx.cfg.index_header = xstrdup(value); | 26 | ctx.cfg.index_header = xstrdup(value); |
26 | else if (!strcmp(name, "index-info")) | 27 | else if (!strcmp(name, "index-info")) |
27 | ctx.cfg.index_info = xstrdup(value); | 28 | ctx.cfg.index_info = xstrdup(value); |
28 | else if (!strcmp(name, "logo-link")) | 29 | else if (!strcmp(name, "logo-link")) |
29 | ctx.cfg.logo_link = xstrdup(value); | 30 | ctx.cfg.logo_link = xstrdup(value); |
30 | else if (!strcmp(name, "module-link")) | 31 | else if (!strcmp(name, "module-link")) |
31 | ctx.cfg.module_link = xstrdup(value); | 32 | ctx.cfg.module_link = xstrdup(value); |
32 | else if (!strcmp(name, "virtual-root")) { | 33 | else if (!strcmp(name, "virtual-root")) { |
33 | ctx.cfg.virtual_root = trim_end(value, '/'); | 34 | ctx.cfg.virtual_root = trim_end(value, '/'); |
34 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 35 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
35 | ctx.cfg.virtual_root = ""; | 36 | ctx.cfg.virtual_root = ""; |
36 | } else if (!strcmp(name, "nocache")) | 37 | } else if (!strcmp(name, "nocache")) |
37 | ctx.cfg.nocache = atoi(value); | 38 | ctx.cfg.nocache = atoi(value); |
38 | else if (!strcmp(name, "snapshots")) | 39 | else if (!strcmp(name, "snapshots")) |
39 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 40 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
40 | else if (!strcmp(name, "enable-index-links")) | 41 | else if (!strcmp(name, "enable-index-links")) |
41 | ctx.cfg.enable_index_links = atoi(value); | 42 | ctx.cfg.enable_index_links = atoi(value); |
42 | else if (!strcmp(name, "enable-log-filecount")) | 43 | else if (!strcmp(name, "enable-log-filecount")) |
43 | ctx.cfg.enable_log_filecount = atoi(value); | 44 | ctx.cfg.enable_log_filecount = atoi(value); |
44 | else if (!strcmp(name, "enable-log-linecount")) | 45 | else if (!strcmp(name, "enable-log-linecount")) |
45 | ctx.cfg.enable_log_linecount = atoi(value); | 46 | ctx.cfg.enable_log_linecount = atoi(value); |
46 | else if (!strcmp(name, "cache-root")) | 47 | else if (!strcmp(name, "cache-root")) |
47 | ctx.cfg.cache_root = xstrdup(value); | 48 | ctx.cfg.cache_root = xstrdup(value); |
48 | else if (!strcmp(name, "cache-root-ttl")) | 49 | else if (!strcmp(name, "cache-root-ttl")) |
49 | ctx.cfg.cache_root_ttl = atoi(value); | 50 | ctx.cfg.cache_root_ttl = atoi(value); |
50 | else if (!strcmp(name, "cache-repo-ttl")) | 51 | else if (!strcmp(name, "cache-repo-ttl")) |
51 | ctx.cfg.cache_repo_ttl = atoi(value); | 52 | ctx.cfg.cache_repo_ttl = atoi(value); |
52 | else if (!strcmp(name, "cache-static-ttl")) | 53 | else if (!strcmp(name, "cache-static-ttl")) |
53 | ctx.cfg.cache_static_ttl = atoi(value); | 54 | ctx.cfg.cache_static_ttl = atoi(value); |
54 | else if (!strcmp(name, "cache-dynamic-ttl")) | 55 | else if (!strcmp(name, "cache-dynamic-ttl")) |
55 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 56 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
56 | else if (!strcmp(name, "max-message-length")) | 57 | else if (!strcmp(name, "max-message-length")) |
57 | ctx.cfg.max_msg_len = atoi(value); | 58 | ctx.cfg.max_msg_len = atoi(value); |
58 | else if (!strcmp(name, "max-repodesc-length")) | 59 | else if (!strcmp(name, "max-repodesc-length")) |
59 | ctx.cfg.max_repodesc_len = atoi(value); | 60 | ctx.cfg.max_repodesc_len = atoi(value); |
60 | else if (!strcmp(name, "max-commit-count")) | 61 | else if (!strcmp(name, "max-commit-count")) |
61 | ctx.cfg.max_commit_count = atoi(value); | 62 | ctx.cfg.max_commit_count = atoi(value); |
62 | else if (!strcmp(name, "summary-log")) | 63 | else if (!strcmp(name, "summary-log")) |
63 | ctx.cfg.summary_log = atoi(value); | 64 | ctx.cfg.summary_log = atoi(value); |
64 | else if (!strcmp(name, "summary-branches")) | 65 | else if (!strcmp(name, "summary-branches")) |
65 | ctx.cfg.summary_branches = atoi(value); | 66 | ctx.cfg.summary_branches = atoi(value); |
66 | else if (!strcmp(name, "summary-tags")) | 67 | else if (!strcmp(name, "summary-tags")) |
67 | ctx.cfg.summary_tags = atoi(value); | 68 | ctx.cfg.summary_tags = atoi(value); |
68 | else if (!strcmp(name, "agefile")) | 69 | else if (!strcmp(name, "agefile")) |
69 | ctx.cfg.agefile = xstrdup(value); | 70 | ctx.cfg.agefile = xstrdup(value); |
70 | else if (!strcmp(name, "renamelimit")) | 71 | else if (!strcmp(name, "renamelimit")) |
71 | ctx.cfg.renamelimit = atoi(value); | 72 | ctx.cfg.renamelimit = atoi(value); |
72 | else if (!strcmp(name, "robots")) | 73 | else if (!strcmp(name, "robots")) |
73 | ctx.cfg.robots = xstrdup(value); | 74 | ctx.cfg.robots = xstrdup(value); |
74 | else if (!strcmp(name, "clone-prefix")) | 75 | else if (!strcmp(name, "clone-prefix")) |
75 | ctx.cfg.clone_prefix = xstrdup(value); | 76 | ctx.cfg.clone_prefix = xstrdup(value); |
76 | else if (!strcmp(name, "repo.group")) | 77 | else if (!strcmp(name, "repo.group")) |
77 | ctx.cfg.repo_group = xstrdup(value); | 78 | ctx.cfg.repo_group = xstrdup(value); |
78 | else if (!strcmp(name, "repo.url")) | 79 | else if (!strcmp(name, "repo.url")) |
79 | ctx.repo = cgit_add_repo(value); | 80 | ctx.repo = cgit_add_repo(value); |
80 | else if (!strcmp(name, "repo.name")) | 81 | else if (!strcmp(name, "repo.name")) |
81 | ctx.repo->name = xstrdup(value); | 82 | ctx.repo->name = xstrdup(value); |
82 | else if (ctx.repo && !strcmp(name, "repo.path")) | 83 | else if (ctx.repo && !strcmp(name, "repo.path")) |
83 | ctx.repo->path = trim_end(value, '/'); | 84 | ctx.repo->path = trim_end(value, '/'); |
84 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | 85 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
85 | ctx.repo->clone_url = xstrdup(value); | 86 | ctx.repo->clone_url = xstrdup(value); |
86 | else if (ctx.repo && !strcmp(name, "repo.desc")) | 87 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
87 | ctx.repo->desc = xstrdup(value); | 88 | ctx.repo->desc = xstrdup(value); |
88 | else if (ctx.repo && !strcmp(name, "repo.owner")) | 89 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
89 | ctx.repo->owner = xstrdup(value); | 90 | ctx.repo->owner = xstrdup(value); |
90 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | 91 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
91 | ctx.repo->defbranch = xstrdup(value); | 92 | ctx.repo->defbranch = xstrdup(value); |
92 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | 93 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
93 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 94 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
94 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | 95 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
95 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 96 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
96 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | 97 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
97 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 98 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
98 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | 99 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
99 | ctx.repo->module_link= xstrdup(value); | 100 | ctx.repo->module_link= xstrdup(value); |
100 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | 101 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
101 | if (*value == '/') | 102 | if (*value == '/') |
102 | ctx.repo->readme = xstrdup(value); | 103 | ctx.repo->readme = xstrdup(value); |
103 | else | 104 | else |
104 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | 105 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
105 | } else if (!strcmp(name, "include")) | 106 | } else if (!strcmp(name, "include")) |
106 | cgit_read_config(value, config_cb); | 107 | parse_configfile(value, config_cb); |
107 | } | 108 | } |
108 | 109 | ||
109 | static void querystring_cb(const char *name, const char *value) | 110 | static void querystring_cb(const char *name, const char *value) |
110 | { | 111 | { |
111 | if (!strcmp(name,"r")) { | 112 | if (!strcmp(name,"r")) { |
112 | ctx.qry.repo = xstrdup(value); | 113 | ctx.qry.repo = xstrdup(value); |
113 | ctx.repo = cgit_get_repoinfo(value); | 114 | ctx.repo = cgit_get_repoinfo(value); |
114 | } else if (!strcmp(name, "p")) { | 115 | } else if (!strcmp(name, "p")) { |
115 | ctx.qry.page = xstrdup(value); | 116 | ctx.qry.page = xstrdup(value); |
116 | } else if (!strcmp(name, "url")) { | 117 | } else if (!strcmp(name, "url")) { |
117 | cgit_parse_url(value); | 118 | cgit_parse_url(value); |
118 | } else if (!strcmp(name, "qt")) { | 119 | } else if (!strcmp(name, "qt")) { |
119 | ctx.qry.grep = xstrdup(value); | 120 | ctx.qry.grep = xstrdup(value); |
120 | } else if (!strcmp(name, "q")) { | 121 | } else if (!strcmp(name, "q")) { |
121 | ctx.qry.search = xstrdup(value); | 122 | ctx.qry.search = xstrdup(value); |
122 | } else if (!strcmp(name, "h")) { | 123 | } else if (!strcmp(name, "h")) { |
123 | ctx.qry.head = xstrdup(value); | 124 | ctx.qry.head = xstrdup(value); |
124 | ctx.qry.has_symref = 1; | 125 | ctx.qry.has_symref = 1; |
125 | } else if (!strcmp(name, "id")) { | 126 | } else if (!strcmp(name, "id")) { |
126 | ctx.qry.sha1 = xstrdup(value); | 127 | ctx.qry.sha1 = xstrdup(value); |
127 | ctx.qry.has_sha1 = 1; | 128 | ctx.qry.has_sha1 = 1; |
128 | } else if (!strcmp(name, "id2")) { | 129 | } else if (!strcmp(name, "id2")) { |
129 | ctx.qry.sha2 = xstrdup(value); | 130 | ctx.qry.sha2 = xstrdup(value); |
130 | ctx.qry.has_sha1 = 1; | 131 | ctx.qry.has_sha1 = 1; |
131 | } else if (!strcmp(name, "ofs")) { | 132 | } else if (!strcmp(name, "ofs")) { |
132 | ctx.qry.ofs = atoi(value); | 133 | ctx.qry.ofs = atoi(value); |
133 | } else if (!strcmp(name, "path")) { | 134 | } else if (!strcmp(name, "path")) { |
134 | ctx.qry.path = trim_end(value, '/'); | 135 | ctx.qry.path = trim_end(value, '/'); |
135 | } else if (!strcmp(name, "name")) { | 136 | } else if (!strcmp(name, "name")) { |
136 | ctx.qry.name = xstrdup(value); | 137 | ctx.qry.name = xstrdup(value); |
137 | } | 138 | } |
138 | } | 139 | } |
139 | 140 | ||
140 | static void prepare_context(struct cgit_context *ctx) | 141 | static void prepare_context(struct cgit_context *ctx) |
141 | { | 142 | { |
142 | memset(ctx, 0, sizeof(ctx)); | 143 | memset(ctx, 0, sizeof(ctx)); |
143 | ctx->cfg.agefile = "info/web/last-modified"; | 144 | ctx->cfg.agefile = "info/web/last-modified"; |
144 | ctx->cfg.cache_dynamic_ttl = 5; | 145 | ctx->cfg.cache_dynamic_ttl = 5; |
145 | ctx->cfg.cache_max_create_time = 5; | 146 | ctx->cfg.cache_max_create_time = 5; |
146 | ctx->cfg.cache_repo_ttl = 5; | 147 | ctx->cfg.cache_repo_ttl = 5; |
147 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 148 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
148 | ctx->cfg.cache_root_ttl = 5; | 149 | ctx->cfg.cache_root_ttl = 5; |
149 | ctx->cfg.cache_static_ttl = -1; | 150 | ctx->cfg.cache_static_ttl = -1; |
150 | ctx->cfg.css = "/cgit.css"; | 151 | ctx->cfg.css = "/cgit.css"; |
151 | ctx->cfg.logo = "/git-logo.png"; | 152 | ctx->cfg.logo = "/git-logo.png"; |
152 | ctx->cfg.max_commit_count = 50; | 153 | ctx->cfg.max_commit_count = 50; |
153 | ctx->cfg.max_lock_attempts = 5; | 154 | ctx->cfg.max_lock_attempts = 5; |
154 | ctx->cfg.max_msg_len = 60; | 155 | ctx->cfg.max_msg_len = 60; |
155 | ctx->cfg.max_repodesc_len = 60; | 156 | ctx->cfg.max_repodesc_len = 60; |
156 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 157 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
157 | ctx->cfg.renamelimit = -1; | 158 | ctx->cfg.renamelimit = -1; |
158 | ctx->cfg.robots = "index, nofollow"; | 159 | ctx->cfg.robots = "index, nofollow"; |
159 | ctx->cfg.root_title = "Git repository browser"; | 160 | ctx->cfg.root_title = "Git repository browser"; |
160 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 161 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
161 | ctx->page.mimetype = "text/html"; | 162 | ctx->page.mimetype = "text/html"; |
162 | ctx->page.charset = PAGE_ENCODING; | 163 | ctx->page.charset = PAGE_ENCODING; |
163 | ctx->page.filename = NULL; | 164 | ctx->page.filename = NULL; |
164 | } | 165 | } |
165 | 166 | ||
166 | static int cgit_prepare_cache(struct cacheitem *item) | 167 | static int cgit_prepare_cache(struct cacheitem *item) |
167 | { | 168 | { |
168 | if (!ctx.repo && ctx.qry.repo) { | 169 | if (!ctx.repo && ctx.qry.repo) { |
169 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | 170 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
170 | "Bad request"); | 171 | "Bad request"); |
171 | cgit_print_http_headers(&ctx); | 172 | cgit_print_http_headers(&ctx); |
172 | cgit_print_docstart(&ctx); | 173 | cgit_print_docstart(&ctx); |
173 | cgit_print_pageheader(&ctx); | 174 | cgit_print_pageheader(&ctx); |
174 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 175 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
175 | cgit_print_docend(); | 176 | cgit_print_docend(); |
176 | return 0; | 177 | return 0; |
177 | } | 178 | } |
178 | 179 | ||
179 | if (!ctx.repo) { | 180 | if (!ctx.repo) { |
180 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); | 181 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
181 | item->ttl = ctx.cfg.cache_root_ttl; | 182 | item->ttl = ctx.cfg.cache_root_ttl; |
182 | return 1; | 183 | return 1; |
183 | } | 184 | } |
184 | 185 | ||
185 | if (!ctx.qry.page) { | 186 | if (!ctx.qry.page) { |
186 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | 187 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
187 | cache_safe_filename(ctx.repo->url), | 188 | cache_safe_filename(ctx.repo->url), |
188 | cache_safe_filename(ctx.qry.raw))); | 189 | cache_safe_filename(ctx.qry.raw))); |
189 | item->ttl = ctx.cfg.cache_repo_ttl; | 190 | item->ttl = ctx.cfg.cache_repo_ttl; |
190 | } else { | 191 | } else { |
191 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | 192 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
192 | cache_safe_filename(ctx.repo->url), | 193 | cache_safe_filename(ctx.repo->url), |
193 | ctx.qry.page, | 194 | ctx.qry.page, |
194 | cache_safe_filename(ctx.qry.raw))); | 195 | cache_safe_filename(ctx.qry.raw))); |
195 | if (ctx.qry.has_symref) | 196 | if (ctx.qry.has_symref) |
196 | item->ttl = ctx.cfg.cache_dynamic_ttl; | 197 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
197 | else if (ctx.qry.has_sha1) | 198 | else if (ctx.qry.has_sha1) |
198 | item->ttl = ctx.cfg.cache_static_ttl; | 199 | item->ttl = ctx.cfg.cache_static_ttl; |
199 | else | 200 | else |
200 | item->ttl = ctx.cfg.cache_repo_ttl; | 201 | item->ttl = ctx.cfg.cache_repo_ttl; |
201 | } | 202 | } |
202 | return 1; | 203 | return 1; |
203 | } | 204 | } |
204 | 205 | ||
205 | struct refmatch { | 206 | struct refmatch { |
206 | char *req_ref; | 207 | char *req_ref; |
207 | char *first_ref; | 208 | char *first_ref; |
208 | int match; | 209 | int match; |
209 | }; | 210 | }; |
210 | 211 | ||
211 | int find_current_ref(const char *refname, const unsigned char *sha1, | 212 | int find_current_ref(const char *refname, const unsigned char *sha1, |
212 | int flags, void *cb_data) | 213 | int flags, void *cb_data) |
213 | { | 214 | { |
214 | struct refmatch *info; | 215 | struct refmatch *info; |
215 | 216 | ||
216 | info = (struct refmatch *)cb_data; | 217 | info = (struct refmatch *)cb_data; |
217 | if (!strcmp(refname, info->req_ref)) | 218 | if (!strcmp(refname, info->req_ref)) |
218 | info->match = 1; | 219 | info->match = 1; |
219 | if (!info->first_ref) | 220 | if (!info->first_ref) |
220 | info->first_ref = xstrdup(refname); | 221 | info->first_ref = xstrdup(refname); |
221 | return info->match; | 222 | return info->match; |
222 | } | 223 | } |
223 | 224 | ||
224 | char *find_default_branch(struct cgit_repo *repo) | 225 | char *find_default_branch(struct cgit_repo *repo) |
225 | { | 226 | { |
226 | struct refmatch info; | 227 | struct refmatch info; |
227 | 228 | ||
228 | info.req_ref = repo->defbranch; | 229 | info.req_ref = repo->defbranch; |
229 | info.first_ref = NULL; | 230 | info.first_ref = NULL; |
230 | info.match = 0; | 231 | info.match = 0; |
231 | for_each_branch_ref(find_current_ref, &info); | 232 | for_each_branch_ref(find_current_ref, &info); |
232 | if (info.match) | 233 | if (info.match) |
233 | return info.req_ref; | 234 | return info.req_ref; |
234 | else | 235 | else |
235 | return info.first_ref; | 236 | return info.first_ref; |
236 | } | 237 | } |
237 | 238 | ||
238 | static int prepare_repo_cmd(struct cgit_context *ctx) | 239 | static int prepare_repo_cmd(struct cgit_context *ctx) |
239 | { | 240 | { |
240 | char *tmp; | 241 | char *tmp; |
241 | unsigned char sha1[20]; | 242 | unsigned char sha1[20]; |
242 | int nongit = 0; | 243 | int nongit = 0; |
243 | 244 | ||
244 | setenv("GIT_DIR", ctx->repo->path, 1); | 245 | setenv("GIT_DIR", ctx->repo->path, 1); |
245 | setup_git_directory_gently(&nongit); | 246 | setup_git_directory_gently(&nongit); |
246 | if (nongit) { | 247 | if (nongit) { |
247 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 248 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
248 | "config error"); | 249 | "config error"); |
249 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 250 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
250 | ctx->repo = NULL; | 251 | ctx->repo = NULL; |
251 | cgit_print_http_headers(ctx); | 252 | cgit_print_http_headers(ctx); |
252 | cgit_print_docstart(ctx); | 253 | cgit_print_docstart(ctx); |
253 | cgit_print_pageheader(ctx); | 254 | cgit_print_pageheader(ctx); |
254 | cgit_print_error(tmp); | 255 | cgit_print_error(tmp); |
255 | cgit_print_docend(); | 256 | cgit_print_docend(); |
256 | return 1; | 257 | return 1; |
257 | } | 258 | } |
258 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 259 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
259 | 260 | ||
260 | if (!ctx->qry.head) { | 261 | if (!ctx->qry.head) { |
261 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 262 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
262 | ctx->repo->defbranch = ctx->qry.head; | 263 | ctx->repo->defbranch = ctx->qry.head; |
263 | } | 264 | } |
264 | 265 | ||
265 | if (!ctx->qry.head) { | 266 | if (!ctx->qry.head) { |
266 | cgit_print_http_headers(ctx); | 267 | cgit_print_http_headers(ctx); |
267 | cgit_print_docstart(ctx); | 268 | cgit_print_docstart(ctx); |
268 | cgit_print_pageheader(ctx); | 269 | cgit_print_pageheader(ctx); |
269 | cgit_print_error("Repository seems to be empty"); | 270 | cgit_print_error("Repository seems to be empty"); |
270 | cgit_print_docend(); | 271 | cgit_print_docend(); |
271 | return 1; | 272 | return 1; |
272 | } | 273 | } |
273 | 274 | ||
274 | if (get_sha1(ctx->qry.head, sha1)) { | 275 | if (get_sha1(ctx->qry.head, sha1)) { |
275 | tmp = xstrdup(ctx->qry.head); | 276 | tmp = xstrdup(ctx->qry.head); |
276 | ctx->qry.head = ctx->repo->defbranch; | 277 | ctx->qry.head = ctx->repo->defbranch; |
277 | cgit_print_http_headers(ctx); | 278 | cgit_print_http_headers(ctx); |
278 | cgit_print_docstart(ctx); | 279 | cgit_print_docstart(ctx); |
279 | cgit_print_pageheader(ctx); | 280 | cgit_print_pageheader(ctx); |
280 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 281 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
281 | cgit_print_docend(); | 282 | cgit_print_docend(); |
282 | return 1; | 283 | return 1; |
283 | } | 284 | } |
284 | return 0; | 285 | return 0; |
285 | } | 286 | } |
286 | 287 | ||
287 | static void process_request(struct cgit_context *ctx) | 288 | static void process_request(struct cgit_context *ctx) |
288 | { | 289 | { |
289 | struct cgit_cmd *cmd; | 290 | struct cgit_cmd *cmd; |
290 | 291 | ||
291 | cmd = cgit_get_cmd(ctx); | 292 | cmd = cgit_get_cmd(ctx); |
292 | if (!cmd) { | 293 | if (!cmd) { |
293 | ctx->page.title = "cgit error"; | 294 | ctx->page.title = "cgit error"; |
294 | ctx->repo = NULL; | 295 | ctx->repo = NULL; |
295 | cgit_print_http_headers(ctx); | 296 | cgit_print_http_headers(ctx); |
296 | cgit_print_docstart(ctx); | 297 | cgit_print_docstart(ctx); |
297 | cgit_print_pageheader(ctx); | 298 | cgit_print_pageheader(ctx); |
298 | cgit_print_error("Invalid request"); | 299 | cgit_print_error("Invalid request"); |
299 | cgit_print_docend(); | 300 | cgit_print_docend(); |
300 | return; | 301 | return; |
301 | } | 302 | } |
302 | 303 | ||
303 | if (cmd->want_repo && prepare_repo_cmd(ctx)) | 304 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
304 | return; | 305 | return; |
305 | 306 | ||
306 | if (cmd->want_layout) { | 307 | if (cmd->want_layout) { |
307 | cgit_print_http_headers(ctx); | 308 | cgit_print_http_headers(ctx); |
308 | cgit_print_docstart(ctx); | 309 | cgit_print_docstart(ctx); |
309 | cgit_print_pageheader(ctx); | 310 | cgit_print_pageheader(ctx); |
310 | } | 311 | } |
311 | 312 | ||
312 | cmd->fn(ctx); | 313 | cmd->fn(ctx); |
313 | 314 | ||
314 | if (cmd->want_layout) | 315 | if (cmd->want_layout) |
315 | cgit_print_docend(); | 316 | cgit_print_docend(); |
316 | } | 317 | } |
317 | 318 | ||
318 | static long ttl_seconds(long ttl) | 319 | static long ttl_seconds(long ttl) |
319 | { | 320 | { |
320 | if (ttl<0) | 321 | if (ttl<0) |
321 | return 60 * 60 * 24 * 365; | 322 | return 60 * 60 * 24 * 365; |
322 | else | 323 | else |
323 | return ttl * 60; | 324 | return ttl * 60; |
324 | } | 325 | } |
325 | 326 | ||
326 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 327 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
327 | { | 328 | { |
328 | int stdout2; | 329 | int stdout2; |
329 | 330 | ||
330 | if (use_cache) { | 331 | if (use_cache) { |
331 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 332 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
332 | "Preserving STDOUT"); | 333 | "Preserving STDOUT"); |
333 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 334 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
334 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 335 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
335 | } | 336 | } |
336 | 337 | ||
337 | ctx.page.modified = time(NULL); | 338 | ctx.page.modified = time(NULL); |
338 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | 339 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
339 | process_request(&ctx); | 340 | process_request(&ctx); |
340 | 341 | ||
341 | if (use_cache) { | 342 | if (use_cache) { |
342 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 343 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
343 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 344 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
344 | "Restoring original STDOUT"); | 345 | "Restoring original STDOUT"); |
345 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 346 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
346 | } | 347 | } |
347 | } | 348 | } |
348 | 349 | ||
349 | static void cgit_check_cache(struct cacheitem *item) | 350 | static void cgit_check_cache(struct cacheitem *item) |
350 | { | 351 | { |
351 | int i = 0; | 352 | int i = 0; |
352 | 353 | ||
353 | top: | 354 | top: |
354 | if (++i > ctx.cfg.max_lock_attempts) { | 355 | if (++i > ctx.cfg.max_lock_attempts) { |
355 | die("cgit_refresh_cache: unable to lock %s: %s", | 356 | die("cgit_refresh_cache: unable to lock %s: %s", |
356 | item->name, strerror(errno)); | 357 | item->name, strerror(errno)); |
357 | } | 358 | } |
358 | if (!cache_exist(item)) { | 359 | if (!cache_exist(item)) { |
359 | if (!cache_lock(item)) { | 360 | if (!cache_lock(item)) { |
360 | sleep(1); | 361 | sleep(1); |
361 | goto top; | 362 | goto top; |
362 | } | 363 | } |
363 | if (!cache_exist(item)) { | 364 | if (!cache_exist(item)) { |
364 | cgit_fill_cache(item, 1); | 365 | cgit_fill_cache(item, 1); |
365 | cache_unlock(item); | 366 | cache_unlock(item); |
366 | } else { | 367 | } else { |
367 | cache_cancel_lock(item); | 368 | cache_cancel_lock(item); |
368 | } | 369 | } |
369 | } else if (cache_expired(item) && cache_lock(item)) { | 370 | } else if (cache_expired(item) && cache_lock(item)) { |
370 | if (cache_expired(item)) { | 371 | if (cache_expired(item)) { |
371 | cgit_fill_cache(item, 1); | 372 | cgit_fill_cache(item, 1); |
372 | cache_unlock(item); | 373 | cache_unlock(item); |
373 | } else { | 374 | } else { |
374 | cache_cancel_lock(item); | 375 | cache_cancel_lock(item); |
375 | } | 376 | } |
376 | } | 377 | } |
377 | } | 378 | } |
378 | 379 | ||
379 | static void cgit_print_cache(struct cacheitem *item) | 380 | static void cgit_print_cache(struct cacheitem *item) |
380 | { | 381 | { |
381 | static char buf[4096]; | 382 | static char buf[4096]; |
382 | ssize_t i; | 383 | ssize_t i; |
383 | 384 | ||
384 | int fd = open(item->name, O_RDONLY); | 385 | int fd = open(item->name, O_RDONLY); |
385 | if (fd<0) | 386 | if (fd<0) |
386 | die("Unable to open cached file %s", item->name); | 387 | die("Unable to open cached file %s", item->name); |
387 | 388 | ||
388 | while((i=read(fd, buf, sizeof(buf))) > 0) | 389 | while((i=read(fd, buf, sizeof(buf))) > 0) |
389 | write(STDOUT_FILENO, buf, i); | 390 | write(STDOUT_FILENO, buf, i); |
390 | 391 | ||
391 | close(fd); | 392 | close(fd); |
392 | } | 393 | } |
393 | 394 | ||
394 | static void cgit_parse_args(int argc, const char **argv) | 395 | static void cgit_parse_args(int argc, const char **argv) |
395 | { | 396 | { |
396 | int i; | 397 | int i; |
397 | 398 | ||
398 | for (i = 1; i < argc; i++) { | 399 | for (i = 1; i < argc; i++) { |
399 | if (!strncmp(argv[i], "--cache=", 8)) { | 400 | if (!strncmp(argv[i], "--cache=", 8)) { |
400 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 401 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
401 | } | 402 | } |
402 | if (!strcmp(argv[i], "--nocache")) { | 403 | if (!strcmp(argv[i], "--nocache")) { |
403 | ctx.cfg.nocache = 1; | 404 | ctx.cfg.nocache = 1; |
404 | } | 405 | } |
405 | if (!strncmp(argv[i], "--query=", 8)) { | 406 | if (!strncmp(argv[i], "--query=", 8)) { |
406 | ctx.qry.raw = xstrdup(argv[i]+8); | 407 | ctx.qry.raw = xstrdup(argv[i]+8); |
407 | } | 408 | } |
408 | if (!strncmp(argv[i], "--repo=", 7)) { | 409 | if (!strncmp(argv[i], "--repo=", 7)) { |
409 | ctx.qry.repo = xstrdup(argv[i]+7); | 410 | ctx.qry.repo = xstrdup(argv[i]+7); |
410 | } | 411 | } |
411 | if (!strncmp(argv[i], "--page=", 7)) { | 412 | if (!strncmp(argv[i], "--page=", 7)) { |
412 | ctx.qry.page = xstrdup(argv[i]+7); | 413 | ctx.qry.page = xstrdup(argv[i]+7); |
413 | } | 414 | } |
414 | if (!strncmp(argv[i], "--head=", 7)) { | 415 | if (!strncmp(argv[i], "--head=", 7)) { |
415 | ctx.qry.head = xstrdup(argv[i]+7); | 416 | ctx.qry.head = xstrdup(argv[i]+7); |
416 | ctx.qry.has_symref = 1; | 417 | ctx.qry.has_symref = 1; |
417 | } | 418 | } |
418 | if (!strncmp(argv[i], "--sha1=", 7)) { | 419 | if (!strncmp(argv[i], "--sha1=", 7)) { |
419 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 420 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
420 | ctx.qry.has_sha1 = 1; | 421 | ctx.qry.has_sha1 = 1; |
421 | } | 422 | } |
422 | if (!strncmp(argv[i], "--ofs=", 6)) { | 423 | if (!strncmp(argv[i], "--ofs=", 6)) { |
423 | ctx.qry.ofs = atoi(argv[i]+6); | 424 | ctx.qry.ofs = atoi(argv[i]+6); |
424 | } | 425 | } |
425 | } | 426 | } |
426 | } | 427 | } |
427 | 428 | ||
428 | int main(int argc, const char **argv) | 429 | int main(int argc, const char **argv) |
429 | { | 430 | { |
430 | struct cacheitem item; | 431 | struct cacheitem item; |
431 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 432 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
432 | 433 | ||
433 | prepare_context(&ctx); | 434 | prepare_context(&ctx); |
434 | item.st.st_mtime = time(NULL); | 435 | item.st.st_mtime = time(NULL); |
435 | cgit_repolist.length = 0; | 436 | cgit_repolist.length = 0; |
436 | cgit_repolist.count = 0; | 437 | cgit_repolist.count = 0; |
437 | cgit_repolist.repos = NULL; | 438 | cgit_repolist.repos = NULL; |
438 | 439 | ||
439 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 440 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
440 | config_cb); | 441 | config_cb); |
441 | if (getenv("SCRIPT_NAME")) | 442 | if (getenv("SCRIPT_NAME")) |
442 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 443 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
443 | if (getenv("QUERY_STRING")) | 444 | if (getenv("QUERY_STRING")) |
444 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 445 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
445 | cgit_parse_args(argc, argv); | 446 | cgit_parse_args(argc, argv); |
446 | cgit_parse_query(ctx.qry.raw, querystring_cb); | 447 | cgit_parse_query(ctx.qry.raw, querystring_cb); |
447 | if (!cgit_prepare_cache(&item)) | 448 | if (!cgit_prepare_cache(&item)) |
448 | return 0; | 449 | return 0; |
449 | if (ctx.cfg.nocache) { | 450 | if (ctx.cfg.nocache) { |
450 | cgit_fill_cache(&item, 0); | 451 | cgit_fill_cache(&item, 0); |
451 | } else { | 452 | } else { |
452 | cgit_check_cache(&item); | 453 | cgit_check_cache(&item); |
453 | cgit_print_cache(&item); | 454 | cgit_print_cache(&item); |
454 | } | 455 | } |
455 | return 0; | 456 | return 0; |
456 | } | 457 | } |
@@ -1,227 +1,226 @@ | |||
1 | #ifndef CGIT_H | 1 | #ifndef CGIT_H |
2 | #define CGIT_H | 2 | #define CGIT_H |
3 | 3 | ||
4 | 4 | ||
5 | #include <git-compat-util.h> | 5 | #include <git-compat-util.h> |
6 | #include <cache.h> | 6 | #include <cache.h> |
7 | #include <grep.h> | 7 | #include <grep.h> |
8 | #include <object.h> | 8 | #include <object.h> |
9 | #include <tree.h> | 9 | #include <tree.h> |
10 | #include <commit.h> | 10 | #include <commit.h> |
11 | #include <tag.h> | 11 | #include <tag.h> |
12 | #include <diff.h> | 12 | #include <diff.h> |
13 | #include <diffcore.h> | 13 | #include <diffcore.h> |
14 | #include <refs.h> | 14 | #include <refs.h> |
15 | #include <revision.h> | 15 | #include <revision.h> |
16 | #include <log-tree.h> | 16 | #include <log-tree.h> |
17 | #include <archive.h> | 17 | #include <archive.h> |
18 | #include <xdiff/xdiff.h> | 18 | #include <xdiff/xdiff.h> |
19 | #include <utf8.h> | 19 | #include <utf8.h> |
20 | 20 | ||
21 | 21 | ||
22 | /* | 22 | /* |
23 | * Dateformats used on misc. pages | 23 | * Dateformats used on misc. pages |
24 | */ | 24 | */ |
25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" | 25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" |
26 | #define FMT_SHORTDATE "%Y-%m-%d" | 26 | #define FMT_SHORTDATE "%Y-%m-%d" |
27 | 27 | ||
28 | 28 | ||
29 | /* | 29 | /* |
30 | * Limits used for relative dates | 30 | * Limits used for relative dates |
31 | */ | 31 | */ |
32 | #define TM_MIN 60 | 32 | #define TM_MIN 60 |
33 | #define TM_HOUR (TM_MIN * 60) | 33 | #define TM_HOUR (TM_MIN * 60) |
34 | #define TM_DAY (TM_HOUR * 24) | 34 | #define TM_DAY (TM_HOUR * 24) |
35 | #define TM_WEEK (TM_DAY * 7) | 35 | #define TM_WEEK (TM_DAY * 7) |
36 | #define TM_YEAR (TM_DAY * 365) | 36 | #define TM_YEAR (TM_DAY * 365) |
37 | #define TM_MONTH (TM_YEAR / 12.0) | 37 | #define TM_MONTH (TM_YEAR / 12.0) |
38 | 38 | ||
39 | 39 | ||
40 | /* | 40 | /* |
41 | * Default encoding | 41 | * Default encoding |
42 | */ | 42 | */ |
43 | #define PAGE_ENCODING "UTF-8" | 43 | #define PAGE_ENCODING "UTF-8" |
44 | 44 | ||
45 | typedef void (*configfn)(const char *name, const char *value); | 45 | typedef void (*configfn)(const char *name, const char *value); |
46 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 46 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
47 | typedef void (*linediff_fn)(char *line, int len); | 47 | typedef void (*linediff_fn)(char *line, int len); |
48 | 48 | ||
49 | struct cgit_repo { | 49 | struct cgit_repo { |
50 | char *url; | 50 | char *url; |
51 | char *name; | 51 | char *name; |
52 | char *path; | 52 | char *path; |
53 | char *desc; | 53 | char *desc; |
54 | char *owner; | 54 | char *owner; |
55 | char *defbranch; | 55 | char *defbranch; |
56 | char *group; | 56 | char *group; |
57 | char *module_link; | 57 | char *module_link; |
58 | char *readme; | 58 | char *readme; |
59 | char *clone_url; | 59 | char *clone_url; |
60 | int snapshots; | 60 | int snapshots; |
61 | int enable_log_filecount; | 61 | int enable_log_filecount; |
62 | int enable_log_linecount; | 62 | int enable_log_linecount; |
63 | }; | 63 | }; |
64 | 64 | ||
65 | struct cgit_repolist { | 65 | struct cgit_repolist { |
66 | int length; | 66 | int length; |
67 | int count; | 67 | int count; |
68 | struct cgit_repo *repos; | 68 | struct cgit_repo *repos; |
69 | }; | 69 | }; |
70 | 70 | ||
71 | struct commitinfo { | 71 | struct commitinfo { |
72 | struct commit *commit; | 72 | struct commit *commit; |
73 | char *author; | 73 | char *author; |
74 | char *author_email; | 74 | char *author_email; |
75 | unsigned long author_date; | 75 | unsigned long author_date; |
76 | char *committer; | 76 | char *committer; |
77 | char *committer_email; | 77 | char *committer_email; |
78 | unsigned long committer_date; | 78 | unsigned long committer_date; |
79 | char *subject; | 79 | char *subject; |
80 | char *msg; | 80 | char *msg; |
81 | char *msg_encoding; | 81 | char *msg_encoding; |
82 | }; | 82 | }; |
83 | 83 | ||
84 | struct taginfo { | 84 | struct taginfo { |
85 | char *tagger; | 85 | char *tagger; |
86 | char *tagger_email; | 86 | char *tagger_email; |
87 | int tagger_date; | 87 | int tagger_date; |
88 | char *msg; | 88 | char *msg; |
89 | }; | 89 | }; |
90 | 90 | ||
91 | struct refinfo { | 91 | struct refinfo { |
92 | const char *refname; | 92 | const char *refname; |
93 | struct object *object; | 93 | struct object *object; |
94 | union { | 94 | union { |
95 | struct taginfo *tag; | 95 | struct taginfo *tag; |
96 | struct commitinfo *commit; | 96 | struct commitinfo *commit; |
97 | }; | 97 | }; |
98 | }; | 98 | }; |
99 | 99 | ||
100 | struct reflist { | 100 | struct reflist { |
101 | struct refinfo **refs; | 101 | struct refinfo **refs; |
102 | int alloc; | 102 | int alloc; |
103 | int count; | 103 | int count; |
104 | }; | 104 | }; |
105 | 105 | ||
106 | struct cgit_query { | 106 | struct cgit_query { |
107 | int has_symref; | 107 | int has_symref; |
108 | int has_sha1; | 108 | int has_sha1; |
109 | char *raw; | 109 | char *raw; |
110 | char *repo; | 110 | char *repo; |
111 | char *page; | 111 | char *page; |
112 | char *search; | 112 | char *search; |
113 | char *grep; | 113 | char *grep; |
114 | char *head; | 114 | char *head; |
115 | char *sha1; | 115 | char *sha1; |
116 | char *sha2; | 116 | char *sha2; |
117 | char *path; | 117 | char *path; |
118 | char *name; | 118 | char *name; |
119 | int ofs; | 119 | int ofs; |
120 | }; | 120 | }; |
121 | 121 | ||
122 | struct cgit_config { | 122 | struct cgit_config { |
123 | char *agefile; | 123 | char *agefile; |
124 | char *cache_root; | 124 | char *cache_root; |
125 | char *clone_prefix; | 125 | char *clone_prefix; |
126 | char *css; | 126 | char *css; |
127 | char *index_header; | 127 | char *index_header; |
128 | char *index_info; | 128 | char *index_info; |
129 | char *logo; | 129 | char *logo; |
130 | char *logo_link; | 130 | char *logo_link; |
131 | char *module_link; | 131 | char *module_link; |
132 | char *repo_group; | 132 | char *repo_group; |
133 | char *robots; | 133 | char *robots; |
134 | char *root_title; | 134 | char *root_title; |
135 | char *script_name; | 135 | char *script_name; |
136 | char *virtual_root; | 136 | char *virtual_root; |
137 | int cache_dynamic_ttl; | 137 | int cache_dynamic_ttl; |
138 | int cache_max_create_time; | 138 | int cache_max_create_time; |
139 | int cache_repo_ttl; | 139 | int cache_repo_ttl; |
140 | int cache_root_ttl; | 140 | int cache_root_ttl; |
141 | int cache_static_ttl; | 141 | int cache_static_ttl; |
142 | int enable_index_links; | 142 | int enable_index_links; |
143 | int enable_log_filecount; | 143 | int enable_log_filecount; |
144 | int enable_log_linecount; | 144 | int enable_log_linecount; |
145 | int max_commit_count; | 145 | int max_commit_count; |
146 | int max_lock_attempts; | 146 | int max_lock_attempts; |
147 | int max_msg_len; | 147 | int max_msg_len; |
148 | int max_repodesc_len; | 148 | int max_repodesc_len; |
149 | int nocache; | 149 | int nocache; |
150 | int renamelimit; | 150 | int renamelimit; |
151 | int snapshots; | 151 | int snapshots; |
152 | int summary_branches; | 152 | int summary_branches; |
153 | int summary_log; | 153 | int summary_log; |
154 | int summary_tags; | 154 | int summary_tags; |
155 | }; | 155 | }; |
156 | 156 | ||
157 | struct cgit_page { | 157 | struct cgit_page { |
158 | time_t modified; | 158 | time_t modified; |
159 | time_t expires; | 159 | time_t expires; |
160 | char *mimetype; | 160 | char *mimetype; |
161 | char *charset; | 161 | char *charset; |
162 | char *filename; | 162 | char *filename; |
163 | char *title; | 163 | char *title; |
164 | }; | 164 | }; |
165 | 165 | ||
166 | struct cgit_context { | 166 | struct cgit_context { |
167 | struct cgit_query qry; | 167 | struct cgit_query qry; |
168 | struct cgit_config cfg; | 168 | struct cgit_config cfg; |
169 | struct cgit_repo *repo; | 169 | struct cgit_repo *repo; |
170 | struct cgit_page page; | 170 | struct cgit_page page; |
171 | }; | 171 | }; |
172 | 172 | ||
173 | struct cgit_snapshot_format { | 173 | struct cgit_snapshot_format { |
174 | const char *suffix; | 174 | const char *suffix; |
175 | const char *mimetype; | 175 | const char *mimetype; |
176 | write_archive_fn_t write_func; | 176 | write_archive_fn_t write_func; |
177 | int bit; | 177 | int bit; |
178 | }; | 178 | }; |
179 | 179 | ||
180 | extern const char *cgit_version; | 180 | extern const char *cgit_version; |
181 | 181 | ||
182 | extern struct cgit_repolist cgit_repolist; | 182 | extern struct cgit_repolist cgit_repolist; |
183 | extern struct cgit_context ctx; | 183 | extern struct cgit_context ctx; |
184 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; | 184 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; |
185 | 185 | ||
186 | extern struct cgit_repo *cgit_add_repo(const char *url); | 186 | extern struct cgit_repo *cgit_add_repo(const char *url); |
187 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); | 187 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); |
188 | extern void cgit_repo_config_cb(const char *name, const char *value); | 188 | extern void cgit_repo_config_cb(const char *name, const char *value); |
189 | 189 | ||
190 | extern int chk_zero(int result, char *msg); | 190 | extern int chk_zero(int result, char *msg); |
191 | extern int chk_positive(int result, char *msg); | 191 | extern int chk_positive(int result, char *msg); |
192 | extern int chk_non_negative(int result, char *msg); | 192 | extern int chk_non_negative(int result, char *msg); |
193 | 193 | ||
194 | extern int hextoint(char c); | 194 | extern int hextoint(char c); |
195 | extern char *trim_end(const char *str, char c); | 195 | extern char *trim_end(const char *str, char c); |
196 | extern char *strlpart(char *txt, int maxlen); | 196 | extern char *strlpart(char *txt, int maxlen); |
197 | extern char *strrpart(char *txt, int maxlen); | 197 | extern char *strrpart(char *txt, int maxlen); |
198 | 198 | ||
199 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 199 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
200 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 200 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
201 | int flags, void *cb_data); | 201 | int flags, void *cb_data); |
202 | 202 | ||
203 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 203 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
204 | 204 | ||
205 | extern int cgit_diff_files(const unsigned char *old_sha1, | 205 | extern int cgit_diff_files(const unsigned char *old_sha1, |
206 | const unsigned char *new_sha1, | 206 | const unsigned char *new_sha1, |
207 | linediff_fn fn); | 207 | linediff_fn fn); |
208 | 208 | ||
209 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 209 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
210 | const unsigned char *new_sha1, | 210 | const unsigned char *new_sha1, |
211 | filepair_fn fn, const char *prefix); | 211 | filepair_fn fn, const char *prefix); |
212 | 212 | ||
213 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 213 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
214 | 214 | ||
215 | extern char *fmt(const char *format,...); | 215 | extern char *fmt(const char *format,...); |
216 | 216 | ||
217 | extern int cgit_read_config(const char *filename, configfn fn); | ||
218 | extern int cgit_parse_query(char *txt, configfn fn); | 217 | extern int cgit_parse_query(char *txt, configfn fn); |
219 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 218 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
220 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 219 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
221 | extern void cgit_parse_url(const char *url); | 220 | extern void cgit_parse_url(const char *url); |
222 | 221 | ||
223 | extern const char *cgit_repobasename(const char *reponame); | 222 | extern const char *cgit_repobasename(const char *reponame); |
224 | 223 | ||
225 | extern int cgit_parse_snapshots_mask(const char *str); | 224 | extern int cgit_parse_snapshots_mask(const char *str); |
226 | 225 | ||
227 | #endif /* CGIT_H */ | 226 | #endif /* CGIT_H */ |
diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..4908058 --- a/dev/null +++ b/configfile.c | |||
@@ -0,0 +1,87 @@ | |||
1 | /* configfile.c: parsing of config files | ||
2 | * | ||
3 | * Copyright (C) 2008 Lars Hjemli | ||
4 | * | ||
5 | * Licensed under GNU General Public License v2 | ||
6 | * (see COPYING for full license text) | ||
7 | */ | ||
8 | |||
9 | #include <ctype.h> | ||
10 | #include <stdio.h> | ||
11 | #include "configfile.h" | ||
12 | |||
13 | int next_char(FILE *f) | ||
14 | { | ||
15 | int c = fgetc(f); | ||
16 | if (c=='\r') { | ||
17 | c = fgetc(f); | ||
18 | if (c!='\n') { | ||
19 | ungetc(c, f); | ||
20 | c = '\r'; | ||
21 | } | ||
22 | } | ||
23 | return c; | ||
24 | } | ||
25 | |||
26 | void skip_line(FILE *f) | ||
27 | { | ||
28 | int c; | ||
29 | |||
30 | while((c=next_char(f)) && c!='\n' && c!=EOF) | ||
31 | ; | ||
32 | } | ||
33 | |||
34 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) | ||
35 | { | ||
36 | int i = 0, isname = 0; | ||
37 | |||
38 | *value = NULL; | ||
39 | while(i<bufsize-1) { | ||
40 | int c = next_char(f); | ||
41 | if (!isname && (c=='#' || c==';')) { | ||
42 | skip_line(f); | ||
43 | continue; | ||
44 | } | ||
45 | if (!isname && isspace(c)) | ||
46 | continue; | ||
47 | |||
48 | if (c=='=' && !*value) { | ||
49 | line[i] = 0; | ||
50 | *value = &line[i+1]; | ||
51 | } else if (c=='\n' && !isname) { | ||
52 | i = 0; | ||
53 | continue; | ||
54 | } else if (c=='\n' || c==EOF) { | ||
55 | line[i] = 0; | ||
56 | break; | ||
57 | } else { | ||
58 | line[i]=c; | ||
59 | } | ||
60 | isname = 1; | ||
61 | i++; | ||
62 | } | ||
63 | line[i+1] = 0; | ||
64 | return i; | ||
65 | } | ||
66 | |||
67 | int parse_configfile(const char *filename, configfile_value_fn fn) | ||
68 | { | ||
69 | static int nesting; | ||
70 | int len; | ||
71 | char line[256]; | ||
72 | const char *value; | ||
73 | FILE *f; | ||
74 | |||
75 | /* cancel deeply nested include-commands */ | ||
76 | if (nesting > 8) | ||
77 | return -1; | ||
78 | if (!(f = fopen(filename, "r"))) | ||
79 | return -1; | ||
80 | nesting++; | ||
81 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | ||
82 | fn(line, value); | ||
83 | nesting--; | ||
84 | fclose(f); | ||
85 | return 0; | ||
86 | } | ||
87 | |||
diff --git a/configfile.h b/configfile.h new file mode 100644 index 0000000..04235e5 --- a/dev/null +++ b/configfile.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef CONFIGFILE_H | ||
2 | #define CONFIGFILE_H | ||
3 | |||
4 | typedef void (*configfile_value_fn)(const char *name, const char *value); | ||
5 | |||
6 | extern int parse_configfile(const char *filename, configfile_value_fn fn); | ||
7 | |||
8 | #endif /* CONFIGFILE_H */ | ||
@@ -1,332 +1,257 @@ | |||
1 | /* config.c: parsing of config files | 1 | /* config.c: parsing of config files |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | int next_char(FILE *f) | ||
12 | { | ||
13 | int c = fgetc(f); | ||
14 | if (c=='\r') { | ||
15 | c = fgetc(f); | ||
16 | if (c!='\n') { | ||
17 | ungetc(c, f); | ||
18 | c = '\r'; | ||
19 | } | ||
20 | } | ||
21 | return c; | ||
22 | } | ||
23 | |||
24 | void skip_line(FILE *f) | ||
25 | { | ||
26 | int c; | ||
27 | |||
28 | while((c=next_char(f)) && c!='\n' && c!=EOF) | ||
29 | ; | ||
30 | } | ||
31 | |||
32 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) | ||
33 | { | ||
34 | int i = 0, isname = 0; | ||
35 | |||
36 | *value = NULL; | ||
37 | while(i<bufsize-1) { | ||
38 | int c = next_char(f); | ||
39 | if (!isname && (c=='#' || c==';')) { | ||
40 | skip_line(f); | ||
41 | continue; | ||
42 | } | ||
43 | if (!isname && isspace(c)) | ||
44 | continue; | ||
45 | |||
46 | if (c=='=' && !*value) { | ||
47 | line[i] = 0; | ||
48 | *value = &line[i+1]; | ||
49 | } else if (c=='\n' && !isname) { | ||
50 | i = 0; | ||
51 | continue; | ||
52 | } else if (c=='\n' || c==EOF) { | ||
53 | line[i] = 0; | ||
54 | break; | ||
55 | } else { | ||
56 | line[i]=c; | ||
57 | } | ||
58 | isname = 1; | ||
59 | i++; | ||
60 | } | ||
61 | line[i+1] = 0; | ||
62 | return i; | ||
63 | } | ||
64 | |||
65 | int cgit_read_config(const char *filename, configfn fn) | ||
66 | { | ||
67 | static int nesting; | ||
68 | int len; | ||
69 | char line[256]; | ||
70 | const char *value; | ||
71 | FILE *f; | ||
72 | |||
73 | /* cancel deeply nested include-commands */ | ||
74 | if (nesting > 8) | ||
75 | return -1; | ||
76 | if (!(f = fopen(filename, "r"))) | ||
77 | return -1; | ||
78 | nesting++; | ||
79 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | ||
80 | (*fn)(line, value); | ||
81 | nesting--; | ||
82 | fclose(f); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | char *convert_query_hexchar(char *txt) | 11 | char *convert_query_hexchar(char *txt) |
87 | { | 12 | { |
88 | int d1, d2; | 13 | int d1, d2; |
89 | if (strlen(txt) < 3) { | 14 | if (strlen(txt) < 3) { |
90 | *txt = '\0'; | 15 | *txt = '\0'; |
91 | return txt-1; | 16 | return txt-1; |
92 | } | 17 | } |
93 | d1 = hextoint(*(txt+1)); | 18 | d1 = hextoint(*(txt+1)); |
94 | d2 = hextoint(*(txt+2)); | 19 | d2 = hextoint(*(txt+2)); |
95 | if (d1<0 || d2<0) { | 20 | if (d1<0 || d2<0) { |
96 | strcpy(txt, txt+3); | 21 | strcpy(txt, txt+3); |
97 | return txt-1; | 22 | return txt-1; |
98 | } else { | 23 | } else { |
99 | *txt = d1 * 16 + d2; | 24 | *txt = d1 * 16 + d2; |
100 | strcpy(txt+1, txt+3); | 25 | strcpy(txt+1, txt+3); |
101 | return txt; | 26 | return txt; |
102 | } | 27 | } |
103 | } | 28 | } |
104 | 29 | ||
105 | int cgit_parse_query(char *txt, configfn fn) | 30 | int cgit_parse_query(char *txt, configfn fn) |
106 | { | 31 | { |
107 | char *t, *value = NULL, c; | 32 | char *t, *value = NULL, c; |
108 | 33 | ||
109 | if (!txt) | 34 | if (!txt) |
110 | return 0; | 35 | return 0; |
111 | 36 | ||
112 | t = txt = xstrdup(txt); | 37 | t = txt = xstrdup(txt); |
113 | 38 | ||
114 | while((c=*t) != '\0') { | 39 | while((c=*t) != '\0') { |
115 | if (c=='=') { | 40 | if (c=='=') { |
116 | *t = '\0'; | 41 | *t = '\0'; |
117 | value = t+1; | 42 | value = t+1; |
118 | } else if (c=='+') { | 43 | } else if (c=='+') { |
119 | *t = ' '; | 44 | *t = ' '; |
120 | } else if (c=='%') { | 45 | } else if (c=='%') { |
121 | t = convert_query_hexchar(t); | 46 | t = convert_query_hexchar(t); |
122 | } else if (c=='&') { | 47 | } else if (c=='&') { |
123 | *t = '\0'; | 48 | *t = '\0'; |
124 | (*fn)(txt, value); | 49 | (*fn)(txt, value); |
125 | txt = t+1; | 50 | txt = t+1; |
126 | value = NULL; | 51 | value = NULL; |
127 | } | 52 | } |
128 | t++; | 53 | t++; |
129 | } | 54 | } |
130 | if (t!=txt) | 55 | if (t!=txt) |
131 | (*fn)(txt, value); | 56 | (*fn)(txt, value); |
132 | return 0; | 57 | return 0; |
133 | } | 58 | } |
134 | 59 | ||
135 | /* | 60 | /* |
136 | * url syntax: [repo ['/' cmd [ '/' path]]] | 61 | * url syntax: [repo ['/' cmd [ '/' path]]] |
137 | * repo: any valid repo url, may contain '/' | 62 | * repo: any valid repo url, may contain '/' |
138 | * cmd: log | commit | diff | tree | view | blob | snapshot | 63 | * cmd: log | commit | diff | tree | view | blob | snapshot |
139 | * path: any valid path, may contain '/' | 64 | * path: any valid path, may contain '/' |
140 | * | 65 | * |
141 | */ | 66 | */ |
142 | void cgit_parse_url(const char *url) | 67 | void cgit_parse_url(const char *url) |
143 | { | 68 | { |
144 | char *cmd, *p; | 69 | char *cmd, *p; |
145 | 70 | ||
146 | ctx.repo = NULL; | 71 | ctx.repo = NULL; |
147 | if (!url || url[0] == '\0') | 72 | if (!url || url[0] == '\0') |
148 | return; | 73 | return; |
149 | 74 | ||
150 | ctx.repo = cgit_get_repoinfo(url); | 75 | ctx.repo = cgit_get_repoinfo(url); |
151 | if (ctx.repo) { | 76 | if (ctx.repo) { |
152 | ctx.qry.repo = ctx.repo->url; | 77 | ctx.qry.repo = ctx.repo->url; |
153 | return; | 78 | return; |
154 | } | 79 | } |
155 | 80 | ||
156 | cmd = strchr(url, '/'); | 81 | cmd = strchr(url, '/'); |
157 | while (!ctx.repo && cmd) { | 82 | while (!ctx.repo && cmd) { |
158 | cmd[0] = '\0'; | 83 | cmd[0] = '\0'; |
159 | ctx.repo = cgit_get_repoinfo(url); | 84 | ctx.repo = cgit_get_repoinfo(url); |
160 | if (ctx.repo == NULL) { | 85 | if (ctx.repo == NULL) { |
161 | cmd[0] = '/'; | 86 | cmd[0] = '/'; |
162 | cmd = strchr(cmd + 1, '/'); | 87 | cmd = strchr(cmd + 1, '/'); |
163 | continue; | 88 | continue; |
164 | } | 89 | } |
165 | 90 | ||
166 | ctx.qry.repo = ctx.repo->url; | 91 | ctx.qry.repo = ctx.repo->url; |
167 | p = strchr(cmd + 1, '/'); | 92 | p = strchr(cmd + 1, '/'); |
168 | if (p) { | 93 | if (p) { |
169 | p[0] = '\0'; | 94 | p[0] = '\0'; |
170 | if (p[1]) | 95 | if (p[1]) |
171 | ctx.qry.path = trim_end(p + 1, '/'); | 96 | ctx.qry.path = trim_end(p + 1, '/'); |
172 | } | 97 | } |
173 | if (cmd[1]) | 98 | if (cmd[1]) |
174 | ctx.qry.page = xstrdup(cmd + 1); | 99 | ctx.qry.page = xstrdup(cmd + 1); |
175 | return; | 100 | return; |
176 | } | 101 | } |
177 | } | 102 | } |
178 | 103 | ||
179 | char *substr(const char *head, const char *tail) | 104 | char *substr(const char *head, const char *tail) |
180 | { | 105 | { |
181 | char *buf; | 106 | char *buf; |
182 | 107 | ||
183 | buf = xmalloc(tail - head + 1); | 108 | buf = xmalloc(tail - head + 1); |
184 | strncpy(buf, head, tail - head); | 109 | strncpy(buf, head, tail - head); |
185 | buf[tail - head] = '\0'; | 110 | buf[tail - head] = '\0'; |
186 | return buf; | 111 | return buf; |
187 | } | 112 | } |
188 | 113 | ||
189 | struct commitinfo *cgit_parse_commit(struct commit *commit) | 114 | struct commitinfo *cgit_parse_commit(struct commit *commit) |
190 | { | 115 | { |
191 | struct commitinfo *ret; | 116 | struct commitinfo *ret; |
192 | char *p = commit->buffer, *t = commit->buffer; | 117 | char *p = commit->buffer, *t = commit->buffer; |
193 | 118 | ||
194 | ret = xmalloc(sizeof(*ret)); | 119 | ret = xmalloc(sizeof(*ret)); |
195 | ret->commit = commit; | 120 | ret->commit = commit; |
196 | ret->author = NULL; | 121 | ret->author = NULL; |
197 | ret->author_email = NULL; | 122 | ret->author_email = NULL; |
198 | ret->committer = NULL; | 123 | ret->committer = NULL; |
199 | ret->committer_email = NULL; | 124 | ret->committer_email = NULL; |
200 | ret->subject = NULL; | 125 | ret->subject = NULL; |
201 | ret->msg = NULL; | 126 | ret->msg = NULL; |
202 | ret->msg_encoding = NULL; | 127 | ret->msg_encoding = NULL; |
203 | 128 | ||
204 | if (p == NULL) | 129 | if (p == NULL) |
205 | return ret; | 130 | return ret; |
206 | 131 | ||
207 | if (strncmp(p, "tree ", 5)) | 132 | if (strncmp(p, "tree ", 5)) |
208 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); | 133 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); |
209 | else | 134 | else |
210 | p += 46; // "tree " + hex[40] + "\n" | 135 | p += 46; // "tree " + hex[40] + "\n" |
211 | 136 | ||
212 | while (!strncmp(p, "parent ", 7)) | 137 | while (!strncmp(p, "parent ", 7)) |
213 | p += 48; // "parent " + hex[40] + "\n" | 138 | p += 48; // "parent " + hex[40] + "\n" |
214 | 139 | ||
215 | if (!strncmp(p, "author ", 7)) { | 140 | if (!strncmp(p, "author ", 7)) { |
216 | p += 7; | 141 | p += 7; |
217 | t = strchr(p, '<') - 1; | 142 | t = strchr(p, '<') - 1; |
218 | ret->author = substr(p, t); | 143 | ret->author = substr(p, t); |
219 | p = t; | 144 | p = t; |
220 | t = strchr(t, '>') + 1; | 145 | t = strchr(t, '>') + 1; |
221 | ret->author_email = substr(p, t); | 146 | ret->author_email = substr(p, t); |
222 | ret->author_date = atol(t+1); | 147 | ret->author_date = atol(t+1); |
223 | p = strchr(t, '\n') + 1; | 148 | p = strchr(t, '\n') + 1; |
224 | } | 149 | } |
225 | 150 | ||
226 | if (!strncmp(p, "committer ", 9)) { | 151 | if (!strncmp(p, "committer ", 9)) { |
227 | p += 9; | 152 | p += 9; |
228 | t = strchr(p, '<') - 1; | 153 | t = strchr(p, '<') - 1; |
229 | ret->committer = substr(p, t); | 154 | ret->committer = substr(p, t); |
230 | p = t; | 155 | p = t; |
231 | t = strchr(t, '>') + 1; | 156 | t = strchr(t, '>') + 1; |
232 | ret->committer_email = substr(p, t); | 157 | ret->committer_email = substr(p, t); |
233 | ret->committer_date = atol(t+1); | 158 | ret->committer_date = atol(t+1); |
234 | p = strchr(t, '\n') + 1; | 159 | p = strchr(t, '\n') + 1; |
235 | } | 160 | } |
236 | 161 | ||
237 | if (!strncmp(p, "encoding ", 9)) { | 162 | if (!strncmp(p, "encoding ", 9)) { |
238 | p += 9; | 163 | p += 9; |
239 | t = strchr(p, '\n') + 1; | 164 | t = strchr(p, '\n') + 1; |
240 | ret->msg_encoding = substr(p, t); | 165 | ret->msg_encoding = substr(p, t); |
241 | p = t; | 166 | p = t; |
242 | } else | 167 | } else |
243 | ret->msg_encoding = xstrdup(PAGE_ENCODING); | 168 | ret->msg_encoding = xstrdup(PAGE_ENCODING); |
244 | 169 | ||
245 | while (*p && (*p != '\n')) | 170 | while (*p && (*p != '\n')) |
246 | p = strchr(p, '\n') + 1; // skip unknown header fields | 171 | p = strchr(p, '\n') + 1; // skip unknown header fields |
247 | 172 | ||
248 | while (*p == '\n') | 173 | while (*p == '\n') |
249 | p = strchr(p, '\n') + 1; | 174 | p = strchr(p, '\n') + 1; |
250 | 175 | ||
251 | t = strchr(p, '\n'); | 176 | t = strchr(p, '\n'); |
252 | if (t) { | 177 | if (t) { |
253 | if (*t == '\0') | 178 | if (*t == '\0') |
254 | ret->subject = "** empty **"; | 179 | ret->subject = "** empty **"; |
255 | else | 180 | else |
256 | ret->subject = substr(p, t); | 181 | ret->subject = substr(p, t); |
257 | p = t + 1; | 182 | p = t + 1; |
258 | 183 | ||
259 | while (*p == '\n') | 184 | while (*p == '\n') |
260 | p = strchr(p, '\n') + 1; | 185 | p = strchr(p, '\n') + 1; |
261 | ret->msg = xstrdup(p); | 186 | ret->msg = xstrdup(p); |
262 | } else | 187 | } else |
263 | ret->subject = substr(p, p+strlen(p)); | 188 | ret->subject = substr(p, p+strlen(p)); |
264 | 189 | ||
265 | if(strcmp(ret->msg_encoding, PAGE_ENCODING)) { | 190 | if(strcmp(ret->msg_encoding, PAGE_ENCODING)) { |
266 | t = reencode_string(ret->subject, PAGE_ENCODING, | 191 | t = reencode_string(ret->subject, PAGE_ENCODING, |
267 | ret->msg_encoding); | 192 | ret->msg_encoding); |
268 | if(t) { | 193 | if(t) { |
269 | free(ret->subject); | 194 | free(ret->subject); |
270 | ret->subject = t; | 195 | ret->subject = t; |
271 | } | 196 | } |
272 | 197 | ||
273 | t = reencode_string(ret->msg, PAGE_ENCODING, | 198 | t = reencode_string(ret->msg, PAGE_ENCODING, |
274 | ret->msg_encoding); | 199 | ret->msg_encoding); |
275 | if(t) { | 200 | if(t) { |
276 | free(ret->msg); | 201 | free(ret->msg); |
277 | ret->msg = t; | 202 | ret->msg = t; |
278 | } | 203 | } |
279 | } | 204 | } |
280 | 205 | ||
281 | return ret; | 206 | return ret; |
282 | } | 207 | } |
283 | 208 | ||
284 | 209 | ||
285 | struct taginfo *cgit_parse_tag(struct tag *tag) | 210 | struct taginfo *cgit_parse_tag(struct tag *tag) |
286 | { | 211 | { |
287 | void *data; | 212 | void *data; |
288 | enum object_type type; | 213 | enum object_type type; |
289 | unsigned long size; | 214 | unsigned long size; |
290 | char *p, *t; | 215 | char *p, *t; |
291 | struct taginfo *ret; | 216 | struct taginfo *ret; |
292 | 217 | ||
293 | data = read_sha1_file(tag->object.sha1, &type, &size); | 218 | data = read_sha1_file(tag->object.sha1, &type, &size); |
294 | if (!data || type != OBJ_TAG) { | 219 | if (!data || type != OBJ_TAG) { |
295 | free(data); | 220 | free(data); |
296 | return 0; | 221 | return 0; |
297 | } | 222 | } |
298 | 223 | ||
299 | ret = xmalloc(sizeof(*ret)); | 224 | ret = xmalloc(sizeof(*ret)); |
300 | ret->tagger = NULL; | 225 | ret->tagger = NULL; |
301 | ret->tagger_email = NULL; | 226 | ret->tagger_email = NULL; |
302 | ret->tagger_date = 0; | 227 | ret->tagger_date = 0; |
303 | ret->msg = NULL; | 228 | ret->msg = NULL; |
304 | 229 | ||
305 | p = data; | 230 | p = data; |
306 | 231 | ||
307 | while (p && *p) { | 232 | while (p && *p) { |
308 | if (*p == '\n') | 233 | if (*p == '\n') |
309 | break; | 234 | break; |
310 | 235 | ||
311 | if (!strncmp(p, "tagger ", 7)) { | 236 | if (!strncmp(p, "tagger ", 7)) { |
312 | p += 7; | 237 | p += 7; |
313 | t = strchr(p, '<') - 1; | 238 | t = strchr(p, '<') - 1; |
314 | ret->tagger = substr(p, t); | 239 | ret->tagger = substr(p, t); |
315 | p = t; | 240 | p = t; |
316 | t = strchr(t, '>') + 1; | 241 | t = strchr(t, '>') + 1; |
317 | ret->tagger_email = substr(p, t); | 242 | ret->tagger_email = substr(p, t); |
318 | ret->tagger_date = atol(t+1); | 243 | ret->tagger_date = atol(t+1); |
319 | } | 244 | } |
320 | p = strchr(p, '\n') + 1; | 245 | p = strchr(p, '\n') + 1; |
321 | } | 246 | } |
322 | 247 | ||
323 | while (p && *p && (*p != '\n')) | 248 | while (p && *p && (*p != '\n')) |
324 | p = strchr(p, '\n') + 1; // skip unknown tag fields | 249 | p = strchr(p, '\n') + 1; // skip unknown tag fields |
325 | 250 | ||
326 | while (p && (*p == '\n')) | 251 | while (p && (*p == '\n')) |
327 | p = strchr(p, '\n') + 1; | 252 | p = strchr(p, '\n') + 1; |
328 | if (p && *p) | 253 | if (p && *p) |
329 | ret->msg = xstrdup(p); | 254 | ret->msg = xstrdup(p); |
330 | free(data); | 255 | free(data); |
331 | return ret; | 256 | return ret; |
332 | } | 257 | } |