summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile4
-rw-r--r--cgit.c2
-rw-r--r--shared.c2
3 files changed, 5 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index faf7b98..c9be8fa 100644
--- a/Makefile
+++ b/Makefile
@@ -1,107 +1,109 @@
1CGIT_VERSION = v0.7.2 1CGIT_VERSION = v0.7.2
2CGIT_SCRIPT_NAME = cgit.cgi 2CGIT_SCRIPT_NAME = cgit.cgi
3CGIT_SCRIPT_PATH = /var/www/htdocs/cgit 3CGIT_SCRIPT_PATH = /var/www/htdocs/cgit
4CGIT_CONFIG = /etc/cgitrc 4CGIT_CONFIG = /etc/cgitrc
5CACHE_ROOT = /var/cache/cgit 5CACHE_ROOT = /var/cache/cgit
6SHA1_HEADER = <openssl/sha.h> 6SHA1_HEADER = <openssl/sha.h>
7GIT_VER = 1.5.4.1 7GIT_VER = 1.5.4.1
8GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 8GIT_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# 16#
17# Define a pattern rule for automatic dependency building 17# Define a pattern rule for automatic dependency building
18# 18#
19%.d: %.c 19%.d: %.c
20 $(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ 20 $(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@
21 21
22 22
23EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto 23EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
24OBJECTS = 24OBJECTS =
25OBJECTS += cache.o 25OBJECTS += cache.o
26OBJECTS += cgit.o 26OBJECTS += cgit.o
27OBJECTS += cmd.o 27OBJECTS += cmd.o
28OBJECTS += html.o 28OBJECTS += html.o
29OBJECTS += parsing.o 29OBJECTS += parsing.o
30OBJECTS += shared.o 30OBJECTS += shared.o
31OBJECTS += ui-blob.o 31OBJECTS += ui-blob.o
32OBJECTS += ui-commit.o 32OBJECTS += ui-commit.o
33OBJECTS += ui-diff.o 33OBJECTS += ui-diff.o
34OBJECTS += ui-log.o 34OBJECTS += ui-log.o
35OBJECTS += ui-patch.o 35OBJECTS += ui-patch.o
36OBJECTS += ui-refs.o 36OBJECTS += ui-refs.o
37OBJECTS += ui-repolist.o 37OBJECTS += ui-repolist.o
38OBJECTS += ui-shared.o 38OBJECTS += ui-shared.o
39OBJECTS += ui-snapshot.o 39OBJECTS += ui-snapshot.o
40OBJECTS += ui-summary.o 40OBJECTS += ui-summary.o
41OBJECTS += ui-tag.o 41OBJECTS += ui-tag.o
42OBJECTS += ui-tree.o 42OBJECTS += ui-tree.o
43 43
44ifdef NEEDS_LIBICONV 44ifdef NEEDS_LIBICONV
45 EXTLIBS += -liconv 45 EXTLIBS += -liconv
46endif 46endif
47 47
48 48
49.PHONY: all git test install clean distclean emptycache force-version get-git 49.PHONY: all git test install clean distclean emptycache force-version get-git
50 50
51all: cgit git 51all: cgit git
52 52
53VERSION: force-version 53VERSION: force-version
54 @./gen-version.sh "$(CGIT_VERSION)" 54 @./gen-version.sh "$(CGIT_VERSION)"
55-include VERSION 55-include VERSION
56 56
57 57
58CFLAGS += -g -Wall -Igit 58CFLAGS += -g -Wall -Igit
59CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' 59CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)'
60CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' 60CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"'
61CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' 61CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
62CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' 62CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
63CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' 63CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
64 64
65 65
66cgit: $(OBJECTS) 66cgit: $(OBJECTS)
67 $(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) 67 $(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
68 68
69$(OBJECTS): git/xdiff/lib.a git/libgit.a VERSION 69$(OBJECTS): git/xdiff/lib.a git/libgit.a
70
71cgit.o: VERSION
70 72
71-include $(OBJECTS:.o=.d) 73-include $(OBJECTS:.o=.d)
72 74
73git/xdiff/lib.a: | git 75git/xdiff/lib.a: | git
74 76
75git/libgit.a: | git 77git/libgit.a: | git
76 78
77git: 79git:
78 cd git && $(MAKE) xdiff/lib.a 80 cd git && $(MAKE) xdiff/lib.a
79 cd git && $(MAKE) libgit.a 81 cd git && $(MAKE) libgit.a
80 82
81test: all 83test: all
82 $(MAKE) -C tests 84 $(MAKE) -C tests
83 85
84install: all 86install: all
85 mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) 87 mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH)
86 install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) 88 install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME)
87 install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css 89 install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css
88 install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png 90 install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png
89 91
90uninstall: 92uninstall:
91 rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) 93 rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME)
92 rm -f $(CGIT_SCRIPT_PATH)/cgit.css 94 rm -f $(CGIT_SCRIPT_PATH)/cgit.css
93 rm -f $(CGIT_SCRIPT_PATH)/cgit.png 95 rm -f $(CGIT_SCRIPT_PATH)/cgit.png
94 96
95clean: 97clean:
96 rm -f cgit VERSION *.o *.d 98 rm -f cgit VERSION *.o *.d
97 cd git && $(MAKE) clean 99 cd git && $(MAKE) clean
98 100
99distclean: clean 101distclean: clean
100 git clean -d -x 102 git clean -d -x
101 cd git && git clean -d -x 103 cd git && git clean -d -x
102 104
103emptycache: 105emptycache:
104 rm -rf $(DESTDIR)$(CACHE_ROOT)/* 106 rm -rf $(DESTDIR)$(CACHE_ROOT)/*
105 107
106get-git: 108get-git:
107 curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git 109 curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git
diff --git a/cgit.c b/cgit.c
index b3dd119..3943a0f 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,396 +1,398 @@
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 "cmd.h" 10#include "cmd.h"
11#include "ui-shared.h" 11#include "ui-shared.h"
12 12
13const char *cgit_version = CGIT_VERSION;
14
13void config_cb(const char *name, const char *value) 15void config_cb(const char *name, const char *value)
14{ 16{
15 if (!strcmp(name, "root-title")) 17 if (!strcmp(name, "root-title"))
16 ctx.cfg.root_title = xstrdup(value); 18 ctx.cfg.root_title = xstrdup(value);
17 else if (!strcmp(name, "css")) 19 else if (!strcmp(name, "css"))
18 ctx.cfg.css = xstrdup(value); 20 ctx.cfg.css = xstrdup(value);
19 else if (!strcmp(name, "logo")) 21 else if (!strcmp(name, "logo"))
20 ctx.cfg.logo = xstrdup(value); 22 ctx.cfg.logo = xstrdup(value);
21 else if (!strcmp(name, "index-header")) 23 else if (!strcmp(name, "index-header"))
22 ctx.cfg.index_header = xstrdup(value); 24 ctx.cfg.index_header = xstrdup(value);
23 else if (!strcmp(name, "index-info")) 25 else if (!strcmp(name, "index-info"))
24 ctx.cfg.index_info = xstrdup(value); 26 ctx.cfg.index_info = xstrdup(value);
25 else if (!strcmp(name, "logo-link")) 27 else if (!strcmp(name, "logo-link"))
26 ctx.cfg.logo_link = xstrdup(value); 28 ctx.cfg.logo_link = xstrdup(value);
27 else if (!strcmp(name, "module-link")) 29 else if (!strcmp(name, "module-link"))
28 ctx.cfg.module_link = xstrdup(value); 30 ctx.cfg.module_link = xstrdup(value);
29 else if (!strcmp(name, "virtual-root")) { 31 else if (!strcmp(name, "virtual-root")) {
30 ctx.cfg.virtual_root = trim_end(value, '/'); 32 ctx.cfg.virtual_root = trim_end(value, '/');
31 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 33 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
32 ctx.cfg.virtual_root = ""; 34 ctx.cfg.virtual_root = "";
33 } else if (!strcmp(name, "nocache")) 35 } else if (!strcmp(name, "nocache"))
34 ctx.cfg.nocache = atoi(value); 36 ctx.cfg.nocache = atoi(value);
35 else if (!strcmp(name, "snapshots")) 37 else if (!strcmp(name, "snapshots"))
36 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 38 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
37 else if (!strcmp(name, "enable-index-links")) 39 else if (!strcmp(name, "enable-index-links"))
38 ctx.cfg.enable_index_links = atoi(value); 40 ctx.cfg.enable_index_links = atoi(value);
39 else if (!strcmp(name, "enable-log-filecount")) 41 else if (!strcmp(name, "enable-log-filecount"))
40 ctx.cfg.enable_log_filecount = atoi(value); 42 ctx.cfg.enable_log_filecount = atoi(value);
41 else if (!strcmp(name, "enable-log-linecount")) 43 else if (!strcmp(name, "enable-log-linecount"))
42 ctx.cfg.enable_log_linecount = atoi(value); 44 ctx.cfg.enable_log_linecount = atoi(value);
43 else if (!strcmp(name, "cache-root")) 45 else if (!strcmp(name, "cache-root"))
44 ctx.cfg.cache_root = xstrdup(value); 46 ctx.cfg.cache_root = xstrdup(value);
45 else if (!strcmp(name, "cache-root-ttl")) 47 else if (!strcmp(name, "cache-root-ttl"))
46 ctx.cfg.cache_root_ttl = atoi(value); 48 ctx.cfg.cache_root_ttl = atoi(value);
47 else if (!strcmp(name, "cache-repo-ttl")) 49 else if (!strcmp(name, "cache-repo-ttl"))
48 ctx.cfg.cache_repo_ttl = atoi(value); 50 ctx.cfg.cache_repo_ttl = atoi(value);
49 else if (!strcmp(name, "cache-static-ttl")) 51 else if (!strcmp(name, "cache-static-ttl"))
50 ctx.cfg.cache_static_ttl = atoi(value); 52 ctx.cfg.cache_static_ttl = atoi(value);
51 else if (!strcmp(name, "cache-dynamic-ttl")) 53 else if (!strcmp(name, "cache-dynamic-ttl"))
52 ctx.cfg.cache_dynamic_ttl = atoi(value); 54 ctx.cfg.cache_dynamic_ttl = atoi(value);
53 else if (!strcmp(name, "max-message-length")) 55 else if (!strcmp(name, "max-message-length"))
54 ctx.cfg.max_msg_len = atoi(value); 56 ctx.cfg.max_msg_len = atoi(value);
55 else if (!strcmp(name, "max-repodesc-length")) 57 else if (!strcmp(name, "max-repodesc-length"))
56 ctx.cfg.max_repodesc_len = atoi(value); 58 ctx.cfg.max_repodesc_len = atoi(value);
57 else if (!strcmp(name, "max-commit-count")) 59 else if (!strcmp(name, "max-commit-count"))
58 ctx.cfg.max_commit_count = atoi(value); 60 ctx.cfg.max_commit_count = atoi(value);
59 else if (!strcmp(name, "summary-log")) 61 else if (!strcmp(name, "summary-log"))
60 ctx.cfg.summary_log = atoi(value); 62 ctx.cfg.summary_log = atoi(value);
61 else if (!strcmp(name, "summary-branches")) 63 else if (!strcmp(name, "summary-branches"))
62 ctx.cfg.summary_branches = atoi(value); 64 ctx.cfg.summary_branches = atoi(value);
63 else if (!strcmp(name, "summary-tags")) 65 else if (!strcmp(name, "summary-tags"))
64 ctx.cfg.summary_tags = atoi(value); 66 ctx.cfg.summary_tags = atoi(value);
65 else if (!strcmp(name, "agefile")) 67 else if (!strcmp(name, "agefile"))
66 ctx.cfg.agefile = xstrdup(value); 68 ctx.cfg.agefile = xstrdup(value);
67 else if (!strcmp(name, "renamelimit")) 69 else if (!strcmp(name, "renamelimit"))
68 ctx.cfg.renamelimit = atoi(value); 70 ctx.cfg.renamelimit = atoi(value);
69 else if (!strcmp(name, "robots")) 71 else if (!strcmp(name, "robots"))
70 ctx.cfg.robots = xstrdup(value); 72 ctx.cfg.robots = xstrdup(value);
71 else if (!strcmp(name, "clone-prefix")) 73 else if (!strcmp(name, "clone-prefix"))
72 ctx.cfg.clone_prefix = xstrdup(value); 74 ctx.cfg.clone_prefix = xstrdup(value);
73 else if (!strcmp(name, "repo.group")) 75 else if (!strcmp(name, "repo.group"))
74 ctx.cfg.repo_group = xstrdup(value); 76 ctx.cfg.repo_group = xstrdup(value);
75 else if (!strcmp(name, "repo.url")) 77 else if (!strcmp(name, "repo.url"))
76 ctx.repo = cgit_add_repo(value); 78 ctx.repo = cgit_add_repo(value);
77 else if (!strcmp(name, "repo.name")) 79 else if (!strcmp(name, "repo.name"))
78 ctx.repo->name = xstrdup(value); 80 ctx.repo->name = xstrdup(value);
79 else if (ctx.repo && !strcmp(name, "repo.path")) 81 else if (ctx.repo && !strcmp(name, "repo.path"))
80 ctx.repo->path = trim_end(value, '/'); 82 ctx.repo->path = trim_end(value, '/');
81 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 83 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
82 ctx.repo->clone_url = xstrdup(value); 84 ctx.repo->clone_url = xstrdup(value);
83 else if (ctx.repo && !strcmp(name, "repo.desc")) 85 else if (ctx.repo && !strcmp(name, "repo.desc"))
84 ctx.repo->desc = xstrdup(value); 86 ctx.repo->desc = xstrdup(value);
85 else if (ctx.repo && !strcmp(name, "repo.owner")) 87 else if (ctx.repo && !strcmp(name, "repo.owner"))
86 ctx.repo->owner = xstrdup(value); 88 ctx.repo->owner = xstrdup(value);
87 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 89 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
88 ctx.repo->defbranch = xstrdup(value); 90 ctx.repo->defbranch = xstrdup(value);
89 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 91 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
90 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 92 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
91 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 93 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
92 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 94 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
93 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 95 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
94 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 96 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
95 else if (ctx.repo && !strcmp(name, "repo.module-link")) 97 else if (ctx.repo && !strcmp(name, "repo.module-link"))
96 ctx.repo->module_link= xstrdup(value); 98 ctx.repo->module_link= xstrdup(value);
97 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 99 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
98 if (*value == '/') 100 if (*value == '/')
99 ctx.repo->readme = xstrdup(value); 101 ctx.repo->readme = xstrdup(value);
100 else 102 else
101 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 103 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
102 } else if (!strcmp(name, "include")) 104 } else if (!strcmp(name, "include"))
103 cgit_read_config(value, config_cb); 105 cgit_read_config(value, config_cb);
104} 106}
105 107
106static void querystring_cb(const char *name, const char *value) 108static void querystring_cb(const char *name, const char *value)
107{ 109{
108 if (!strcmp(name,"r")) { 110 if (!strcmp(name,"r")) {
109 ctx.qry.repo = xstrdup(value); 111 ctx.qry.repo = xstrdup(value);
110 ctx.repo = cgit_get_repoinfo(value); 112 ctx.repo = cgit_get_repoinfo(value);
111 } else if (!strcmp(name, "p")) { 113 } else if (!strcmp(name, "p")) {
112 ctx.qry.page = xstrdup(value); 114 ctx.qry.page = xstrdup(value);
113 } else if (!strcmp(name, "url")) { 115 } else if (!strcmp(name, "url")) {
114 cgit_parse_url(value); 116 cgit_parse_url(value);
115 } else if (!strcmp(name, "qt")) { 117 } else if (!strcmp(name, "qt")) {
116 ctx.qry.grep = xstrdup(value); 118 ctx.qry.grep = xstrdup(value);
117 } else if (!strcmp(name, "q")) { 119 } else if (!strcmp(name, "q")) {
118 ctx.qry.search = xstrdup(value); 120 ctx.qry.search = xstrdup(value);
119 } else if (!strcmp(name, "h")) { 121 } else if (!strcmp(name, "h")) {
120 ctx.qry.head = xstrdup(value); 122 ctx.qry.head = xstrdup(value);
121 ctx.qry.has_symref = 1; 123 ctx.qry.has_symref = 1;
122 } else if (!strcmp(name, "id")) { 124 } else if (!strcmp(name, "id")) {
123 ctx.qry.sha1 = xstrdup(value); 125 ctx.qry.sha1 = xstrdup(value);
124 ctx.qry.has_sha1 = 1; 126 ctx.qry.has_sha1 = 1;
125 } else if (!strcmp(name, "id2")) { 127 } else if (!strcmp(name, "id2")) {
126 ctx.qry.sha2 = xstrdup(value); 128 ctx.qry.sha2 = xstrdup(value);
127 ctx.qry.has_sha1 = 1; 129 ctx.qry.has_sha1 = 1;
128 } else if (!strcmp(name, "ofs")) { 130 } else if (!strcmp(name, "ofs")) {
129 ctx.qry.ofs = atoi(value); 131 ctx.qry.ofs = atoi(value);
130 } else if (!strcmp(name, "path")) { 132 } else if (!strcmp(name, "path")) {
131 ctx.qry.path = trim_end(value, '/'); 133 ctx.qry.path = trim_end(value, '/');
132 } else if (!strcmp(name, "name")) { 134 } else if (!strcmp(name, "name")) {
133 ctx.qry.name = xstrdup(value); 135 ctx.qry.name = xstrdup(value);
134 } 136 }
135} 137}
136 138
137static void prepare_context(struct cgit_context *ctx) 139static void prepare_context(struct cgit_context *ctx)
138{ 140{
139 memset(ctx, 0, sizeof(ctx)); 141 memset(ctx, 0, sizeof(ctx));
140 ctx->cfg.agefile = "info/web/last-modified"; 142 ctx->cfg.agefile = "info/web/last-modified";
141 ctx->cfg.cache_dynamic_ttl = 5; 143 ctx->cfg.cache_dynamic_ttl = 5;
142 ctx->cfg.cache_max_create_time = 5; 144 ctx->cfg.cache_max_create_time = 5;
143 ctx->cfg.cache_repo_ttl = 5; 145 ctx->cfg.cache_repo_ttl = 5;
144 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 146 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
145 ctx->cfg.cache_root_ttl = 5; 147 ctx->cfg.cache_root_ttl = 5;
146 ctx->cfg.cache_static_ttl = -1; 148 ctx->cfg.cache_static_ttl = -1;
147 ctx->cfg.css = "/cgit.css"; 149 ctx->cfg.css = "/cgit.css";
148 ctx->cfg.logo = "/git-logo.png"; 150 ctx->cfg.logo = "/git-logo.png";
149 ctx->cfg.max_commit_count = 50; 151 ctx->cfg.max_commit_count = 50;
150 ctx->cfg.max_lock_attempts = 5; 152 ctx->cfg.max_lock_attempts = 5;
151 ctx->cfg.max_msg_len = 60; 153 ctx->cfg.max_msg_len = 60;
152 ctx->cfg.max_repodesc_len = 60; 154 ctx->cfg.max_repodesc_len = 60;
153 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 155 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
154 ctx->cfg.renamelimit = -1; 156 ctx->cfg.renamelimit = -1;
155 ctx->cfg.robots = "index, nofollow"; 157 ctx->cfg.robots = "index, nofollow";
156 ctx->cfg.root_title = "Git repository browser"; 158 ctx->cfg.root_title = "Git repository browser";
157 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 159 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
158 ctx->page.mimetype = "text/html"; 160 ctx->page.mimetype = "text/html";
159 ctx->page.charset = PAGE_ENCODING; 161 ctx->page.charset = PAGE_ENCODING;
160 ctx->page.filename = NULL; 162 ctx->page.filename = NULL;
161} 163}
162 164
163static int cgit_prepare_cache(struct cacheitem *item) 165static int cgit_prepare_cache(struct cacheitem *item)
164{ 166{
165 if (!ctx.repo && ctx.qry.repo) { 167 if (!ctx.repo && ctx.qry.repo) {
166 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, 168 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
167 "Bad request"); 169 "Bad request");
168 cgit_print_http_headers(&ctx); 170 cgit_print_http_headers(&ctx);
169 cgit_print_docstart(&ctx); 171 cgit_print_docstart(&ctx);
170 cgit_print_pageheader(&ctx); 172 cgit_print_pageheader(&ctx);
171 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 173 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
172 cgit_print_docend(); 174 cgit_print_docend();
173 return 0; 175 return 0;
174 } 176 }
175 177
176 if (!ctx.repo) { 178 if (!ctx.repo) {
177 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); 179 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
178 item->ttl = ctx.cfg.cache_root_ttl; 180 item->ttl = ctx.cfg.cache_root_ttl;
179 return 1; 181 return 1;
180 } 182 }
181 183
182 if (!cgit_cmd) { 184 if (!cgit_cmd) {
183 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, 185 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
184 cache_safe_filename(ctx.repo->url), 186 cache_safe_filename(ctx.repo->url),
185 cache_safe_filename(ctx.qry.raw))); 187 cache_safe_filename(ctx.qry.raw)));
186 item->ttl = ctx.cfg.cache_repo_ttl; 188 item->ttl = ctx.cfg.cache_repo_ttl;
187 } else { 189 } else {
188 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, 190 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
189 cache_safe_filename(ctx.repo->url), 191 cache_safe_filename(ctx.repo->url),
190 ctx.qry.page, 192 ctx.qry.page,
191 cache_safe_filename(ctx.qry.raw))); 193 cache_safe_filename(ctx.qry.raw)));
192 if (ctx.qry.has_symref) 194 if (ctx.qry.has_symref)
193 item->ttl = ctx.cfg.cache_dynamic_ttl; 195 item->ttl = ctx.cfg.cache_dynamic_ttl;
194 else if (ctx.qry.has_sha1) 196 else if (ctx.qry.has_sha1)
195 item->ttl = ctx.cfg.cache_static_ttl; 197 item->ttl = ctx.cfg.cache_static_ttl;
196 else 198 else
197 item->ttl = ctx.cfg.cache_repo_ttl; 199 item->ttl = ctx.cfg.cache_repo_ttl;
198 } 200 }
199 return 1; 201 return 1;
200} 202}
201 203
202struct refmatch { 204struct refmatch {
203 char *req_ref; 205 char *req_ref;
204 char *first_ref; 206 char *first_ref;
205 int match; 207 int match;
206}; 208};
207 209
208int find_current_ref(const char *refname, const unsigned char *sha1, 210int find_current_ref(const char *refname, const unsigned char *sha1,
209 int flags, void *cb_data) 211 int flags, void *cb_data)
210{ 212{
211 struct refmatch *info; 213 struct refmatch *info;
212 214
213 info = (struct refmatch *)cb_data; 215 info = (struct refmatch *)cb_data;
214 if (!strcmp(refname, info->req_ref)) 216 if (!strcmp(refname, info->req_ref))
215 info->match = 1; 217 info->match = 1;
216 if (!info->first_ref) 218 if (!info->first_ref)
217 info->first_ref = xstrdup(refname); 219 info->first_ref = xstrdup(refname);
218 return info->match; 220 return info->match;
219} 221}
220 222
221char *find_default_branch(struct cgit_repo *repo) 223char *find_default_branch(struct cgit_repo *repo)
222{ 224{
223 struct refmatch info; 225 struct refmatch info;
224 226
225 info.req_ref = repo->defbranch; 227 info.req_ref = repo->defbranch;
226 info.first_ref = NULL; 228 info.first_ref = NULL;
227 info.match = 0; 229 info.match = 0;
228 for_each_branch_ref(find_current_ref, &info); 230 for_each_branch_ref(find_current_ref, &info);
229 if (info.match) 231 if (info.match)
230 return info.req_ref; 232 return info.req_ref;
231 else 233 else
232 return info.first_ref; 234 return info.first_ref;
233} 235}
234 236
235static int prepare_repo_cmd(struct cgit_context *ctx) 237static int prepare_repo_cmd(struct cgit_context *ctx)
236{ 238{
237 char *tmp; 239 char *tmp;
238 unsigned char sha1[20]; 240 unsigned char sha1[20];
239 int nongit = 0; 241 int nongit = 0;
240 242
241 setenv("GIT_DIR", ctx->repo->path, 1); 243 setenv("GIT_DIR", ctx->repo->path, 1);
242 setup_git_directory_gently(&nongit); 244 setup_git_directory_gently(&nongit);
243 if (nongit) { 245 if (nongit) {
244 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 246 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
245 "config error"); 247 "config error");
246 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 248 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
247 ctx->repo = NULL; 249 ctx->repo = NULL;
248 cgit_print_http_headers(ctx); 250 cgit_print_http_headers(ctx);
249 cgit_print_docstart(ctx); 251 cgit_print_docstart(ctx);
250 cgit_print_pageheader(ctx); 252 cgit_print_pageheader(ctx);
251 cgit_print_error(tmp); 253 cgit_print_error(tmp);
252 cgit_print_docend(); 254 cgit_print_docend();
253 return 1; 255 return 1;
254 } 256 }
255 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 257 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
256 258
257 if (!ctx->qry.head) { 259 if (!ctx->qry.head) {
258 ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); 260 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
259 ctx->repo->defbranch = ctx->qry.head; 261 ctx->repo->defbranch = ctx->qry.head;
260 } 262 }
261 263
262 if (!ctx->qry.head) { 264 if (!ctx->qry.head) {
263 cgit_print_http_headers(ctx); 265 cgit_print_http_headers(ctx);
264 cgit_print_docstart(ctx); 266 cgit_print_docstart(ctx);
265 cgit_print_pageheader(ctx); 267 cgit_print_pageheader(ctx);
266 cgit_print_error("Repository seems to be empty"); 268 cgit_print_error("Repository seems to be empty");
267 cgit_print_docend(); 269 cgit_print_docend();
268 return 1; 270 return 1;
269 } 271 }
270 272
271 if (get_sha1(ctx->qry.head, sha1)) { 273 if (get_sha1(ctx->qry.head, sha1)) {
272 tmp = xstrdup(ctx->qry.head); 274 tmp = xstrdup(ctx->qry.head);
273 ctx->qry.head = ctx->repo->defbranch; 275 ctx->qry.head = ctx->repo->defbranch;
274 cgit_print_http_headers(ctx); 276 cgit_print_http_headers(ctx);
275 cgit_print_docstart(ctx); 277 cgit_print_docstart(ctx);
276 cgit_print_pageheader(ctx); 278 cgit_print_pageheader(ctx);
277 cgit_print_error(fmt("Invalid branch: %s", tmp)); 279 cgit_print_error(fmt("Invalid branch: %s", tmp));
278 cgit_print_docend(); 280 cgit_print_docend();
279 return 1; 281 return 1;
280 } 282 }
281 return 0; 283 return 0;
282} 284}
283 285
284static void process_request(struct cgit_context *ctx) 286static void process_request(struct cgit_context *ctx)
285{ 287{
286 struct cgit_cmd *cmd; 288 struct cgit_cmd *cmd;
287 289
288 cmd = cgit_get_cmd(ctx); 290 cmd = cgit_get_cmd(ctx);
289 if (!cmd) { 291 if (!cmd) {
290 ctx->page.title = "cgit error"; 292 ctx->page.title = "cgit error";
291 ctx->repo = NULL; 293 ctx->repo = NULL;
292 cgit_print_http_headers(ctx); 294 cgit_print_http_headers(ctx);
293 cgit_print_docstart(ctx); 295 cgit_print_docstart(ctx);
294 cgit_print_pageheader(ctx); 296 cgit_print_pageheader(ctx);
295 cgit_print_error("Invalid request"); 297 cgit_print_error("Invalid request");
296 cgit_print_docend(); 298 cgit_print_docend();
297 return; 299 return;
298 } 300 }
299 301
300 if (cmd->want_repo && prepare_repo_cmd(ctx)) 302 if (cmd->want_repo && prepare_repo_cmd(ctx))
301 return; 303 return;
302 304
303 if (cmd->want_layout) { 305 if (cmd->want_layout) {
304 cgit_print_http_headers(ctx); 306 cgit_print_http_headers(ctx);
305 cgit_print_docstart(ctx); 307 cgit_print_docstart(ctx);
306 cgit_print_pageheader(ctx); 308 cgit_print_pageheader(ctx);
307 } 309 }
308 310
309 cmd->fn(ctx); 311 cmd->fn(ctx);
310 312
311 if (cmd->want_layout) 313 if (cmd->want_layout)
312 cgit_print_docend(); 314 cgit_print_docend();
313} 315}
314 316
315static long ttl_seconds(long ttl) 317static long ttl_seconds(long ttl)
316{ 318{
317 if (ttl<0) 319 if (ttl<0)
318 return 60 * 60 * 24 * 365; 320 return 60 * 60 * 24 * 365;
319 else 321 else
320 return ttl * 60; 322 return ttl * 60;
321} 323}
322 324
323static void cgit_fill_cache(struct cacheitem *item, int use_cache) 325static void cgit_fill_cache(struct cacheitem *item, int use_cache)
324{ 326{
325 int stdout2; 327 int stdout2;
326 328
327 if (use_cache) { 329 if (use_cache) {
328 stdout2 = chk_positive(dup(STDOUT_FILENO), 330 stdout2 = chk_positive(dup(STDOUT_FILENO),
329 "Preserving STDOUT"); 331 "Preserving STDOUT");
330 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 332 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
331 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 333 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
332 } 334 }
333 335
334 ctx.page.modified = time(NULL); 336 ctx.page.modified = time(NULL);
335 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); 337 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
336 process_request(&ctx); 338 process_request(&ctx);
337 339
338 if (use_cache) { 340 if (use_cache) {
339 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 341 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
340 chk_positive(dup2(stdout2, STDOUT_FILENO), 342 chk_positive(dup2(stdout2, STDOUT_FILENO),
341 "Restoring original STDOUT"); 343 "Restoring original STDOUT");
342 chk_zero(close(stdout2), "Closing temporary STDOUT"); 344 chk_zero(close(stdout2), "Closing temporary STDOUT");
343 } 345 }
344} 346}
345 347
346static void cgit_check_cache(struct cacheitem *item) 348static void cgit_check_cache(struct cacheitem *item)
347{ 349{
348 int i = 0; 350 int i = 0;
349 351
350 top: 352 top:
351 if (++i > ctx.cfg.max_lock_attempts) { 353 if (++i > ctx.cfg.max_lock_attempts) {
352 die("cgit_refresh_cache: unable to lock %s: %s", 354 die("cgit_refresh_cache: unable to lock %s: %s",
353 item->name, strerror(errno)); 355 item->name, strerror(errno));
354 } 356 }
355 if (!cache_exist(item)) { 357 if (!cache_exist(item)) {
356 if (!cache_lock(item)) { 358 if (!cache_lock(item)) {
357 sleep(1); 359 sleep(1);
358 goto top; 360 goto top;
359 } 361 }
360 if (!cache_exist(item)) { 362 if (!cache_exist(item)) {
361 cgit_fill_cache(item, 1); 363 cgit_fill_cache(item, 1);
362 cache_unlock(item); 364 cache_unlock(item);
363 } else { 365 } else {
364 cache_cancel_lock(item); 366 cache_cancel_lock(item);
365 } 367 }
366 } else if (cache_expired(item) && cache_lock(item)) { 368 } else if (cache_expired(item) && cache_lock(item)) {
367 if (cache_expired(item)) { 369 if (cache_expired(item)) {
368 cgit_fill_cache(item, 1); 370 cgit_fill_cache(item, 1);
369 cache_unlock(item); 371 cache_unlock(item);
370 } else { 372 } else {
371 cache_cancel_lock(item); 373 cache_cancel_lock(item);
372 } 374 }
373 } 375 }
374} 376}
375 377
376static void cgit_print_cache(struct cacheitem *item) 378static void cgit_print_cache(struct cacheitem *item)
377{ 379{
378 static char buf[4096]; 380 static char buf[4096];
379 ssize_t i; 381 ssize_t i;
380 382
381 int fd = open(item->name, O_RDONLY); 383 int fd = open(item->name, O_RDONLY);
382 if (fd<0) 384 if (fd<0)
383 die("Unable to open cached file %s", item->name); 385 die("Unable to open cached file %s", item->name);
384 386
385 while((i=read(fd, buf, sizeof(buf))) > 0) 387 while((i=read(fd, buf, sizeof(buf))) > 0)
386 write(STDOUT_FILENO, buf, i); 388 write(STDOUT_FILENO, buf, i);
387 389
388 close(fd); 390 close(fd);
389} 391}
390 392
391static void cgit_parse_args(int argc, const char **argv) 393static void cgit_parse_args(int argc, const char **argv)
392{ 394{
393 int i; 395 int i;
394 396
395 for (i = 1; i < argc; i++) { 397 for (i = 1; i < argc; i++) {
396 if (!strncmp(argv[i], "--cache=", 8)) { 398 if (!strncmp(argv[i], "--cache=", 8)) {
diff --git a/shared.c b/shared.c
index cd60da5..48002ac 100644
--- a/shared.c
+++ b/shared.c
@@ -1,358 +1,356 @@
1/* shared.c: global vars + some callback functions 1/* shared.c: global vars + some callback functions
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
11struct cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd; 13int cgit_cmd;
14 14
15const char *cgit_version = CGIT_VERSION;
16
17int chk_zero(int result, char *msg) 15int chk_zero(int result, char *msg)
18{ 16{
19 if (result != 0) 17 if (result != 0)
20 die("%s: %s", msg, strerror(errno)); 18 die("%s: %s", msg, strerror(errno));
21 return result; 19 return result;
22} 20}
23 21
24int chk_positive(int result, char *msg) 22int chk_positive(int result, char *msg)
25{ 23{
26 if (result <= 0) 24 if (result <= 0)
27 die("%s: %s", msg, strerror(errno)); 25 die("%s: %s", msg, strerror(errno));
28 return result; 26 return result;
29} 27}
30 28
31int chk_non_negative(int result, char *msg) 29int chk_non_negative(int result, char *msg)
32{ 30{
33 if (result < 0) 31 if (result < 0)
34 die("%s: %s",msg, strerror(errno)); 32 die("%s: %s",msg, strerror(errno));
35 return result; 33 return result;
36} 34}
37 35
38struct cgit_repo *cgit_add_repo(const char *url) 36struct cgit_repo *cgit_add_repo(const char *url)
39{ 37{
40 struct cgit_repo *ret; 38 struct cgit_repo *ret;
41 39
42 if (++cgit_repolist.count > cgit_repolist.length) { 40 if (++cgit_repolist.count > cgit_repolist.length) {
43 if (cgit_repolist.length == 0) 41 if (cgit_repolist.length == 0)
44 cgit_repolist.length = 8; 42 cgit_repolist.length = 8;
45 else 43 else
46 cgit_repolist.length *= 2; 44 cgit_repolist.length *= 2;
47 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
48 cgit_repolist.length * 46 cgit_repolist.length *
49 sizeof(struct cgit_repo)); 47 sizeof(struct cgit_repo));
50 } 48 }
51 49
52 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
53 ret->url = trim_end(url, '/'); 51 ret->url = trim_end(url, '/');
54 ret->name = ret->url; 52 ret->name = ret->url;
55 ret->path = NULL; 53 ret->path = NULL;
56 ret->desc = "[no description]"; 54 ret->desc = "[no description]";
57 ret->owner = NULL; 55 ret->owner = NULL;
58 ret->group = ctx.cfg.repo_group; 56 ret->group = ctx.cfg.repo_group;
59 ret->defbranch = "master"; 57 ret->defbranch = "master";
60 ret->snapshots = ctx.cfg.snapshots; 58 ret->snapshots = ctx.cfg.snapshots;
61 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 59 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
63 ret->module_link = ctx.cfg.module_link; 61 ret->module_link = ctx.cfg.module_link;
64 ret->readme = NULL; 62 ret->readme = NULL;
65 return ret; 63 return ret;
66} 64}
67 65
68struct cgit_repo *cgit_get_repoinfo(const char *url) 66struct cgit_repo *cgit_get_repoinfo(const char *url)
69{ 67{
70 int i; 68 int i;
71 struct cgit_repo *repo; 69 struct cgit_repo *repo;
72 70
73 for (i=0; i<cgit_repolist.count; i++) { 71 for (i=0; i<cgit_repolist.count; i++) {
74 repo = &cgit_repolist.repos[i]; 72 repo = &cgit_repolist.repos[i];
75 if (!strcmp(repo->url, url)) 73 if (!strcmp(repo->url, url))
76 return repo; 74 return repo;
77 } 75 }
78 return NULL; 76 return NULL;
79} 77}
80 78
81void *cgit_free_commitinfo(struct commitinfo *info) 79void *cgit_free_commitinfo(struct commitinfo *info)
82{ 80{
83 free(info->author); 81 free(info->author);
84 free(info->author_email); 82 free(info->author_email);
85 free(info->committer); 83 free(info->committer);
86 free(info->committer_email); 84 free(info->committer_email);
87 free(info->subject); 85 free(info->subject);
88 free(info->msg); 86 free(info->msg);
89 free(info->msg_encoding); 87 free(info->msg_encoding);
90 free(info); 88 free(info);
91 return NULL; 89 return NULL;
92} 90}
93 91
94int hextoint(char c) 92int hextoint(char c)
95{ 93{
96 if (c >= 'a' && c <= 'f') 94 if (c >= 'a' && c <= 'f')
97 return 10 + c - 'a'; 95 return 10 + c - 'a';
98 else if (c >= 'A' && c <= 'F') 96 else if (c >= 'A' && c <= 'F')
99 return 10 + c - 'A'; 97 return 10 + c - 'A';
100 else if (c >= '0' && c <= '9') 98 else if (c >= '0' && c <= '9')
101 return c - '0'; 99 return c - '0';
102 else 100 else
103 return -1; 101 return -1;
104} 102}
105 103
106char *trim_end(const char *str, char c) 104char *trim_end(const char *str, char c)
107{ 105{
108 int len; 106 int len;
109 char *s, *t; 107 char *s, *t;
110 108
111 if (str == NULL) 109 if (str == NULL)
112 return NULL; 110 return NULL;
113 t = (char *)str; 111 t = (char *)str;
114 len = strlen(t); 112 len = strlen(t);
115 while(len > 0 && t[len - 1] == c) 113 while(len > 0 && t[len - 1] == c)
116 len--; 114 len--;
117 115
118 if (len == 0) 116 if (len == 0)
119 return NULL; 117 return NULL;
120 118
121 c = t[len]; 119 c = t[len];
122 t[len] = '\0'; 120 t[len] = '\0';
123 s = xstrdup(t); 121 s = xstrdup(t);
124 t[len] = c; 122 t[len] = c;
125 return s; 123 return s;
126} 124}
127 125
128char *strlpart(char *txt, int maxlen) 126char *strlpart(char *txt, int maxlen)
129{ 127{
130 char *result; 128 char *result;
131 129
132 if (!txt) 130 if (!txt)
133 return txt; 131 return txt;
134 132
135 if (strlen(txt) <= maxlen) 133 if (strlen(txt) <= maxlen)
136 return txt; 134 return txt;
137 result = xmalloc(maxlen + 1); 135 result = xmalloc(maxlen + 1);
138 memcpy(result, txt, maxlen - 3); 136 memcpy(result, txt, maxlen - 3);
139 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; 137 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
140 result[maxlen] = '\0'; 138 result[maxlen] = '\0';
141 return result; 139 return result;
142} 140}
143 141
144char *strrpart(char *txt, int maxlen) 142char *strrpart(char *txt, int maxlen)
145{ 143{
146 char *result; 144 char *result;
147 145
148 if (!txt) 146 if (!txt)
149 return txt; 147 return txt;
150 148
151 if (strlen(txt) <= maxlen) 149 if (strlen(txt) <= maxlen)
152 return txt; 150 return txt;
153 result = xmalloc(maxlen + 1); 151 result = xmalloc(maxlen + 1);
154 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); 152 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
155 result[0] = result[1] = result[2] = '.'; 153 result[0] = result[1] = result[2] = '.';
156 return result; 154 return result;
157} 155}
158 156
159void cgit_add_ref(struct reflist *list, struct refinfo *ref) 157void cgit_add_ref(struct reflist *list, struct refinfo *ref)
160{ 158{
161 size_t size; 159 size_t size;
162 160
163 if (list->count >= list->alloc) { 161 if (list->count >= list->alloc) {
164 list->alloc += (list->alloc ? list->alloc : 4); 162 list->alloc += (list->alloc ? list->alloc : 4);
165 size = list->alloc * sizeof(struct refinfo *); 163 size = list->alloc * sizeof(struct refinfo *);
166 list->refs = xrealloc(list->refs, size); 164 list->refs = xrealloc(list->refs, size);
167 } 165 }
168 list->refs[list->count++] = ref; 166 list->refs[list->count++] = ref;
169} 167}
170 168
171struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) 169struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
172{ 170{
173 struct refinfo *ref; 171 struct refinfo *ref;
174 172
175 ref = xmalloc(sizeof (struct refinfo)); 173 ref = xmalloc(sizeof (struct refinfo));
176 ref->refname = xstrdup(refname); 174 ref->refname = xstrdup(refname);
177 ref->object = parse_object(sha1); 175 ref->object = parse_object(sha1);
178 switch (ref->object->type) { 176 switch (ref->object->type) {
179 case OBJ_TAG: 177 case OBJ_TAG:
180 ref->tag = cgit_parse_tag((struct tag *)ref->object); 178 ref->tag = cgit_parse_tag((struct tag *)ref->object);
181 break; 179 break;
182 case OBJ_COMMIT: 180 case OBJ_COMMIT:
183 ref->commit = cgit_parse_commit((struct commit *)ref->object); 181 ref->commit = cgit_parse_commit((struct commit *)ref->object);
184 break; 182 break;
185 } 183 }
186 return ref; 184 return ref;
187} 185}
188 186
189int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, 187int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
190 void *cb_data) 188 void *cb_data)
191{ 189{
192 struct reflist *list = (struct reflist *)cb_data; 190 struct reflist *list = (struct reflist *)cb_data;
193 struct refinfo *info = cgit_mk_refinfo(refname, sha1); 191 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
194 192
195 if (info) 193 if (info)
196 cgit_add_ref(list, info); 194 cgit_add_ref(list, info);
197 return 0; 195 return 0;
198} 196}
199 197
200void cgit_diff_tree_cb(struct diff_queue_struct *q, 198void cgit_diff_tree_cb(struct diff_queue_struct *q,
201 struct diff_options *options, void *data) 199 struct diff_options *options, void *data)
202{ 200{
203 int i; 201 int i;
204 202
205 for (i = 0; i < q->nr; i++) { 203 for (i = 0; i < q->nr; i++) {
206 if (q->queue[i]->status == 'U') 204 if (q->queue[i]->status == 'U')
207 continue; 205 continue;
208 ((filepair_fn)data)(q->queue[i]); 206 ((filepair_fn)data)(q->queue[i]);
209 } 207 }
210} 208}
211 209
212static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 210static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
213{ 211{
214 enum object_type type; 212 enum object_type type;
215 213
216 if (is_null_sha1(sha1)) { 214 if (is_null_sha1(sha1)) {
217 file->ptr = (char *)""; 215 file->ptr = (char *)"";
218 file->size = 0; 216 file->size = 0;
219 } else { 217 } else {
220 file->ptr = read_sha1_file(sha1, &type, 218 file->ptr = read_sha1_file(sha1, &type,
221 (unsigned long *)&file->size); 219 (unsigned long *)&file->size);
222 } 220 }
223 return 1; 221 return 1;
224} 222}
225 223
226/* 224/*
227 * Receive diff-buffers from xdiff and concatenate them as 225 * Receive diff-buffers from xdiff and concatenate them as
228 * needed across multiple callbacks. 226 * needed across multiple callbacks.
229 * 227 *
230 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 228 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
231 * ripped from git and modified to use globals instead of 229 * ripped from git and modified to use globals instead of
232 * a special callback-struct. 230 * a special callback-struct.
233 */ 231 */
234char *diffbuf = NULL; 232char *diffbuf = NULL;
235int buflen = 0; 233int buflen = 0;
236 234
237int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 235int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
238{ 236{
239 int i; 237 int i;
240 238
241 for (i = 0; i < nbuf; i++) { 239 for (i = 0; i < nbuf; i++) {
242 if (mb[i].ptr[mb[i].size-1] != '\n') { 240 if (mb[i].ptr[mb[i].size-1] != '\n') {
243 /* Incomplete line */ 241 /* Incomplete line */
244 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 242 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
245 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 243 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
246 buflen += mb[i].size; 244 buflen += mb[i].size;
247 continue; 245 continue;
248 } 246 }
249 247
250 /* we have a complete line */ 248 /* we have a complete line */
251 if (!diffbuf) { 249 if (!diffbuf) {
252 ((linediff_fn)priv)(mb[i].ptr, mb[i].size); 250 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
253 continue; 251 continue;
254 } 252 }
255 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 253 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
256 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 254 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
257 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); 255 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
258 free(diffbuf); 256 free(diffbuf);
259 diffbuf = NULL; 257 diffbuf = NULL;
260 buflen = 0; 258 buflen = 0;
261 } 259 }
262 if (diffbuf) { 260 if (diffbuf) {
263 ((linediff_fn)priv)(diffbuf, buflen); 261 ((linediff_fn)priv)(diffbuf, buflen);
264 free(diffbuf); 262 free(diffbuf);
265 diffbuf = NULL; 263 diffbuf = NULL;
266 buflen = 0; 264 buflen = 0;
267 } 265 }
268 return 0; 266 return 0;
269} 267}
270 268
271int cgit_diff_files(const unsigned char *old_sha1, 269int cgit_diff_files(const unsigned char *old_sha1,
272 const unsigned char *new_sha1, 270 const unsigned char *new_sha1,
273 linediff_fn fn) 271 linediff_fn fn)
274{ 272{
275 mmfile_t file1, file2; 273 mmfile_t file1, file2;
276 xpparam_t diff_params; 274 xpparam_t diff_params;
277 xdemitconf_t emit_params; 275 xdemitconf_t emit_params;
278 xdemitcb_t emit_cb; 276 xdemitcb_t emit_cb;
279 277
280 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) 278 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
281 return 1; 279 return 1;
282 280
283 diff_params.flags = XDF_NEED_MINIMAL; 281 diff_params.flags = XDF_NEED_MINIMAL;
284 emit_params.ctxlen = 3; 282 emit_params.ctxlen = 3;
285 emit_params.flags = XDL_EMIT_FUNCNAMES; 283 emit_params.flags = XDL_EMIT_FUNCNAMES;
286 emit_params.find_func = NULL; 284 emit_params.find_func = NULL;
287 emit_cb.outf = filediff_cb; 285 emit_cb.outf = filediff_cb;
288 emit_cb.priv = fn; 286 emit_cb.priv = fn;
289 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 287 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
290 return 0; 288 return 0;
291} 289}
292 290
293void cgit_diff_tree(const unsigned char *old_sha1, 291void cgit_diff_tree(const unsigned char *old_sha1,
294 const unsigned char *new_sha1, 292 const unsigned char *new_sha1,
295 filepair_fn fn, const char *prefix) 293 filepair_fn fn, const char *prefix)
296{ 294{
297 struct diff_options opt; 295 struct diff_options opt;
298 int ret; 296 int ret;
299 int prefixlen; 297 int prefixlen;
300 298
301 diff_setup(&opt); 299 diff_setup(&opt);
302 opt.output_format = DIFF_FORMAT_CALLBACK; 300 opt.output_format = DIFF_FORMAT_CALLBACK;
303 opt.detect_rename = 1; 301 opt.detect_rename = 1;
304 opt.rename_limit = ctx.cfg.renamelimit; 302 opt.rename_limit = ctx.cfg.renamelimit;
305 DIFF_OPT_SET(&opt, RECURSIVE); 303 DIFF_OPT_SET(&opt, RECURSIVE);
306 opt.format_callback = cgit_diff_tree_cb; 304 opt.format_callback = cgit_diff_tree_cb;
307 opt.format_callback_data = fn; 305 opt.format_callback_data = fn;
308 if (prefix) { 306 if (prefix) {
309 opt.nr_paths = 1; 307 opt.nr_paths = 1;
310 opt.paths = &prefix; 308 opt.paths = &prefix;
311 prefixlen = strlen(prefix); 309 prefixlen = strlen(prefix);
312 opt.pathlens = &prefixlen; 310 opt.pathlens = &prefixlen;
313 } 311 }
314 diff_setup_done(&opt); 312 diff_setup_done(&opt);
315 313
316 if (old_sha1 && !is_null_sha1(old_sha1)) 314 if (old_sha1 && !is_null_sha1(old_sha1))
317 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 315 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
318 else 316 else
319 ret = diff_root_tree_sha1(new_sha1, "", &opt); 317 ret = diff_root_tree_sha1(new_sha1, "", &opt);
320 diffcore_std(&opt); 318 diffcore_std(&opt);
321 diff_flush(&opt); 319 diff_flush(&opt);
322} 320}
323 321
324void cgit_diff_commit(struct commit *commit, filepair_fn fn) 322void cgit_diff_commit(struct commit *commit, filepair_fn fn)
325{ 323{
326 unsigned char *old_sha1 = NULL; 324 unsigned char *old_sha1 = NULL;
327 325
328 if (commit->parents) 326 if (commit->parents)
329 old_sha1 = commit->parents->item->object.sha1; 327 old_sha1 = commit->parents->item->object.sha1;
330 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 328 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
331} 329}
332 330
333int cgit_parse_snapshots_mask(const char *str) 331int cgit_parse_snapshots_mask(const char *str)
334{ 332{
335 const struct cgit_snapshot_format *f; 333 const struct cgit_snapshot_format *f;
336 static const char *delim = " \t,:/|;"; 334 static const char *delim = " \t,:/|;";
337 int tl, sl, rv = 0; 335 int tl, sl, rv = 0;
338 336
339 /* favor legacy setting */ 337 /* favor legacy setting */
340 if(atoi(str)) 338 if(atoi(str))
341 return 1; 339 return 1;
342 for(;;) { 340 for(;;) {
343 str += strspn(str,delim); 341 str += strspn(str,delim);
344 tl = strcspn(str,delim); 342 tl = strcspn(str,delim);
345 if (!tl) 343 if (!tl)
346 break; 344 break;
347 for (f = cgit_snapshot_formats; f->suffix; f++) { 345 for (f = cgit_snapshot_formats; f->suffix; f++) {
348 sl = strlen(f->suffix); 346 sl = strlen(f->suffix);
349 if((tl == sl && !strncmp(f->suffix, str, tl)) || 347 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
350 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { 348 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
351 rv |= f->bit; 349 rv |= f->bit;
352 break; 350 break;
353 } 351 }
354 } 352 }
355 str += tl; 353 str += tl;
356 } 354 }
357 return rv; 355 return rv;
358} 356}