summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2007-02-03 14:02:55 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2007-02-04 20:47:46 (UTC)
commitce1c7336e5b3e3ebe8f8c9029c405aedec98c208 (patch) (unidiff)
treeb51a59a9552b32519cf694c0f5dc68c5a739069c
parentebd7b0fbc378e9beca0b275c5cd9150c930bde56 (diff)
downloadcgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.zip
cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.tar.gz
cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.tar.bz2
Read repo-info from /etc/cgitrc
This makes cgit read all repo-info from the configfile, instead of scanning for possible git-dirs below a common root path. This is primarily done to get better security (separate physical path from logical repo-name). In /etc/cgitrc each repo is registered with the following keys: repo.url repo.name repo.path repo.desc repo.owner Note: *Required keys are repo.url and repo.path, all others are optional *Each occurrence of repo.url starts a new repository registration *Default value for repo.name is taken from repo.url *The value of repo.url cannot contain characters with special meaning for urls (i.e. one of /?%&), while repo.name can contain anything. Example: repo.url=cgit-pub repo.name=cgit/public repo.path=/pub/git/cgit repo.desc=My public cgit repo repo.owner=Lars Hjemli repo.url=cgit-priv repo.name=cgit/private repo.path=/home/larsh/src/cgit/.git repo.desc=My private cgit repo repo.owner=Lars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c64
-rw-r--r--cgit.h18
-rw-r--r--cgitrc13
-rw-r--r--shared.c41
-rw-r--r--ui-repolist.c39
5 files changed, 120 insertions, 55 deletions
diff --git a/cgit.c b/cgit.c
index 9535abc..0fa203c 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,185 +1,219 @@
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
11const char cgit_version[] = CGIT_VERSION; 11const char cgit_version[] = CGIT_VERSION;
12 12
13static void cgit_prepare_cache(struct cacheitem *item) 13
14static struct repoinfo *cgit_get_repoinfo(char *url)
15{
16 int i;
17 struct repoinfo *repo;
18
19 for (i=0; i<cgit_repolist.count; i++) {
20 repo = &cgit_repolist.repos[i];
21 if (!strcmp(repo->url, url))
22 return repo;
23 }
24 return NULL;
25}
26
27
28static int cgit_prepare_cache(struct cacheitem *item)
14{ 29{
15 if (!cgit_query_repo) { 30 if (!cgit_query_repo) {
16 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 31 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
17 item->ttl = cgit_cache_root_ttl; 32 item->ttl = cgit_cache_root_ttl;
18 } else if (!cgit_query_page) { 33 return 1;
34 }
35 cgit_repo = cgit_get_repoinfo(cgit_query_repo);
36 if (!cgit_repo) {
37 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
38 cgit_print_docstart(title, item);
39 cgit_print_pageheader(title, 0);
40 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo));
41 cgit_print_docend();
42 return 0;
43 }
44
45 if (!cgit_query_page) {
19 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 46 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
20 cgit_query_repo)); 47 cgit_repo->url));
21 item->ttl = cgit_cache_repo_ttl; 48 item->ttl = cgit_cache_repo_ttl;
22 } else { 49 } else {
23 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 50 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
24 cgit_query_repo, cgit_query_page, 51 cgit_repo->url, cgit_query_page,
25 cache_safe_filename(cgit_querystring))); 52 cache_safe_filename(cgit_querystring)));
26 if (cgit_query_has_symref) 53 if (cgit_query_has_symref)
27 item->ttl = cgit_cache_dynamic_ttl; 54 item->ttl = cgit_cache_dynamic_ttl;
28 else if (cgit_query_has_sha1) 55 else if (cgit_query_has_sha1)
29 item->ttl = cgit_cache_static_ttl; 56 item->ttl = cgit_cache_static_ttl;
30 else 57 else
31 item->ttl = cgit_cache_repo_ttl; 58 item->ttl = cgit_cache_repo_ttl;
32 } 59 }
60 return 1;
33} 61}
34 62
35static void cgit_print_repo_page(struct cacheitem *item) 63static void cgit_print_repo_page(struct cacheitem *item)
36{ 64{
37 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || 65 char *title;
38 cgit_read_config("info/cgit", cgit_repo_config_cb)) { 66 int show_search;
39 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 67
68 if (chdir(cgit_repo->path)) {
69 title = fmt("%s - %s", cgit_root_title, "Bad request");
40 cgit_print_docstart(title, item); 70 cgit_print_docstart(title, item);
41 cgit_print_pageheader(title, 0); 71 cgit_print_pageheader(title, 0);
42 cgit_print_error(fmt("Unable to scan repository: %s", 72 cgit_print_error(fmt("Unable to scan repository: %s",
43 strerror(errno))); 73 strerror(errno)));
44 cgit_print_docend(); 74 cgit_print_docend();
45 return; 75 return;
46 } 76 }
47 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); 77
48 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); 78 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
49 int show_search = 0; 79 show_search = 0;
80 setenv("GIT_DIR", cgit_repo->path, 1);
50 if (cgit_query_page && !strcmp(cgit_query_page, "log")) 81 if (cgit_query_page && !strcmp(cgit_query_page, "log"))
51 show_search = 1; 82 show_search = 1;
52 cgit_print_docstart(title, item); 83 cgit_print_docstart(title, item);
53 cgit_print_pageheader(title, show_search); 84 cgit_print_pageheader(title, show_search);
54 if (!cgit_query_page) { 85 if (!cgit_query_page) {
55 cgit_print_summary(); 86 cgit_print_summary();
56 } else if (!strcmp(cgit_query_page, "log")) { 87 } else if (!strcmp(cgit_query_page, "log")) {
57 cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search); 88 cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search);
58 } else if (!strcmp(cgit_query_page, "tree")) { 89 } else if (!strcmp(cgit_query_page, "tree")) {
59 cgit_print_tree(cgit_query_sha1, cgit_query_path); 90 cgit_print_tree(cgit_query_sha1, cgit_query_path);
60 } else if (!strcmp(cgit_query_page, "commit")) { 91 } else if (!strcmp(cgit_query_page, "commit")) {
61 cgit_print_commit(cgit_query_sha1); 92 cgit_print_commit(cgit_query_sha1);
62 } else if (!strcmp(cgit_query_page, "view")) { 93 } else if (!strcmp(cgit_query_page, "view")) {
63 cgit_print_view(cgit_query_sha1); 94 cgit_print_view(cgit_query_sha1);
64 } else if (!strcmp(cgit_query_page, "diff")) { 95 } else if (!strcmp(cgit_query_page, "diff")) {
65 cgit_print_diff(cgit_query_sha1, cgit_query_sha2); 96 cgit_print_diff(cgit_query_sha1, cgit_query_sha2);
66 } 97 }
67 cgit_print_docend(); 98 cgit_print_docend();
68} 99}
69 100
70static void cgit_fill_cache(struct cacheitem *item) 101static void cgit_fill_cache(struct cacheitem *item)
71{ 102{
72 static char buf[PATH_MAX]; 103 static char buf[PATH_MAX];
73 104
74 getcwd(buf, sizeof(buf)); 105 getcwd(buf, sizeof(buf));
75 htmlfd = item->fd; 106 htmlfd = item->fd;
76 item->st.st_mtime = time(NULL); 107 item->st.st_mtime = time(NULL);
77 if (cgit_query_repo) 108 if (cgit_query_repo)
78 cgit_print_repo_page(item); 109 cgit_print_repo_page(item);
79 else 110 else
80 cgit_print_repolist(item); 111 cgit_print_repolist(item);
81 chdir(buf); 112 chdir(buf);
82} 113}
83 114
84static void cgit_check_cache(struct cacheitem *item) 115static void cgit_check_cache(struct cacheitem *item)
85{ 116{
86 int i = 0; 117 int i = 0;
87 118
88 top: 119 top:
89 if (++i > cgit_max_lock_attempts) { 120 if (++i > cgit_max_lock_attempts) {
90 die("cgit_refresh_cache: unable to lock %s: %s", 121 die("cgit_refresh_cache: unable to lock %s: %s",
91 item->name, strerror(errno)); 122 item->name, strerror(errno));
92 } 123 }
93 if (!cache_exist(item)) { 124 if (!cache_exist(item)) {
94 if (!cache_lock(item)) { 125 if (!cache_lock(item)) {
95 sleep(1); 126 sleep(1);
96 goto top; 127 goto top;
97 } 128 }
98 if (!cache_exist(item)) { 129 if (!cache_exist(item)) {
99 cgit_fill_cache(item); 130 cgit_fill_cache(item);
100 cache_unlock(item); 131 cache_unlock(item);
101 } else { 132 } else {
102 cache_cancel_lock(item); 133 cache_cancel_lock(item);
103 } 134 }
104 } else if (cache_expired(item) && cache_lock(item)) { 135 } else if (cache_expired(item) && cache_lock(item)) {
105 if (cache_expired(item)) { 136 if (cache_expired(item)) {
106 cgit_fill_cache(item); 137 cgit_fill_cache(item);
107 cache_unlock(item); 138 cache_unlock(item);
108 } else { 139 } else {
109 cache_cancel_lock(item); 140 cache_cancel_lock(item);
110 } 141 }
111 } 142 }
112} 143}
113 144
114static void cgit_print_cache(struct cacheitem *item) 145static void cgit_print_cache(struct cacheitem *item)
115{ 146{
116 static char buf[4096]; 147 static char buf[4096];
117 ssize_t i; 148 ssize_t i;
118 149
119 int fd = open(item->name, O_RDONLY); 150 int fd = open(item->name, O_RDONLY);
120 if (fd<0) 151 if (fd<0)
121 die("Unable to open cached file %s", item->name); 152 die("Unable to open cached file %s", item->name);
122 153
123 while((i=read(fd, buf, sizeof(buf))) > 0) 154 while((i=read(fd, buf, sizeof(buf))) > 0)
124 write(STDOUT_FILENO, buf, i); 155 write(STDOUT_FILENO, buf, i);
125 156
126 close(fd); 157 close(fd);
127} 158}
128 159
129static void cgit_parse_args(int argc, const char **argv) 160static void cgit_parse_args(int argc, const char **argv)
130{ 161{
131 int i; 162 int i;
132 163
133 for (i = 1; i < argc; i++) { 164 for (i = 1; i < argc; i++) {
134 if (!strncmp(argv[i], "--root=", 7)) {
135 cgit_root = xstrdup(argv[i]+7);
136 }
137 if (!strncmp(argv[i], "--cache=", 8)) { 165 if (!strncmp(argv[i], "--cache=", 8)) {
138 cgit_cache_root = xstrdup(argv[i]+8); 166 cgit_cache_root = xstrdup(argv[i]+8);
139 } 167 }
140 if (!strcmp(argv[i], "--nocache")) { 168 if (!strcmp(argv[i], "--nocache")) {
141 cgit_nocache = 1; 169 cgit_nocache = 1;
142 } 170 }
143 if (!strncmp(argv[i], "--query=", 8)) { 171 if (!strncmp(argv[i], "--query=", 8)) {
144 cgit_querystring = xstrdup(argv[i]+8); 172 cgit_querystring = xstrdup(argv[i]+8);
145 } 173 }
146 if (!strncmp(argv[i], "--repo=", 7)) { 174 if (!strncmp(argv[i], "--repo=", 7)) {
147 cgit_query_repo = xstrdup(argv[i]+7); 175 cgit_query_repo = xstrdup(argv[i]+7);
148 } 176 }
149 if (!strncmp(argv[i], "--page=", 7)) { 177 if (!strncmp(argv[i], "--page=", 7)) {
150 cgit_query_page = xstrdup(argv[i]+7); 178 cgit_query_page = xstrdup(argv[i]+7);
151 } 179 }
152 if (!strncmp(argv[i], "--head=", 7)) { 180 if (!strncmp(argv[i], "--head=", 7)) {
153 cgit_query_head = xstrdup(argv[i]+7); 181 cgit_query_head = xstrdup(argv[i]+7);
154 cgit_query_has_symref = 1; 182 cgit_query_has_symref = 1;
155 } 183 }
156 if (!strncmp(argv[i], "--sha1=", 7)) { 184 if (!strncmp(argv[i], "--sha1=", 7)) {
157 cgit_query_sha1 = xstrdup(argv[i]+7); 185 cgit_query_sha1 = xstrdup(argv[i]+7);
158 cgit_query_has_sha1 = 1; 186 cgit_query_has_sha1 = 1;
159 } 187 }
160 if (!strncmp(argv[i], "--ofs=", 6)) { 188 if (!strncmp(argv[i], "--ofs=", 6)) {
161 cgit_query_ofs = atoi(argv[i]+6); 189 cgit_query_ofs = atoi(argv[i]+6);
162 } 190 }
163 } 191 }
164} 192}
165 193
166int main(int argc, const char **argv) 194int main(int argc, const char **argv)
167{ 195{
168 struct cacheitem item; 196 struct cacheitem item;
169 197
198 htmlfd = STDOUT_FILENO;
199 item.st.st_mtime = time(NULL);
200 cgit_repolist.length = 0;
201 cgit_repolist.count = 0;
202 cgit_repolist.repos = NULL;
203
170 cgit_read_config("/etc/cgitrc", cgit_global_config_cb); 204 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
171 if (getenv("QUERY_STRING")) 205 if (getenv("QUERY_STRING"))
172 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 206 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
173 cgit_parse_args(argc, argv); 207 cgit_parse_args(argc, argv);
174 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 208 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
175 209 if (!cgit_prepare_cache(&item))
176 cgit_prepare_cache(&item); 210 return 0;
177 if (cgit_nocache) { 211 if (cgit_nocache) {
178 item.fd = STDOUT_FILENO; 212 item.fd = STDOUT_FILENO;
179 cgit_fill_cache(&item); 213 cgit_fill_cache(&item);
180 } else { 214 } else {
181 cgit_check_cache(&item); 215 cgit_check_cache(&item);
182 cgit_print_cache(&item); 216 cgit_print_cache(&item);
183 } 217 }
184 return 0; 218 return 0;
185} 219}
diff --git a/cgit.h b/cgit.h
index a3b5385..2a3cd5a 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,126 +1,142 @@
1#ifndef CGIT_H 1#ifndef CGIT_H
2#define CGIT_H 2#define CGIT_H
3 3
4#include "git.h" 4#include "git.h"
5#include <openssl/sha.h> 5#include <openssl/sha.h>
6#include <ctype.h> 6#include <ctype.h>
7#include <sched.h> 7#include <sched.h>
8 8
9typedef void (*configfn)(const char *name, const char *value); 9typedef void (*configfn)(const char *name, const char *value);
10 10
11struct cacheitem { 11struct cacheitem {
12 char *name; 12 char *name;
13 struct stat st; 13 struct stat st;
14 int ttl; 14 int ttl;
15 int fd; 15 int fd;
16}; 16};
17 17
18struct repoinfo {
19 char *url;
20 char *name;
21 char *path;
22 char *desc;
23 char *owner;
24};
25
26struct repolist {
27 int length;
28 int count;
29 struct repoinfo *repos;
30};
31
18struct commitinfo { 32struct commitinfo {
19 struct commit *commit; 33 struct commit *commit;
20 char *author; 34 char *author;
21 char *author_email; 35 char *author_email;
22 unsigned long author_date; 36 unsigned long author_date;
23 char *committer; 37 char *committer;
24 char *committer_email; 38 char *committer_email;
25 unsigned long committer_date; 39 unsigned long committer_date;
26 char *subject; 40 char *subject;
27 char *msg; 41 char *msg;
28}; 42};
29 43
30struct taginfo { 44struct taginfo {
31 char *tagger; 45 char *tagger;
32 char *tagger_email; 46 char *tagger_email;
33 int tagger_date; 47 int tagger_date;
34 char *msg; 48 char *msg;
35}; 49};
36 50
37extern const char cgit_version[]; 51extern const char cgit_version[];
38 52
39extern char *cgit_root; 53extern struct repolist cgit_repolist;
54extern struct repoinfo *cgit_repo;
55
40extern char *cgit_root_title; 56extern char *cgit_root_title;
41extern char *cgit_css; 57extern char *cgit_css;
42extern char *cgit_logo; 58extern char *cgit_logo;
43extern char *cgit_logo_link; 59extern char *cgit_logo_link;
44extern char *cgit_virtual_root; 60extern char *cgit_virtual_root;
45extern char *cgit_cache_root; 61extern char *cgit_cache_root;
46 62
47extern int cgit_nocache; 63extern int cgit_nocache;
48extern int cgit_max_lock_attempts; 64extern int cgit_max_lock_attempts;
49extern int cgit_cache_root_ttl; 65extern int cgit_cache_root_ttl;
50extern int cgit_cache_repo_ttl; 66extern int cgit_cache_repo_ttl;
51extern int cgit_cache_dynamic_ttl; 67extern int cgit_cache_dynamic_ttl;
52extern int cgit_cache_static_ttl; 68extern int cgit_cache_static_ttl;
53extern int cgit_cache_max_create_time; 69extern int cgit_cache_max_create_time;
54 70
55extern int cgit_max_msg_len; 71extern int cgit_max_msg_len;
56 72
57extern char *cgit_repo_name; 73extern char *cgit_repo_name;
58extern char *cgit_repo_desc; 74extern char *cgit_repo_desc;
59extern char *cgit_repo_owner; 75extern char *cgit_repo_owner;
60 76
61extern int cgit_query_has_symref; 77extern int cgit_query_has_symref;
62extern int cgit_query_has_sha1; 78extern int cgit_query_has_sha1;
63 79
64extern char *cgit_querystring; 80extern char *cgit_querystring;
65extern char *cgit_query_repo; 81extern char *cgit_query_repo;
66extern char *cgit_query_page; 82extern char *cgit_query_page;
67extern char *cgit_query_search; 83extern char *cgit_query_search;
68extern char *cgit_query_head; 84extern char *cgit_query_head;
69extern char *cgit_query_sha1; 85extern char *cgit_query_sha1;
70extern char *cgit_query_sha2; 86extern char *cgit_query_sha2;
71extern char *cgit_query_path; 87extern char *cgit_query_path;
72extern int cgit_query_ofs; 88extern int cgit_query_ofs;
73 89
74extern int htmlfd; 90extern int htmlfd;
75 91
76extern void cgit_global_config_cb(const char *name, const char *value); 92extern void cgit_global_config_cb(const char *name, const char *value);
77extern void cgit_repo_config_cb(const char *name, const char *value); 93extern void cgit_repo_config_cb(const char *name, const char *value);
78extern void cgit_querystring_cb(const char *name, const char *value); 94extern void cgit_querystring_cb(const char *name, const char *value);
79 95
80extern int hextoint(char c); 96extern int hextoint(char c);
81 97
82extern void *cgit_free_commitinfo(struct commitinfo *info); 98extern void *cgit_free_commitinfo(struct commitinfo *info);
83 99
84extern char *fmt(const char *format,...); 100extern char *fmt(const char *format,...);
85 101
86extern void html(const char *txt); 102extern void html(const char *txt);
87extern void htmlf(const char *format,...); 103extern void htmlf(const char *format,...);
88extern void html_txt(char *txt); 104extern void html_txt(char *txt);
89extern void html_ntxt(int len, char *txt); 105extern void html_ntxt(int len, char *txt);
90extern void html_attr(char *txt); 106extern void html_attr(char *txt);
91extern void html_hidden(char *name, char *value); 107extern void html_hidden(char *name, char *value);
92extern void html_link_open(char *url, char *title, char *class); 108extern void html_link_open(char *url, char *title, char *class);
93extern void html_link_close(void); 109extern void html_link_close(void);
94extern void html_filemode(unsigned short mode); 110extern void html_filemode(unsigned short mode);
95 111
96extern int cgit_read_config(const char *filename, configfn fn); 112extern int cgit_read_config(const char *filename, configfn fn);
97extern int cgit_parse_query(char *txt, configfn fn); 113extern int cgit_parse_query(char *txt, configfn fn);
98extern struct commitinfo *cgit_parse_commit(struct commit *commit); 114extern struct commitinfo *cgit_parse_commit(struct commit *commit);
99extern struct taginfo *cgit_parse_tag(struct tag *tag); 115extern struct taginfo *cgit_parse_tag(struct tag *tag);
100 116
101extern char *cache_safe_filename(const char *unsafe); 117extern char *cache_safe_filename(const char *unsafe);
102extern int cache_lock(struct cacheitem *item); 118extern int cache_lock(struct cacheitem *item);
103extern int cache_unlock(struct cacheitem *item); 119extern int cache_unlock(struct cacheitem *item);
104extern int cache_cancel_lock(struct cacheitem *item); 120extern int cache_cancel_lock(struct cacheitem *item);
105extern int cache_exist(struct cacheitem *item); 121extern int cache_exist(struct cacheitem *item);
106extern int cache_expired(struct cacheitem *item); 122extern int cache_expired(struct cacheitem *item);
107 123
108extern char *cgit_repourl(const char *reponame); 124extern char *cgit_repourl(const char *reponame);
109extern char *cgit_pageurl(const char *reponame, const char *pagename, 125extern char *cgit_pageurl(const char *reponame, const char *pagename,
110 const char *query); 126 const char *query);
111 127
112extern void cgit_print_error(char *msg); 128extern void cgit_print_error(char *msg);
113extern void cgit_print_date(unsigned long secs); 129extern void cgit_print_date(unsigned long secs);
114extern void cgit_print_docstart(char *title, struct cacheitem *item); 130extern void cgit_print_docstart(char *title, struct cacheitem *item);
115extern void cgit_print_docend(); 131extern void cgit_print_docend();
116extern void cgit_print_pageheader(char *title, int show_search); 132extern void cgit_print_pageheader(char *title, int show_search);
117 133
118extern void cgit_print_repolist(struct cacheitem *item); 134extern void cgit_print_repolist(struct cacheitem *item);
119extern void cgit_print_summary(); 135extern void cgit_print_summary();
120extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep); 136extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep);
121extern void cgit_print_view(const char *hex); 137extern void cgit_print_view(const char *hex);
122extern void cgit_print_tree(const char *hex, char *path); 138extern void cgit_print_tree(const char *hex, char *path);
123extern void cgit_print_commit(const char *hex); 139extern void cgit_print_commit(const char *hex);
124extern void cgit_print_diff(const char *old_hex, const char *new_hex); 140extern void cgit_print_diff(const char *old_hex, const char *new_hex);
125 141
126#endif /* CGIT_H */ 142#endif /* CGIT_H */
diff --git a/cgitrc b/cgitrc
index 7e7fae5..da3d138 100644
--- a/cgitrc
+++ b/cgitrc
@@ -1,63 +1,68 @@
1## 1##
2## cgitrc: template for /etc/cgitrc 2## cgitrc: template for /etc/cgitrc
3## 3##
4 4
5 5
6## root folder for git repos
7#root=/usr/src/git
8
9
10## base for virtual urls. If specified, rewrite rules must be added to 6## base for virtual urls. If specified, rewrite rules must be added to
11## httpd.conf. Possible rules for /git/ when cgit.cgi is accessed as /cgit.cgi: 7## httpd.conf. Possible rules for /git/ when cgit.cgi is accessed as /cgit.cgi:
12## 8##
13## RewriteRule ^/git/$ /cgit.cgi [L,QSA] 9## RewriteRule ^/git/$ /cgit.cgi [L,QSA]
14## RewriteRule ^/git/([^/]+)/$ /cgit.cgi?r=$1 [L,QSA] 10## RewriteRule ^/git/([^/]+)/$ /cgit.cgi?r=$1 [L,QSA]
15## RewriteRule ^/git/([^/]+)/([^/]+)/$ /cgit.cgi?r=$1&p=$2 [L,QSA] 11## RewriteRule ^/git/([^/]+)/([^/]+)/$ /cgit.cgi?r=$1&p=$2 [L,QSA]
16## 12##
17#virtual-root=/git 13#virtual-root=/git
18 14
19 15
20## page title for the root page (repo listing) 16## page title for the root page (repo listing)
21#root-title=Git repository browser 17#root-title=Git repository browser
22 18
23 19
24## link to css file 20## link to css file
25#css=/cgit.css 21#css=/cgit.css
26 22
27 23
28## link to logo file 24## link to logo file
29#logo=/git-logo.png 25#logo=/git-logo.png
30 26
31 27
32## url loaded when clicking the logo 28## url loaded when clicking the logo
33#logo-link=http://www.kernel.org/pub/software/scm/git/docs/ 29#logo-link=http://www.kernel.org/pub/software/scm/git/docs/
34 30
35 31
36## set number of initial chars to show of commit subject message in log views 32## set number of initial chars to show of commit subject message in log views
37#max-message-length=60 33#max-message-length=60
38 34
39 35
40## Set to 1 to deactivate caching of generated pages 36## Set to 1 to deactivate caching of generated pages
41#nocache=0 37#nocache=0
42 38
43 39
44## root path for cached output 40## root path for cached output
45#cache-root=/var/cache/cgit 41#cache-root=/var/cache/cgit
46 42
47 43
48## 44##
49## Time-To-Live settings: specify how long (in minutes) different pages should 45## Time-To-Live settings: specify how long (in minutes) different pages should
50## be cached. Specify 0 for instant expiration and -1 for immortal pages 46## be cached. Specify 0 for instant expiration and -1 for immortal pages
51## 47##
52 48
53## ttl for root page (repo listing) 49## ttl for root page (repo listing)
54#cache-root-ttl=5 50#cache-root-ttl=5
55 51
56## ttl for repo summary page 52## ttl for repo summary page
57#cache-repo-ttl=5 53#cache-repo-ttl=5
58 54
59## ttl for other dynamic pages 55## ttl for other dynamic pages
60#cache-dynamic-ttl=5 56#cache-dynamic-ttl=5
61 57
62## ttl for static pages (addressed by SHA-1) 58## ttl for static pages (addressed by SHA-1)
63#cache-static-ttl=-1 59#cache-static-ttl=-1
60
61
62
63## Example repository entry
64#repo.url=cgit
65#repo.name=cgit
66#repo.desc=the caching cgi for git
67#repo.path=/pub/git/cgit
68#repo.owner=Lars Hjemli
diff --git a/shared.c b/shared.c
index 8e6df31..6fd70a8 100644
--- a/shared.c
+++ b/shared.c
@@ -1,135 +1,168 @@
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
11char *cgit_root = "/usr/src/git"; 11struct repolist cgit_repolist;
12struct repoinfo *cgit_repo;
13
12char *cgit_root_title = "Git repository browser"; 14char *cgit_root_title = "Git repository browser";
13char *cgit_css = "/cgit.css"; 15char *cgit_css = "/cgit.css";
14char *cgit_logo = "/git-logo.png"; 16char *cgit_logo = "/git-logo.png";
15char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; 17char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
16char *cgit_virtual_root = NULL; 18char *cgit_virtual_root = NULL;
17 19
18char *cgit_cache_root = "/var/cache/cgit"; 20char *cgit_cache_root = "/var/cache/cgit";
19 21
20int cgit_nocache = 0; 22int cgit_nocache = 0;
21int cgit_max_lock_attempts = 5; 23int cgit_max_lock_attempts = 5;
22int cgit_cache_root_ttl = 5; 24int cgit_cache_root_ttl = 5;
23int cgit_cache_repo_ttl = 5; 25int cgit_cache_repo_ttl = 5;
24int cgit_cache_dynamic_ttl = 5; 26int cgit_cache_dynamic_ttl = 5;
25int cgit_cache_static_ttl = -1; 27int cgit_cache_static_ttl = -1;
26int cgit_cache_max_create_time = 5; 28int cgit_cache_max_create_time = 5;
27 29
28int cgit_max_msg_len = 60; 30int cgit_max_msg_len = 60;
29 31
30char *cgit_repo_name = NULL; 32char *cgit_repo_name = NULL;
31char *cgit_repo_desc = NULL; 33char *cgit_repo_desc = NULL;
32char *cgit_repo_owner = NULL; 34char *cgit_repo_owner = NULL;
33 35
34int cgit_query_has_symref = 0; 36int cgit_query_has_symref = 0;
35int cgit_query_has_sha1 = 0; 37int cgit_query_has_sha1 = 0;
36 38
37char *cgit_querystring = NULL; 39char *cgit_querystring = NULL;
38char *cgit_query_repo = NULL; 40char *cgit_query_repo = NULL;
39char *cgit_query_page = NULL; 41char *cgit_query_page = NULL;
40char *cgit_query_head = NULL; 42char *cgit_query_head = NULL;
41char *cgit_query_search = NULL; 43char *cgit_query_search = NULL;
42char *cgit_query_sha1 = NULL; 44char *cgit_query_sha1 = NULL;
43char *cgit_query_sha2 = NULL; 45char *cgit_query_sha2 = NULL;
44char *cgit_query_path = NULL; 46char *cgit_query_path = NULL;
45int cgit_query_ofs = 0; 47int cgit_query_ofs = 0;
46 48
47int htmlfd = 0; 49int htmlfd = 0;
48 50
51struct repoinfo *add_repo(const char *url)
52{
53 struct repoinfo *ret;
54
55 if (++cgit_repolist.count > cgit_repolist.length) {
56 if (cgit_repolist.length == 0)
57 cgit_repolist.length = 8;
58 else
59 cgit_repolist.length *= 2;
60 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
61 cgit_repolist.length *
62 sizeof(struct repoinfo));
63 }
64
65 ret = &cgit_repolist.repos[cgit_repolist.count-1];
66 ret->url = xstrdup(url);
67 ret->name = ret->url;
68 ret->path = NULL;
69 ret->desc = NULL;
70 ret->owner = NULL;
71 return ret;
72}
73
49void cgit_global_config_cb(const char *name, const char *value) 74void cgit_global_config_cb(const char *name, const char *value)
50{ 75{
51 if (!strcmp(name, "root")) 76 if (!strcmp(name, "root-title"))
52 cgit_root = xstrdup(value);
53 else if (!strcmp(name, "root-title"))
54 cgit_root_title = xstrdup(value); 77 cgit_root_title = xstrdup(value);
55 else if (!strcmp(name, "css")) 78 else if (!strcmp(name, "css"))
56 cgit_css = xstrdup(value); 79 cgit_css = xstrdup(value);
57 else if (!strcmp(name, "logo")) 80 else if (!strcmp(name, "logo"))
58 cgit_logo = xstrdup(value); 81 cgit_logo = xstrdup(value);
59 else if (!strcmp(name, "logo-link")) 82 else if (!strcmp(name, "logo-link"))
60 cgit_logo_link = xstrdup(value); 83 cgit_logo_link = xstrdup(value);
61 else if (!strcmp(name, "virtual-root")) 84 else if (!strcmp(name, "virtual-root"))
62 cgit_virtual_root = xstrdup(value); 85 cgit_virtual_root = xstrdup(value);
63 else if (!strcmp(name, "nocache")) 86 else if (!strcmp(name, "nocache"))
64 cgit_nocache = atoi(value); 87 cgit_nocache = atoi(value);
65 else if (!strcmp(name, "cache-root")) 88 else if (!strcmp(name, "cache-root"))
66 cgit_cache_root = xstrdup(value); 89 cgit_cache_root = xstrdup(value);
67 else if (!strcmp(name, "cache-root-ttl")) 90 else if (!strcmp(name, "cache-root-ttl"))
68 cgit_cache_root_ttl = atoi(value); 91 cgit_cache_root_ttl = atoi(value);
69 else if (!strcmp(name, "cache-repo-ttl")) 92 else if (!strcmp(name, "cache-repo-ttl"))
70 cgit_cache_repo_ttl = atoi(value); 93 cgit_cache_repo_ttl = atoi(value);
71 else if (!strcmp(name, "cache-static-ttl")) 94 else if (!strcmp(name, "cache-static-ttl"))
72 cgit_cache_static_ttl = atoi(value); 95 cgit_cache_static_ttl = atoi(value);
73 else if (!strcmp(name, "cache-dynamic-ttl")) 96 else if (!strcmp(name, "cache-dynamic-ttl"))
74 cgit_cache_dynamic_ttl = atoi(value); 97 cgit_cache_dynamic_ttl = atoi(value);
75 else if (!strcmp(name, "max-message-length")) 98 else if (!strcmp(name, "max-message-length"))
76 cgit_max_msg_len = atoi(value); 99 cgit_max_msg_len = atoi(value);
100 else if (!strcmp(name, "repo.url"))
101 cgit_repo = add_repo(value);
102 else if (!strcmp(name, "repo.name"))
103 cgit_repo->name = xstrdup(value);
104 else if (cgit_repo && !strcmp(name, "repo.path"))
105 cgit_repo->path = xstrdup(value);
106 else if (cgit_repo && !strcmp(name, "repo.desc"))
107 cgit_repo->desc = xstrdup(value);
108 else if (cgit_repo && !strcmp(name, "repo.owner"))
109 cgit_repo->owner = xstrdup(value);
77} 110}
78 111
79void cgit_repo_config_cb(const char *name, const char *value) 112void cgit_repo_config_cb(const char *name, const char *value)
80{ 113{
81 if (!strcmp(name, "name")) 114 if (!strcmp(name, "name"))
82 cgit_repo_name = xstrdup(value); 115 cgit_repo_name = xstrdup(value);
83 else if (!strcmp(name, "desc")) 116 else if (!strcmp(name, "desc"))
84 cgit_repo_desc = xstrdup(value); 117 cgit_repo_desc = xstrdup(value);
85 else if (!strcmp(name, "owner")) 118 else if (!strcmp(name, "owner"))
86 cgit_repo_owner = xstrdup(value); 119 cgit_repo_owner = xstrdup(value);
87} 120}
88 121
89void cgit_querystring_cb(const char *name, const char *value) 122void cgit_querystring_cb(const char *name, const char *value)
90{ 123{
91 if (!strcmp(name,"r")) { 124 if (!strcmp(name,"r")) {
92 cgit_query_repo = xstrdup(value); 125 cgit_query_repo = xstrdup(value);
93 } else if (!strcmp(name, "p")) { 126 } else if (!strcmp(name, "p")) {
94 cgit_query_page = xstrdup(value); 127 cgit_query_page = xstrdup(value);
95 } else if (!strcmp(name, "q")) { 128 } else if (!strcmp(name, "q")) {
96 cgit_query_search = xstrdup(value); 129 cgit_query_search = xstrdup(value);
97 } else if (!strcmp(name, "h")) { 130 } else if (!strcmp(name, "h")) {
98 cgit_query_head = xstrdup(value); 131 cgit_query_head = xstrdup(value);
99 cgit_query_has_symref = 1; 132 cgit_query_has_symref = 1;
100 } else if (!strcmp(name, "id")) { 133 } else if (!strcmp(name, "id")) {
101 cgit_query_sha1 = xstrdup(value); 134 cgit_query_sha1 = xstrdup(value);
102 cgit_query_has_sha1 = 1; 135 cgit_query_has_sha1 = 1;
103 } else if (!strcmp(name, "id2")) { 136 } else if (!strcmp(name, "id2")) {
104 cgit_query_sha2 = xstrdup(value); 137 cgit_query_sha2 = xstrdup(value);
105 cgit_query_has_sha1 = 1; 138 cgit_query_has_sha1 = 1;
106 } else if (!strcmp(name, "ofs")) { 139 } else if (!strcmp(name, "ofs")) {
107 cgit_query_ofs = atoi(value); 140 cgit_query_ofs = atoi(value);
108 } else if (!strcmp(name, "path")) { 141 } else if (!strcmp(name, "path")) {
109 cgit_query_path = xstrdup(value); 142 cgit_query_path = xstrdup(value);
110 } 143 }
111} 144}
112 145
113void *cgit_free_commitinfo(struct commitinfo *info) 146void *cgit_free_commitinfo(struct commitinfo *info)
114{ 147{
115 free(info->author); 148 free(info->author);
116 free(info->author_email); 149 free(info->author_email);
117 free(info->committer); 150 free(info->committer);
118 free(info->committer_email); 151 free(info->committer_email);
119 free(info->subject); 152 free(info->subject);
120 free(info); 153 free(info);
121 return NULL; 154 return NULL;
122} 155}
123 156
124int hextoint(char c) 157int hextoint(char c)
125{ 158{
126 if (c >= 'a' && c <= 'f') 159 if (c >= 'a' && c <= 'f')
127 return 10 + c - 'a'; 160 return 10 + c - 'a';
128 else if (c >= 'A' && c <= 'F') 161 else if (c >= 'A' && c <= 'F')
129 return 10 + c - 'A'; 162 return 10 + c - 'A';
130 else if (c >= '0' && c <= '9') 163 else if (c >= '0' && c <= '9')
131 return c - '0'; 164 return c - '0';
132 else 165 else
133 return -1; 166 return -1;
134} 167}
135 168
diff --git a/ui-repolist.c b/ui-repolist.c
index bd4af59..011ec95 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -1,63 +1,40 @@
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 10
11void cgit_print_repolist(struct cacheitem *item) 11void cgit_print_repolist(struct cacheitem *item)
12{ 12{
13 DIR *d; 13 struct repoinfo *repo;
14 struct dirent *de; 14 int i;
15 struct stat st;
16 char *name;
17 15
18 chdir(cgit_root);
19 cgit_print_docstart(cgit_root_title, item); 16 cgit_print_docstart(cgit_root_title, item);
20 cgit_print_pageheader(cgit_root_title, 0); 17 cgit_print_pageheader(cgit_root_title, 0);
21 18
22 if (!(d = opendir("."))) {
23 cgit_print_error(fmt("Unable to scan repository directory: %s",
24 strerror(errno)));
25 cgit_print_docend();
26 return;
27 }
28
29 html("<h2>Repositories</h2>\n"); 19 html("<h2>Repositories</h2>\n");
30 html("<table class='list nowrap'>"); 20 html("<table class='list nowrap'>");
31 html("<tr class='nohover'>" 21 html("<tr class='nohover'>"
32 "<th class='left'>Name</th>" 22 "<th class='left'>Name</th>"
33 "<th class='left'>Description</th>" 23 "<th class='left'>Description</th>"
34 "<th class='left'>Owner</th></tr>\n"); 24 "<th class='left'>Owner</th></tr>\n");
35 while ((de = readdir(d)) != NULL) {
36 if (de->d_name[0] == '.')
37 continue;
38 if (stat(de->d_name, &st) < 0)
39 continue;
40 if (!S_ISDIR(st.st_mode))
41 continue;
42
43 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
44 name = fmt("%s/info/cgit", de->d_name);
45 if (cgit_read_config(name, cgit_repo_config_cb))
46 continue;
47 25
26 for (i=0; i<cgit_repolist.count; i++) {
27 repo = &cgit_repolist.repos[i];
48 html("<tr><td>"); 28 html("<tr><td>");
49 html_link_open(cgit_repourl(de->d_name), NULL, NULL); 29 html_link_open(cgit_repourl(repo->url), NULL, NULL);
50 html_txt(cgit_repo_name); 30 html_txt(repo->name);
51 html_link_close(); 31 html_link_close();
52 html("</td><td>"); 32 html("</td><td>");
53 html_txt(cgit_repo_desc); 33 html_txt(repo->desc);
54 html("</td><td>"); 34 html("</td><td>");
55 html_txt(cgit_repo_owner); 35 html_txt(repo->owner);
56 html("</td></tr>\n"); 36 html("</td></tr>\n");
57 } 37 }
58 closedir(d);
59 html("</table>"); 38 html("</table>");
60 cgit_print_docend(); 39 cgit_print_docend();
61} 40}
62
63