author | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 12:07:13 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 12:10:50 (UTC) |
commit | b228d4ff82a65fdcd4a7364759fe36a0bdda5978 (patch) (unidiff) | |
tree | 33b8cc2ff48113f8d7ad3ba88c7ea19a7cac570a | |
parent | d14d77fe95c3b6224b40df9b101dded0deea913c (diff) | |
download | cgit-b228d4ff82a65fdcd4a7364759fe36a0bdda5978.zip cgit-b228d4ff82a65fdcd4a7364759fe36a0bdda5978.tar.gz cgit-b228d4ff82a65fdcd4a7364759fe36a0bdda5978.tar.bz2 |
Add all config variables into struct cgit_context
This removes another big set of global variables, and introduces the
cgit_prepare_context() function which populates a context-variable with
compile-time default values.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cache.c | 10 | ||||
-rw-r--r-- | cgit.c | 33 | ||||
-rw-r--r-- | cgit.h | 72 | ||||
-rw-r--r-- | shared.c | 138 | ||||
-rw-r--r-- | ui-repolist.c | 18 | ||||
-rw-r--r-- | ui-shared.c | 60 | ||||
-rw-r--r-- | ui-summary.c | 10 |
7 files changed, 166 insertions, 175 deletions
@@ -1,119 +1,119 @@ | |||
1 | /* cache.c: cache management | 1 | /* cache.c: cache management |
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 | const int NOLOCK = -1; | 11 | const int NOLOCK = -1; |
12 | 12 | ||
13 | char *cache_safe_filename(const char *unsafe) | 13 | char *cache_safe_filename(const char *unsafe) |
14 | { | 14 | { |
15 | static char buf[4][PATH_MAX]; | 15 | static char buf[4][PATH_MAX]; |
16 | static int bufidx; | 16 | static int bufidx; |
17 | char *s; | 17 | char *s; |
18 | char c; | 18 | char c; |
19 | 19 | ||
20 | bufidx++; | 20 | bufidx++; |
21 | bufidx &= 3; | 21 | bufidx &= 3; |
22 | s = buf[bufidx]; | 22 | s = buf[bufidx]; |
23 | 23 | ||
24 | while(unsafe && (c = *unsafe++) != 0) { | 24 | while(unsafe && (c = *unsafe++) != 0) { |
25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || |
26 | c == '>' || c == '<' || c == '.') | 26 | c == '>' || c == '<' || c == '.') |
27 | c = '_'; | 27 | c = '_'; |
28 | *s++ = c; | 28 | *s++ = c; |
29 | } | 29 | } |
30 | *s = '\0'; | 30 | *s = '\0'; |
31 | return buf[bufidx]; | 31 | return buf[bufidx]; |
32 | } | 32 | } |
33 | 33 | ||
34 | int cache_exist(struct cacheitem *item) | 34 | int cache_exist(struct cacheitem *item) |
35 | { | 35 | { |
36 | if (stat(item->name, &item->st)) { | 36 | if (stat(item->name, &item->st)) { |
37 | item->st.st_mtime = 0; | 37 | item->st.st_mtime = 0; |
38 | return 0; | 38 | return 0; |
39 | } | 39 | } |
40 | return 1; | 40 | return 1; |
41 | } | 41 | } |
42 | 42 | ||
43 | int cache_create_dirs() | 43 | int cache_create_dirs() |
44 | { | 44 | { |
45 | char *path; | 45 | char *path; |
46 | 46 | ||
47 | path = fmt("%s", cgit_cache_root); | 47 | path = fmt("%s", ctx.cfg.cache_root); |
48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
49 | return 0; | 49 | return 0; |
50 | 50 | ||
51 | if (!cgit_repo) | 51 | if (!cgit_repo) |
52 | return 0; | 52 | return 0; |
53 | 53 | ||
54 | path = fmt("%s/%s", cgit_cache_root, | 54 | path = fmt("%s/%s", ctx.cfg.cache_root, |
55 | cache_safe_filename(cgit_repo->url)); | 55 | cache_safe_filename(cgit_repo->url)); |
56 | 56 | ||
57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
58 | return 0; | 58 | return 0; |
59 | 59 | ||
60 | if (ctx.qry.page) { | 60 | if (ctx.qry.page) { |
61 | path = fmt("%s/%s/%s", cgit_cache_root, | 61 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, |
62 | cache_safe_filename(cgit_repo->url), | 62 | cache_safe_filename(cgit_repo->url), |
63 | ctx.qry.page); | 63 | ctx.qry.page); |
64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
65 | return 0; | 65 | return 0; |
66 | } | 66 | } |
67 | return 1; | 67 | return 1; |
68 | } | 68 | } |
69 | 69 | ||
70 | int cache_refill_overdue(const char *lockfile) | 70 | int cache_refill_overdue(const char *lockfile) |
71 | { | 71 | { |
72 | struct stat st; | 72 | struct stat st; |
73 | 73 | ||
74 | if (stat(lockfile, &st)) | 74 | if (stat(lockfile, &st)) |
75 | return 0; | 75 | return 0; |
76 | else | 76 | else |
77 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); | 77 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); |
78 | } | 78 | } |
79 | 79 | ||
80 | int cache_lock(struct cacheitem *item) | 80 | int cache_lock(struct cacheitem *item) |
81 | { | 81 | { |
82 | int i = 0; | 82 | int i = 0; |
83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
84 | 84 | ||
85 | top: | 85 | top: |
86 | if (++i > cgit_max_lock_attempts) | 86 | if (++i > ctx.cfg.max_lock_attempts) |
87 | die("cache_lock: unable to lock %s: %s", | 87 | die("cache_lock: unable to lock %s: %s", |
88 | item->name, strerror(errno)); | 88 | item->name, strerror(errno)); |
89 | 89 | ||
90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
91 | 91 | ||
92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
93 | goto top; | 93 | goto top; |
94 | 94 | ||
95 | if (item->fd == NOLOCK && errno == EEXIST && | 95 | if (item->fd == NOLOCK && errno == EEXIST && |
96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
97 | goto top; | 97 | goto top; |
98 | 98 | ||
99 | free(lockfile); | 99 | free(lockfile); |
100 | return (item->fd > 0); | 100 | return (item->fd > 0); |
101 | } | 101 | } |
102 | 102 | ||
103 | int cache_unlock(struct cacheitem *item) | 103 | int cache_unlock(struct cacheitem *item) |
104 | { | 104 | { |
105 | close(item->fd); | 105 | close(item->fd); |
106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); |
107 | } | 107 | } |
108 | 108 | ||
109 | int cache_cancel_lock(struct cacheitem *item) | 109 | int cache_cancel_lock(struct cacheitem *item) |
110 | { | 110 | { |
111 | return (unlink(fmt("%s.lock", item->name)) == 0); | 111 | return (unlink(fmt("%s.lock", item->name)) == 0); |
112 | } | 112 | } |
113 | 113 | ||
114 | int cache_expired(struct cacheitem *item) | 114 | int cache_expired(struct cacheitem *item) |
115 | { | 115 | { |
116 | if (item->ttl < 0) | 116 | if (item->ttl < 0) |
117 | return 0; | 117 | return 0; |
118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); |
119 | } | 119 | } |
@@ -1,318 +1,319 @@ | |||
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 | 10 | ||
11 | static int cgit_prepare_cache(struct cacheitem *item) | 11 | static int cgit_prepare_cache(struct cacheitem *item) |
12 | { | 12 | { |
13 | if (!cgit_repo && ctx.qry.repo) { | 13 | if (!cgit_repo && ctx.qry.repo) { |
14 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | 14 | char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); |
15 | cgit_print_docstart(title, item); | 15 | cgit_print_docstart(title, item); |
16 | cgit_print_pageheader(title, 0); | 16 | cgit_print_pageheader(title, 0); |
17 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 17 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
18 | cgit_print_docend(); | 18 | cgit_print_docend(); |
19 | return 0; | 19 | return 0; |
20 | } | 20 | } |
21 | 21 | ||
22 | if (!cgit_repo) { | 22 | if (!cgit_repo) { |
23 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); | 23 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
24 | item->ttl = cgit_cache_root_ttl; | 24 | item->ttl = ctx.cfg.cache_root_ttl; |
25 | return 1; | 25 | return 1; |
26 | } | 26 | } |
27 | 27 | ||
28 | if (!cgit_cmd) { | 28 | if (!cgit_cmd) { |
29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, | 29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
30 | cache_safe_filename(cgit_repo->url), | 30 | cache_safe_filename(cgit_repo->url), |
31 | cache_safe_filename(ctx.qry.raw))); | 31 | cache_safe_filename(ctx.qry.raw))); |
32 | item->ttl = cgit_cache_repo_ttl; | 32 | item->ttl = ctx.cfg.cache_repo_ttl; |
33 | } else { | 33 | } else { |
34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, | 34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
35 | cache_safe_filename(cgit_repo->url), | 35 | cache_safe_filename(cgit_repo->url), |
36 | ctx.qry.page, | 36 | ctx.qry.page, |
37 | cache_safe_filename(ctx.qry.raw))); | 37 | cache_safe_filename(ctx.qry.raw))); |
38 | if (ctx.qry.has_symref) | 38 | if (ctx.qry.has_symref) |
39 | item->ttl = cgit_cache_dynamic_ttl; | 39 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
40 | else if (ctx.qry.has_sha1) | 40 | else if (ctx.qry.has_sha1) |
41 | item->ttl = cgit_cache_static_ttl; | 41 | item->ttl = ctx.cfg.cache_static_ttl; |
42 | else | 42 | else |
43 | item->ttl = cgit_cache_repo_ttl; | 43 | item->ttl = ctx.cfg.cache_repo_ttl; |
44 | } | 44 | } |
45 | return 1; | 45 | return 1; |
46 | } | 46 | } |
47 | 47 | ||
48 | struct refmatch { | 48 | struct refmatch { |
49 | char *req_ref; | 49 | char *req_ref; |
50 | char *first_ref; | 50 | char *first_ref; |
51 | int match; | 51 | int match; |
52 | }; | 52 | }; |
53 | 53 | ||
54 | int find_current_ref(const char *refname, const unsigned char *sha1, | 54 | int find_current_ref(const char *refname, const unsigned char *sha1, |
55 | int flags, void *cb_data) | 55 | int flags, void *cb_data) |
56 | { | 56 | { |
57 | struct refmatch *info; | 57 | struct refmatch *info; |
58 | 58 | ||
59 | info = (struct refmatch *)cb_data; | 59 | info = (struct refmatch *)cb_data; |
60 | if (!strcmp(refname, info->req_ref)) | 60 | if (!strcmp(refname, info->req_ref)) |
61 | info->match = 1; | 61 | info->match = 1; |
62 | if (!info->first_ref) | 62 | if (!info->first_ref) |
63 | info->first_ref = xstrdup(refname); | 63 | info->first_ref = xstrdup(refname); |
64 | return info->match; | 64 | return info->match; |
65 | } | 65 | } |
66 | 66 | ||
67 | char *find_default_branch(struct repoinfo *repo) | 67 | char *find_default_branch(struct repoinfo *repo) |
68 | { | 68 | { |
69 | struct refmatch info; | 69 | struct refmatch info; |
70 | 70 | ||
71 | info.req_ref = repo->defbranch; | 71 | info.req_ref = repo->defbranch; |
72 | info.first_ref = NULL; | 72 | info.first_ref = NULL; |
73 | info.match = 0; | 73 | info.match = 0; |
74 | for_each_branch_ref(find_current_ref, &info); | 74 | for_each_branch_ref(find_current_ref, &info); |
75 | if (info.match) | 75 | if (info.match) |
76 | return info.req_ref; | 76 | return info.req_ref; |
77 | else | 77 | else |
78 | return info.first_ref; | 78 | return info.first_ref; |
79 | } | 79 | } |
80 | 80 | ||
81 | static void cgit_print_repo_page(struct cacheitem *item) | 81 | static void cgit_print_repo_page(struct cacheitem *item) |
82 | { | 82 | { |
83 | char *title, *tmp; | 83 | char *title, *tmp; |
84 | int show_search; | 84 | int show_search; |
85 | unsigned char sha1[20]; | 85 | unsigned char sha1[20]; |
86 | 86 | ||
87 | if (chdir(cgit_repo->path)) { | 87 | if (chdir(cgit_repo->path)) { |
88 | title = fmt("%s - %s", cgit_root_title, "Bad request"); | 88 | title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); |
89 | cgit_print_docstart(title, item); | 89 | cgit_print_docstart(title, item); |
90 | cgit_print_pageheader(title, 0); | 90 | cgit_print_pageheader(title, 0); |
91 | cgit_print_error(fmt("Unable to scan repository: %s", | 91 | cgit_print_error(fmt("Unable to scan repository: %s", |
92 | strerror(errno))); | 92 | strerror(errno))); |
93 | cgit_print_docend(); | 93 | cgit_print_docend(); |
94 | return; | 94 | return; |
95 | } | 95 | } |
96 | 96 | ||
97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); | 97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); |
98 | show_search = 0; | 98 | show_search = 0; |
99 | setenv("GIT_DIR", cgit_repo->path, 1); | 99 | setenv("GIT_DIR", cgit_repo->path, 1); |
100 | 100 | ||
101 | if (!ctx.qry.head) { | 101 | if (!ctx.qry.head) { |
102 | ctx.qry.head = xstrdup(find_default_branch(cgit_repo)); | 102 | ctx.qry.head = xstrdup(find_default_branch(cgit_repo)); |
103 | cgit_repo->defbranch = ctx.qry.head; | 103 | cgit_repo->defbranch = ctx.qry.head; |
104 | } | 104 | } |
105 | 105 | ||
106 | if (!ctx.qry.head) { | 106 | if (!ctx.qry.head) { |
107 | cgit_print_docstart(title, item); | 107 | cgit_print_docstart(title, item); |
108 | cgit_print_pageheader(title, 0); | 108 | cgit_print_pageheader(title, 0); |
109 | cgit_print_error("Repository seems to be empty"); | 109 | cgit_print_error("Repository seems to be empty"); |
110 | cgit_print_docend(); | 110 | cgit_print_docend(); |
111 | return; | 111 | return; |
112 | } | 112 | } |
113 | 113 | ||
114 | if (get_sha1(ctx.qry.head, sha1)) { | 114 | if (get_sha1(ctx.qry.head, sha1)) { |
115 | tmp = xstrdup(ctx.qry.head); | 115 | tmp = xstrdup(ctx.qry.head); |
116 | ctx.qry.head = cgit_repo->defbranch; | 116 | ctx.qry.head = cgit_repo->defbranch; |
117 | cgit_print_docstart(title, item); | 117 | cgit_print_docstart(title, item); |
118 | cgit_print_pageheader(title, 0); | 118 | cgit_print_pageheader(title, 0); |
119 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 119 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
120 | cgit_print_docend(); | 120 | cgit_print_docend(); |
121 | return; | 121 | return; |
122 | } | 122 | } |
123 | 123 | ||
124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { | 124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { |
125 | cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, | 125 | cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, |
126 | cgit_repobasename(cgit_repo->url), | 126 | cgit_repobasename(cgit_repo->url), |
127 | ctx.qry.path, | 127 | ctx.qry.path, |
128 | cgit_repo->snapshots ); | 128 | cgit_repo->snapshots ); |
129 | return; | 129 | return; |
130 | } | 130 | } |
131 | 131 | ||
132 | if (cgit_cmd == CMD_PATCH) { | 132 | if (cgit_cmd == CMD_PATCH) { |
133 | cgit_print_patch(ctx.qry.sha1, item); | 133 | cgit_print_patch(ctx.qry.sha1, item); |
134 | return; | 134 | return; |
135 | } | 135 | } |
136 | 136 | ||
137 | if (cgit_cmd == CMD_BLOB) { | 137 | if (cgit_cmd == CMD_BLOB) { |
138 | cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); | 138 | cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); |
139 | return; | 139 | return; |
140 | } | 140 | } |
141 | 141 | ||
142 | show_search = (cgit_cmd == CMD_LOG); | 142 | show_search = (cgit_cmd == CMD_LOG); |
143 | cgit_print_docstart(title, item); | 143 | cgit_print_docstart(title, item); |
144 | if (!cgit_cmd) { | 144 | if (!cgit_cmd) { |
145 | cgit_print_pageheader("summary", show_search); | 145 | cgit_print_pageheader("summary", show_search); |
146 | cgit_print_summary(); | 146 | cgit_print_summary(); |
147 | cgit_print_docend(); | 147 | cgit_print_docend(); |
148 | return; | 148 | return; |
149 | } | 149 | } |
150 | 150 | ||
151 | cgit_print_pageheader(ctx.qry.page, show_search); | 151 | cgit_print_pageheader(ctx.qry.page, show_search); |
152 | 152 | ||
153 | switch(cgit_cmd) { | 153 | switch(cgit_cmd) { |
154 | case CMD_LOG: | 154 | case CMD_LOG: |
155 | cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, | 155 | cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, |
156 | cgit_max_commit_count, ctx.qry.grep, ctx.qry.search, | 156 | ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, |
157 | ctx.qry.path, 1); | 157 | ctx.qry.path, 1); |
158 | break; | 158 | break; |
159 | case CMD_TREE: | 159 | case CMD_TREE: |
160 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); | 160 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); |
161 | break; | 161 | break; |
162 | case CMD_COMMIT: | 162 | case CMD_COMMIT: |
163 | cgit_print_commit(ctx.qry.sha1); | 163 | cgit_print_commit(ctx.qry.sha1); |
164 | break; | 164 | break; |
165 | case CMD_REFS: | 165 | case CMD_REFS: |
166 | cgit_print_refs(); | 166 | cgit_print_refs(); |
167 | break; | 167 | break; |
168 | case CMD_TAG: | 168 | case CMD_TAG: |
169 | cgit_print_tag(ctx.qry.sha1); | 169 | cgit_print_tag(ctx.qry.sha1); |
170 | break; | 170 | break; |
171 | case CMD_DIFF: | 171 | case CMD_DIFF: |
172 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); | 172 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); |
173 | break; | 173 | break; |
174 | default: | 174 | default: |
175 | cgit_print_error("Invalid request"); | 175 | cgit_print_error("Invalid request"); |
176 | } | 176 | } |
177 | cgit_print_docend(); | 177 | cgit_print_docend(); |
178 | } | 178 | } |
179 | 179 | ||
180 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 180 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
181 | { | 181 | { |
182 | static char buf[PATH_MAX]; | 182 | static char buf[PATH_MAX]; |
183 | int stdout2; | 183 | int stdout2; |
184 | 184 | ||
185 | getcwd(buf, sizeof(buf)); | 185 | getcwd(buf, sizeof(buf)); |
186 | item->st.st_mtime = time(NULL); | 186 | item->st.st_mtime = time(NULL); |
187 | 187 | ||
188 | if (use_cache) { | 188 | if (use_cache) { |
189 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 189 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
190 | "Preserving STDOUT"); | 190 | "Preserving STDOUT"); |
191 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 191 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
192 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 192 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
193 | } | 193 | } |
194 | 194 | ||
195 | if (cgit_repo) | 195 | if (cgit_repo) |
196 | cgit_print_repo_page(item); | 196 | cgit_print_repo_page(item); |
197 | else | 197 | else |
198 | cgit_print_repolist(item); | 198 | cgit_print_repolist(item); |
199 | 199 | ||
200 | if (use_cache) { | 200 | if (use_cache) { |
201 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 201 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
202 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 202 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
203 | "Restoring original STDOUT"); | 203 | "Restoring original STDOUT"); |
204 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 204 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
205 | } | 205 | } |
206 | 206 | ||
207 | chdir(buf); | 207 | chdir(buf); |
208 | } | 208 | } |
209 | 209 | ||
210 | static void cgit_check_cache(struct cacheitem *item) | 210 | static void cgit_check_cache(struct cacheitem *item) |
211 | { | 211 | { |
212 | int i = 0; | 212 | int i = 0; |
213 | 213 | ||
214 | top: | 214 | top: |
215 | if (++i > cgit_max_lock_attempts) { | 215 | if (++i > ctx.cfg.max_lock_attempts) { |
216 | die("cgit_refresh_cache: unable to lock %s: %s", | 216 | die("cgit_refresh_cache: unable to lock %s: %s", |
217 | item->name, strerror(errno)); | 217 | item->name, strerror(errno)); |
218 | } | 218 | } |
219 | if (!cache_exist(item)) { | 219 | if (!cache_exist(item)) { |
220 | if (!cache_lock(item)) { | 220 | if (!cache_lock(item)) { |
221 | sleep(1); | 221 | sleep(1); |
222 | goto top; | 222 | goto top; |
223 | } | 223 | } |
224 | if (!cache_exist(item)) { | 224 | if (!cache_exist(item)) { |
225 | cgit_fill_cache(item, 1); | 225 | cgit_fill_cache(item, 1); |
226 | cache_unlock(item); | 226 | cache_unlock(item); |
227 | } else { | 227 | } else { |
228 | cache_cancel_lock(item); | 228 | cache_cancel_lock(item); |
229 | } | 229 | } |
230 | } else if (cache_expired(item) && cache_lock(item)) { | 230 | } else if (cache_expired(item) && cache_lock(item)) { |
231 | if (cache_expired(item)) { | 231 | if (cache_expired(item)) { |
232 | cgit_fill_cache(item, 1); | 232 | cgit_fill_cache(item, 1); |
233 | cache_unlock(item); | 233 | cache_unlock(item); |
234 | } else { | 234 | } else { |
235 | cache_cancel_lock(item); | 235 | cache_cancel_lock(item); |
236 | } | 236 | } |
237 | } | 237 | } |
238 | } | 238 | } |
239 | 239 | ||
240 | static void cgit_print_cache(struct cacheitem *item) | 240 | static void cgit_print_cache(struct cacheitem *item) |
241 | { | 241 | { |
242 | static char buf[4096]; | 242 | static char buf[4096]; |
243 | ssize_t i; | 243 | ssize_t i; |
244 | 244 | ||
245 | int fd = open(item->name, O_RDONLY); | 245 | int fd = open(item->name, O_RDONLY); |
246 | if (fd<0) | 246 | if (fd<0) |
247 | die("Unable to open cached file %s", item->name); | 247 | die("Unable to open cached file %s", item->name); |
248 | 248 | ||
249 | while((i=read(fd, buf, sizeof(buf))) > 0) | 249 | while((i=read(fd, buf, sizeof(buf))) > 0) |
250 | write(STDOUT_FILENO, buf, i); | 250 | write(STDOUT_FILENO, buf, i); |
251 | 251 | ||
252 | close(fd); | 252 | close(fd); |
253 | } | 253 | } |
254 | 254 | ||
255 | static void cgit_parse_args(int argc, const char **argv) | 255 | static void cgit_parse_args(int argc, const char **argv) |
256 | { | 256 | { |
257 | int i; | 257 | int i; |
258 | 258 | ||
259 | for (i = 1; i < argc; i++) { | 259 | for (i = 1; i < argc; i++) { |
260 | if (!strncmp(argv[i], "--cache=", 8)) { | 260 | if (!strncmp(argv[i], "--cache=", 8)) { |
261 | cgit_cache_root = xstrdup(argv[i]+8); | 261 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
262 | } | 262 | } |
263 | if (!strcmp(argv[i], "--nocache")) { | 263 | if (!strcmp(argv[i], "--nocache")) { |
264 | cgit_nocache = 1; | 264 | ctx.cfg.nocache = 1; |
265 | } | 265 | } |
266 | if (!strncmp(argv[i], "--query=", 8)) { | 266 | if (!strncmp(argv[i], "--query=", 8)) { |
267 | ctx.qry.raw = xstrdup(argv[i]+8); | 267 | ctx.qry.raw = xstrdup(argv[i]+8); |
268 | } | 268 | } |
269 | if (!strncmp(argv[i], "--repo=", 7)) { | 269 | if (!strncmp(argv[i], "--repo=", 7)) { |
270 | ctx.qry.repo = xstrdup(argv[i]+7); | 270 | ctx.qry.repo = xstrdup(argv[i]+7); |
271 | } | 271 | } |
272 | if (!strncmp(argv[i], "--page=", 7)) { | 272 | if (!strncmp(argv[i], "--page=", 7)) { |
273 | ctx.qry.page = xstrdup(argv[i]+7); | 273 | ctx.qry.page = xstrdup(argv[i]+7); |
274 | } | 274 | } |
275 | if (!strncmp(argv[i], "--head=", 7)) { | 275 | if (!strncmp(argv[i], "--head=", 7)) { |
276 | ctx.qry.head = xstrdup(argv[i]+7); | 276 | ctx.qry.head = xstrdup(argv[i]+7); |
277 | ctx.qry.has_symref = 1; | 277 | ctx.qry.has_symref = 1; |
278 | } | 278 | } |
279 | if (!strncmp(argv[i], "--sha1=", 7)) { | 279 | if (!strncmp(argv[i], "--sha1=", 7)) { |
280 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 280 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
281 | ctx.qry.has_sha1 = 1; | 281 | ctx.qry.has_sha1 = 1; |
282 | } | 282 | } |
283 | if (!strncmp(argv[i], "--ofs=", 6)) { | 283 | if (!strncmp(argv[i], "--ofs=", 6)) { |
284 | ctx.qry.ofs = atoi(argv[i]+6); | 284 | ctx.qry.ofs = atoi(argv[i]+6); |
285 | } | 285 | } |
286 | } | 286 | } |
287 | } | 287 | } |
288 | 288 | ||
289 | int main(int argc, const char **argv) | 289 | int main(int argc, const char **argv) |
290 | { | 290 | { |
291 | struct cacheitem item; | 291 | struct cacheitem item; |
292 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 292 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
293 | 293 | ||
294 | cgit_prepare_context(&ctx); | ||
294 | htmlfd = STDOUT_FILENO; | 295 | htmlfd = STDOUT_FILENO; |
295 | item.st.st_mtime = time(NULL); | 296 | item.st.st_mtime = time(NULL); |
296 | cgit_repolist.length = 0; | 297 | cgit_repolist.length = 0; |
297 | cgit_repolist.count = 0; | 298 | cgit_repolist.count = 0; |
298 | cgit_repolist.repos = NULL; | 299 | cgit_repolist.repos = NULL; |
299 | 300 | ||
300 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 301 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
301 | cgit_global_config_cb); | 302 | cgit_global_config_cb); |
302 | cgit_repo = NULL; | 303 | cgit_repo = NULL; |
303 | if (getenv("SCRIPT_NAME")) | 304 | if (getenv("SCRIPT_NAME")) |
304 | cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); | 305 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
305 | if (getenv("QUERY_STRING")) | 306 | if (getenv("QUERY_STRING")) |
306 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 307 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
307 | cgit_parse_args(argc, argv); | 308 | cgit_parse_args(argc, argv); |
308 | cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); | 309 | cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); |
309 | if (!cgit_prepare_cache(&item)) | 310 | if (!cgit_prepare_cache(&item)) |
310 | return 0; | 311 | return 0; |
311 | if (cgit_nocache) { | 312 | if (ctx.cfg.nocache) { |
312 | cgit_fill_cache(&item, 0); | 313 | cgit_fill_cache(&item, 0); |
313 | } else { | 314 | } else { |
314 | cgit_check_cache(&item); | 315 | cgit_check_cache(&item); |
315 | cgit_print_cache(&item); | 316 | cgit_print_cache(&item); |
316 | } | 317 | } |
317 | return 0; | 318 | return 0; |
318 | } | 319 | } |
@@ -1,302 +1,304 @@ | |||
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 | * The valid cgit repo-commands | 23 | * The valid cgit repo-commands |
24 | */ | 24 | */ |
25 | #define CMD_LOG 1 | 25 | #define CMD_LOG 1 |
26 | #define CMD_COMMIT 2 | 26 | #define CMD_COMMIT 2 |
27 | #define CMD_DIFF 3 | 27 | #define CMD_DIFF 3 |
28 | #define CMD_TREE 4 | 28 | #define CMD_TREE 4 |
29 | #define CMD_BLOB 5 | 29 | #define CMD_BLOB 5 |
30 | #define CMD_SNAPSHOT 6 | 30 | #define CMD_SNAPSHOT 6 |
31 | #define CMD_TAG 7 | 31 | #define CMD_TAG 7 |
32 | #define CMD_REFS 8 | 32 | #define CMD_REFS 8 |
33 | #define CMD_PATCH 9 | 33 | #define CMD_PATCH 9 |
34 | 34 | ||
35 | /* | 35 | /* |
36 | * Dateformats used on misc. pages | 36 | * Dateformats used on misc. pages |
37 | */ | 37 | */ |
38 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" | 38 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" |
39 | #define FMT_SHORTDATE "%Y-%m-%d" | 39 | #define FMT_SHORTDATE "%Y-%m-%d" |
40 | 40 | ||
41 | 41 | ||
42 | /* | 42 | /* |
43 | * Limits used for relative dates | 43 | * Limits used for relative dates |
44 | */ | 44 | */ |
45 | #define TM_MIN 60 | 45 | #define TM_MIN 60 |
46 | #define TM_HOUR (TM_MIN * 60) | 46 | #define TM_HOUR (TM_MIN * 60) |
47 | #define TM_DAY (TM_HOUR * 24) | 47 | #define TM_DAY (TM_HOUR * 24) |
48 | #define TM_WEEK (TM_DAY * 7) | 48 | #define TM_WEEK (TM_DAY * 7) |
49 | #define TM_YEAR (TM_DAY * 365) | 49 | #define TM_YEAR (TM_DAY * 365) |
50 | #define TM_MONTH (TM_YEAR / 12.0) | 50 | #define TM_MONTH (TM_YEAR / 12.0) |
51 | 51 | ||
52 | 52 | ||
53 | /* | 53 | /* |
54 | * Default encoding | 54 | * Default encoding |
55 | */ | 55 | */ |
56 | #define PAGE_ENCODING "UTF-8" | 56 | #define PAGE_ENCODING "UTF-8" |
57 | 57 | ||
58 | typedef void (*configfn)(const char *name, const char *value); | 58 | typedef void (*configfn)(const char *name, const char *value); |
59 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 59 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
60 | typedef void (*linediff_fn)(char *line, int len); | 60 | typedef void (*linediff_fn)(char *line, int len); |
61 | 61 | ||
62 | struct cacheitem { | 62 | struct cacheitem { |
63 | char *name; | 63 | char *name; |
64 | struct stat st; | 64 | struct stat st; |
65 | int ttl; | 65 | int ttl; |
66 | int fd; | 66 | int fd; |
67 | }; | 67 | }; |
68 | 68 | ||
69 | struct repoinfo { | 69 | struct repoinfo { |
70 | char *url; | 70 | char *url; |
71 | char *name; | 71 | char *name; |
72 | char *path; | 72 | char *path; |
73 | char *desc; | 73 | char *desc; |
74 | char *owner; | 74 | char *owner; |
75 | char *defbranch; | 75 | char *defbranch; |
76 | char *group; | 76 | char *group; |
77 | char *module_link; | 77 | char *module_link; |
78 | char *readme; | 78 | char *readme; |
79 | char *clone_url; | 79 | char *clone_url; |
80 | int snapshots; | 80 | int snapshots; |
81 | int enable_log_filecount; | 81 | int enable_log_filecount; |
82 | int enable_log_linecount; | 82 | int enable_log_linecount; |
83 | }; | 83 | }; |
84 | 84 | ||
85 | struct repolist { | 85 | struct repolist { |
86 | int length; | 86 | int length; |
87 | int count; | 87 | int count; |
88 | struct repoinfo *repos; | 88 | struct repoinfo *repos; |
89 | }; | 89 | }; |
90 | 90 | ||
91 | struct commitinfo { | 91 | struct commitinfo { |
92 | struct commit *commit; | 92 | struct commit *commit; |
93 | char *author; | 93 | char *author; |
94 | char *author_email; | 94 | char *author_email; |
95 | unsigned long author_date; | 95 | unsigned long author_date; |
96 | char *committer; | 96 | char *committer; |
97 | char *committer_email; | 97 | char *committer_email; |
98 | unsigned long committer_date; | 98 | unsigned long committer_date; |
99 | char *subject; | 99 | char *subject; |
100 | char *msg; | 100 | char *msg; |
101 | char *msg_encoding; | 101 | char *msg_encoding; |
102 | }; | 102 | }; |
103 | 103 | ||
104 | struct taginfo { | 104 | struct taginfo { |
105 | char *tagger; | 105 | char *tagger; |
106 | char *tagger_email; | 106 | char *tagger_email; |
107 | int tagger_date; | 107 | int tagger_date; |
108 | char *msg; | 108 | char *msg; |
109 | }; | 109 | }; |
110 | 110 | ||
111 | struct refinfo { | 111 | struct refinfo { |
112 | const char *refname; | 112 | const char *refname; |
113 | struct object *object; | 113 | struct object *object; |
114 | union { | 114 | union { |
115 | struct taginfo *tag; | 115 | struct taginfo *tag; |
116 | struct commitinfo *commit; | 116 | struct commitinfo *commit; |
117 | }; | 117 | }; |
118 | }; | 118 | }; |
119 | 119 | ||
120 | struct reflist { | 120 | struct reflist { |
121 | struct refinfo **refs; | 121 | struct refinfo **refs; |
122 | int alloc; | 122 | int alloc; |
123 | int count; | 123 | int count; |
124 | }; | 124 | }; |
125 | 125 | ||
126 | struct cgit_query { | 126 | struct cgit_query { |
127 | int has_symref; | 127 | int has_symref; |
128 | int has_sha1; | 128 | int has_sha1; |
129 | char *raw; | 129 | char *raw; |
130 | char *repo; | 130 | char *repo; |
131 | char *page; | 131 | char *page; |
132 | char *search; | 132 | char *search; |
133 | char *grep; | 133 | char *grep; |
134 | char *head; | 134 | char *head; |
135 | char *sha1; | 135 | char *sha1; |
136 | char *sha2; | 136 | char *sha2; |
137 | char *path; | 137 | char *path; |
138 | char *name; | 138 | char *name; |
139 | int ofs; | 139 | int ofs; |
140 | }; | 140 | }; |
141 | 141 | ||
142 | struct cgit_config { | ||
143 | char *agefile; | ||
144 | char *cache_root; | ||
145 | char *clone_prefix; | ||
146 | char *css; | ||
147 | char *index_header; | ||
148 | char *index_info; | ||
149 | char *logo; | ||
150 | char *logo_link; | ||
151 | char *module_link; | ||
152 | char *repo_group; | ||
153 | char *robots; | ||
154 | char *root_title; | ||
155 | char *script_name; | ||
156 | char *virtual_root; | ||
157 | int cache_dynamic_ttl; | ||
158 | int cache_max_create_time; | ||
159 | int cache_repo_ttl; | ||
160 | int cache_root_ttl; | ||
161 | int cache_static_ttl; | ||
162 | int enable_index_links; | ||
163 | int enable_log_filecount; | ||
164 | int enable_log_linecount; | ||
165 | int max_commit_count; | ||
166 | int max_lock_attempts; | ||
167 | int max_msg_len; | ||
168 | int max_repodesc_len; | ||
169 | int nocache; | ||
170 | int renamelimit; | ||
171 | int snapshots; | ||
172 | int summary_branches; | ||
173 | int summary_log; | ||
174 | int summary_tags; | ||
175 | }; | ||
176 | |||
142 | struct cgit_context { | 177 | struct cgit_context { |
143 | struct cgit_query qry; | 178 | struct cgit_query qry; |
179 | struct cgit_config cfg; | ||
144 | }; | 180 | }; |
145 | 181 | ||
146 | extern const char *cgit_version; | 182 | extern const char *cgit_version; |
147 | 183 | ||
148 | extern struct repolist cgit_repolist; | 184 | extern struct repolist cgit_repolist; |
149 | extern struct repoinfo *cgit_repo; | 185 | extern struct repoinfo *cgit_repo; |
150 | extern struct cgit_context ctx; | 186 | extern struct cgit_context ctx; |
151 | extern int cgit_cmd; | 187 | extern int cgit_cmd; |
152 | 188 | ||
153 | extern char *cgit_root_title; | ||
154 | extern char *cgit_css; | ||
155 | extern char *cgit_logo; | ||
156 | extern char *cgit_index_header; | ||
157 | extern char *cgit_index_info; | ||
158 | extern char *cgit_logo_link; | ||
159 | extern char *cgit_module_link; | ||
160 | extern char *cgit_agefile; | ||
161 | extern char *cgit_virtual_root; | ||
162 | extern char *cgit_script_name; | ||
163 | extern char *cgit_cache_root; | ||
164 | extern char *cgit_repo_group; | ||
165 | extern char *cgit_robots; | ||
166 | extern char *cgit_clone_prefix; | ||
167 | |||
168 | extern int cgit_nocache; | ||
169 | extern int cgit_snapshots; | ||
170 | extern int cgit_enable_index_links; | ||
171 | extern int cgit_enable_log_filecount; | ||
172 | extern int cgit_enable_log_linecount; | ||
173 | extern int cgit_max_lock_attempts; | ||
174 | extern int cgit_cache_root_ttl; | ||
175 | extern int cgit_cache_repo_ttl; | ||
176 | extern int cgit_cache_dynamic_ttl; | ||
177 | extern int cgit_cache_static_ttl; | ||
178 | extern int cgit_cache_max_create_time; | ||
179 | extern int cgit_summary_log; | ||
180 | extern int cgit_summary_tags; | ||
181 | extern int cgit_summary_branches; | ||
182 | |||
183 | extern int cgit_max_msg_len; | ||
184 | extern int cgit_max_repodesc_len; | ||
185 | extern int cgit_max_commit_count; | ||
186 | |||
187 | |||
188 | extern int htmlfd; | 189 | extern int htmlfd; |
189 | 190 | ||
191 | extern void cgit_prepare_context(struct cgit_context *ctx); | ||
190 | extern int cgit_get_cmd_index(const char *cmd); | 192 | extern int cgit_get_cmd_index(const char *cmd); |
191 | extern struct repoinfo *cgit_get_repoinfo(const char *url); | 193 | extern struct repoinfo *cgit_get_repoinfo(const char *url); |
192 | extern void cgit_global_config_cb(const char *name, const char *value); | 194 | extern void cgit_global_config_cb(const char *name, const char *value); |
193 | extern void cgit_repo_config_cb(const char *name, const char *value); | 195 | extern void cgit_repo_config_cb(const char *name, const char *value); |
194 | extern void cgit_querystring_cb(const char *name, const char *value); | 196 | extern void cgit_querystring_cb(const char *name, const char *value); |
195 | 197 | ||
196 | extern int chk_zero(int result, char *msg); | 198 | extern int chk_zero(int result, char *msg); |
197 | extern int chk_positive(int result, char *msg); | 199 | extern int chk_positive(int result, char *msg); |
198 | extern int chk_non_negative(int result, char *msg); | 200 | extern int chk_non_negative(int result, char *msg); |
199 | 201 | ||
200 | extern int hextoint(char c); | 202 | extern int hextoint(char c); |
201 | extern char *trim_end(const char *str, char c); | 203 | extern char *trim_end(const char *str, char c); |
202 | extern char *strlpart(char *txt, int maxlen); | 204 | extern char *strlpart(char *txt, int maxlen); |
203 | extern char *strrpart(char *txt, int maxlen); | 205 | extern char *strrpart(char *txt, int maxlen); |
204 | 206 | ||
205 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 207 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
206 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 208 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
207 | int flags, void *cb_data); | 209 | int flags, void *cb_data); |
208 | 210 | ||
209 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 211 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
210 | 212 | ||
211 | extern int cgit_diff_files(const unsigned char *old_sha1, | 213 | extern int cgit_diff_files(const unsigned char *old_sha1, |
212 | const unsigned char *new_sha1, | 214 | const unsigned char *new_sha1, |
213 | linediff_fn fn); | 215 | linediff_fn fn); |
214 | 216 | ||
215 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 217 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
216 | const unsigned char *new_sha1, | 218 | const unsigned char *new_sha1, |
217 | filepair_fn fn, const char *prefix); | 219 | filepair_fn fn, const char *prefix); |
218 | 220 | ||
219 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 221 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
220 | 222 | ||
221 | extern char *fmt(const char *format,...); | 223 | extern char *fmt(const char *format,...); |
222 | 224 | ||
223 | extern void html(const char *txt); | 225 | extern void html(const char *txt); |
224 | extern void htmlf(const char *format,...); | 226 | extern void htmlf(const char *format,...); |
225 | extern void html_txt(char *txt); | 227 | extern void html_txt(char *txt); |
226 | extern void html_ntxt(int len, char *txt); | 228 | extern void html_ntxt(int len, char *txt); |
227 | extern void html_attr(char *txt); | 229 | extern void html_attr(char *txt); |
228 | extern void html_hidden(char *name, char *value); | 230 | extern void html_hidden(char *name, char *value); |
229 | extern void html_option(char *value, char *text, char *selected_value); | 231 | extern void html_option(char *value, char *text, char *selected_value); |
230 | extern void html_link_open(char *url, char *title, char *class); | 232 | extern void html_link_open(char *url, char *title, char *class); |
231 | extern void html_link_close(void); | 233 | extern void html_link_close(void); |
232 | extern void html_filemode(unsigned short mode); | 234 | extern void html_filemode(unsigned short mode); |
233 | extern int html_include(const char *filename); | 235 | extern int html_include(const char *filename); |
234 | 236 | ||
235 | extern int cgit_read_config(const char *filename, configfn fn); | 237 | extern int cgit_read_config(const char *filename, configfn fn); |
236 | extern int cgit_parse_query(char *txt, configfn fn); | 238 | extern int cgit_parse_query(char *txt, configfn fn); |
237 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 239 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
238 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 240 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
239 | extern void cgit_parse_url(const char *url); | 241 | extern void cgit_parse_url(const char *url); |
240 | 242 | ||
241 | extern char *cache_safe_filename(const char *unsafe); | 243 | extern char *cache_safe_filename(const char *unsafe); |
242 | extern int cache_lock(struct cacheitem *item); | 244 | extern int cache_lock(struct cacheitem *item); |
243 | extern int cache_unlock(struct cacheitem *item); | 245 | extern int cache_unlock(struct cacheitem *item); |
244 | extern int cache_cancel_lock(struct cacheitem *item); | 246 | extern int cache_cancel_lock(struct cacheitem *item); |
245 | extern int cache_exist(struct cacheitem *item); | 247 | extern int cache_exist(struct cacheitem *item); |
246 | extern int cache_expired(struct cacheitem *item); | 248 | extern int cache_expired(struct cacheitem *item); |
247 | 249 | ||
248 | extern char *cgit_repourl(const char *reponame); | 250 | extern char *cgit_repourl(const char *reponame); |
249 | extern char *cgit_fileurl(const char *reponame, const char *pagename, | 251 | extern char *cgit_fileurl(const char *reponame, const char *pagename, |
250 | const char *filename, const char *query); | 252 | const char *filename, const char *query); |
251 | extern char *cgit_pageurl(const char *reponame, const char *pagename, | 253 | extern char *cgit_pageurl(const char *reponame, const char *pagename, |
252 | const char *query); | 254 | const char *query); |
253 | 255 | ||
254 | extern const char *cgit_repobasename(const char *reponame); | 256 | extern const char *cgit_repobasename(const char *reponame); |
255 | 257 | ||
256 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, | 258 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, |
257 | char *rev, char *path); | 259 | char *rev, char *path); |
258 | extern void cgit_log_link(char *name, char *title, char *class, char *head, | 260 | extern void cgit_log_link(char *name, char *title, char *class, char *head, |
259 | char *rev, char *path, int ofs, char *grep, | 261 | char *rev, char *path, int ofs, char *grep, |
260 | char *pattern); | 262 | char *pattern); |
261 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, | 263 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, |
262 | char *rev); | 264 | char *rev); |
263 | extern void cgit_refs_link(char *name, char *title, char *class, char *head, | 265 | extern void cgit_refs_link(char *name, char *title, char *class, char *head, |
264 | char *rev, char *path); | 266 | char *rev, char *path); |
265 | extern void cgit_snapshot_link(char *name, char *title, char *class, | 267 | extern void cgit_snapshot_link(char *name, char *title, char *class, |
266 | char *head, char *rev, char *archivename); | 268 | char *head, char *rev, char *archivename); |
267 | extern void cgit_diff_link(char *name, char *title, char *class, char *head, | 269 | extern void cgit_diff_link(char *name, char *title, char *class, char *head, |
268 | char *new_rev, char *old_rev, char *path); | 270 | char *new_rev, char *old_rev, char *path); |
269 | 271 | ||
270 | extern void cgit_object_link(struct object *obj); | 272 | extern void cgit_object_link(struct object *obj); |
271 | 273 | ||
272 | extern void cgit_print_error(char *msg); | 274 | extern void cgit_print_error(char *msg); |
273 | extern void cgit_print_date(time_t secs, char *format); | 275 | extern void cgit_print_date(time_t secs, char *format); |
274 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); | 276 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); |
275 | extern void cgit_print_docstart(char *title, struct cacheitem *item); | 277 | extern void cgit_print_docstart(char *title, struct cacheitem *item); |
276 | extern void cgit_print_docend(); | 278 | extern void cgit_print_docend(); |
277 | extern void cgit_print_pageheader(char *title, int show_search); | 279 | extern void cgit_print_pageheader(char *title, int show_search); |
278 | extern void cgit_print_snapshot_start(const char *mimetype, | 280 | extern void cgit_print_snapshot_start(const char *mimetype, |
279 | const char *filename, | 281 | const char *filename, |
280 | struct cacheitem *item); | 282 | struct cacheitem *item); |
281 | extern void cgit_print_branches(int maxcount); | 283 | extern void cgit_print_branches(int maxcount); |
282 | extern void cgit_print_tags(int maxcount); | 284 | extern void cgit_print_tags(int maxcount); |
283 | 285 | ||
284 | extern void cgit_print_repolist(struct cacheitem *item); | 286 | extern void cgit_print_repolist(struct cacheitem *item); |
285 | extern void cgit_print_summary(); | 287 | extern void cgit_print_summary(); |
286 | extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, | 288 | extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, |
287 | char *pattern, char *path, int pager); | 289 | char *pattern, char *path, int pager); |
288 | extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); | 290 | extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); |
289 | extern void cgit_print_tree(const char *rev, char *path); | 291 | extern void cgit_print_tree(const char *rev, char *path); |
290 | extern void cgit_print_commit(char *hex); | 292 | extern void cgit_print_commit(char *hex); |
291 | extern void cgit_print_refs(); | 293 | extern void cgit_print_refs(); |
292 | extern void cgit_print_tag(char *revname); | 294 | extern void cgit_print_tag(char *revname); |
293 | extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); | 295 | extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); |
294 | extern void cgit_print_patch(char *hex, struct cacheitem *item); | 296 | extern void cgit_print_patch(char *hex, struct cacheitem *item); |
295 | extern void cgit_print_snapshot(struct cacheitem *item, const char *head, | 297 | extern void cgit_print_snapshot(struct cacheitem *item, const char *head, |
296 | const char *hex, const char *prefix, | 298 | const char *hex, const char *prefix, |
297 | const char *filename, int snapshot); | 299 | const char *filename, int snapshot); |
298 | extern void cgit_print_snapshot_links(const char *repo, const char *head, | 300 | extern void cgit_print_snapshot_links(const char *repo, const char *head, |
299 | const char *hex, int snapshots); | 301 | const char *hex, int snapshots); |
300 | extern int cgit_parse_snapshots_mask(const char *str); | 302 | extern int cgit_parse_snapshots_mask(const char *str); |
301 | 303 | ||
302 | #endif /* CGIT_H */ | 304 | #endif /* CGIT_H */ |
@@ -1,506 +1,494 @@ | |||
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 | ||
11 | struct repolist cgit_repolist; | 11 | struct repolist cgit_repolist; |
12 | struct repoinfo *cgit_repo; | 12 | struct repoinfo *cgit_repo; |
13 | struct cgit_context ctx; | 13 | struct cgit_context ctx; |
14 | int cgit_cmd; | 14 | int cgit_cmd; |
15 | 15 | ||
16 | const char *cgit_version = CGIT_VERSION; | 16 | const char *cgit_version = CGIT_VERSION; |
17 | 17 | ||
18 | char *cgit_root_title = "Git repository browser"; | ||
19 | char *cgit_css = "/cgit.css"; | ||
20 | char *cgit_logo = "/git-logo.png"; | ||
21 | char *cgit_index_header = NULL; | ||
22 | char *cgit_index_info = NULL; | ||
23 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; | ||
24 | char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; | ||
25 | char *cgit_agefile = "info/web/last-modified"; | ||
26 | char *cgit_virtual_root = NULL; | ||
27 | char *cgit_script_name = CGIT_SCRIPT_NAME; | ||
28 | char *cgit_cache_root = CGIT_CACHE_ROOT; | ||
29 | char *cgit_repo_group = NULL; | ||
30 | char *cgit_robots = "index, nofollow"; | ||
31 | char *cgit_clone_prefix = NULL; | ||
32 | |||
33 | int cgit_nocache = 0; | ||
34 | int cgit_snapshots = 0; | ||
35 | int cgit_enable_index_links = 0; | ||
36 | int cgit_enable_log_filecount = 0; | ||
37 | int cgit_enable_log_linecount = 0; | ||
38 | int cgit_max_lock_attempts = 5; | ||
39 | int cgit_cache_root_ttl = 5; | ||
40 | int cgit_cache_repo_ttl = 5; | ||
41 | int cgit_cache_dynamic_ttl = 5; | ||
42 | int cgit_cache_static_ttl = -1; | ||
43 | int cgit_cache_max_create_time = 5; | ||
44 | int cgit_summary_log = 0; | ||
45 | int cgit_summary_tags = 0; | ||
46 | int cgit_summary_branches = 0; | ||
47 | int cgit_renamelimit = -1; | ||
48 | |||
49 | int cgit_max_msg_len = 60; | ||
50 | int cgit_max_repodesc_len = 60; | ||
51 | int cgit_max_commit_count = 50; | ||
52 | |||
53 | int htmlfd = 0; | 18 | int htmlfd = 0; |
54 | 19 | ||
20 | void cgit_prepare_context(struct cgit_context *ctx) | ||
21 | { | ||
22 | memset(ctx, 0, sizeof(ctx)); | ||
23 | ctx->cfg.agefile = "info/web/last-modified"; | ||
24 | ctx->cfg.cache_dynamic_ttl = 5; | ||
25 | ctx->cfg.cache_max_create_time = 5; | ||
26 | ctx->cfg.cache_repo_ttl = 5; | ||
27 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
28 | ctx->cfg.cache_root_ttl = 5; | ||
29 | ctx->cfg.cache_static_ttl = -1; | ||
30 | ctx->cfg.css = "/cgit.css"; | ||
31 | ctx->cfg.logo = "/git-logo.png"; | ||
32 | ctx->cfg.max_commit_count = 50; | ||
33 | ctx->cfg.max_lock_attempts = 5; | ||
34 | ctx->cfg.max_msg_len = 60; | ||
35 | ctx->cfg.max_repodesc_len = 60; | ||
36 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
37 | ctx->cfg.renamelimit = -1; | ||
38 | ctx->cfg.robots = "index, nofollow"; | ||
39 | ctx->cfg.root_title = "Git repository browser"; | ||
40 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
41 | } | ||
42 | |||
55 | int cgit_get_cmd_index(const char *cmd) | 43 | int cgit_get_cmd_index(const char *cmd) |
56 | { | 44 | { |
57 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", | 45 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
58 | "snapshot", "tag", "refs", "patch", NULL}; | 46 | "snapshot", "tag", "refs", "patch", NULL}; |
59 | int i; | 47 | int i; |
60 | 48 | ||
61 | for(i = 0; cmds[i]; i++) | 49 | for(i = 0; cmds[i]; i++) |
62 | if (!strcmp(cmd, cmds[i])) | 50 | if (!strcmp(cmd, cmds[i])) |
63 | return i + 1; | 51 | return i + 1; |
64 | return 0; | 52 | return 0; |
65 | } | 53 | } |
66 | 54 | ||
67 | int chk_zero(int result, char *msg) | 55 | int chk_zero(int result, char *msg) |
68 | { | 56 | { |
69 | if (result != 0) | 57 | if (result != 0) |
70 | die("%s: %s", msg, strerror(errno)); | 58 | die("%s: %s", msg, strerror(errno)); |
71 | return result; | 59 | return result; |
72 | } | 60 | } |
73 | 61 | ||
74 | int chk_positive(int result, char *msg) | 62 | int chk_positive(int result, char *msg) |
75 | { | 63 | { |
76 | if (result <= 0) | 64 | if (result <= 0) |
77 | die("%s: %s", msg, strerror(errno)); | 65 | die("%s: %s", msg, strerror(errno)); |
78 | return result; | 66 | return result; |
79 | } | 67 | } |
80 | 68 | ||
81 | int chk_non_negative(int result, char *msg) | 69 | int chk_non_negative(int result, char *msg) |
82 | { | 70 | { |
83 | if (result < 0) | 71 | if (result < 0) |
84 | die("%s: %s",msg, strerror(errno)); | 72 | die("%s: %s",msg, strerror(errno)); |
85 | return result; | 73 | return result; |
86 | } | 74 | } |
87 | 75 | ||
88 | struct repoinfo *add_repo(const char *url) | 76 | struct repoinfo *add_repo(const char *url) |
89 | { | 77 | { |
90 | struct repoinfo *ret; | 78 | struct repoinfo *ret; |
91 | 79 | ||
92 | if (++cgit_repolist.count > cgit_repolist.length) { | 80 | if (++cgit_repolist.count > cgit_repolist.length) { |
93 | if (cgit_repolist.length == 0) | 81 | if (cgit_repolist.length == 0) |
94 | cgit_repolist.length = 8; | 82 | cgit_repolist.length = 8; |
95 | else | 83 | else |
96 | cgit_repolist.length *= 2; | 84 | cgit_repolist.length *= 2; |
97 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 85 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
98 | cgit_repolist.length * | 86 | cgit_repolist.length * |
99 | sizeof(struct repoinfo)); | 87 | sizeof(struct repoinfo)); |
100 | } | 88 | } |
101 | 89 | ||
102 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 90 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
103 | ret->url = trim_end(url, '/'); | 91 | ret->url = trim_end(url, '/'); |
104 | ret->name = ret->url; | 92 | ret->name = ret->url; |
105 | ret->path = NULL; | 93 | ret->path = NULL; |
106 | ret->desc = "[no description]"; | 94 | ret->desc = "[no description]"; |
107 | ret->owner = NULL; | 95 | ret->owner = NULL; |
108 | ret->group = cgit_repo_group; | 96 | ret->group = ctx.cfg.repo_group; |
109 | ret->defbranch = "master"; | 97 | ret->defbranch = "master"; |
110 | ret->snapshots = cgit_snapshots; | 98 | ret->snapshots = ctx.cfg.snapshots; |
111 | ret->enable_log_filecount = cgit_enable_log_filecount; | 99 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
112 | ret->enable_log_linecount = cgit_enable_log_linecount; | 100 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
113 | ret->module_link = cgit_module_link; | 101 | ret->module_link = ctx.cfg.module_link; |
114 | ret->readme = NULL; | 102 | ret->readme = NULL; |
115 | return ret; | 103 | return ret; |
116 | } | 104 | } |
117 | 105 | ||
118 | struct repoinfo *cgit_get_repoinfo(const char *url) | 106 | struct repoinfo *cgit_get_repoinfo(const char *url) |
119 | { | 107 | { |
120 | int i; | 108 | int i; |
121 | struct repoinfo *repo; | 109 | struct repoinfo *repo; |
122 | 110 | ||
123 | for (i=0; i<cgit_repolist.count; i++) { | 111 | for (i=0; i<cgit_repolist.count; i++) { |
124 | repo = &cgit_repolist.repos[i]; | 112 | repo = &cgit_repolist.repos[i]; |
125 | if (!strcmp(repo->url, url)) | 113 | if (!strcmp(repo->url, url)) |
126 | return repo; | 114 | return repo; |
127 | } | 115 | } |
128 | return NULL; | 116 | return NULL; |
129 | } | 117 | } |
130 | 118 | ||
131 | void cgit_global_config_cb(const char *name, const char *value) | 119 | void cgit_global_config_cb(const char *name, const char *value) |
132 | { | 120 | { |
133 | if (!strcmp(name, "root-title")) | 121 | if (!strcmp(name, "root-title")) |
134 | cgit_root_title = xstrdup(value); | 122 | ctx.cfg.root_title = xstrdup(value); |
135 | else if (!strcmp(name, "css")) | 123 | else if (!strcmp(name, "css")) |
136 | cgit_css = xstrdup(value); | 124 | ctx.cfg.css = xstrdup(value); |
137 | else if (!strcmp(name, "logo")) | 125 | else if (!strcmp(name, "logo")) |
138 | cgit_logo = xstrdup(value); | 126 | ctx.cfg.logo = xstrdup(value); |
139 | else if (!strcmp(name, "index-header")) | 127 | else if (!strcmp(name, "index-header")) |
140 | cgit_index_header = xstrdup(value); | 128 | ctx.cfg.index_header = xstrdup(value); |
141 | else if (!strcmp(name, "index-info")) | 129 | else if (!strcmp(name, "index-info")) |
142 | cgit_index_info = xstrdup(value); | 130 | ctx.cfg.index_info = xstrdup(value); |
143 | else if (!strcmp(name, "logo-link")) | 131 | else if (!strcmp(name, "logo-link")) |
144 | cgit_logo_link = xstrdup(value); | 132 | ctx.cfg.logo_link = xstrdup(value); |
145 | else if (!strcmp(name, "module-link")) | 133 | else if (!strcmp(name, "module-link")) |
146 | cgit_module_link = xstrdup(value); | 134 | ctx.cfg.module_link = xstrdup(value); |
147 | else if (!strcmp(name, "virtual-root")) { | 135 | else if (!strcmp(name, "virtual-root")) { |
148 | cgit_virtual_root = trim_end(value, '/'); | 136 | ctx.cfg.virtual_root = trim_end(value, '/'); |
149 | if (!cgit_virtual_root && (!strcmp(value, "/"))) | 137 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
150 | cgit_virtual_root = ""; | 138 | ctx.cfg.virtual_root = ""; |
151 | } else if (!strcmp(name, "nocache")) | 139 | } else if (!strcmp(name, "nocache")) |
152 | cgit_nocache = atoi(value); | 140 | ctx.cfg.nocache = atoi(value); |
153 | else if (!strcmp(name, "snapshots")) | 141 | else if (!strcmp(name, "snapshots")) |
154 | cgit_snapshots = cgit_parse_snapshots_mask(value); | 142 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
155 | else if (!strcmp(name, "enable-index-links")) | 143 | else if (!strcmp(name, "enable-index-links")) |
156 | cgit_enable_index_links = atoi(value); | 144 | ctx.cfg.enable_index_links = atoi(value); |
157 | else if (!strcmp(name, "enable-log-filecount")) | 145 | else if (!strcmp(name, "enable-log-filecount")) |
158 | cgit_enable_log_filecount = atoi(value); | 146 | ctx.cfg.enable_log_filecount = atoi(value); |
159 | else if (!strcmp(name, "enable-log-linecount")) | 147 | else if (!strcmp(name, "enable-log-linecount")) |
160 | cgit_enable_log_linecount = atoi(value); | 148 | ctx.cfg.enable_log_linecount = atoi(value); |
161 | else if (!strcmp(name, "cache-root")) | 149 | else if (!strcmp(name, "cache-root")) |
162 | cgit_cache_root = xstrdup(value); | 150 | ctx.cfg.cache_root = xstrdup(value); |
163 | else if (!strcmp(name, "cache-root-ttl")) | 151 | else if (!strcmp(name, "cache-root-ttl")) |
164 | cgit_cache_root_ttl = atoi(value); | 152 | ctx.cfg.cache_root_ttl = atoi(value); |
165 | else if (!strcmp(name, "cache-repo-ttl")) | 153 | else if (!strcmp(name, "cache-repo-ttl")) |
166 | cgit_cache_repo_ttl = atoi(value); | 154 | ctx.cfg.cache_repo_ttl = atoi(value); |
167 | else if (!strcmp(name, "cache-static-ttl")) | 155 | else if (!strcmp(name, "cache-static-ttl")) |
168 | cgit_cache_static_ttl = atoi(value); | 156 | ctx.cfg.cache_static_ttl = atoi(value); |
169 | else if (!strcmp(name, "cache-dynamic-ttl")) | 157 | else if (!strcmp(name, "cache-dynamic-ttl")) |
170 | cgit_cache_dynamic_ttl = atoi(value); | 158 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
171 | else if (!strcmp(name, "max-message-length")) | 159 | else if (!strcmp(name, "max-message-length")) |
172 | cgit_max_msg_len = atoi(value); | 160 | ctx.cfg.max_msg_len = atoi(value); |
173 | else if (!strcmp(name, "max-repodesc-length")) | 161 | else if (!strcmp(name, "max-repodesc-length")) |
174 | cgit_max_repodesc_len = atoi(value); | 162 | ctx.cfg.max_repodesc_len = atoi(value); |
175 | else if (!strcmp(name, "max-commit-count")) | 163 | else if (!strcmp(name, "max-commit-count")) |
176 | cgit_max_commit_count = atoi(value); | 164 | ctx.cfg.max_commit_count = atoi(value); |
177 | else if (!strcmp(name, "summary-log")) | 165 | else if (!strcmp(name, "summary-log")) |
178 | cgit_summary_log = atoi(value); | 166 | ctx.cfg.summary_log = atoi(value); |
179 | else if (!strcmp(name, "summary-branches")) | 167 | else if (!strcmp(name, "summary-branches")) |
180 | cgit_summary_branches = atoi(value); | 168 | ctx.cfg.summary_branches = atoi(value); |
181 | else if (!strcmp(name, "summary-tags")) | 169 | else if (!strcmp(name, "summary-tags")) |
182 | cgit_summary_tags = atoi(value); | 170 | ctx.cfg.summary_tags = atoi(value); |
183 | else if (!strcmp(name, "agefile")) | 171 | else if (!strcmp(name, "agefile")) |
184 | cgit_agefile = xstrdup(value); | 172 | ctx.cfg.agefile = xstrdup(value); |
185 | else if (!strcmp(name, "renamelimit")) | 173 | else if (!strcmp(name, "renamelimit")) |
186 | cgit_renamelimit = atoi(value); | 174 | ctx.cfg.renamelimit = atoi(value); |
187 | else if (!strcmp(name, "robots")) | 175 | else if (!strcmp(name, "robots")) |
188 | cgit_robots = xstrdup(value); | 176 | ctx.cfg.robots = xstrdup(value); |
189 | else if (!strcmp(name, "clone-prefix")) | 177 | else if (!strcmp(name, "clone-prefix")) |
190 | cgit_clone_prefix = xstrdup(value); | 178 | ctx.cfg.clone_prefix = xstrdup(value); |
191 | else if (!strcmp(name, "repo.group")) | 179 | else if (!strcmp(name, "repo.group")) |
192 | cgit_repo_group = xstrdup(value); | 180 | ctx.cfg.repo_group = xstrdup(value); |
193 | else if (!strcmp(name, "repo.url")) | 181 | else if (!strcmp(name, "repo.url")) |
194 | cgit_repo = add_repo(value); | 182 | cgit_repo = add_repo(value); |
195 | else if (!strcmp(name, "repo.name")) | 183 | else if (!strcmp(name, "repo.name")) |
196 | cgit_repo->name = xstrdup(value); | 184 | cgit_repo->name = xstrdup(value); |
197 | else if (cgit_repo && !strcmp(name, "repo.path")) | 185 | else if (cgit_repo && !strcmp(name, "repo.path")) |
198 | cgit_repo->path = trim_end(value, '/'); | 186 | cgit_repo->path = trim_end(value, '/'); |
199 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) | 187 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) |
200 | cgit_repo->clone_url = xstrdup(value); | 188 | cgit_repo->clone_url = xstrdup(value); |
201 | else if (cgit_repo && !strcmp(name, "repo.desc")) | 189 | else if (cgit_repo && !strcmp(name, "repo.desc")) |
202 | cgit_repo->desc = xstrdup(value); | 190 | cgit_repo->desc = xstrdup(value); |
203 | else if (cgit_repo && !strcmp(name, "repo.owner")) | 191 | else if (cgit_repo && !strcmp(name, "repo.owner")) |
204 | cgit_repo->owner = xstrdup(value); | 192 | cgit_repo->owner = xstrdup(value); |
205 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) | 193 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) |
206 | cgit_repo->defbranch = xstrdup(value); | 194 | cgit_repo->defbranch = xstrdup(value); |
207 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) | 195 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) |
208 | cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 196 | cgit_repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
209 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) | 197 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) |
210 | cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); | 198 | cgit_repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
211 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) | 199 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) |
212 | cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); | 200 | cgit_repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
213 | else if (cgit_repo && !strcmp(name, "repo.module-link")) | 201 | else if (cgit_repo && !strcmp(name, "repo.module-link")) |
214 | cgit_repo->module_link= xstrdup(value); | 202 | cgit_repo->module_link= xstrdup(value); |
215 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { | 203 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { |
216 | if (*value == '/') | 204 | if (*value == '/') |
217 | cgit_repo->readme = xstrdup(value); | 205 | cgit_repo->readme = xstrdup(value); |
218 | else | 206 | else |
219 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); | 207 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); |
220 | } else if (!strcmp(name, "include")) | 208 | } else if (!strcmp(name, "include")) |
221 | cgit_read_config(value, cgit_global_config_cb); | 209 | cgit_read_config(value, cgit_global_config_cb); |
222 | } | 210 | } |
223 | 211 | ||
224 | void cgit_querystring_cb(const char *name, const char *value) | 212 | void cgit_querystring_cb(const char *name, const char *value) |
225 | { | 213 | { |
226 | if (!strcmp(name,"r")) { | 214 | if (!strcmp(name,"r")) { |
227 | ctx.qry.repo = xstrdup(value); | 215 | ctx.qry.repo = xstrdup(value); |
228 | cgit_repo = cgit_get_repoinfo(value); | 216 | cgit_repo = cgit_get_repoinfo(value); |
229 | } else if (!strcmp(name, "p")) { | 217 | } else if (!strcmp(name, "p")) { |
230 | ctx.qry.page = xstrdup(value); | 218 | ctx.qry.page = xstrdup(value); |
231 | cgit_cmd = cgit_get_cmd_index(value); | 219 | cgit_cmd = cgit_get_cmd_index(value); |
232 | } else if (!strcmp(name, "url")) { | 220 | } else if (!strcmp(name, "url")) { |
233 | cgit_parse_url(value); | 221 | cgit_parse_url(value); |
234 | } else if (!strcmp(name, "qt")) { | 222 | } else if (!strcmp(name, "qt")) { |
235 | ctx.qry.grep = xstrdup(value); | 223 | ctx.qry.grep = xstrdup(value); |
236 | } else if (!strcmp(name, "q")) { | 224 | } else if (!strcmp(name, "q")) { |
237 | ctx.qry.search = xstrdup(value); | 225 | ctx.qry.search = xstrdup(value); |
238 | } else if (!strcmp(name, "h")) { | 226 | } else if (!strcmp(name, "h")) { |
239 | ctx.qry.head = xstrdup(value); | 227 | ctx.qry.head = xstrdup(value); |
240 | ctx.qry.has_symref = 1; | 228 | ctx.qry.has_symref = 1; |
241 | } else if (!strcmp(name, "id")) { | 229 | } else if (!strcmp(name, "id")) { |
242 | ctx.qry.sha1 = xstrdup(value); | 230 | ctx.qry.sha1 = xstrdup(value); |
243 | ctx.qry.has_sha1 = 1; | 231 | ctx.qry.has_sha1 = 1; |
244 | } else if (!strcmp(name, "id2")) { | 232 | } else if (!strcmp(name, "id2")) { |
245 | ctx.qry.sha2 = xstrdup(value); | 233 | ctx.qry.sha2 = xstrdup(value); |
246 | ctx.qry.has_sha1 = 1; | 234 | ctx.qry.has_sha1 = 1; |
247 | } else if (!strcmp(name, "ofs")) { | 235 | } else if (!strcmp(name, "ofs")) { |
248 | ctx.qry.ofs = atoi(value); | 236 | ctx.qry.ofs = atoi(value); |
249 | } else if (!strcmp(name, "path")) { | 237 | } else if (!strcmp(name, "path")) { |
250 | ctx.qry.path = trim_end(value, '/'); | 238 | ctx.qry.path = trim_end(value, '/'); |
251 | } else if (!strcmp(name, "name")) { | 239 | } else if (!strcmp(name, "name")) { |
252 | ctx.qry.name = xstrdup(value); | 240 | ctx.qry.name = xstrdup(value); |
253 | } | 241 | } |
254 | } | 242 | } |
255 | 243 | ||
256 | void *cgit_free_commitinfo(struct commitinfo *info) | 244 | void *cgit_free_commitinfo(struct commitinfo *info) |
257 | { | 245 | { |
258 | free(info->author); | 246 | free(info->author); |
259 | free(info->author_email); | 247 | free(info->author_email); |
260 | free(info->committer); | 248 | free(info->committer); |
261 | free(info->committer_email); | 249 | free(info->committer_email); |
262 | free(info->subject); | 250 | free(info->subject); |
263 | free(info->msg); | 251 | free(info->msg); |
264 | free(info->msg_encoding); | 252 | free(info->msg_encoding); |
265 | free(info); | 253 | free(info); |
266 | return NULL; | 254 | return NULL; |
267 | } | 255 | } |
268 | 256 | ||
269 | int hextoint(char c) | 257 | int hextoint(char c) |
270 | { | 258 | { |
271 | if (c >= 'a' && c <= 'f') | 259 | if (c >= 'a' && c <= 'f') |
272 | return 10 + c - 'a'; | 260 | return 10 + c - 'a'; |
273 | else if (c >= 'A' && c <= 'F') | 261 | else if (c >= 'A' && c <= 'F') |
274 | return 10 + c - 'A'; | 262 | return 10 + c - 'A'; |
275 | else if (c >= '0' && c <= '9') | 263 | else if (c >= '0' && c <= '9') |
276 | return c - '0'; | 264 | return c - '0'; |
277 | else | 265 | else |
278 | return -1; | 266 | return -1; |
279 | } | 267 | } |
280 | 268 | ||
281 | char *trim_end(const char *str, char c) | 269 | char *trim_end(const char *str, char c) |
282 | { | 270 | { |
283 | int len; | 271 | int len; |
284 | char *s, *t; | 272 | char *s, *t; |
285 | 273 | ||
286 | if (str == NULL) | 274 | if (str == NULL) |
287 | return NULL; | 275 | return NULL; |
288 | t = (char *)str; | 276 | t = (char *)str; |
289 | len = strlen(t); | 277 | len = strlen(t); |
290 | while(len > 0 && t[len - 1] == c) | 278 | while(len > 0 && t[len - 1] == c) |
291 | len--; | 279 | len--; |
292 | 280 | ||
293 | if (len == 0) | 281 | if (len == 0) |
294 | return NULL; | 282 | return NULL; |
295 | 283 | ||
296 | c = t[len]; | 284 | c = t[len]; |
297 | t[len] = '\0'; | 285 | t[len] = '\0'; |
298 | s = xstrdup(t); | 286 | s = xstrdup(t); |
299 | t[len] = c; | 287 | t[len] = c; |
300 | return s; | 288 | return s; |
301 | } | 289 | } |
302 | 290 | ||
303 | char *strlpart(char *txt, int maxlen) | 291 | char *strlpart(char *txt, int maxlen) |
304 | { | 292 | { |
305 | char *result; | 293 | char *result; |
306 | 294 | ||
307 | if (!txt) | 295 | if (!txt) |
308 | return txt; | 296 | return txt; |
309 | 297 | ||
310 | if (strlen(txt) <= maxlen) | 298 | if (strlen(txt) <= maxlen) |
311 | return txt; | 299 | return txt; |
312 | result = xmalloc(maxlen + 1); | 300 | result = xmalloc(maxlen + 1); |
313 | memcpy(result, txt, maxlen - 3); | 301 | memcpy(result, txt, maxlen - 3); |
314 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 302 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
315 | result[maxlen] = '\0'; | 303 | result[maxlen] = '\0'; |
316 | return result; | 304 | return result; |
317 | } | 305 | } |
318 | 306 | ||
319 | char *strrpart(char *txt, int maxlen) | 307 | char *strrpart(char *txt, int maxlen) |
320 | { | 308 | { |
321 | char *result; | 309 | char *result; |
322 | 310 | ||
323 | if (!txt) | 311 | if (!txt) |
324 | return txt; | 312 | return txt; |
325 | 313 | ||
326 | if (strlen(txt) <= maxlen) | 314 | if (strlen(txt) <= maxlen) |
327 | return txt; | 315 | return txt; |
328 | result = xmalloc(maxlen + 1); | 316 | result = xmalloc(maxlen + 1); |
329 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); | 317 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
330 | result[0] = result[1] = result[2] = '.'; | 318 | result[0] = result[1] = result[2] = '.'; |
331 | return result; | 319 | return result; |
332 | } | 320 | } |
333 | 321 | ||
334 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) | 322 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
335 | { | 323 | { |
336 | size_t size; | 324 | size_t size; |
337 | 325 | ||
338 | if (list->count >= list->alloc) { | 326 | if (list->count >= list->alloc) { |
339 | list->alloc += (list->alloc ? list->alloc : 4); | 327 | list->alloc += (list->alloc ? list->alloc : 4); |
340 | size = list->alloc * sizeof(struct refinfo *); | 328 | size = list->alloc * sizeof(struct refinfo *); |
341 | list->refs = xrealloc(list->refs, size); | 329 | list->refs = xrealloc(list->refs, size); |
342 | } | 330 | } |
343 | list->refs[list->count++] = ref; | 331 | list->refs[list->count++] = ref; |
344 | } | 332 | } |
345 | 333 | ||
346 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) | 334 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
347 | { | 335 | { |
348 | struct refinfo *ref; | 336 | struct refinfo *ref; |
349 | 337 | ||
350 | ref = xmalloc(sizeof (struct refinfo)); | 338 | ref = xmalloc(sizeof (struct refinfo)); |
351 | ref->refname = xstrdup(refname); | 339 | ref->refname = xstrdup(refname); |
352 | ref->object = parse_object(sha1); | 340 | ref->object = parse_object(sha1); |
353 | switch (ref->object->type) { | 341 | switch (ref->object->type) { |
354 | case OBJ_TAG: | 342 | case OBJ_TAG: |
355 | ref->tag = cgit_parse_tag((struct tag *)ref->object); | 343 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
356 | break; | 344 | break; |
357 | case OBJ_COMMIT: | 345 | case OBJ_COMMIT: |
358 | ref->commit = cgit_parse_commit((struct commit *)ref->object); | 346 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
359 | break; | 347 | break; |
360 | } | 348 | } |
361 | return ref; | 349 | return ref; |
362 | } | 350 | } |
363 | 351 | ||
364 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, | 352 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
365 | void *cb_data) | 353 | void *cb_data) |
366 | { | 354 | { |
367 | struct reflist *list = (struct reflist *)cb_data; | 355 | struct reflist *list = (struct reflist *)cb_data; |
368 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); | 356 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
369 | 357 | ||
370 | if (info) | 358 | if (info) |
371 | cgit_add_ref(list, info); | 359 | cgit_add_ref(list, info); |
372 | return 0; | 360 | return 0; |
373 | } | 361 | } |
374 | 362 | ||
375 | void cgit_diff_tree_cb(struct diff_queue_struct *q, | 363 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
376 | struct diff_options *options, void *data) | 364 | struct diff_options *options, void *data) |
377 | { | 365 | { |
378 | int i; | 366 | int i; |
379 | 367 | ||
380 | for (i = 0; i < q->nr; i++) { | 368 | for (i = 0; i < q->nr; i++) { |
381 | if (q->queue[i]->status == 'U') | 369 | if (q->queue[i]->status == 'U') |
382 | continue; | 370 | continue; |
383 | ((filepair_fn)data)(q->queue[i]); | 371 | ((filepair_fn)data)(q->queue[i]); |
384 | } | 372 | } |
385 | } | 373 | } |
386 | 374 | ||
387 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) | 375 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
388 | { | 376 | { |
389 | enum object_type type; | 377 | enum object_type type; |
390 | 378 | ||
391 | if (is_null_sha1(sha1)) { | 379 | if (is_null_sha1(sha1)) { |
392 | file->ptr = (char *)""; | 380 | file->ptr = (char *)""; |
393 | file->size = 0; | 381 | file->size = 0; |
394 | } else { | 382 | } else { |
395 | file->ptr = read_sha1_file(sha1, &type, | 383 | file->ptr = read_sha1_file(sha1, &type, |
396 | (unsigned long *)&file->size); | 384 | (unsigned long *)&file->size); |
397 | } | 385 | } |
398 | return 1; | 386 | return 1; |
399 | } | 387 | } |
400 | 388 | ||
401 | /* | 389 | /* |
402 | * Receive diff-buffers from xdiff and concatenate them as | 390 | * Receive diff-buffers from xdiff and concatenate them as |
403 | * needed across multiple callbacks. | 391 | * needed across multiple callbacks. |
404 | * | 392 | * |
405 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), | 393 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
406 | * ripped from git and modified to use globals instead of | 394 | * ripped from git and modified to use globals instead of |
407 | * a special callback-struct. | 395 | * a special callback-struct. |
408 | */ | 396 | */ |
409 | char *diffbuf = NULL; | 397 | char *diffbuf = NULL; |
410 | int buflen = 0; | 398 | int buflen = 0; |
411 | 399 | ||
412 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) | 400 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
413 | { | 401 | { |
414 | int i; | 402 | int i; |
415 | 403 | ||
416 | for (i = 0; i < nbuf; i++) { | 404 | for (i = 0; i < nbuf; i++) { |
417 | if (mb[i].ptr[mb[i].size-1] != '\n') { | 405 | if (mb[i].ptr[mb[i].size-1] != '\n') { |
418 | /* Incomplete line */ | 406 | /* Incomplete line */ |
419 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 407 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
420 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 408 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
421 | buflen += mb[i].size; | 409 | buflen += mb[i].size; |
422 | continue; | 410 | continue; |
423 | } | 411 | } |
424 | 412 | ||
425 | /* we have a complete line */ | 413 | /* we have a complete line */ |
426 | if (!diffbuf) { | 414 | if (!diffbuf) { |
427 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); | 415 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); |
428 | continue; | 416 | continue; |
429 | } | 417 | } |
430 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 418 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
431 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 419 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
432 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); | 420 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); |
433 | free(diffbuf); | 421 | free(diffbuf); |
434 | diffbuf = NULL; | 422 | diffbuf = NULL; |
435 | buflen = 0; | 423 | buflen = 0; |
436 | } | 424 | } |
437 | if (diffbuf) { | 425 | if (diffbuf) { |
438 | ((linediff_fn)priv)(diffbuf, buflen); | 426 | ((linediff_fn)priv)(diffbuf, buflen); |
439 | free(diffbuf); | 427 | free(diffbuf); |
440 | diffbuf = NULL; | 428 | diffbuf = NULL; |
441 | buflen = 0; | 429 | buflen = 0; |
442 | } | 430 | } |
443 | return 0; | 431 | return 0; |
444 | } | 432 | } |
445 | 433 | ||
446 | int cgit_diff_files(const unsigned char *old_sha1, | 434 | int cgit_diff_files(const unsigned char *old_sha1, |
447 | const unsigned char *new_sha1, | 435 | const unsigned char *new_sha1, |
448 | linediff_fn fn) | 436 | linediff_fn fn) |
449 | { | 437 | { |
450 | mmfile_t file1, file2; | 438 | mmfile_t file1, file2; |
451 | xpparam_t diff_params; | 439 | xpparam_t diff_params; |
452 | xdemitconf_t emit_params; | 440 | xdemitconf_t emit_params; |
453 | xdemitcb_t emit_cb; | 441 | xdemitcb_t emit_cb; |
454 | 442 | ||
455 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) | 443 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) |
456 | return 1; | 444 | return 1; |
457 | 445 | ||
458 | diff_params.flags = XDF_NEED_MINIMAL; | 446 | diff_params.flags = XDF_NEED_MINIMAL; |
459 | emit_params.ctxlen = 3; | 447 | emit_params.ctxlen = 3; |
460 | emit_params.flags = XDL_EMIT_FUNCNAMES; | 448 | emit_params.flags = XDL_EMIT_FUNCNAMES; |
461 | emit_params.find_func = NULL; | 449 | emit_params.find_func = NULL; |
462 | emit_cb.outf = filediff_cb; | 450 | emit_cb.outf = filediff_cb; |
463 | emit_cb.priv = fn; | 451 | emit_cb.priv = fn; |
464 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); | 452 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); |
465 | return 0; | 453 | return 0; |
466 | } | 454 | } |
467 | 455 | ||
468 | void cgit_diff_tree(const unsigned char *old_sha1, | 456 | void cgit_diff_tree(const unsigned char *old_sha1, |
469 | const unsigned char *new_sha1, | 457 | const unsigned char *new_sha1, |
470 | filepair_fn fn, const char *prefix) | 458 | filepair_fn fn, const char *prefix) |
471 | { | 459 | { |
472 | struct diff_options opt; | 460 | struct diff_options opt; |
473 | int ret; | 461 | int ret; |
474 | int prefixlen; | 462 | int prefixlen; |
475 | 463 | ||
476 | diff_setup(&opt); | 464 | diff_setup(&opt); |
477 | opt.output_format = DIFF_FORMAT_CALLBACK; | 465 | opt.output_format = DIFF_FORMAT_CALLBACK; |
478 | opt.detect_rename = 1; | 466 | opt.detect_rename = 1; |
479 | opt.rename_limit = cgit_renamelimit; | 467 | opt.rename_limit = ctx.cfg.renamelimit; |
480 | DIFF_OPT_SET(&opt, RECURSIVE); | 468 | DIFF_OPT_SET(&opt, RECURSIVE); |
481 | opt.format_callback = cgit_diff_tree_cb; | 469 | opt.format_callback = cgit_diff_tree_cb; |
482 | opt.format_callback_data = fn; | 470 | opt.format_callback_data = fn; |
483 | if (prefix) { | 471 | if (prefix) { |
484 | opt.nr_paths = 1; | 472 | opt.nr_paths = 1; |
485 | opt.paths = &prefix; | 473 | opt.paths = &prefix; |
486 | prefixlen = strlen(prefix); | 474 | prefixlen = strlen(prefix); |
487 | opt.pathlens = &prefixlen; | 475 | opt.pathlens = &prefixlen; |
488 | } | 476 | } |
489 | diff_setup_done(&opt); | 477 | diff_setup_done(&opt); |
490 | 478 | ||
491 | if (old_sha1 && !is_null_sha1(old_sha1)) | 479 | if (old_sha1 && !is_null_sha1(old_sha1)) |
492 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); | 480 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); |
493 | else | 481 | else |
494 | ret = diff_root_tree_sha1(new_sha1, "", &opt); | 482 | ret = diff_root_tree_sha1(new_sha1, "", &opt); |
495 | diffcore_std(&opt); | 483 | diffcore_std(&opt); |
496 | diff_flush(&opt); | 484 | diff_flush(&opt); |
497 | } | 485 | } |
498 | 486 | ||
499 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) | 487 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) |
500 | { | 488 | { |
501 | unsigned char *old_sha1 = NULL; | 489 | unsigned char *old_sha1 = NULL; |
502 | 490 | ||
503 | if (commit->parents) | 491 | if (commit->parents) |
504 | old_sha1 = commit->parents->item->object.sha1; | 492 | old_sha1 = commit->parents->item->object.sha1; |
505 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); | 493 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); |
506 | } | 494 | } |
diff --git a/ui-repolist.c b/ui-repolist.c index 3e97ca9..a6cc2cc 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -1,110 +1,110 @@ | |||
1 | /* ui-repolist.c: functions for generating the repolist page | 1 | /* ui-repolist.c: functions for generating the repolist page |
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 <time.h> | 10 | #include <time.h> |
11 | 11 | ||
12 | 12 | ||
13 | time_t read_agefile(char *path) | 13 | time_t read_agefile(char *path) |
14 | { | 14 | { |
15 | FILE *f; | 15 | FILE *f; |
16 | static char buf[64], buf2[64]; | 16 | static char buf[64], buf2[64]; |
17 | 17 | ||
18 | if (!(f = fopen(path, "r"))) | 18 | if (!(f = fopen(path, "r"))) |
19 | return -1; | 19 | return -1; |
20 | fgets(buf, sizeof(buf), f); | 20 | fgets(buf, sizeof(buf), f); |
21 | fclose(f); | 21 | fclose(f); |
22 | if (parse_date(buf, buf2, sizeof(buf2))) | 22 | if (parse_date(buf, buf2, sizeof(buf2))) |
23 | return strtoul(buf2, NULL, 10); | 23 | return strtoul(buf2, NULL, 10); |
24 | else | 24 | else |
25 | return 0; | 25 | return 0; |
26 | } | 26 | } |
27 | 27 | ||
28 | static void print_modtime(struct repoinfo *repo) | 28 | static void print_modtime(struct repoinfo *repo) |
29 | { | 29 | { |
30 | char *path; | 30 | char *path; |
31 | struct stat s; | 31 | struct stat s; |
32 | 32 | ||
33 | path = fmt("%s/%s", repo->path, cgit_agefile); | 33 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
34 | if (stat(path, &s) == 0) { | 34 | if (stat(path, &s) == 0) { |
35 | cgit_print_age(read_agefile(path), -1, NULL); | 35 | cgit_print_age(read_agefile(path), -1, NULL); |
36 | return; | 36 | return; |
37 | } | 37 | } |
38 | 38 | ||
39 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); | 39 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); |
40 | if (stat(path, &s) != 0) | 40 | if (stat(path, &s) != 0) |
41 | return; | 41 | return; |
42 | cgit_print_age(s.st_mtime, -1, NULL); | 42 | cgit_print_age(s.st_mtime, -1, NULL); |
43 | } | 43 | } |
44 | 44 | ||
45 | void cgit_print_repolist(struct cacheitem *item) | 45 | void cgit_print_repolist(struct cacheitem *item) |
46 | { | 46 | { |
47 | int i, columns = 4; | 47 | int i, columns = 4; |
48 | char *last_group = NULL; | 48 | char *last_group = NULL; |
49 | 49 | ||
50 | if (cgit_enable_index_links) | 50 | if (ctx.cfg.enable_index_links) |
51 | columns++; | 51 | columns++; |
52 | 52 | ||
53 | cgit_print_docstart(cgit_root_title, item); | 53 | cgit_print_docstart(ctx.cfg.root_title, item); |
54 | cgit_print_pageheader(cgit_root_title, 0); | 54 | cgit_print_pageheader(ctx.cfg.root_title, 0); |
55 | 55 | ||
56 | html("<table summary='repository list' class='list nowrap'>"); | 56 | html("<table summary='repository list' class='list nowrap'>"); |
57 | if (cgit_index_header) { | 57 | if (ctx.cfg.index_header) { |
58 | htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", | 58 | htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", |
59 | columns); | 59 | columns); |
60 | html_include(cgit_index_header); | 60 | html_include(ctx.cfg.index_header); |
61 | html("</td></tr>"); | 61 | html("</td></tr>"); |
62 | } | 62 | } |
63 | html("<tr class='nohover'>" | 63 | html("<tr class='nohover'>" |
64 | "<th class='left'>Name</th>" | 64 | "<th class='left'>Name</th>" |
65 | "<th class='left'>Description</th>" | 65 | "<th class='left'>Description</th>" |
66 | "<th class='left'>Owner</th>" | 66 | "<th class='left'>Owner</th>" |
67 | "<th class='left'>Idle</th>"); | 67 | "<th class='left'>Idle</th>"); |
68 | if (cgit_enable_index_links) | 68 | if (ctx.cfg.enable_index_links) |
69 | html("<th>Links</th>"); | 69 | html("<th>Links</th>"); |
70 | html("</tr>\n"); | 70 | html("</tr>\n"); |
71 | 71 | ||
72 | for (i=0; i<cgit_repolist.count; i++) { | 72 | for (i=0; i<cgit_repolist.count; i++) { |
73 | cgit_repo = &cgit_repolist.repos[i]; | 73 | cgit_repo = &cgit_repolist.repos[i]; |
74 | if ((last_group == NULL && cgit_repo->group != NULL) || | 74 | if ((last_group == NULL && cgit_repo->group != NULL) || |
75 | (last_group != NULL && cgit_repo->group == NULL) || | 75 | (last_group != NULL && cgit_repo->group == NULL) || |
76 | (last_group != NULL && cgit_repo->group != NULL && | 76 | (last_group != NULL && cgit_repo->group != NULL && |
77 | strcmp(cgit_repo->group, last_group))) { | 77 | strcmp(cgit_repo->group, last_group))) { |
78 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", | 78 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", |
79 | columns); | 79 | columns); |
80 | html_txt(cgit_repo->group); | 80 | html_txt(cgit_repo->group); |
81 | html("</td></tr>"); | 81 | html("</td></tr>"); |
82 | last_group = cgit_repo->group; | 82 | last_group = cgit_repo->group; |
83 | } | 83 | } |
84 | htmlf("<tr><td class='%s'>", | 84 | htmlf("<tr><td class='%s'>", |
85 | cgit_repo->group ? "sublevel-repo" : "toplevel-repo"); | 85 | cgit_repo->group ? "sublevel-repo" : "toplevel-repo"); |
86 | html_link_open(cgit_repourl(cgit_repo->url), NULL, NULL); | 86 | html_link_open(cgit_repourl(cgit_repo->url), NULL, NULL); |
87 | html_txt(cgit_repo->name); | 87 | html_txt(cgit_repo->name); |
88 | html_link_close(); | 88 | html_link_close(); |
89 | html("</td><td>"); | 89 | html("</td><td>"); |
90 | html_ntxt(cgit_max_repodesc_len, cgit_repo->desc); | 90 | html_ntxt(ctx.cfg.max_repodesc_len, cgit_repo->desc); |
91 | html("</td><td>"); | 91 | html("</td><td>"); |
92 | html_txt(cgit_repo->owner); | 92 | html_txt(cgit_repo->owner); |
93 | html("</td><td>"); | 93 | html("</td><td>"); |
94 | print_modtime(cgit_repo); | 94 | print_modtime(cgit_repo); |
95 | html("</td>"); | 95 | html("</td>"); |
96 | if (cgit_enable_index_links) { | 96 | if (ctx.cfg.enable_index_links) { |
97 | html("<td>"); | 97 | html("<td>"); |
98 | html_link_open(cgit_repourl(cgit_repo->url), | 98 | html_link_open(cgit_repourl(cgit_repo->url), |
99 | NULL, "button"); | 99 | NULL, "button"); |
100 | html("summary</a>"); | 100 | html("summary</a>"); |
101 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, | 101 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, |
102 | 0, NULL, NULL); | 102 | 0, NULL, NULL); |
103 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); | 103 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); |
104 | html("</td>"); | 104 | html("</td>"); |
105 | } | 105 | } |
106 | html("</tr>\n"); | 106 | html("</tr>\n"); |
107 | } | 107 | } |
108 | html("</table>"); | 108 | html("</table>"); |
109 | cgit_print_docend(); | 109 | cgit_print_docend(); |
110 | } | 110 | } |
diff --git a/ui-shared.c b/ui-shared.c index 6a41fb0..b96237d 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,570 +1,570 @@ | |||
1 | /* ui-shared.c: common web output functions | 1 | /* ui-shared.c: common web output 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 | ||
11 | const char cgit_doctype[] = | 11 | const char cgit_doctype[] = |
12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
14 | 14 | ||
15 | static char *http_date(time_t t) | 15 | static char *http_date(time_t t) |
16 | { | 16 | { |
17 | static char day[][4] = | 17 | static char day[][4] = |
18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
19 | static char month[][4] = | 19 | static char month[][4] = |
20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
22 | struct tm *tm = gmtime(&t); | 22 | struct tm *tm = gmtime(&t); |
23 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 23 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
24 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 24 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
25 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 25 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
26 | } | 26 | } |
27 | 27 | ||
28 | static long ttl_seconds(long ttl) | 28 | static long ttl_seconds(long ttl) |
29 | { | 29 | { |
30 | if (ttl<0) | 30 | if (ttl<0) |
31 | return 60 * 60 * 24 * 365; | 31 | return 60 * 60 * 24 * 365; |
32 | else | 32 | else |
33 | return ttl * 60; | 33 | return ttl * 60; |
34 | } | 34 | } |
35 | 35 | ||
36 | void cgit_print_error(char *msg) | 36 | void cgit_print_error(char *msg) |
37 | { | 37 | { |
38 | html("<div class='error'>"); | 38 | html("<div class='error'>"); |
39 | html_txt(msg); | 39 | html_txt(msg); |
40 | html("</div>\n"); | 40 | html("</div>\n"); |
41 | } | 41 | } |
42 | 42 | ||
43 | char *cgit_rooturl() | 43 | char *cgit_rooturl() |
44 | { | 44 | { |
45 | if (cgit_virtual_root) | 45 | if (ctx.cfg.virtual_root) |
46 | return fmt("%s/", cgit_virtual_root); | 46 | return fmt("%s/", ctx.cfg.virtual_root); |
47 | else | 47 | else |
48 | return cgit_script_name; | 48 | return ctx.cfg.script_name; |
49 | } | 49 | } |
50 | 50 | ||
51 | char *cgit_repourl(const char *reponame) | 51 | char *cgit_repourl(const char *reponame) |
52 | { | 52 | { |
53 | if (cgit_virtual_root) { | 53 | if (ctx.cfg.virtual_root) { |
54 | return fmt("%s/%s/", cgit_virtual_root, reponame); | 54 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
55 | } else { | 55 | } else { |
56 | return fmt("?r=%s", reponame); | 56 | return fmt("?r=%s", reponame); |
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
60 | char *cgit_fileurl(const char *reponame, const char *pagename, | 60 | char *cgit_fileurl(const char *reponame, const char *pagename, |
61 | const char *filename, const char *query) | 61 | const char *filename, const char *query) |
62 | { | 62 | { |
63 | char *tmp; | 63 | char *tmp; |
64 | char *delim; | 64 | char *delim; |
65 | 65 | ||
66 | if (cgit_virtual_root) { | 66 | if (ctx.cfg.virtual_root) { |
67 | tmp = fmt("%s/%s/%s/%s", cgit_virtual_root, reponame, | 67 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
68 | pagename, (filename ? filename:"")); | 68 | pagename, (filename ? filename:"")); |
69 | delim = "?"; | 69 | delim = "?"; |
70 | } else { | 70 | } else { |
71 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 71 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
72 | (filename ? filename : "")); | 72 | (filename ? filename : "")); |
73 | delim = "&"; | 73 | delim = "&"; |
74 | } | 74 | } |
75 | if (query) | 75 | if (query) |
76 | tmp = fmt("%s%s%s", tmp, delim, query); | 76 | tmp = fmt("%s%s%s", tmp, delim, query); |
77 | return tmp; | 77 | return tmp; |
78 | } | 78 | } |
79 | 79 | ||
80 | char *cgit_pageurl(const char *reponame, const char *pagename, | 80 | char *cgit_pageurl(const char *reponame, const char *pagename, |
81 | const char *query) | 81 | const char *query) |
82 | { | 82 | { |
83 | return cgit_fileurl(reponame,pagename,0,query); | 83 | return cgit_fileurl(reponame,pagename,0,query); |
84 | } | 84 | } |
85 | 85 | ||
86 | const char *cgit_repobasename(const char *reponame) | 86 | const char *cgit_repobasename(const char *reponame) |
87 | { | 87 | { |
88 | /* I assume we don't need to store more than one repo basename */ | 88 | /* I assume we don't need to store more than one repo basename */ |
89 | static char rvbuf[1024]; | 89 | static char rvbuf[1024]; |
90 | int p; | 90 | int p; |
91 | const char *rv; | 91 | const char *rv; |
92 | strncpy(rvbuf,reponame,sizeof(rvbuf)); | 92 | strncpy(rvbuf,reponame,sizeof(rvbuf)); |
93 | if(rvbuf[sizeof(rvbuf)-1]) | 93 | if(rvbuf[sizeof(rvbuf)-1]) |
94 | die("cgit_repobasename: truncated repository name '%s'", reponame); | 94 | die("cgit_repobasename: truncated repository name '%s'", reponame); |
95 | p = strlen(rvbuf)-1; | 95 | p = strlen(rvbuf)-1; |
96 | /* strip trailing slashes */ | 96 | /* strip trailing slashes */ |
97 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; | 97 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; |
98 | /* strip trailing .git */ | 98 | /* strip trailing .git */ |
99 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { | 99 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { |
100 | p -= 3; rvbuf[p--] = 0; | 100 | p -= 3; rvbuf[p--] = 0; |
101 | } | 101 | } |
102 | /* strip more trailing slashes if any */ | 102 | /* strip more trailing slashes if any */ |
103 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; | 103 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; |
104 | /* find last slash in the remaining string */ | 104 | /* find last slash in the remaining string */ |
105 | rv = strrchr(rvbuf,'/'); | 105 | rv = strrchr(rvbuf,'/'); |
106 | if(rv) | 106 | if(rv) |
107 | return ++rv; | 107 | return ++rv; |
108 | return rvbuf; | 108 | return rvbuf; |
109 | } | 109 | } |
110 | 110 | ||
111 | char *cgit_currurl() | 111 | char *cgit_currurl() |
112 | { | 112 | { |
113 | if (!cgit_virtual_root) | 113 | if (!ctx.cfg.virtual_root) |
114 | return cgit_script_name; | 114 | return ctx.cfg.script_name; |
115 | else if (ctx.qry.page) | 115 | else if (ctx.qry.page) |
116 | return fmt("%s/%s/%s/", cgit_virtual_root, ctx.qry.repo, ctx.qry.page); | 116 | return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); |
117 | else if (ctx.qry.repo) | 117 | else if (ctx.qry.repo) |
118 | return fmt("%s/%s/", cgit_virtual_root, ctx.qry.repo); | 118 | return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); |
119 | else | 119 | else |
120 | return fmt("%s/", cgit_virtual_root); | 120 | return fmt("%s/", ctx.cfg.virtual_root); |
121 | } | 121 | } |
122 | 122 | ||
123 | static char *repolink(char *title, char *class, char *page, char *head, | 123 | static char *repolink(char *title, char *class, char *page, char *head, |
124 | char *path) | 124 | char *path) |
125 | { | 125 | { |
126 | char *delim = "?"; | 126 | char *delim = "?"; |
127 | 127 | ||
128 | html("<a"); | 128 | html("<a"); |
129 | if (title) { | 129 | if (title) { |
130 | html(" title='"); | 130 | html(" title='"); |
131 | html_attr(title); | 131 | html_attr(title); |
132 | html("'"); | 132 | html("'"); |
133 | } | 133 | } |
134 | if (class) { | 134 | if (class) { |
135 | html(" class='"); | 135 | html(" class='"); |
136 | html_attr(class); | 136 | html_attr(class); |
137 | html("'"); | 137 | html("'"); |
138 | } | 138 | } |
139 | html(" href='"); | 139 | html(" href='"); |
140 | if (cgit_virtual_root) { | 140 | if (ctx.cfg.virtual_root) { |
141 | html_attr(cgit_virtual_root); | 141 | html_attr(ctx.cfg.virtual_root); |
142 | if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') | 142 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') |
143 | html("/"); | 143 | html("/"); |
144 | html_attr(cgit_repo->url); | 144 | html_attr(cgit_repo->url); |
145 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 145 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') |
146 | html("/"); | 146 | html("/"); |
147 | if (page) { | 147 | if (page) { |
148 | html(page); | 148 | html(page); |
149 | html("/"); | 149 | html("/"); |
150 | if (path) | 150 | if (path) |
151 | html_attr(path); | 151 | html_attr(path); |
152 | } | 152 | } |
153 | } else { | 153 | } else { |
154 | html(cgit_script_name); | 154 | html(ctx.cfg.script_name); |
155 | html("?url="); | 155 | html("?url="); |
156 | html_attr(cgit_repo->url); | 156 | html_attr(cgit_repo->url); |
157 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 157 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') |
158 | html("/"); | 158 | html("/"); |
159 | if (page) { | 159 | if (page) { |
160 | html(page); | 160 | html(page); |
161 | html("/"); | 161 | html("/"); |
162 | if (path) | 162 | if (path) |
163 | html_attr(path); | 163 | html_attr(path); |
164 | } | 164 | } |
165 | delim = "&"; | 165 | delim = "&"; |
166 | } | 166 | } |
167 | if (head && strcmp(head, cgit_repo->defbranch)) { | 167 | if (head && strcmp(head, cgit_repo->defbranch)) { |
168 | html(delim); | 168 | html(delim); |
169 | html("h="); | 169 | html("h="); |
170 | html_attr(head); | 170 | html_attr(head); |
171 | delim = "&"; | 171 | delim = "&"; |
172 | } | 172 | } |
173 | return fmt("%s", delim); | 173 | return fmt("%s", delim); |
174 | } | 174 | } |
175 | 175 | ||
176 | static void reporevlink(char *page, char *name, char *title, char *class, | 176 | static void reporevlink(char *page, char *name, char *title, char *class, |
177 | char *head, char *rev, char *path) | 177 | char *head, char *rev, char *path) |
178 | { | 178 | { |
179 | char *delim; | 179 | char *delim; |
180 | 180 | ||
181 | delim = repolink(title, class, page, head, path); | 181 | delim = repolink(title, class, page, head, path); |
182 | if (rev && strcmp(rev, ctx.qry.head)) { | 182 | if (rev && strcmp(rev, ctx.qry.head)) { |
183 | html(delim); | 183 | html(delim); |
184 | html("id="); | 184 | html("id="); |
185 | html_attr(rev); | 185 | html_attr(rev); |
186 | } | 186 | } |
187 | html("'>"); | 187 | html("'>"); |
188 | html_txt(name); | 188 | html_txt(name); |
189 | html("</a>"); | 189 | html("</a>"); |
190 | } | 190 | } |
191 | 191 | ||
192 | void cgit_tree_link(char *name, char *title, char *class, char *head, | 192 | void cgit_tree_link(char *name, char *title, char *class, char *head, |
193 | char *rev, char *path) | 193 | char *rev, char *path) |
194 | { | 194 | { |
195 | reporevlink("tree", name, title, class, head, rev, path); | 195 | reporevlink("tree", name, title, class, head, rev, path); |
196 | } | 196 | } |
197 | 197 | ||
198 | void cgit_log_link(char *name, char *title, char *class, char *head, | 198 | void cgit_log_link(char *name, char *title, char *class, char *head, |
199 | char *rev, char *path, int ofs, char *grep, char *pattern) | 199 | char *rev, char *path, int ofs, char *grep, char *pattern) |
200 | { | 200 | { |
201 | char *delim; | 201 | char *delim; |
202 | 202 | ||
203 | delim = repolink(title, class, "log", head, path); | 203 | delim = repolink(title, class, "log", head, path); |
204 | if (rev && strcmp(rev, ctx.qry.head)) { | 204 | if (rev && strcmp(rev, ctx.qry.head)) { |
205 | html(delim); | 205 | html(delim); |
206 | html("id="); | 206 | html("id="); |
207 | html_attr(rev); | 207 | html_attr(rev); |
208 | delim = "&"; | 208 | delim = "&"; |
209 | } | 209 | } |
210 | if (grep && pattern) { | 210 | if (grep && pattern) { |
211 | html(delim); | 211 | html(delim); |
212 | html("qt="); | 212 | html("qt="); |
213 | html_attr(grep); | 213 | html_attr(grep); |
214 | delim = "&"; | 214 | delim = "&"; |
215 | html(delim); | 215 | html(delim); |
216 | html("q="); | 216 | html("q="); |
217 | html_attr(pattern); | 217 | html_attr(pattern); |
218 | } | 218 | } |
219 | if (ofs > 0) { | 219 | if (ofs > 0) { |
220 | html(delim); | 220 | html(delim); |
221 | html("ofs="); | 221 | html("ofs="); |
222 | htmlf("%d", ofs); | 222 | htmlf("%d", ofs); |
223 | } | 223 | } |
224 | html("'>"); | 224 | html("'>"); |
225 | html_txt(name); | 225 | html_txt(name); |
226 | html("</a>"); | 226 | html("</a>"); |
227 | } | 227 | } |
228 | 228 | ||
229 | void cgit_commit_link(char *name, char *title, char *class, char *head, | 229 | void cgit_commit_link(char *name, char *title, char *class, char *head, |
230 | char *rev) | 230 | char *rev) |
231 | { | 231 | { |
232 | if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { | 232 | if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { |
233 | name[cgit_max_msg_len] = '\0'; | 233 | name[ctx.cfg.max_msg_len] = '\0'; |
234 | name[cgit_max_msg_len - 1] = '.'; | 234 | name[ctx.cfg.max_msg_len - 1] = '.'; |
235 | name[cgit_max_msg_len - 2] = '.'; | 235 | name[ctx.cfg.max_msg_len - 2] = '.'; |
236 | name[cgit_max_msg_len - 3] = '.'; | 236 | name[ctx.cfg.max_msg_len - 3] = '.'; |
237 | } | 237 | } |
238 | reporevlink("commit", name, title, class, head, rev, NULL); | 238 | reporevlink("commit", name, title, class, head, rev, NULL); |
239 | } | 239 | } |
240 | 240 | ||
241 | void cgit_refs_link(char *name, char *title, char *class, char *head, | 241 | void cgit_refs_link(char *name, char *title, char *class, char *head, |
242 | char *rev, char *path) | 242 | char *rev, char *path) |
243 | { | 243 | { |
244 | reporevlink("refs", name, title, class, head, rev, path); | 244 | reporevlink("refs", name, title, class, head, rev, path); |
245 | } | 245 | } |
246 | 246 | ||
247 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, | 247 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, |
248 | char *rev, char *archivename) | 248 | char *rev, char *archivename) |
249 | { | 249 | { |
250 | reporevlink("snapshot", name, title, class, head, rev, archivename); | 250 | reporevlink("snapshot", name, title, class, head, rev, archivename); |
251 | } | 251 | } |
252 | 252 | ||
253 | void cgit_diff_link(char *name, char *title, char *class, char *head, | 253 | void cgit_diff_link(char *name, char *title, char *class, char *head, |
254 | char *new_rev, char *old_rev, char *path) | 254 | char *new_rev, char *old_rev, char *path) |
255 | { | 255 | { |
256 | char *delim; | 256 | char *delim; |
257 | 257 | ||
258 | delim = repolink(title, class, "diff", head, path); | 258 | delim = repolink(title, class, "diff", head, path); |
259 | if (new_rev && strcmp(new_rev, ctx.qry.head)) { | 259 | if (new_rev && strcmp(new_rev, ctx.qry.head)) { |
260 | html(delim); | 260 | html(delim); |
261 | html("id="); | 261 | html("id="); |
262 | html_attr(new_rev); | 262 | html_attr(new_rev); |
263 | delim = "&"; | 263 | delim = "&"; |
264 | } | 264 | } |
265 | if (old_rev) { | 265 | if (old_rev) { |
266 | html(delim); | 266 | html(delim); |
267 | html("id2="); | 267 | html("id2="); |
268 | html_attr(old_rev); | 268 | html_attr(old_rev); |
269 | } | 269 | } |
270 | html("'>"); | 270 | html("'>"); |
271 | html_txt(name); | 271 | html_txt(name); |
272 | html("</a>"); | 272 | html("</a>"); |
273 | } | 273 | } |
274 | 274 | ||
275 | void cgit_patch_link(char *name, char *title, char *class, char *head, | 275 | void cgit_patch_link(char *name, char *title, char *class, char *head, |
276 | char *rev) | 276 | char *rev) |
277 | { | 277 | { |
278 | reporevlink("patch", name, title, class, head, rev, NULL); | 278 | reporevlink("patch", name, title, class, head, rev, NULL); |
279 | } | 279 | } |
280 | 280 | ||
281 | void cgit_object_link(struct object *obj) | 281 | void cgit_object_link(struct object *obj) |
282 | { | 282 | { |
283 | char *page, *arg, *url; | 283 | char *page, *arg, *url; |
284 | 284 | ||
285 | if (obj->type == OBJ_COMMIT) { | 285 | if (obj->type == OBJ_COMMIT) { |
286 | cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, | 286 | cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, |
287 | ctx.qry.head, sha1_to_hex(obj->sha1)); | 287 | ctx.qry.head, sha1_to_hex(obj->sha1)); |
288 | return; | 288 | return; |
289 | } else if (obj->type == OBJ_TREE) { | 289 | } else if (obj->type == OBJ_TREE) { |
290 | page = "tree"; | 290 | page = "tree"; |
291 | arg = "id"; | 291 | arg = "id"; |
292 | } else if (obj->type == OBJ_TAG) { | 292 | } else if (obj->type == OBJ_TAG) { |
293 | page = "tag"; | 293 | page = "tag"; |
294 | arg = "id"; | 294 | arg = "id"; |
295 | } else { | 295 | } else { |
296 | page = "blob"; | 296 | page = "blob"; |
297 | arg = "id"; | 297 | arg = "id"; |
298 | } | 298 | } |
299 | 299 | ||
300 | url = cgit_pageurl(ctx.qry.repo, page, | 300 | url = cgit_pageurl(ctx.qry.repo, page, |
301 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); | 301 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); |
302 | html_link_open(url, NULL, NULL); | 302 | html_link_open(url, NULL, NULL); |
303 | htmlf("%s %s", typename(obj->type), | 303 | htmlf("%s %s", typename(obj->type), |
304 | sha1_to_hex(obj->sha1)); | 304 | sha1_to_hex(obj->sha1)); |
305 | html_link_close(); | 305 | html_link_close(); |
306 | } | 306 | } |
307 | 307 | ||
308 | void cgit_print_date(time_t secs, char *format) | 308 | void cgit_print_date(time_t secs, char *format) |
309 | { | 309 | { |
310 | char buf[64]; | 310 | char buf[64]; |
311 | struct tm *time; | 311 | struct tm *time; |
312 | 312 | ||
313 | if (!secs) | 313 | if (!secs) |
314 | return; | 314 | return; |
315 | time = gmtime(&secs); | 315 | time = gmtime(&secs); |
316 | strftime(buf, sizeof(buf)-1, format, time); | 316 | strftime(buf, sizeof(buf)-1, format, time); |
317 | html_txt(buf); | 317 | html_txt(buf); |
318 | } | 318 | } |
319 | 319 | ||
320 | void cgit_print_age(time_t t, time_t max_relative, char *format) | 320 | void cgit_print_age(time_t t, time_t max_relative, char *format) |
321 | { | 321 | { |
322 | time_t now, secs; | 322 | time_t now, secs; |
323 | 323 | ||
324 | if (!t) | 324 | if (!t) |
325 | return; | 325 | return; |
326 | time(&now); | 326 | time(&now); |
327 | secs = now - t; | 327 | secs = now - t; |
328 | 328 | ||
329 | if (secs > max_relative && max_relative >= 0) { | 329 | if (secs > max_relative && max_relative >= 0) { |
330 | cgit_print_date(t, format); | 330 | cgit_print_date(t, format); |
331 | return; | 331 | return; |
332 | } | 332 | } |
333 | 333 | ||
334 | if (secs < TM_HOUR * 2) { | 334 | if (secs < TM_HOUR * 2) { |
335 | htmlf("<span class='age-mins'>%.0f min.</span>", | 335 | htmlf("<span class='age-mins'>%.0f min.</span>", |
336 | secs * 1.0 / TM_MIN); | 336 | secs * 1.0 / TM_MIN); |
337 | return; | 337 | return; |
338 | } | 338 | } |
339 | if (secs < TM_DAY * 2) { | 339 | if (secs < TM_DAY * 2) { |
340 | htmlf("<span class='age-hours'>%.0f hours</span>", | 340 | htmlf("<span class='age-hours'>%.0f hours</span>", |
341 | secs * 1.0 / TM_HOUR); | 341 | secs * 1.0 / TM_HOUR); |
342 | return; | 342 | return; |
343 | } | 343 | } |
344 | if (secs < TM_WEEK * 2) { | 344 | if (secs < TM_WEEK * 2) { |
345 | htmlf("<span class='age-days'>%.0f days</span>", | 345 | htmlf("<span class='age-days'>%.0f days</span>", |
346 | secs * 1.0 / TM_DAY); | 346 | secs * 1.0 / TM_DAY); |
347 | return; | 347 | return; |
348 | } | 348 | } |
349 | if (secs < TM_MONTH * 2) { | 349 | if (secs < TM_MONTH * 2) { |
350 | htmlf("<span class='age-weeks'>%.0f weeks</span>", | 350 | htmlf("<span class='age-weeks'>%.0f weeks</span>", |
351 | secs * 1.0 / TM_WEEK); | 351 | secs * 1.0 / TM_WEEK); |
352 | return; | 352 | return; |
353 | } | 353 | } |
354 | if (secs < TM_YEAR * 2) { | 354 | if (secs < TM_YEAR * 2) { |
355 | htmlf("<span class='age-months'>%.0f months</span>", | 355 | htmlf("<span class='age-months'>%.0f months</span>", |
356 | secs * 1.0 / TM_MONTH); | 356 | secs * 1.0 / TM_MONTH); |
357 | return; | 357 | return; |
358 | } | 358 | } |
359 | htmlf("<span class='age-years'>%.0f years</span>", | 359 | htmlf("<span class='age-years'>%.0f years</span>", |
360 | secs * 1.0 / TM_YEAR); | 360 | secs * 1.0 / TM_YEAR); |
361 | } | 361 | } |
362 | 362 | ||
363 | void cgit_print_docstart(char *title, struct cacheitem *item) | 363 | void cgit_print_docstart(char *title, struct cacheitem *item) |
364 | { | 364 | { |
365 | html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); | 365 | html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); |
366 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 366 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); |
367 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 367 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + |
368 | ttl_seconds(item->ttl))); | 368 | ttl_seconds(item->ttl))); |
369 | html("\n"); | 369 | html("\n"); |
370 | html(cgit_doctype); | 370 | html(cgit_doctype); |
371 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); | 371 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); |
372 | html("<head>\n"); | 372 | html("<head>\n"); |
373 | html("<title>"); | 373 | html("<title>"); |
374 | html_txt(title); | 374 | html_txt(title); |
375 | html("</title>\n"); | 375 | html("</title>\n"); |
376 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 376 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
377 | if (cgit_robots && *cgit_robots) | 377 | if (ctx.cfg.robots && *ctx.cfg.robots) |
378 | htmlf("<meta name='robots' content='%s'/>\n", cgit_robots); | 378 | htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots); |
379 | html("<link rel='stylesheet' type='text/css' href='"); | 379 | html("<link rel='stylesheet' type='text/css' href='"); |
380 | html_attr(cgit_css); | 380 | html_attr(ctx.cfg.css); |
381 | html("'/>\n"); | 381 | html("'/>\n"); |
382 | html("</head>\n"); | 382 | html("</head>\n"); |
383 | html("<body>\n"); | 383 | html("<body>\n"); |
384 | } | 384 | } |
385 | 385 | ||
386 | void cgit_print_docend() | 386 | void cgit_print_docend() |
387 | { | 387 | { |
388 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); | 388 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); |
389 | } | 389 | } |
390 | 390 | ||
391 | int print_branch_option(const char *refname, const unsigned char *sha1, | 391 | int print_branch_option(const char *refname, const unsigned char *sha1, |
392 | int flags, void *cb_data) | 392 | int flags, void *cb_data) |
393 | { | 393 | { |
394 | char *name = (char *)refname; | 394 | char *name = (char *)refname; |
395 | html_option(name, name, ctx.qry.head); | 395 | html_option(name, name, ctx.qry.head); |
396 | return 0; | 396 | return 0; |
397 | } | 397 | } |
398 | 398 | ||
399 | int print_archive_ref(const char *refname, const unsigned char *sha1, | 399 | int print_archive_ref(const char *refname, const unsigned char *sha1, |
400 | int flags, void *cb_data) | 400 | int flags, void *cb_data) |
401 | { | 401 | { |
402 | struct tag *tag; | 402 | struct tag *tag; |
403 | struct taginfo *info; | 403 | struct taginfo *info; |
404 | struct object *obj; | 404 | struct object *obj; |
405 | char buf[256], *url; | 405 | char buf[256], *url; |
406 | unsigned char fileid[20]; | 406 | unsigned char fileid[20]; |
407 | int *header = (int *)cb_data; | 407 | int *header = (int *)cb_data; |
408 | 408 | ||
409 | if (prefixcmp(refname, "refs/archives")) | 409 | if (prefixcmp(refname, "refs/archives")) |
410 | return 0; | 410 | return 0; |
411 | strncpy(buf, refname+14, sizeof(buf)); | 411 | strncpy(buf, refname+14, sizeof(buf)); |
412 | obj = parse_object(sha1); | 412 | obj = parse_object(sha1); |
413 | if (!obj) | 413 | if (!obj) |
414 | return 1; | 414 | return 1; |
415 | if (obj->type == OBJ_TAG) { | 415 | if (obj->type == OBJ_TAG) { |
416 | tag = lookup_tag(sha1); | 416 | tag = lookup_tag(sha1); |
417 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) | 417 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) |
418 | return 0; | 418 | return 0; |
419 | hashcpy(fileid, tag->tagged->sha1); | 419 | hashcpy(fileid, tag->tagged->sha1); |
420 | } else if (obj->type != OBJ_BLOB) { | 420 | } else if (obj->type != OBJ_BLOB) { |
421 | return 0; | 421 | return 0; |
422 | } else { | 422 | } else { |
423 | hashcpy(fileid, sha1); | 423 | hashcpy(fileid, sha1); |
424 | } | 424 | } |
425 | if (!*header) { | 425 | if (!*header) { |
426 | html("<h1>download</h1>\n"); | 426 | html("<h1>download</h1>\n"); |
427 | *header = 1; | 427 | *header = 1; |
428 | } | 428 | } |
429 | url = cgit_pageurl(ctx.qry.repo, "blob", | 429 | url = cgit_pageurl(ctx.qry.repo, "blob", |
430 | fmt("id=%s&path=%s", sha1_to_hex(fileid), | 430 | fmt("id=%s&path=%s", sha1_to_hex(fileid), |
431 | buf)); | 431 | buf)); |
432 | html_link_open(url, NULL, "menu"); | 432 | html_link_open(url, NULL, "menu"); |
433 | html_txt(strlpart(buf, 20)); | 433 | html_txt(strlpart(buf, 20)); |
434 | html_link_close(); | 434 | html_link_close(); |
435 | return 0; | 435 | return 0; |
436 | } | 436 | } |
437 | 437 | ||
438 | void add_hidden_formfields(int incl_head, int incl_search, char *page) | 438 | void add_hidden_formfields(int incl_head, int incl_search, char *page) |
439 | { | 439 | { |
440 | char *url; | 440 | char *url; |
441 | 441 | ||
442 | if (!cgit_virtual_root) { | 442 | if (!ctx.cfg.virtual_root) { |
443 | url = fmt("%s/%s", ctx.qry.repo, page); | 443 | url = fmt("%s/%s", ctx.qry.repo, page); |
444 | if (ctx.qry.path) | 444 | if (ctx.qry.path) |
445 | url = fmt("%s/%s", url, ctx.qry.path); | 445 | url = fmt("%s/%s", url, ctx.qry.path); |
446 | html_hidden("url", url); | 446 | html_hidden("url", url); |
447 | } | 447 | } |
448 | 448 | ||
449 | if (incl_head && strcmp(ctx.qry.head, cgit_repo->defbranch)) | 449 | if (incl_head && strcmp(ctx.qry.head, cgit_repo->defbranch)) |
450 | html_hidden("h", ctx.qry.head); | 450 | html_hidden("h", ctx.qry.head); |
451 | 451 | ||
452 | if (ctx.qry.sha1) | 452 | if (ctx.qry.sha1) |
453 | html_hidden("id", ctx.qry.sha1); | 453 | html_hidden("id", ctx.qry.sha1); |
454 | if (ctx.qry.sha2) | 454 | if (ctx.qry.sha2) |
455 | html_hidden("id2", ctx.qry.sha2); | 455 | html_hidden("id2", ctx.qry.sha2); |
456 | 456 | ||
457 | if (incl_search) { | 457 | if (incl_search) { |
458 | if (ctx.qry.grep) | 458 | if (ctx.qry.grep) |
459 | html_hidden("qt", ctx.qry.grep); | 459 | html_hidden("qt", ctx.qry.grep); |
460 | if (ctx.qry.search) | 460 | if (ctx.qry.search) |
461 | html_hidden("q", ctx.qry.search); | 461 | html_hidden("q", ctx.qry.search); |
462 | } | 462 | } |
463 | } | 463 | } |
464 | 464 | ||
465 | void cgit_print_pageheader(char *title, int show_search) | 465 | void cgit_print_pageheader(char *title, int show_search) |
466 | { | 466 | { |
467 | static const char *default_info = "This is cgit, a fast webinterface for git repositories"; | 467 | static const char *default_info = "This is cgit, a fast webinterface for git repositories"; |
468 | int header = 0; | 468 | int header = 0; |
469 | char *url; | 469 | char *url; |
470 | 470 | ||
471 | html("<table id='layout' summary=''>\n"); | 471 | html("<table id='layout' summary=''>\n"); |
472 | html("<tr><td id='sidebar'>\n"); | 472 | html("<tr><td id='sidebar'>\n"); |
473 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); | 473 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); |
474 | html("<tr><td class='sidebar'>\n<a href='"); | 474 | html("<tr><td class='sidebar'>\n<a href='"); |
475 | html_attr(cgit_rooturl()); | 475 | html_attr(cgit_rooturl()); |
476 | htmlf("'><img src='%s' alt='cgit'/></a>\n", | 476 | htmlf("'><img src='%s' alt='cgit'/></a>\n", |
477 | cgit_logo); | 477 | ctx.cfg.logo); |
478 | html("</td></tr>\n<tr><td class='sidebar'>\n"); | 478 | html("</td></tr>\n<tr><td class='sidebar'>\n"); |
479 | if (ctx.qry.repo) { | 479 | if (ctx.qry.repo) { |
480 | html("<h1 class='first'>"); | 480 | html("<h1 class='first'>"); |
481 | html_txt(strrpart(cgit_repo->name, 20)); | 481 | html_txt(strrpart(cgit_repo->name, 20)); |
482 | html("</h1>\n"); | 482 | html("</h1>\n"); |
483 | html_txt(cgit_repo->desc); | 483 | html_txt(cgit_repo->desc); |
484 | if (cgit_repo->owner) { | 484 | if (cgit_repo->owner) { |
485 | html("<h1>owner</h1>\n"); | 485 | html("<h1>owner</h1>\n"); |
486 | html_txt(cgit_repo->owner); | 486 | html_txt(cgit_repo->owner); |
487 | } | 487 | } |
488 | html("<h1>navigate</h1>\n"); | 488 | html("<h1>navigate</h1>\n"); |
489 | reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head, | 489 | reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head, |
490 | NULL, NULL); | 490 | NULL, NULL); |
491 | cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL, | 491 | cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL, |
492 | 0, NULL, NULL); | 492 | 0, NULL, NULL); |
493 | cgit_tree_link("tree", NULL, "menu", ctx.qry.head, | 493 | cgit_tree_link("tree", NULL, "menu", ctx.qry.head, |
494 | ctx.qry.sha1, NULL); | 494 | ctx.qry.sha1, NULL); |
495 | cgit_commit_link("commit", NULL, "menu", ctx.qry.head, | 495 | cgit_commit_link("commit", NULL, "menu", ctx.qry.head, |
496 | ctx.qry.sha1); | 496 | ctx.qry.sha1); |
497 | cgit_diff_link("diff", NULL, "menu", ctx.qry.head, | 497 | cgit_diff_link("diff", NULL, "menu", ctx.qry.head, |
498 | ctx.qry.sha1, ctx.qry.sha2, NULL); | 498 | ctx.qry.sha1, ctx.qry.sha2, NULL); |
499 | cgit_patch_link("patch", NULL, "menu", ctx.qry.head, | 499 | cgit_patch_link("patch", NULL, "menu", ctx.qry.head, |
500 | ctx.qry.sha1); | 500 | ctx.qry.sha1); |
501 | 501 | ||
502 | for_each_ref(print_archive_ref, &header); | 502 | for_each_ref(print_archive_ref, &header); |
503 | 503 | ||
504 | if (cgit_repo->clone_url || cgit_clone_prefix) { | 504 | if (cgit_repo->clone_url || ctx.cfg.clone_prefix) { |
505 | html("<h1>clone</h1>\n"); | 505 | html("<h1>clone</h1>\n"); |
506 | if (cgit_repo->clone_url) | 506 | if (cgit_repo->clone_url) |
507 | url = cgit_repo->clone_url; | 507 | url = cgit_repo->clone_url; |
508 | else | 508 | else |
509 | url = fmt("%s%s", cgit_clone_prefix, | 509 | url = fmt("%s%s", ctx.cfg.clone_prefix, |
510 | cgit_repo->url); | 510 | cgit_repo->url); |
511 | html("<a class='menu' href='"); | 511 | html("<a class='menu' href='"); |
512 | html_attr(url); | 512 | html_attr(url); |
513 | html("' title='"); | 513 | html("' title='"); |
514 | html_attr(url); | 514 | html_attr(url); |
515 | html("'>\n"); | 515 | html("'>\n"); |
516 | html_txt(strrpart(url, 20)); | 516 | html_txt(strrpart(url, 20)); |
517 | html("</a>\n"); | 517 | html("</a>\n"); |
518 | } | 518 | } |
519 | 519 | ||
520 | html("<h1>branch</h1>\n"); | 520 | html("<h1>branch</h1>\n"); |
521 | html("<form method='get' action=''>\n"); | 521 | html("<form method='get' action=''>\n"); |
522 | add_hidden_formfields(0, 1, ctx.qry.page); | 522 | add_hidden_formfields(0, 1, ctx.qry.page); |
523 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); | 523 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); |
524 | html("<select name='h' onchange='this.form.submit();'>\n"); | 524 | html("<select name='h' onchange='this.form.submit();'>\n"); |
525 | for_each_branch_ref(print_branch_option, ctx.qry.head); | 525 | for_each_branch_ref(print_branch_option, ctx.qry.head); |
526 | html("</select>\n"); | 526 | html("</select>\n"); |
527 | // html("</td><td>"); | 527 | // html("</td><td>"); |
528 | html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); | 528 | html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); |
529 | // html("</td></tr></table>"); | 529 | // html("</td></tr></table>"); |
530 | html("</form>\n"); | 530 | html("</form>\n"); |
531 | 531 | ||
532 | html("<h1>search</h1>\n"); | 532 | html("<h1>search</h1>\n"); |
533 | html("<form method='get' action='"); | 533 | html("<form method='get' action='"); |
534 | if (cgit_virtual_root) | 534 | if (ctx.cfg.virtual_root) |
535 | html_attr(cgit_fileurl(ctx.qry.repo, "log", | 535 | html_attr(cgit_fileurl(ctx.qry.repo, "log", |
536 | ctx.qry.path, NULL)); | 536 | ctx.qry.path, NULL)); |
537 | html("'>\n"); | 537 | html("'>\n"); |
538 | add_hidden_formfields(1, 0, "log"); | 538 | add_hidden_formfields(1, 0, "log"); |
539 | html("<select name='qt'>\n"); | 539 | html("<select name='qt'>\n"); |
540 | html_option("grep", "log msg", ctx.qry.grep); | 540 | html_option("grep", "log msg", ctx.qry.grep); |
541 | html_option("author", "author", ctx.qry.grep); | 541 | html_option("author", "author", ctx.qry.grep); |
542 | html_option("committer", "committer", ctx.qry.grep); | 542 | html_option("committer", "committer", ctx.qry.grep); |
543 | html("</select>\n"); | 543 | html("</select>\n"); |
544 | html("<input class='txt' type='text' name='q' value='"); | 544 | html("<input class='txt' type='text' name='q' value='"); |
545 | html_attr(ctx.qry.search); | 545 | html_attr(ctx.qry.search); |
546 | html("'/>\n"); | 546 | html("'/>\n"); |
547 | html("</form>\n"); | 547 | html("</form>\n"); |
548 | } else { | 548 | } else { |
549 | if (!cgit_index_info || html_include(cgit_index_info)) | 549 | if (!ctx.cfg.index_info || html_include(ctx.cfg.index_info)) |
550 | html(default_info); | 550 | html(default_info); |
551 | } | 551 | } |
552 | 552 | ||
553 | html("</td></tr></table></td>\n"); | 553 | html("</td></tr></table></td>\n"); |
554 | 554 | ||
555 | html("<td id='content'>\n"); | 555 | html("<td id='content'>\n"); |
556 | } | 556 | } |
557 | 557 | ||
558 | 558 | ||
559 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, | 559 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, |
560 | struct cacheitem *item) | 560 | struct cacheitem *item) |
561 | { | 561 | { |
562 | htmlf("Content-Type: %s\n", mimetype); | 562 | htmlf("Content-Type: %s\n", mimetype); |
563 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); | 563 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); |
564 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 564 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); |
565 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 565 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + |
566 | ttl_seconds(item->ttl))); | 566 | ttl_seconds(item->ttl))); |
567 | html("\n"); | 567 | html("\n"); |
568 | } | 568 | } |
569 | 569 | ||
570 | /* vim:set sw=8: */ | 570 | /* vim:set sw=8: */ |
diff --git a/ui-summary.c b/ui-summary.c index bbd4464..0a44994 100644 --- a/ui-summary.c +++ b/ui-summary.c | |||
@@ -1,200 +1,200 @@ | |||
1 | /* ui-summary.c: functions for generating repo summary page | 1 | /* ui-summary.c: functions for generating repo summary page |
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 | static int header; | 11 | static int header; |
12 | 12 | ||
13 | static int cmp_age(int age1, int age2) | 13 | static int cmp_age(int age1, int age2) |
14 | { | 14 | { |
15 | if (age1 != 0 && age2 != 0) | 15 | if (age1 != 0 && age2 != 0) |
16 | return age2 - age1; | 16 | return age2 - age1; |
17 | 17 | ||
18 | if (age1 == 0 && age2 == 0) | 18 | if (age1 == 0 && age2 == 0) |
19 | return 0; | 19 | return 0; |
20 | 20 | ||
21 | if (age1 == 0) | 21 | if (age1 == 0) |
22 | return +1; | 22 | return +1; |
23 | 23 | ||
24 | return -1; | 24 | return -1; |
25 | } | 25 | } |
26 | 26 | ||
27 | static int cmp_ref_name(const void *a, const void *b) | 27 | static int cmp_ref_name(const void *a, const void *b) |
28 | { | 28 | { |
29 | struct refinfo *r1 = *(struct refinfo **)a; | 29 | struct refinfo *r1 = *(struct refinfo **)a; |
30 | struct refinfo *r2 = *(struct refinfo **)b; | 30 | struct refinfo *r2 = *(struct refinfo **)b; |
31 | 31 | ||
32 | return strcmp(r1->refname, r2->refname); | 32 | return strcmp(r1->refname, r2->refname); |
33 | } | 33 | } |
34 | 34 | ||
35 | static int cmp_branch_age(const void *a, const void *b) | 35 | static int cmp_branch_age(const void *a, const void *b) |
36 | { | 36 | { |
37 | struct refinfo *r1 = *(struct refinfo **)a; | 37 | struct refinfo *r1 = *(struct refinfo **)a; |
38 | struct refinfo *r2 = *(struct refinfo **)b; | 38 | struct refinfo *r2 = *(struct refinfo **)b; |
39 | 39 | ||
40 | return cmp_age(r1->commit->committer_date, r2->commit->committer_date); | 40 | return cmp_age(r1->commit->committer_date, r2->commit->committer_date); |
41 | } | 41 | } |
42 | 42 | ||
43 | static int cmp_tag_age(const void *a, const void *b) | 43 | static int cmp_tag_age(const void *a, const void *b) |
44 | { | 44 | { |
45 | struct refinfo *r1 = *(struct refinfo **)a; | 45 | struct refinfo *r1 = *(struct refinfo **)a; |
46 | struct refinfo *r2 = *(struct refinfo **)b; | 46 | struct refinfo *r2 = *(struct refinfo **)b; |
47 | 47 | ||
48 | return cmp_age(r1->tag->tagger_date, r2->tag->tagger_date); | 48 | return cmp_age(r1->tag->tagger_date, r2->tag->tagger_date); |
49 | } | 49 | } |
50 | 50 | ||
51 | static int print_branch(struct refinfo *ref) | 51 | static int print_branch(struct refinfo *ref) |
52 | { | 52 | { |
53 | struct commitinfo *info = ref->commit; | 53 | struct commitinfo *info = ref->commit; |
54 | char *name = (char *)ref->refname; | 54 | char *name = (char *)ref->refname; |
55 | 55 | ||
56 | if (!info) | 56 | if (!info) |
57 | return 1; | 57 | return 1; |
58 | html("<tr><td>"); | 58 | html("<tr><td>"); |
59 | cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL); | 59 | cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL); |
60 | html("</td><td>"); | 60 | html("</td><td>"); |
61 | 61 | ||
62 | if (ref->object->type == OBJ_COMMIT) { | 62 | if (ref->object->type == OBJ_COMMIT) { |
63 | cgit_print_age(info->commit->date, -1, NULL); | 63 | cgit_print_age(info->commit->date, -1, NULL); |
64 | html("</td><td>"); | 64 | html("</td><td>"); |
65 | html_txt(info->author); | 65 | html_txt(info->author); |
66 | html("</td><td>"); | 66 | html("</td><td>"); |
67 | cgit_commit_link(info->subject, NULL, NULL, name, NULL); | 67 | cgit_commit_link(info->subject, NULL, NULL, name, NULL); |
68 | } else { | 68 | } else { |
69 | html("</td><td></td><td>"); | 69 | html("</td><td></td><td>"); |
70 | cgit_object_link(ref->object); | 70 | cgit_object_link(ref->object); |
71 | } | 71 | } |
72 | html("</td></tr>\n"); | 72 | html("</td></tr>\n"); |
73 | return 0; | 73 | return 0; |
74 | } | 74 | } |
75 | 75 | ||
76 | static void print_tag_header() | 76 | static void print_tag_header() |
77 | { | 77 | { |
78 | html("<tr class='nohover'><th class='left'>Tag</th>" | 78 | html("<tr class='nohover'><th class='left'>Tag</th>" |
79 | "<th class='left'>Age</th>" | 79 | "<th class='left'>Age</th>" |
80 | "<th class='left'>Author</th>" | 80 | "<th class='left'>Author</th>" |
81 | "<th class='left'>Reference</th></tr>\n"); | 81 | "<th class='left'>Reference</th></tr>\n"); |
82 | header = 1; | 82 | header = 1; |
83 | } | 83 | } |
84 | 84 | ||
85 | static int print_tag(struct refinfo *ref) | 85 | static int print_tag(struct refinfo *ref) |
86 | { | 86 | { |
87 | struct tag *tag; | 87 | struct tag *tag; |
88 | struct taginfo *info; | 88 | struct taginfo *info; |
89 | char *url, *name = (char *)ref->refname; | 89 | char *url, *name = (char *)ref->refname; |
90 | 90 | ||
91 | if (ref->object->type == OBJ_TAG) { | 91 | if (ref->object->type == OBJ_TAG) { |
92 | tag = (struct tag *)ref->object; | 92 | tag = (struct tag *)ref->object; |
93 | info = ref->tag; | 93 | info = ref->tag; |
94 | if (!tag || !info) | 94 | if (!tag || !info) |
95 | return 1; | 95 | return 1; |
96 | html("<tr><td>"); | 96 | html("<tr><td>"); |
97 | url = cgit_pageurl(ctx.qry.repo, "tag", | 97 | url = cgit_pageurl(ctx.qry.repo, "tag", |
98 | fmt("id=%s", name)); | 98 | fmt("id=%s", name)); |
99 | html_link_open(url, NULL, NULL); | 99 | html_link_open(url, NULL, NULL); |
100 | html_txt(name); | 100 | html_txt(name); |
101 | html_link_close(); | 101 | html_link_close(); |
102 | html("</td><td>"); | 102 | html("</td><td>"); |
103 | if (info->tagger_date > 0) | 103 | if (info->tagger_date > 0) |
104 | cgit_print_age(info->tagger_date, -1, NULL); | 104 | cgit_print_age(info->tagger_date, -1, NULL); |
105 | html("</td><td>"); | 105 | html("</td><td>"); |
106 | if (info->tagger) | 106 | if (info->tagger) |
107 | html(info->tagger); | 107 | html(info->tagger); |
108 | html("</td><td>"); | 108 | html("</td><td>"); |
109 | cgit_object_link(tag->tagged); | 109 | cgit_object_link(tag->tagged); |
110 | html("</td></tr>\n"); | 110 | html("</td></tr>\n"); |
111 | } else { | 111 | } else { |
112 | if (!header) | 112 | if (!header) |
113 | print_tag_header(); | 113 | print_tag_header(); |
114 | html("<tr><td>"); | 114 | html("<tr><td>"); |
115 | html_txt(name); | 115 | html_txt(name); |
116 | html("</td><td colspan='2'/><td>"); | 116 | html("</td><td colspan='2'/><td>"); |
117 | cgit_object_link(ref->object); | 117 | cgit_object_link(ref->object); |
118 | html("</td></tr>\n"); | 118 | html("</td></tr>\n"); |
119 | } | 119 | } |
120 | return 0; | 120 | return 0; |
121 | } | 121 | } |
122 | 122 | ||
123 | static void print_refs_link(char *path) | 123 | static void print_refs_link(char *path) |
124 | { | 124 | { |
125 | html("<tr class='nohover'><td colspan='4'>"); | 125 | html("<tr class='nohover'><td colspan='4'>"); |
126 | cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path); | 126 | cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path); |
127 | html("</td></tr>"); | 127 | html("</td></tr>"); |
128 | } | 128 | } |
129 | 129 | ||
130 | void cgit_print_branches(int maxcount) | 130 | void cgit_print_branches(int maxcount) |
131 | { | 131 | { |
132 | struct reflist list; | 132 | struct reflist list; |
133 | int i; | 133 | int i; |
134 | 134 | ||
135 | html("<tr class='nohover'><th class='left'>Branch</th>" | 135 | html("<tr class='nohover'><th class='left'>Branch</th>" |
136 | "<th class='left'>Idle</th>" | 136 | "<th class='left'>Idle</th>" |
137 | "<th class='left'>Author</th>" | 137 | "<th class='left'>Author</th>" |
138 | "<th class='left'>Head commit</th></tr>\n"); | 138 | "<th class='left'>Head commit</th></tr>\n"); |
139 | 139 | ||
140 | list.refs = NULL; | 140 | list.refs = NULL; |
141 | list.alloc = list.count = 0; | 141 | list.alloc = list.count = 0; |
142 | for_each_branch_ref(cgit_refs_cb, &list); | 142 | for_each_branch_ref(cgit_refs_cb, &list); |
143 | 143 | ||
144 | if (maxcount == 0 || maxcount > list.count) | 144 | if (maxcount == 0 || maxcount > list.count) |
145 | maxcount = list.count; | 145 | maxcount = list.count; |
146 | 146 | ||
147 | if (maxcount < list.count) { | 147 | if (maxcount < list.count) { |
148 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age); | 148 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age); |
149 | qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name); | 149 | qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name); |
150 | } | 150 | } |
151 | 151 | ||
152 | for(i=0; i<maxcount; i++) | 152 | for(i=0; i<maxcount; i++) |
153 | print_branch(list.refs[i]); | 153 | print_branch(list.refs[i]); |
154 | 154 | ||
155 | if (maxcount < list.count) | 155 | if (maxcount < list.count) |
156 | print_refs_link("heads"); | 156 | print_refs_link("heads"); |
157 | } | 157 | } |
158 | 158 | ||
159 | void cgit_print_tags(int maxcount) | 159 | void cgit_print_tags(int maxcount) |
160 | { | 160 | { |
161 | struct reflist list; | 161 | struct reflist list; |
162 | int i; | 162 | int i; |
163 | 163 | ||
164 | header = 0; | 164 | header = 0; |
165 | list.refs = NULL; | 165 | list.refs = NULL; |
166 | list.alloc = list.count = 0; | 166 | list.alloc = list.count = 0; |
167 | for_each_tag_ref(cgit_refs_cb, &list); | 167 | for_each_tag_ref(cgit_refs_cb, &list); |
168 | if (list.count == 0) | 168 | if (list.count == 0) |
169 | return; | 169 | return; |
170 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age); | 170 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age); |
171 | if (!maxcount) | 171 | if (!maxcount) |
172 | maxcount = list.count; | 172 | maxcount = list.count; |
173 | else if (maxcount > list.count) | 173 | else if (maxcount > list.count) |
174 | maxcount = list.count; | 174 | maxcount = list.count; |
175 | print_tag_header(); | 175 | print_tag_header(); |
176 | for(i=0; i<maxcount; i++) | 176 | for(i=0; i<maxcount; i++) |
177 | print_tag(list.refs[i]); | 177 | print_tag(list.refs[i]); |
178 | 178 | ||
179 | if (maxcount < list.count) | 179 | if (maxcount < list.count) |
180 | print_refs_link("tags"); | 180 | print_refs_link("tags"); |
181 | } | 181 | } |
182 | 182 | ||
183 | void cgit_print_summary() | 183 | void cgit_print_summary() |
184 | { | 184 | { |
185 | if (cgit_repo->readme) { | 185 | if (cgit_repo->readme) { |
186 | html("<div id='summary'>"); | 186 | html("<div id='summary'>"); |
187 | html_include(cgit_repo->readme); | 187 | html_include(cgit_repo->readme); |
188 | html("</div>"); | 188 | html("</div>"); |
189 | } | 189 | } |
190 | if (cgit_summary_log > 0) | 190 | if (ctx.cfg.summary_log > 0) |
191 | cgit_print_log(ctx.qry.head, 0, cgit_summary_log, NULL, | 191 | cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL, |
192 | NULL, NULL, 0); | 192 | NULL, NULL, 0); |
193 | html("<table summary='repository info' class='list nowrap'>"); | 193 | html("<table summary='repository info' class='list nowrap'>"); |
194 | if (cgit_summary_log > 0) | 194 | if (ctx.cfg.summary_log > 0) |
195 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); | 195 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); |
196 | cgit_print_branches(cgit_summary_branches); | 196 | cgit_print_branches(ctx.cfg.summary_branches); |
197 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); | 197 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); |
198 | cgit_print_tags(cgit_summary_tags); | 198 | cgit_print_tags(ctx.cfg.summary_tags); |
199 | html("</table>"); | 199 | html("</table>"); |
200 | } | 200 | } |