]>
git.cameronkatri.com Git - cgit.git/blob - cgit.c
1 /* cgit.c: cgi for the git scm
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "configfile.h"
14 #include "ui-shared.h"
16 const char *cgit_version
= CGIT_VERSION
;
18 void config_cb(const char *name
, const char *value
)
20 if (!strcmp(name
, "root-title"))
21 ctx
.cfg
.root_title
= xstrdup(value
);
22 else if (!strcmp(name
, "root-desc"))
23 ctx
.cfg
.root_desc
= xstrdup(value
);
24 else if (!strcmp(name
, "root-readme"))
25 ctx
.cfg
.root_readme
= xstrdup(value
);
26 else if (!strcmp(name
, "css"))
27 ctx
.cfg
.css
= xstrdup(value
);
28 else if (!strcmp(name
, "logo"))
29 ctx
.cfg
.logo
= xstrdup(value
);
30 else if (!strcmp(name
, "index-header"))
31 ctx
.cfg
.index_header
= xstrdup(value
);
32 else if (!strcmp(name
, "index-info"))
33 ctx
.cfg
.index_info
= xstrdup(value
);
34 else if (!strcmp(name
, "logo-link"))
35 ctx
.cfg
.logo_link
= xstrdup(value
);
36 else if (!strcmp(name
, "module-link"))
37 ctx
.cfg
.module_link
= xstrdup(value
);
38 else if (!strcmp(name
, "virtual-root")) {
39 ctx
.cfg
.virtual_root
= trim_end(value
, '/');
40 if (!ctx
.cfg
.virtual_root
&& (!strcmp(value
, "/")))
41 ctx
.cfg
.virtual_root
= "";
42 } else if (!strcmp(name
, "nocache"))
43 ctx
.cfg
.nocache
= atoi(value
);
44 else if (!strcmp(name
, "snapshots"))
45 ctx
.cfg
.snapshots
= cgit_parse_snapshots_mask(value
);
46 else if (!strcmp(name
, "enable-index-links"))
47 ctx
.cfg
.enable_index_links
= atoi(value
);
48 else if (!strcmp(name
, "enable-log-filecount"))
49 ctx
.cfg
.enable_log_filecount
= atoi(value
);
50 else if (!strcmp(name
, "enable-log-linecount"))
51 ctx
.cfg
.enable_log_linecount
= atoi(value
);
52 else if (!strcmp(name
, "cache-root"))
53 ctx
.cfg
.cache_root
= xstrdup(value
);
54 else if (!strcmp(name
, "cache-root-ttl"))
55 ctx
.cfg
.cache_root_ttl
= atoi(value
);
56 else if (!strcmp(name
, "cache-repo-ttl"))
57 ctx
.cfg
.cache_repo_ttl
= atoi(value
);
58 else if (!strcmp(name
, "cache-static-ttl"))
59 ctx
.cfg
.cache_static_ttl
= atoi(value
);
60 else if (!strcmp(name
, "cache-dynamic-ttl"))
61 ctx
.cfg
.cache_dynamic_ttl
= atoi(value
);
62 else if (!strcmp(name
, "max-message-length"))
63 ctx
.cfg
.max_msg_len
= atoi(value
);
64 else if (!strcmp(name
, "max-repodesc-length"))
65 ctx
.cfg
.max_repodesc_len
= atoi(value
);
66 else if (!strcmp(name
, "max-commit-count"))
67 ctx
.cfg
.max_commit_count
= atoi(value
);
68 else if (!strcmp(name
, "summary-log"))
69 ctx
.cfg
.summary_log
= atoi(value
);
70 else if (!strcmp(name
, "summary-branches"))
71 ctx
.cfg
.summary_branches
= atoi(value
);
72 else if (!strcmp(name
, "summary-tags"))
73 ctx
.cfg
.summary_tags
= atoi(value
);
74 else if (!strcmp(name
, "agefile"))
75 ctx
.cfg
.agefile
= xstrdup(value
);
76 else if (!strcmp(name
, "renamelimit"))
77 ctx
.cfg
.renamelimit
= atoi(value
);
78 else if (!strcmp(name
, "robots"))
79 ctx
.cfg
.robots
= xstrdup(value
);
80 else if (!strcmp(name
, "clone-prefix"))
81 ctx
.cfg
.clone_prefix
= xstrdup(value
);
82 else if (!strcmp(name
, "repo.group"))
83 ctx
.cfg
.repo_group
= xstrdup(value
);
84 else if (!strcmp(name
, "repo.url"))
85 ctx
.repo
= cgit_add_repo(value
);
86 else if (!strcmp(name
, "repo.name"))
87 ctx
.repo
->name
= xstrdup(value
);
88 else if (ctx
.repo
&& !strcmp(name
, "repo.path"))
89 ctx
.repo
->path
= trim_end(value
, '/');
90 else if (ctx
.repo
&& !strcmp(name
, "repo.clone-url"))
91 ctx
.repo
->clone_url
= xstrdup(value
);
92 else if (ctx
.repo
&& !strcmp(name
, "repo.desc"))
93 ctx
.repo
->desc
= xstrdup(value
);
94 else if (ctx
.repo
&& !strcmp(name
, "repo.owner"))
95 ctx
.repo
->owner
= xstrdup(value
);
96 else if (ctx
.repo
&& !strcmp(name
, "repo.defbranch"))
97 ctx
.repo
->defbranch
= xstrdup(value
);
98 else if (ctx
.repo
&& !strcmp(name
, "repo.snapshots"))
99 ctx
.repo
->snapshots
= ctx
.cfg
.snapshots
& cgit_parse_snapshots_mask(value
); /* XXX: &? */
100 else if (ctx
.repo
&& !strcmp(name
, "repo.enable-log-filecount"))
101 ctx
.repo
->enable_log_filecount
= ctx
.cfg
.enable_log_filecount
* atoi(value
);
102 else if (ctx
.repo
&& !strcmp(name
, "repo.enable-log-linecount"))
103 ctx
.repo
->enable_log_linecount
= ctx
.cfg
.enable_log_linecount
* atoi(value
);
104 else if (ctx
.repo
&& !strcmp(name
, "repo.module-link"))
105 ctx
.repo
->module_link
= xstrdup(value
);
106 else if (ctx
.repo
&& !strcmp(name
, "repo.readme") && value
!= NULL
) {
108 ctx
.repo
->readme
= xstrdup(value
);
110 ctx
.repo
->readme
= xstrdup(fmt("%s/%s", ctx
.repo
->path
, value
));
111 } else if (!strcmp(name
, "include"))
112 parse_configfile(value
, config_cb
);
115 static void querystring_cb(const char *name
, const char *value
)
117 if (!strcmp(name
,"r")) {
118 ctx
.qry
.repo
= xstrdup(value
);
119 ctx
.repo
= cgit_get_repoinfo(value
);
120 } else if (!strcmp(name
, "p")) {
121 ctx
.qry
.page
= xstrdup(value
);
122 } else if (!strcmp(name
, "url")) {
123 cgit_parse_url(value
);
124 } else if (!strcmp(name
, "qt")) {
125 ctx
.qry
.grep
= xstrdup(value
);
126 } else if (!strcmp(name
, "q")) {
127 ctx
.qry
.search
= xstrdup(value
);
128 } else if (!strcmp(name
, "h")) {
129 ctx
.qry
.head
= xstrdup(value
);
130 ctx
.qry
.has_symref
= 1;
131 } else if (!strcmp(name
, "id")) {
132 ctx
.qry
.sha1
= xstrdup(value
);
133 ctx
.qry
.has_sha1
= 1;
134 } else if (!strcmp(name
, "id2")) {
135 ctx
.qry
.sha2
= xstrdup(value
);
136 ctx
.qry
.has_sha1
= 1;
137 } else if (!strcmp(name
, "ofs")) {
138 ctx
.qry
.ofs
= atoi(value
);
139 } else if (!strcmp(name
, "path")) {
140 ctx
.qry
.path
= trim_end(value
, '/');
141 } else if (!strcmp(name
, "name")) {
142 ctx
.qry
.name
= xstrdup(value
);
146 static void prepare_context(struct cgit_context
*ctx
)
148 memset(ctx
, 0, sizeof(ctx
));
149 ctx
->cfg
.agefile
= "info/web/last-modified";
150 ctx
->cfg
.cache_dynamic_ttl
= 5;
151 ctx
->cfg
.cache_max_create_time
= 5;
152 ctx
->cfg
.cache_repo_ttl
= 5;
153 ctx
->cfg
.cache_root
= CGIT_CACHE_ROOT
;
154 ctx
->cfg
.cache_root_ttl
= 5;
155 ctx
->cfg
.cache_static_ttl
= -1;
156 ctx
->cfg
.css
= "/cgit.css";
157 ctx
->cfg
.logo
= "/git-logo.png";
158 ctx
->cfg
.max_commit_count
= 50;
159 ctx
->cfg
.max_lock_attempts
= 5;
160 ctx
->cfg
.max_msg_len
= 60;
161 ctx
->cfg
.max_repodesc_len
= 60;
162 ctx
->cfg
.module_link
= "./?repo=%s&page=commit&id=%s";
163 ctx
->cfg
.renamelimit
= -1;
164 ctx
->cfg
.robots
= "index, nofollow";
165 ctx
->cfg
.root_title
= "Git repository browser";
166 ctx
->cfg
.root_desc
= "a fast webinterface for the git dscm";
167 ctx
->cfg
.script_name
= CGIT_SCRIPT_NAME
;
168 ctx
->page
.mimetype
= "text/html";
169 ctx
->page
.charset
= PAGE_ENCODING
;
170 ctx
->page
.filename
= NULL
;
173 static int cgit_prepare_cache(struct cacheitem
*item
)
175 if (!ctx
.repo
&& ctx
.qry
.repo
) {
176 ctx
.page
.title
= fmt("%s - %s", ctx
.cfg
.root_title
,
178 cgit_print_http_headers(&ctx
);
179 cgit_print_docstart(&ctx
);
180 cgit_print_pageheader(&ctx
);
181 cgit_print_error(fmt("Unknown repo: %s", ctx
.qry
.repo
));
187 item
->name
= xstrdup(fmt("%s/index.%s.html",
189 cache_safe_filename(ctx
.qry
.raw
)));
190 item
->ttl
= ctx
.cfg
.cache_root_ttl
;
195 item
->name
= xstrdup(fmt("%s/%s/index.%s.html", ctx
.cfg
.cache_root
,
196 cache_safe_filename(ctx
.repo
->url
),
197 cache_safe_filename(ctx
.qry
.raw
)));
198 item
->ttl
= ctx
.cfg
.cache_repo_ttl
;
200 item
->name
= xstrdup(fmt("%s/%s/%s/%s.html", ctx
.cfg
.cache_root
,
201 cache_safe_filename(ctx
.repo
->url
),
203 cache_safe_filename(ctx
.qry
.raw
)));
204 if (ctx
.qry
.has_symref
)
205 item
->ttl
= ctx
.cfg
.cache_dynamic_ttl
;
206 else if (ctx
.qry
.has_sha1
)
207 item
->ttl
= ctx
.cfg
.cache_static_ttl
;
209 item
->ttl
= ctx
.cfg
.cache_repo_ttl
;
220 int find_current_ref(const char *refname
, const unsigned char *sha1
,
221 int flags
, void *cb_data
)
223 struct refmatch
*info
;
225 info
= (struct refmatch
*)cb_data
;
226 if (!strcmp(refname
, info
->req_ref
))
228 if (!info
->first_ref
)
229 info
->first_ref
= xstrdup(refname
);
233 char *find_default_branch(struct cgit_repo
*repo
)
235 struct refmatch info
;
237 info
.req_ref
= repo
->defbranch
;
238 info
.first_ref
= NULL
;
240 for_each_branch_ref(find_current_ref
, &info
);
244 return info
.first_ref
;
247 static int prepare_repo_cmd(struct cgit_context
*ctx
)
250 unsigned char sha1
[20];
253 setenv("GIT_DIR", ctx
->repo
->path
, 1);
254 setup_git_directory_gently(&nongit
);
256 ctx
->page
.title
= fmt("%s - %s", ctx
->cfg
.root_title
,
258 tmp
= fmt("Not a git repository: '%s'", ctx
->repo
->path
);
260 cgit_print_http_headers(ctx
);
261 cgit_print_docstart(ctx
);
262 cgit_print_pageheader(ctx
);
263 cgit_print_error(tmp
);
267 ctx
->page
.title
= fmt("%s - %s", ctx
->repo
->name
, ctx
->repo
->desc
);
269 if (!ctx
->qry
.head
) {
270 ctx
->qry
.head
= xstrdup(find_default_branch(ctx
->repo
));
271 ctx
->repo
->defbranch
= ctx
->qry
.head
;
274 if (!ctx
->qry
.head
) {
275 cgit_print_http_headers(ctx
);
276 cgit_print_docstart(ctx
);
277 cgit_print_pageheader(ctx
);
278 cgit_print_error("Repository seems to be empty");
283 if (get_sha1(ctx
->qry
.head
, sha1
)) {
284 tmp
= xstrdup(ctx
->qry
.head
);
285 ctx
->qry
.head
= ctx
->repo
->defbranch
;
286 cgit_print_http_headers(ctx
);
287 cgit_print_docstart(ctx
);
288 cgit_print_pageheader(ctx
);
289 cgit_print_error(fmt("Invalid branch: %s", tmp
));
296 static void process_request(struct cgit_context
*ctx
)
298 struct cgit_cmd
*cmd
;
300 cmd
= cgit_get_cmd(ctx
);
302 ctx
->page
.title
= "cgit error";
304 cgit_print_http_headers(ctx
);
305 cgit_print_docstart(ctx
);
306 cgit_print_pageheader(ctx
);
307 cgit_print_error("Invalid request");
312 if (cmd
->want_repo
&& !ctx
->repo
) {
313 cgit_print_http_headers(ctx
);
314 cgit_print_docstart(ctx
);
315 cgit_print_pageheader(ctx
);
316 cgit_print_error(fmt("No repository selected"));
321 if (ctx
->repo
&& prepare_repo_cmd(ctx
))
324 if (cmd
->want_layout
) {
325 cgit_print_http_headers(ctx
);
326 cgit_print_docstart(ctx
);
327 cgit_print_pageheader(ctx
);
332 if (cmd
->want_layout
)
336 static long ttl_seconds(long ttl
)
339 return 60 * 60 * 24 * 365;
344 static void cgit_fill_cache(struct cacheitem
*item
, int use_cache
)
349 stdout2
= chk_positive(dup(STDOUT_FILENO
),
350 "Preserving STDOUT");
351 chk_zero(close(STDOUT_FILENO
), "Closing STDOUT");
352 chk_positive(dup2(item
->fd
, STDOUT_FILENO
), "Dup2(cachefile)");
355 ctx
.page
.modified
= time(NULL
);
356 ctx
.page
.expires
= ctx
.page
.modified
+ ttl_seconds(item
->ttl
);
357 process_request(&ctx
);
360 chk_zero(close(STDOUT_FILENO
), "Close redirected STDOUT");
361 chk_positive(dup2(stdout2
, STDOUT_FILENO
),
362 "Restoring original STDOUT");
363 chk_zero(close(stdout2
), "Closing temporary STDOUT");
367 static void cgit_check_cache(struct cacheitem
*item
)
372 if (++i
> ctx
.cfg
.max_lock_attempts
) {
373 die("cgit_refresh_cache: unable to lock %s: %s",
374 item
->name
, strerror(errno
));
376 if (!cache_exist(item
)) {
377 if (!cache_lock(item
)) {
381 if (!cache_exist(item
)) {
382 cgit_fill_cache(item
, 1);
385 cache_cancel_lock(item
);
387 } else if (cache_expired(item
) && cache_lock(item
)) {
388 if (cache_expired(item
)) {
389 cgit_fill_cache(item
, 1);
392 cache_cancel_lock(item
);
397 static void cgit_print_cache(struct cacheitem
*item
)
399 static char buf
[4096];
402 int fd
= open(item
->name
, O_RDONLY
);
404 die("Unable to open cached file %s", item
->name
);
406 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
407 write(STDOUT_FILENO
, buf
, i
);
412 static void cgit_parse_args(int argc
, const char **argv
)
416 for (i
= 1; i
< argc
; i
++) {
417 if (!strncmp(argv
[i
], "--cache=", 8)) {
418 ctx
.cfg
.cache_root
= xstrdup(argv
[i
]+8);
420 if (!strcmp(argv
[i
], "--nocache")) {
423 if (!strncmp(argv
[i
], "--query=", 8)) {
424 ctx
.qry
.raw
= xstrdup(argv
[i
]+8);
426 if (!strncmp(argv
[i
], "--repo=", 7)) {
427 ctx
.qry
.repo
= xstrdup(argv
[i
]+7);
429 if (!strncmp(argv
[i
], "--page=", 7)) {
430 ctx
.qry
.page
= xstrdup(argv
[i
]+7);
432 if (!strncmp(argv
[i
], "--head=", 7)) {
433 ctx
.qry
.head
= xstrdup(argv
[i
]+7);
434 ctx
.qry
.has_symref
= 1;
436 if (!strncmp(argv
[i
], "--sha1=", 7)) {
437 ctx
.qry
.sha1
= xstrdup(argv
[i
]+7);
438 ctx
.qry
.has_sha1
= 1;
440 if (!strncmp(argv
[i
], "--ofs=", 6)) {
441 ctx
.qry
.ofs
= atoi(argv
[i
]+6);
446 int main(int argc
, const char **argv
)
448 struct cacheitem item
;
449 const char *cgit_config_env
= getenv("CGIT_CONFIG");
451 prepare_context(&ctx
);
452 item
.st
.st_mtime
= time(NULL
);
453 cgit_repolist
.length
= 0;
454 cgit_repolist
.count
= 0;
455 cgit_repolist
.repos
= NULL
;
457 parse_configfile(cgit_config_env
? cgit_config_env
: CGIT_CONFIG
,
460 if (getenv("SCRIPT_NAME"))
461 ctx
.cfg
.script_name
= xstrdup(getenv("SCRIPT_NAME"));
462 if (getenv("QUERY_STRING"))
463 ctx
.qry
.raw
= xstrdup(getenv("QUERY_STRING"));
464 cgit_parse_args(argc
, argv
);
465 http_parse_querystring(ctx
.qry
.raw
, querystring_cb
);
466 if (!cgit_prepare_cache(&item
))
468 if (ctx
.cfg
.nocache
) {
469 cgit_fill_cache(&item
, 0);
471 cgit_check_cache(&item
);
472 cgit_print_cache(&item
);