author | Lars Hjemli <hjemli@gmail.com> | 2008-04-08 19:29:21 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-04-08 19:29:21 (UTC) |
commit | 23296ad648c0e2a9e3cf40a3de322b10ad25cce3 (patch) (unidiff) | |
tree | 136493d8228b0ff4971feb06b0e8aee296367b00 | |
parent | e2a44cf0923398396b7a321d5ce894ad3bf6f580 (diff) | |
parent | c6f747649ace1a92ed5dfaae9cc1ea3affe0bf51 (diff) | |
download | cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.zip cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.gz cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.bz2 |
Merge branch 'lh/cleanup'
* lh/cleanup: (21 commits)
Reset ctx.repo to NULL when the config parser is finished
Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
Move function for configfile parsing into configfile.[ch]
Add cache.h
Remove global and obsolete cgit_cmd
Makefile: copy the QUIET constructs from the Makefile in git.git
Move cgit_version from shared.c to cgit.c
Makefile: autobuild dependency rules
Initial Makefile cleanup
Move non-generic functions from shared.c to cgit.c
Add ui-shared.h
Add separate header-files for each page/view
Refactor snapshot support
Add command dispatcher
Remove obsolete cacheitem parameter to ui-functions
Add struct cgit_page to cgit_context
Introduce html.h
Improve initialization of git directory
Move cgit_repo into cgit_context
Add all config variables into struct cgit_context
...
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 75 | ||||
-rw-r--r-- | cache.c | 21 | ||||
-rw-r--r-- | cache.h | 23 | ||||
-rw-r--r-- | cgit.c | 391 | ||||
-rw-r--r-- | cgit.h | 237 | ||||
-rw-r--r-- | cmd.c | 112 | ||||
-rw-r--r-- | cmd.h | 15 | ||||
-rw-r--r-- | configfile.c | 87 | ||||
-rw-r--r-- | configfile.h | 8 | ||||
-rw-r--r-- | html.c | 95 | ||||
-rw-r--r-- | html.h | 20 | ||||
-rw-r--r-- | parsing.c | 146 | ||||
-rw-r--r-- | shared.c | 257 | ||||
-rw-r--r-- | ui-blob.c | 16 | ||||
-rw-r--r-- | ui-blob.h | 6 | ||||
-rw-r--r-- | ui-commit.c | 26 | ||||
-rw-r--r-- | ui-commit.h | 6 | ||||
-rw-r--r-- | ui-diff.c | 9 | ||||
-rw-r--r-- | ui-diff.h | 7 | ||||
-rw-r--r-- | ui-log.c | 32 | ||||
-rw-r--r-- | ui-log.h | 7 | ||||
-rw-r--r-- | ui-patch.c | 10 | ||||
-rw-r--r-- | ui-patch.h | 6 | ||||
-rw-r--r-- | ui-refs.c | 175 | ||||
-rw-r--r-- | ui-refs.h | 8 | ||||
-rw-r--r-- | ui-repolist.c | 54 | ||||
-rw-r--r-- | ui-repolist.h | 6 | ||||
-rw-r--r-- | ui-shared.c | 237 | ||||
-rw-r--r-- | ui-shared.h | 36 | ||||
-rw-r--r-- | ui-snapshot.c | 123 | ||||
-rw-r--r-- | ui-snapshot.h | 8 | ||||
-rw-r--r-- | ui-summary.c | 189 | ||||
-rw-r--r-- | ui-summary.h | 6 | ||||
-rw-r--r-- | ui-tag.c | 3 | ||||
-rw-r--r-- | ui-tag.h | 6 | ||||
-rw-r--r-- | ui-tree.c | 26 | ||||
-rw-r--r-- | ui-tree.h | 6 |
38 files changed, 1379 insertions, 1117 deletions
@@ -1,5 +1,6 @@ | |||
1 | # Files I don't care to see in git-status/commit | 1 | # Files I don't care to see in git-status/commit |
2 | cgit | 2 | cgit |
3 | cgit.conf | 3 | cgit.conf |
4 | VERSION | 4 | VERSION |
5 | *.o | 5 | *.o |
6 | *.d | ||
@@ -1,83 +1,136 @@ | |||
1 | CGIT_VERSION = v0.7.2 | 1 | CGIT_VERSION = v0.7.2 |
2 | CGIT_SCRIPT_NAME = cgit.cgi | 2 | CGIT_SCRIPT_NAME = cgit.cgi |
3 | CGIT_SCRIPT_PATH = /var/www/htdocs/cgit | 3 | CGIT_SCRIPT_PATH = /var/www/htdocs/cgit |
4 | CGIT_CONFIG = /etc/cgitrc | 4 | CGIT_CONFIG = /etc/cgitrc |
5 | CACHE_ROOT = /var/cache/cgit | 5 | CACHE_ROOT = /var/cache/cgit |
6 | SHA1_HEADER = <openssl/sha.h> | 6 | SHA1_HEADER = <openssl/sha.h> |
7 | GIT_VER = 1.5.5.rc1 | 7 | GIT_VER = 1.5.5.rc1 |
8 | GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 | 8 | GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 |
9 | 9 | ||
10 | # | 10 | # |
11 | # Let the user override the above settings. | 11 | # Let the user override the above settings. |
12 | # | 12 | # |
13 | -include cgit.conf | 13 | -include cgit.conf |
14 | 14 | ||
15 | # | ||
16 | # Define a way to invoke make in subdirs quietly, shamelessly ripped | ||
17 | # from git.git | ||
18 | # | ||
19 | QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir | ||
20 | QUIET_SUBDIR1 = | ||
15 | 21 | ||
16 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto | 22 | ifneq ($(findstring $(MAKEFLAGS),w),w) |
17 | OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ | 23 | PRINT_DIR = --no-print-directory |
18 | ui-summary.o ui-log.o ui-tree.o ui-commit.o ui-diff.o \ | 24 | else # "make -w" |
19 | ui-snapshot.o ui-blob.o ui-tag.o ui-refs.o ui-patch.o | 25 | NO_SUBDIR = : |
26 | endif | ||
27 | |||
28 | ifndef V | ||
29 | QUIET_CC = @echo ' ' CC $@; | ||
30 | QUIET_MM = @echo ' ' MM $@; | ||
31 | QUIET_SUBDIR0 = +@subdir= | ||
32 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ | ||
33 | $(MAKE) $(PRINT_DIR) -C $$subdir | ||
34 | endif | ||
35 | |||
36 | # | ||
37 | # Define a pattern rule for automatic dependency building | ||
38 | # | ||
39 | %.d: %.c | ||
40 | $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ | ||
41 | |||
42 | # | ||
43 | # Define a pattern rule for silent object building | ||
44 | # | ||
45 | %.o: %.c | ||
46 | $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< | ||
20 | 47 | ||
21 | 48 | ||
49 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto | ||
50 | OBJECTS = | ||
51 | OBJECTS += cache.o | ||
52 | OBJECTS += cgit.o | ||
53 | OBJECTS += cmd.o | ||
54 | OBJECTS += configfile.o | ||
55 | OBJECTS += html.o | ||
56 | OBJECTS += parsing.o | ||
57 | OBJECTS += shared.o | ||
58 | OBJECTS += ui-blob.o | ||
59 | OBJECTS += ui-commit.o | ||
60 | OBJECTS += ui-diff.o | ||
61 | OBJECTS += ui-log.o | ||
62 | OBJECTS += ui-patch.o | ||
63 | OBJECTS += ui-refs.o | ||
64 | OBJECTS += ui-repolist.o | ||
65 | OBJECTS += ui-shared.o | ||
66 | OBJECTS += ui-snapshot.o | ||
67 | OBJECTS += ui-summary.o | ||
68 | OBJECTS += ui-tag.o | ||
69 | OBJECTS += ui-tree.o | ||
70 | |||
22 | ifdef NEEDS_LIBICONV | 71 | ifdef NEEDS_LIBICONV |
23 | EXTLIBS += -liconv | 72 | EXTLIBS += -liconv |
24 | endif | 73 | endif |
25 | 74 | ||
26 | 75 | ||
27 | .PHONY: all git test install clean distclean emptycache force-version get-git | 76 | .PHONY: all git test install clean distclean emptycache force-version get-git |
28 | 77 | ||
29 | all: cgit git | 78 | all: cgit git |
30 | 79 | ||
31 | VERSION: force-version | 80 | VERSION: force-version |
32 | @./gen-version.sh "$(CGIT_VERSION)" | 81 | @./gen-version.sh "$(CGIT_VERSION)" |
33 | -include VERSION | 82 | -include VERSION |
34 | 83 | ||
35 | 84 | ||
36 | CFLAGS += -g -Wall -Igit | 85 | CFLAGS += -g -Wall -Igit |
37 | CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' | 86 | CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' |
38 | CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' | 87 | CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' |
39 | CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' | 88 | CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' |
40 | CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' | 89 | CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' |
41 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' | 90 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' |
42 | 91 | ||
43 | 92 | ||
44 | cgit: cgit.c $(OBJECTS) | 93 | cgit: $(OBJECTS) |
45 | $(CC) $(CFLAGS) cgit.c -o cgit $(OBJECTS) $(EXTLIBS) | 94 | $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) |
95 | |||
96 | $(OBJECTS): git/xdiff/lib.a git/libgit.a | ||
97 | |||
98 | cgit.o: VERSION | ||
46 | 99 | ||
47 | $(OBJECTS): cgit.h git/xdiff/lib.a git/libgit.a VERSION | 100 | -include $(OBJECTS:.o=.d) |
48 | 101 | ||
49 | git/xdiff/lib.a: | git | 102 | git/xdiff/lib.a: | git |
50 | 103 | ||
51 | git/libgit.a: | git | 104 | git/libgit.a: | git |
52 | 105 | ||
53 | git: | 106 | git: |
54 | cd git && $(MAKE) xdiff/lib.a | 107 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a |
55 | cd git && $(MAKE) libgit.a | 108 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a |
56 | 109 | ||
57 | test: all | 110 | test: all |
58 | $(MAKE) -C tests | 111 | $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all |
59 | 112 | ||
60 | install: all | 113 | install: all |
61 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) | 114 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) |
62 | install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) | 115 | install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) |
63 | install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css | 116 | install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css |
64 | install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png | 117 | install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png |
65 | 118 | ||
66 | uninstall: | 119 | uninstall: |
67 | rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) | 120 | rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) |
68 | rm -f $(CGIT_SCRIPT_PATH)/cgit.css | 121 | rm -f $(CGIT_SCRIPT_PATH)/cgit.css |
69 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png | 122 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png |
70 | 123 | ||
71 | clean: | 124 | clean: |
72 | rm -f cgit VERSION *.o | 125 | rm -f cgit VERSION *.o *.d |
73 | cd git && $(MAKE) clean | 126 | cd git && $(MAKE) clean |
74 | 127 | ||
75 | distclean: clean | 128 | distclean: clean |
76 | git clean -d -x | 129 | git clean -d -x |
77 | cd git && git clean -d -x | 130 | cd git && git clean -d -x |
78 | 131 | ||
79 | emptycache: | 132 | emptycache: |
80 | rm -rf $(DESTDIR)$(CACHE_ROOT)/* | 133 | rm -rf $(DESTDIR)$(CACHE_ROOT)/* |
81 | 134 | ||
82 | get-git: | 135 | get-git: |
83 | curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git | 136 | curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git |
@@ -1,119 +1,120 @@ | |||
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 | #include "cache.h" | ||
10 | 11 | ||
11 | const int NOLOCK = -1; | 12 | const int NOLOCK = -1; |
12 | 13 | ||
13 | char *cache_safe_filename(const char *unsafe) | 14 | char *cache_safe_filename(const char *unsafe) |
14 | { | 15 | { |
15 | static char buf[4][PATH_MAX]; | 16 | static char buf[4][PATH_MAX]; |
16 | static int bufidx; | 17 | static int bufidx; |
17 | char *s; | 18 | char *s; |
18 | char c; | 19 | char c; |
19 | 20 | ||
20 | bufidx++; | 21 | bufidx++; |
21 | bufidx &= 3; | 22 | bufidx &= 3; |
22 | s = buf[bufidx]; | 23 | s = buf[bufidx]; |
23 | 24 | ||
24 | while(unsafe && (c = *unsafe++) != 0) { | 25 | while(unsafe && (c = *unsafe++) != 0) { |
25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 26 | if (c == '/' || c == ' ' || c == '&' || c == '|' || |
26 | c == '>' || c == '<' || c == '.') | 27 | c == '>' || c == '<' || c == '.') |
27 | c = '_'; | 28 | c = '_'; |
28 | *s++ = c; | 29 | *s++ = c; |
29 | } | 30 | } |
30 | *s = '\0'; | 31 | *s = '\0'; |
31 | return buf[bufidx]; | 32 | return buf[bufidx]; |
32 | } | 33 | } |
33 | 34 | ||
34 | int cache_exist(struct cacheitem *item) | 35 | int cache_exist(struct cacheitem *item) |
35 | { | 36 | { |
36 | if (stat(item->name, &item->st)) { | 37 | if (stat(item->name, &item->st)) { |
37 | item->st.st_mtime = 0; | 38 | item->st.st_mtime = 0; |
38 | return 0; | 39 | return 0; |
39 | } | 40 | } |
40 | return 1; | 41 | return 1; |
41 | } | 42 | } |
42 | 43 | ||
43 | int cache_create_dirs() | 44 | int cache_create_dirs() |
44 | { | 45 | { |
45 | char *path; | 46 | char *path; |
46 | 47 | ||
47 | path = fmt("%s", cgit_cache_root); | 48 | path = fmt("%s", ctx.cfg.cache_root); |
48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 49 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
49 | return 0; | 50 | return 0; |
50 | 51 | ||
51 | if (!cgit_repo) | 52 | if (!ctx.repo) |
52 | return 0; | 53 | return 0; |
53 | 54 | ||
54 | path = fmt("%s/%s", cgit_cache_root, | 55 | path = fmt("%s/%s", ctx.cfg.cache_root, |
55 | cache_safe_filename(cgit_repo->url)); | 56 | cache_safe_filename(ctx.repo->url)); |
56 | 57 | ||
57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 58 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
58 | return 0; | 59 | return 0; |
59 | 60 | ||
60 | if (cgit_query_page) { | 61 | if (ctx.qry.page) { |
61 | path = fmt("%s/%s/%s", cgit_cache_root, | 62 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, |
62 | cache_safe_filename(cgit_repo->url), | 63 | cache_safe_filename(ctx.repo->url), |
63 | cgit_query_page); | 64 | ctx.qry.page); |
64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 65 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
65 | return 0; | 66 | return 0; |
66 | } | 67 | } |
67 | return 1; | 68 | return 1; |
68 | } | 69 | } |
69 | 70 | ||
70 | int cache_refill_overdue(const char *lockfile) | 71 | int cache_refill_overdue(const char *lockfile) |
71 | { | 72 | { |
72 | struct stat st; | 73 | struct stat st; |
73 | 74 | ||
74 | if (stat(lockfile, &st)) | 75 | if (stat(lockfile, &st)) |
75 | return 0; | 76 | return 0; |
76 | else | 77 | else |
77 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); | 78 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); |
78 | } | 79 | } |
79 | 80 | ||
80 | int cache_lock(struct cacheitem *item) | 81 | int cache_lock(struct cacheitem *item) |
81 | { | 82 | { |
82 | int i = 0; | 83 | int i = 0; |
83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 84 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
84 | 85 | ||
85 | top: | 86 | top: |
86 | if (++i > cgit_max_lock_attempts) | 87 | if (++i > ctx.cfg.max_lock_attempts) |
87 | die("cache_lock: unable to lock %s: %s", | 88 | die("cache_lock: unable to lock %s: %s", |
88 | item->name, strerror(errno)); | 89 | item->name, strerror(errno)); |
89 | 90 | ||
90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 91 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
91 | 92 | ||
92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 93 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
93 | goto top; | 94 | goto top; |
94 | 95 | ||
95 | if (item->fd == NOLOCK && errno == EEXIST && | 96 | if (item->fd == NOLOCK && errno == EEXIST && |
96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 97 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
97 | goto top; | 98 | goto top; |
98 | 99 | ||
99 | free(lockfile); | 100 | free(lockfile); |
100 | return (item->fd > 0); | 101 | return (item->fd > 0); |
101 | } | 102 | } |
102 | 103 | ||
103 | int cache_unlock(struct cacheitem *item) | 104 | int cache_unlock(struct cacheitem *item) |
104 | { | 105 | { |
105 | close(item->fd); | 106 | close(item->fd); |
106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 107 | return (rename(fmt("%s.lock", item->name), item->name) == 0); |
107 | } | 108 | } |
108 | 109 | ||
109 | int cache_cancel_lock(struct cacheitem *item) | 110 | int cache_cancel_lock(struct cacheitem *item) |
110 | { | 111 | { |
111 | return (unlink(fmt("%s.lock", item->name)) == 0); | 112 | return (unlink(fmt("%s.lock", item->name)) == 0); |
112 | } | 113 | } |
113 | 114 | ||
114 | int cache_expired(struct cacheitem *item) | 115 | int cache_expired(struct cacheitem *item) |
115 | { | 116 | { |
116 | if (item->ttl < 0) | 117 | if (item->ttl < 0) |
117 | return 0; | 118 | return 0; |
118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 119 | return item->st.st_mtime + item->ttl * 60 < time(NULL); |
119 | } | 120 | } |
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | * Since git has it's own cache.h which we include, | ||
3 | * lets test on CGIT_CACHE_H to avoid confusion | ||
4 | */ | ||
5 | |||
6 | #ifndef CGIT_CACHE_H | ||
7 | #define CGIT_CACHE_H | ||
8 | |||
9 | struct cacheitem { | ||
10 | char *name; | ||
11 | struct stat st; | ||
12 | int ttl; | ||
13 | int fd; | ||
14 | }; | ||
15 | |||
16 | extern char *cache_safe_filename(const char *unsafe); | ||
17 | extern int cache_lock(struct cacheitem *item); | ||
18 | extern int cache_unlock(struct cacheitem *item); | ||
19 | extern int cache_cancel_lock(struct cacheitem *item); | ||
20 | extern int cache_exist(struct cacheitem *item); | ||
21 | extern int cache_expired(struct cacheitem *item); | ||
22 | |||
23 | #endif /* CGIT_CACHE_H */ | ||
@@ -1,318 +1,459 @@ | |||
1 | /* cgit.c: cgi for the git scm | 1 | /* cgit.c: cgi for the git scm |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cache.h" | ||
11 | #include "cmd.h" | ||
12 | #include "configfile.h" | ||
13 | #include "html.h" | ||
14 | #include "ui-shared.h" | ||
15 | |||
16 | const char *cgit_version = CGIT_VERSION; | ||
17 | |||
18 | void config_cb(const char *name, const char *value) | ||
19 | { | ||
20 | if (!strcmp(name, "root-title")) | ||
21 | ctx.cfg.root_title = xstrdup(value); | ||
22 | else if (!strcmp(name, "css")) | ||
23 | ctx.cfg.css = xstrdup(value); | ||
24 | else if (!strcmp(name, "logo")) | ||
25 | ctx.cfg.logo = xstrdup(value); | ||
26 | else if (!strcmp(name, "index-header")) | ||
27 | ctx.cfg.index_header = xstrdup(value); | ||
28 | else if (!strcmp(name, "index-info")) | ||
29 | ctx.cfg.index_info = xstrdup(value); | ||
30 | else if (!strcmp(name, "logo-link")) | ||
31 | ctx.cfg.logo_link = xstrdup(value); | ||
32 | else if (!strcmp(name, "module-link")) | ||
33 | ctx.cfg.module_link = xstrdup(value); | ||
34 | else if (!strcmp(name, "virtual-root")) { | ||
35 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
36 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
37 | ctx.cfg.virtual_root = ""; | ||
38 | } else if (!strcmp(name, "nocache")) | ||
39 | ctx.cfg.nocache = atoi(value); | ||
40 | else if (!strcmp(name, "snapshots")) | ||
41 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
42 | else if (!strcmp(name, "enable-index-links")) | ||
43 | ctx.cfg.enable_index_links = atoi(value); | ||
44 | else if (!strcmp(name, "enable-log-filecount")) | ||
45 | ctx.cfg.enable_log_filecount = atoi(value); | ||
46 | else if (!strcmp(name, "enable-log-linecount")) | ||
47 | ctx.cfg.enable_log_linecount = atoi(value); | ||
48 | else if (!strcmp(name, "cache-root")) | ||
49 | ctx.cfg.cache_root = xstrdup(value); | ||
50 | else if (!strcmp(name, "cache-root-ttl")) | ||
51 | ctx.cfg.cache_root_ttl = atoi(value); | ||
52 | else if (!strcmp(name, "cache-repo-ttl")) | ||
53 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
54 | else if (!strcmp(name, "cache-static-ttl")) | ||
55 | ctx.cfg.cache_static_ttl = atoi(value); | ||
56 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
57 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
58 | else if (!strcmp(name, "max-message-length")) | ||
59 | ctx.cfg.max_msg_len = atoi(value); | ||
60 | else if (!strcmp(name, "max-repodesc-length")) | ||
61 | ctx.cfg.max_repodesc_len = atoi(value); | ||
62 | else if (!strcmp(name, "max-commit-count")) | ||
63 | ctx.cfg.max_commit_count = atoi(value); | ||
64 | else if (!strcmp(name, "summary-log")) | ||
65 | ctx.cfg.summary_log = atoi(value); | ||
66 | else if (!strcmp(name, "summary-branches")) | ||
67 | ctx.cfg.summary_branches = atoi(value); | ||
68 | else if (!strcmp(name, "summary-tags")) | ||
69 | ctx.cfg.summary_tags = atoi(value); | ||
70 | else if (!strcmp(name, "agefile")) | ||
71 | ctx.cfg.agefile = xstrdup(value); | ||
72 | else if (!strcmp(name, "renamelimit")) | ||
73 | ctx.cfg.renamelimit = atoi(value); | ||
74 | else if (!strcmp(name, "robots")) | ||
75 | ctx.cfg.robots = xstrdup(value); | ||
76 | else if (!strcmp(name, "clone-prefix")) | ||
77 | ctx.cfg.clone_prefix = xstrdup(value); | ||
78 | else if (!strcmp(name, "repo.group")) | ||
79 | ctx.cfg.repo_group = xstrdup(value); | ||
80 | else if (!strcmp(name, "repo.url")) | ||
81 | ctx.repo = cgit_add_repo(value); | ||
82 | else if (!strcmp(name, "repo.name")) | ||
83 | ctx.repo->name = xstrdup(value); | ||
84 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
85 | ctx.repo->path = trim_end(value, '/'); | ||
86 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
87 | ctx.repo->clone_url = xstrdup(value); | ||
88 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
89 | ctx.repo->desc = xstrdup(value); | ||
90 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
91 | ctx.repo->owner = xstrdup(value); | ||
92 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
93 | ctx.repo->defbranch = xstrdup(value); | ||
94 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
95 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
96 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
97 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
98 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
99 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
100 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
101 | ctx.repo->module_link= xstrdup(value); | ||
102 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
103 | if (*value == '/') | ||
104 | ctx.repo->readme = xstrdup(value); | ||
105 | else | ||
106 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
107 | } else if (!strcmp(name, "include")) | ||
108 | parse_configfile(value, config_cb); | ||
109 | } | ||
110 | |||
111 | static void querystring_cb(const char *name, const char *value) | ||
112 | { | ||
113 | if (!strcmp(name,"r")) { | ||
114 | ctx.qry.repo = xstrdup(value); | ||
115 | ctx.repo = cgit_get_repoinfo(value); | ||
116 | } else if (!strcmp(name, "p")) { | ||
117 | ctx.qry.page = xstrdup(value); | ||
118 | } else if (!strcmp(name, "url")) { | ||
119 | cgit_parse_url(value); | ||
120 | } else if (!strcmp(name, "qt")) { | ||
121 | ctx.qry.grep = xstrdup(value); | ||
122 | } else if (!strcmp(name, "q")) { | ||
123 | ctx.qry.search = xstrdup(value); | ||
124 | } else if (!strcmp(name, "h")) { | ||
125 | ctx.qry.head = xstrdup(value); | ||
126 | ctx.qry.has_symref = 1; | ||
127 | } else if (!strcmp(name, "id")) { | ||
128 | ctx.qry.sha1 = xstrdup(value); | ||
129 | ctx.qry.has_sha1 = 1; | ||
130 | } else if (!strcmp(name, "id2")) { | ||
131 | ctx.qry.sha2 = xstrdup(value); | ||
132 | ctx.qry.has_sha1 = 1; | ||
133 | } else if (!strcmp(name, "ofs")) { | ||
134 | ctx.qry.ofs = atoi(value); | ||
135 | } else if (!strcmp(name, "path")) { | ||
136 | ctx.qry.path = trim_end(value, '/'); | ||
137 | } else if (!strcmp(name, "name")) { | ||
138 | ctx.qry.name = xstrdup(value); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | static void prepare_context(struct cgit_context *ctx) | ||
143 | { | ||
144 | memset(ctx, 0, sizeof(ctx)); | ||
145 | ctx->cfg.agefile = "info/web/last-modified"; | ||
146 | ctx->cfg.cache_dynamic_ttl = 5; | ||
147 | ctx->cfg.cache_max_create_time = 5; | ||
148 | ctx->cfg.cache_repo_ttl = 5; | ||
149 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
150 | ctx->cfg.cache_root_ttl = 5; | ||
151 | ctx->cfg.cache_static_ttl = -1; | ||
152 | ctx->cfg.css = "/cgit.css"; | ||
153 | ctx->cfg.logo = "/git-logo.png"; | ||
154 | ctx->cfg.max_commit_count = 50; | ||
155 | ctx->cfg.max_lock_attempts = 5; | ||
156 | ctx->cfg.max_msg_len = 60; | ||
157 | ctx->cfg.max_repodesc_len = 60; | ||
158 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
159 | ctx->cfg.renamelimit = -1; | ||
160 | ctx->cfg.robots = "index, nofollow"; | ||
161 | ctx->cfg.root_title = "Git repository browser"; | ||
162 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
163 | ctx->page.mimetype = "text/html"; | ||
164 | ctx->page.charset = PAGE_ENCODING; | ||
165 | ctx->page.filename = NULL; | ||
166 | } | ||
10 | 167 | ||
11 | static int cgit_prepare_cache(struct cacheitem *item) | 168 | static int cgit_prepare_cache(struct cacheitem *item) |
12 | { | 169 | { |
13 | if (!cgit_repo && cgit_query_repo) { | 170 | if (!ctx.repo && ctx.qry.repo) { |
14 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | 171 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
15 | cgit_print_docstart(title, item); | 172 | "Bad request"); |
16 | cgit_print_pageheader(title, 0); | 173 | cgit_print_http_headers(&ctx); |
17 | cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); | 174 | cgit_print_docstart(&ctx); |
175 | cgit_print_pageheader(&ctx); | ||
176 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | ||
18 | cgit_print_docend(); | 177 | cgit_print_docend(); |
19 | return 0; | 178 | return 0; |
20 | } | 179 | } |
21 | 180 | ||
22 | if (!cgit_repo) { | 181 | if (!ctx.repo) { |
23 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); | 182 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
24 | item->ttl = cgit_cache_root_ttl; | 183 | item->ttl = ctx.cfg.cache_root_ttl; |
25 | return 1; | 184 | return 1; |
26 | } | 185 | } |
27 | 186 | ||
28 | if (!cgit_cmd) { | 187 | if (!ctx.qry.page) { |
29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, | 188 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
30 | cache_safe_filename(cgit_repo->url), | 189 | cache_safe_filename(ctx.repo->url), |
31 | cache_safe_filename(cgit_querystring))); | 190 | cache_safe_filename(ctx.qry.raw))); |
32 | item->ttl = cgit_cache_repo_ttl; | 191 | item->ttl = ctx.cfg.cache_repo_ttl; |
33 | } else { | 192 | } else { |
34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, | 193 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
35 | cache_safe_filename(cgit_repo->url), | 194 | cache_safe_filename(ctx.repo->url), |
36 | cgit_query_page, | 195 | ctx.qry.page, |
37 | cache_safe_filename(cgit_querystring))); | 196 | cache_safe_filename(ctx.qry.raw))); |
38 | if (cgit_query_has_symref) | 197 | if (ctx.qry.has_symref) |
39 | item->ttl = cgit_cache_dynamic_ttl; | 198 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
40 | else if (cgit_query_has_sha1) | 199 | else if (ctx.qry.has_sha1) |
41 | item->ttl = cgit_cache_static_ttl; | 200 | item->ttl = ctx.cfg.cache_static_ttl; |
42 | else | 201 | else |
43 | item->ttl = cgit_cache_repo_ttl; | 202 | item->ttl = ctx.cfg.cache_repo_ttl; |
44 | } | 203 | } |
45 | return 1; | 204 | return 1; |
46 | } | 205 | } |
47 | 206 | ||
48 | struct refmatch { | 207 | struct refmatch { |
49 | char *req_ref; | 208 | char *req_ref; |
50 | char *first_ref; | 209 | char *first_ref; |
51 | int match; | 210 | int match; |
52 | }; | 211 | }; |
53 | 212 | ||
54 | int find_current_ref(const char *refname, const unsigned char *sha1, | 213 | int find_current_ref(const char *refname, const unsigned char *sha1, |
55 | int flags, void *cb_data) | 214 | int flags, void *cb_data) |
56 | { | 215 | { |
57 | struct refmatch *info; | 216 | struct refmatch *info; |
58 | 217 | ||
59 | info = (struct refmatch *)cb_data; | 218 | info = (struct refmatch *)cb_data; |
60 | if (!strcmp(refname, info->req_ref)) | 219 | if (!strcmp(refname, info->req_ref)) |
61 | info->match = 1; | 220 | info->match = 1; |
62 | if (!info->first_ref) | 221 | if (!info->first_ref) |
63 | info->first_ref = xstrdup(refname); | 222 | info->first_ref = xstrdup(refname); |
64 | return info->match; | 223 | return info->match; |
65 | } | 224 | } |
66 | 225 | ||
67 | char *find_default_branch(struct repoinfo *repo) | 226 | char *find_default_branch(struct cgit_repo *repo) |
68 | { | 227 | { |
69 | struct refmatch info; | 228 | struct refmatch info; |
70 | 229 | ||
71 | info.req_ref = repo->defbranch; | 230 | info.req_ref = repo->defbranch; |
72 | info.first_ref = NULL; | 231 | info.first_ref = NULL; |
73 | info.match = 0; | 232 | info.match = 0; |
74 | for_each_branch_ref(find_current_ref, &info); | 233 | for_each_branch_ref(find_current_ref, &info); |
75 | if (info.match) | 234 | if (info.match) |
76 | return info.req_ref; | 235 | return info.req_ref; |
77 | else | 236 | else |
78 | return info.first_ref; | 237 | return info.first_ref; |
79 | } | 238 | } |
80 | 239 | ||
81 | static void cgit_print_repo_page(struct cacheitem *item) | 240 | static int prepare_repo_cmd(struct cgit_context *ctx) |
82 | { | 241 | { |
83 | char *title, *tmp; | 242 | char *tmp; |
84 | int show_search; | ||
85 | unsigned char sha1[20]; | 243 | unsigned char sha1[20]; |
86 | 244 | int nongit = 0; | |
87 | if (chdir(cgit_repo->path)) { | 245 | |
88 | title = fmt("%s - %s", cgit_root_title, "Bad request"); | 246 | setenv("GIT_DIR", ctx->repo->path, 1); |
89 | cgit_print_docstart(title, item); | 247 | setup_git_directory_gently(&nongit); |
90 | cgit_print_pageheader(title, 0); | 248 | if (nongit) { |
91 | cgit_print_error(fmt("Unable to scan repository: %s", | 249 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
92 | strerror(errno))); | 250 | "config error"); |
251 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | ||
252 | ctx->repo = NULL; | ||
253 | cgit_print_http_headers(ctx); | ||
254 | cgit_print_docstart(ctx); | ||
255 | cgit_print_pageheader(ctx); | ||
256 | cgit_print_error(tmp); | ||
93 | cgit_print_docend(); | 257 | cgit_print_docend(); |
94 | return; | 258 | return 1; |
95 | } | 259 | } |
260 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | ||
96 | 261 | ||
97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); | 262 | if (!ctx->qry.head) { |
98 | show_search = 0; | 263 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
99 | setenv("GIT_DIR", cgit_repo->path, 1); | 264 | ctx->repo->defbranch = ctx->qry.head; |
100 | |||
101 | if (!cgit_query_head) { | ||
102 | cgit_query_head = xstrdup(find_default_branch(cgit_repo)); | ||
103 | cgit_repo->defbranch = cgit_query_head; | ||
104 | } | 265 | } |
105 | 266 | ||
106 | if (!cgit_query_head) { | 267 | if (!ctx->qry.head) { |
107 | cgit_print_docstart(title, item); | 268 | cgit_print_http_headers(ctx); |
108 | cgit_print_pageheader(title, 0); | 269 | cgit_print_docstart(ctx); |
270 | cgit_print_pageheader(ctx); | ||
109 | cgit_print_error("Repository seems to be empty"); | 271 | cgit_print_error("Repository seems to be empty"); |
110 | cgit_print_docend(); | 272 | cgit_print_docend(); |
111 | return; | 273 | return 1; |
112 | } | 274 | } |
113 | 275 | ||
114 | if (get_sha1(cgit_query_head, sha1)) { | 276 | if (get_sha1(ctx->qry.head, sha1)) { |
115 | tmp = xstrdup(cgit_query_head); | 277 | tmp = xstrdup(ctx->qry.head); |
116 | cgit_query_head = cgit_repo->defbranch; | 278 | ctx->qry.head = ctx->repo->defbranch; |
117 | cgit_print_docstart(title, item); | 279 | cgit_print_http_headers(ctx); |
118 | cgit_print_pageheader(title, 0); | 280 | cgit_print_docstart(ctx); |
281 | cgit_print_pageheader(ctx); | ||
119 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 282 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
120 | cgit_print_docend(); | 283 | cgit_print_docend(); |
121 | return; | 284 | return 1; |
122 | } | 285 | } |
286 | return 0; | ||
287 | } | ||
123 | 288 | ||
124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { | 289 | static void process_request(struct cgit_context *ctx) |
125 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, | 290 | { |
126 | cgit_repobasename(cgit_repo->url), | 291 | struct cgit_cmd *cmd; |
127 | cgit_query_path, | 292 | |
128 | cgit_repo->snapshots ); | 293 | cmd = cgit_get_cmd(ctx); |
294 | if (!cmd) { | ||
295 | ctx->page.title = "cgit error"; | ||
296 | ctx->repo = NULL; | ||
297 | cgit_print_http_headers(ctx); | ||
298 | cgit_print_docstart(ctx); | ||
299 | cgit_print_pageheader(ctx); | ||
300 | cgit_print_error("Invalid request"); | ||
301 | cgit_print_docend(); | ||
129 | return; | 302 | return; |
130 | } | 303 | } |
131 | 304 | ||
132 | if (cgit_cmd == CMD_PATCH) { | 305 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
133 | cgit_print_patch(cgit_query_sha1, item); | ||
134 | return; | 306 | return; |
135 | } | ||
136 | 307 | ||
137 | if (cgit_cmd == CMD_BLOB) { | 308 | if (cmd->want_layout) { |
138 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); | 309 | cgit_print_http_headers(ctx); |
139 | return; | 310 | cgit_print_docstart(ctx); |
311 | cgit_print_pageheader(ctx); | ||
140 | } | 312 | } |
141 | 313 | ||
142 | show_search = (cgit_cmd == CMD_LOG); | 314 | cmd->fn(ctx); |
143 | cgit_print_docstart(title, item); | 315 | |
144 | if (!cgit_cmd) { | 316 | if (cmd->want_layout) |
145 | cgit_print_pageheader("summary", show_search); | ||
146 | cgit_print_summary(); | ||
147 | cgit_print_docend(); | 317 | cgit_print_docend(); |
148 | return; | 318 | } |
149 | } | ||
150 | 319 | ||
151 | cgit_print_pageheader(cgit_query_page, show_search); | 320 | static long ttl_seconds(long ttl) |
152 | 321 | { | |
153 | switch(cgit_cmd) { | 322 | if (ttl<0) |
154 | case CMD_LOG: | 323 | return 60 * 60 * 24 * 365; |
155 | cgit_print_log(cgit_query_sha1, cgit_query_ofs, | 324 | else |
156 | cgit_max_commit_count, cgit_query_grep, cgit_query_search, | 325 | return ttl * 60; |
157 | cgit_query_path, 1); | ||
158 | break; | ||
159 | case CMD_TREE: | ||
160 | cgit_print_tree(cgit_query_sha1, cgit_query_path); | ||
161 | break; | ||
162 | case CMD_COMMIT: | ||
163 | cgit_print_commit(cgit_query_sha1); | ||
164 | break; | ||
165 | case CMD_REFS: | ||
166 | cgit_print_refs(); | ||
167 | break; | ||
168 | case CMD_TAG: | ||
169 | cgit_print_tag(cgit_query_sha1); | ||
170 | break; | ||
171 | case CMD_DIFF: | ||
172 | cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path); | ||
173 | break; | ||
174 | default: | ||
175 | cgit_print_error("Invalid request"); | ||
176 | } | ||
177 | cgit_print_docend(); | ||
178 | } | 326 | } |
179 | 327 | ||
180 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 328 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
181 | { | 329 | { |
182 | static char buf[PATH_MAX]; | ||
183 | int stdout2; | 330 | int stdout2; |
184 | 331 | ||
185 | getcwd(buf, sizeof(buf)); | ||
186 | item->st.st_mtime = time(NULL); | ||
187 | |||
188 | if (use_cache) { | 332 | if (use_cache) { |
189 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 333 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
190 | "Preserving STDOUT"); | 334 | "Preserving STDOUT"); |
191 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 335 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
192 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 336 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
193 | } | 337 | } |
194 | 338 | ||
195 | if (cgit_repo) | 339 | ctx.page.modified = time(NULL); |
196 | cgit_print_repo_page(item); | 340 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
197 | else | 341 | process_request(&ctx); |
198 | cgit_print_repolist(item); | ||
199 | 342 | ||
200 | if (use_cache) { | 343 | if (use_cache) { |
201 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 344 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
202 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 345 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
203 | "Restoring original STDOUT"); | 346 | "Restoring original STDOUT"); |
204 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 347 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
205 | } | 348 | } |
206 | |||
207 | chdir(buf); | ||
208 | } | 349 | } |
209 | 350 | ||
210 | static void cgit_check_cache(struct cacheitem *item) | 351 | static void cgit_check_cache(struct cacheitem *item) |
211 | { | 352 | { |
212 | int i = 0; | 353 | int i = 0; |
213 | 354 | ||
214 | top: | 355 | top: |
215 | if (++i > cgit_max_lock_attempts) { | 356 | if (++i > ctx.cfg.max_lock_attempts) { |
216 | die("cgit_refresh_cache: unable to lock %s: %s", | 357 | die("cgit_refresh_cache: unable to lock %s: %s", |
217 | item->name, strerror(errno)); | 358 | item->name, strerror(errno)); |
218 | } | 359 | } |
219 | if (!cache_exist(item)) { | 360 | if (!cache_exist(item)) { |
220 | if (!cache_lock(item)) { | 361 | if (!cache_lock(item)) { |
221 | sleep(1); | 362 | sleep(1); |
222 | goto top; | 363 | goto top; |
223 | } | 364 | } |
224 | if (!cache_exist(item)) { | 365 | if (!cache_exist(item)) { |
225 | cgit_fill_cache(item, 1); | 366 | cgit_fill_cache(item, 1); |
226 | cache_unlock(item); | 367 | cache_unlock(item); |
227 | } else { | 368 | } else { |
228 | cache_cancel_lock(item); | 369 | cache_cancel_lock(item); |
229 | } | 370 | } |
230 | } else if (cache_expired(item) && cache_lock(item)) { | 371 | } else if (cache_expired(item) && cache_lock(item)) { |
231 | if (cache_expired(item)) { | 372 | if (cache_expired(item)) { |
232 | cgit_fill_cache(item, 1); | 373 | cgit_fill_cache(item, 1); |
233 | cache_unlock(item); | 374 | cache_unlock(item); |
234 | } else { | 375 | } else { |
235 | cache_cancel_lock(item); | 376 | cache_cancel_lock(item); |
236 | } | 377 | } |
237 | } | 378 | } |
238 | } | 379 | } |
239 | 380 | ||
240 | static void cgit_print_cache(struct cacheitem *item) | 381 | static void cgit_print_cache(struct cacheitem *item) |
241 | { | 382 | { |
242 | static char buf[4096]; | 383 | static char buf[4096]; |
243 | ssize_t i; | 384 | ssize_t i; |
244 | 385 | ||
245 | int fd = open(item->name, O_RDONLY); | 386 | int fd = open(item->name, O_RDONLY); |
246 | if (fd<0) | 387 | if (fd<0) |
247 | die("Unable to open cached file %s", item->name); | 388 | die("Unable to open cached file %s", item->name); |
248 | 389 | ||
249 | while((i=read(fd, buf, sizeof(buf))) > 0) | 390 | while((i=read(fd, buf, sizeof(buf))) > 0) |
250 | write(STDOUT_FILENO, buf, i); | 391 | write(STDOUT_FILENO, buf, i); |
251 | 392 | ||
252 | close(fd); | 393 | close(fd); |
253 | } | 394 | } |
254 | 395 | ||
255 | static void cgit_parse_args(int argc, const char **argv) | 396 | static void cgit_parse_args(int argc, const char **argv) |
256 | { | 397 | { |
257 | int i; | 398 | int i; |
258 | 399 | ||
259 | for (i = 1; i < argc; i++) { | 400 | for (i = 1; i < argc; i++) { |
260 | if (!strncmp(argv[i], "--cache=", 8)) { | 401 | if (!strncmp(argv[i], "--cache=", 8)) { |
261 | cgit_cache_root = xstrdup(argv[i]+8); | 402 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
262 | } | 403 | } |
263 | if (!strcmp(argv[i], "--nocache")) { | 404 | if (!strcmp(argv[i], "--nocache")) { |
264 | cgit_nocache = 1; | 405 | ctx.cfg.nocache = 1; |
265 | } | 406 | } |
266 | if (!strncmp(argv[i], "--query=", 8)) { | 407 | if (!strncmp(argv[i], "--query=", 8)) { |
267 | cgit_querystring = xstrdup(argv[i]+8); | 408 | ctx.qry.raw = xstrdup(argv[i]+8); |
268 | } | 409 | } |
269 | if (!strncmp(argv[i], "--repo=", 7)) { | 410 | if (!strncmp(argv[i], "--repo=", 7)) { |
270 | cgit_query_repo = xstrdup(argv[i]+7); | 411 | ctx.qry.repo = xstrdup(argv[i]+7); |
271 | } | 412 | } |
272 | if (!strncmp(argv[i], "--page=", 7)) { | 413 | if (!strncmp(argv[i], "--page=", 7)) { |
273 | cgit_query_page = xstrdup(argv[i]+7); | 414 | ctx.qry.page = xstrdup(argv[i]+7); |
274 | } | 415 | } |
275 | if (!strncmp(argv[i], "--head=", 7)) { | 416 | if (!strncmp(argv[i], "--head=", 7)) { |
276 | cgit_query_head = xstrdup(argv[i]+7); | 417 | ctx.qry.head = xstrdup(argv[i]+7); |
277 | cgit_query_has_symref = 1; | 418 | ctx.qry.has_symref = 1; |
278 | } | 419 | } |
279 | if (!strncmp(argv[i], "--sha1=", 7)) { | 420 | if (!strncmp(argv[i], "--sha1=", 7)) { |
280 | cgit_query_sha1 = xstrdup(argv[i]+7); | 421 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
281 | cgit_query_has_sha1 = 1; | 422 | ctx.qry.has_sha1 = 1; |
282 | } | 423 | } |
283 | if (!strncmp(argv[i], "--ofs=", 6)) { | 424 | if (!strncmp(argv[i], "--ofs=", 6)) { |
284 | cgit_query_ofs = atoi(argv[i]+6); | 425 | ctx.qry.ofs = atoi(argv[i]+6); |
285 | } | 426 | } |
286 | } | 427 | } |
287 | } | 428 | } |
288 | 429 | ||
289 | int main(int argc, const char **argv) | 430 | int main(int argc, const char **argv) |
290 | { | 431 | { |
291 | struct cacheitem item; | 432 | struct cacheitem item; |
292 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 433 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
293 | 434 | ||
294 | htmlfd = STDOUT_FILENO; | 435 | prepare_context(&ctx); |
295 | item.st.st_mtime = time(NULL); | 436 | item.st.st_mtime = time(NULL); |
296 | cgit_repolist.length = 0; | 437 | cgit_repolist.length = 0; |
297 | cgit_repolist.count = 0; | 438 | cgit_repolist.count = 0; |
298 | cgit_repolist.repos = NULL; | 439 | cgit_repolist.repos = NULL; |
299 | 440 | ||
300 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 441 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
301 | cgit_global_config_cb); | 442 | config_cb); |
302 | cgit_repo = NULL; | 443 | ctx.repo = NULL; |
303 | if (getenv("SCRIPT_NAME")) | 444 | if (getenv("SCRIPT_NAME")) |
304 | cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); | 445 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
305 | if (getenv("QUERY_STRING")) | 446 | if (getenv("QUERY_STRING")) |
306 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); | 447 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
307 | cgit_parse_args(argc, argv); | 448 | cgit_parse_args(argc, argv); |
308 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); | 449 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
309 | if (!cgit_prepare_cache(&item)) | 450 | if (!cgit_prepare_cache(&item)) |
310 | return 0; | 451 | return 0; |
311 | if (cgit_nocache) { | 452 | if (ctx.cfg.nocache) { |
312 | cgit_fill_cache(&item, 0); | 453 | cgit_fill_cache(&item, 0); |
313 | } else { | 454 | } else { |
314 | cgit_check_cache(&item); | 455 | cgit_check_cache(&item); |
315 | cgit_print_cache(&item); | 456 | cgit_print_cache(&item); |
316 | } | 457 | } |
317 | return 0; | 458 | return 0; |
318 | } | 459 | } |
@@ -1,295 +1,224 @@ | |||
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 | ||
24 | */ | ||
25 | #define CMD_LOG 1 | ||
26 | #define CMD_COMMIT 2 | ||
27 | #define CMD_DIFF 3 | ||
28 | #define CMD_TREE 4 | ||
29 | #define CMD_BLOB 5 | ||
30 | #define CMD_SNAPSHOT 6 | ||
31 | #define CMD_TAG 7 | ||
32 | #define CMD_REFS 8 | ||
33 | #define CMD_PATCH 9 | ||
34 | |||
35 | /* | ||
36 | * Dateformats used on misc. pages | 23 | * Dateformats used on misc. pages |
37 | */ | 24 | */ |
38 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" | 25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" |
39 | #define FMT_SHORTDATE "%Y-%m-%d" | 26 | #define FMT_SHORTDATE "%Y-%m-%d" |
40 | 27 | ||
41 | 28 | ||
42 | /* | 29 | /* |
43 | * Limits used for relative dates | 30 | * Limits used for relative dates |
44 | */ | 31 | */ |
45 | #define TM_MIN 60 | 32 | #define TM_MIN 60 |
46 | #define TM_HOUR (TM_MIN * 60) | 33 | #define TM_HOUR (TM_MIN * 60) |
47 | #define TM_DAY (TM_HOUR * 24) | 34 | #define TM_DAY (TM_HOUR * 24) |
48 | #define TM_WEEK (TM_DAY * 7) | 35 | #define TM_WEEK (TM_DAY * 7) |
49 | #define TM_YEAR (TM_DAY * 365) | 36 | #define TM_YEAR (TM_DAY * 365) |
50 | #define TM_MONTH (TM_YEAR / 12.0) | 37 | #define TM_MONTH (TM_YEAR / 12.0) |
51 | 38 | ||
52 | 39 | ||
53 | /* | 40 | /* |
54 | * Default encoding | 41 | * Default encoding |
55 | */ | 42 | */ |
56 | #define PAGE_ENCODING "UTF-8" | 43 | #define PAGE_ENCODING "UTF-8" |
57 | 44 | ||
58 | typedef void (*configfn)(const char *name, const char *value); | 45 | typedef void (*configfn)(const char *name, const char *value); |
59 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 46 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
60 | typedef void (*linediff_fn)(char *line, int len); | 47 | typedef void (*linediff_fn)(char *line, int len); |
61 | 48 | ||
62 | struct cacheitem { | 49 | struct cgit_repo { |
63 | char *name; | ||
64 | struct stat st; | ||
65 | int ttl; | ||
66 | int fd; | ||
67 | }; | ||
68 | |||
69 | struct repoinfo { | ||
70 | char *url; | 50 | char *url; |
71 | char *name; | 51 | char *name; |
72 | char *path; | 52 | char *path; |
73 | char *desc; | 53 | char *desc; |
74 | char *owner; | 54 | char *owner; |
75 | char *defbranch; | 55 | char *defbranch; |
76 | char *group; | 56 | char *group; |
77 | char *module_link; | 57 | char *module_link; |
78 | char *readme; | 58 | char *readme; |
79 | char *clone_url; | 59 | char *clone_url; |
80 | int snapshots; | 60 | int snapshots; |
81 | int enable_log_filecount; | 61 | int enable_log_filecount; |
82 | int enable_log_linecount; | 62 | int enable_log_linecount; |
83 | }; | 63 | }; |
84 | 64 | ||
85 | struct repolist { | 65 | struct cgit_repolist { |
86 | int length; | 66 | int length; |
87 | int count; | 67 | int count; |
88 | struct repoinfo *repos; | 68 | struct cgit_repo *repos; |
89 | }; | 69 | }; |
90 | 70 | ||
91 | struct commitinfo { | 71 | struct commitinfo { |
92 | struct commit *commit; | 72 | struct commit *commit; |
93 | char *author; | 73 | char *author; |
94 | char *author_email; | 74 | char *author_email; |
95 | unsigned long author_date; | 75 | unsigned long author_date; |
96 | char *committer; | 76 | char *committer; |
97 | char *committer_email; | 77 | char *committer_email; |
98 | unsigned long committer_date; | 78 | unsigned long committer_date; |
99 | char *subject; | 79 | char *subject; |
100 | char *msg; | 80 | char *msg; |
101 | char *msg_encoding; | 81 | char *msg_encoding; |
102 | }; | 82 | }; |
103 | 83 | ||
104 | struct taginfo { | 84 | struct taginfo { |
105 | char *tagger; | 85 | char *tagger; |
106 | char *tagger_email; | 86 | char *tagger_email; |
107 | int tagger_date; | 87 | int tagger_date; |
108 | char *msg; | 88 | char *msg; |
109 | }; | 89 | }; |
110 | 90 | ||
111 | struct refinfo { | 91 | struct refinfo { |
112 | const char *refname; | 92 | const char *refname; |
113 | struct object *object; | 93 | struct object *object; |
114 | union { | 94 | union { |
115 | struct taginfo *tag; | 95 | struct taginfo *tag; |
116 | struct commitinfo *commit; | 96 | struct commitinfo *commit; |
117 | }; | 97 | }; |
118 | }; | 98 | }; |
119 | 99 | ||
120 | struct reflist { | 100 | struct reflist { |
121 | struct refinfo **refs; | 101 | struct refinfo **refs; |
122 | int alloc; | 102 | int alloc; |
123 | int count; | 103 | int count; |
124 | }; | 104 | }; |
125 | 105 | ||
106 | struct cgit_query { | ||
107 | int has_symref; | ||
108 | int has_sha1; | ||
109 | char *raw; | ||
110 | char *repo; | ||
111 | char *page; | ||
112 | char *search; | ||
113 | char *grep; | ||
114 | char *head; | ||
115 | char *sha1; | ||
116 | char *sha2; | ||
117 | char *path; | ||
118 | char *name; | ||
119 | int ofs; | ||
120 | }; | ||
121 | |||
122 | struct cgit_config { | ||
123 | char *agefile; | ||
124 | char *cache_root; | ||
125 | char *clone_prefix; | ||
126 | char *css; | ||
127 | char *index_header; | ||
128 | char *index_info; | ||
129 | char *logo; | ||
130 | char *logo_link; | ||
131 | char *module_link; | ||
132 | char *repo_group; | ||
133 | char *robots; | ||
134 | char *root_title; | ||
135 | char *script_name; | ||
136 | char *virtual_root; | ||
137 | int cache_dynamic_ttl; | ||
138 | int cache_max_create_time; | ||
139 | int cache_repo_ttl; | ||
140 | int cache_root_ttl; | ||
141 | int cache_static_ttl; | ||
142 | int enable_index_links; | ||
143 | int enable_log_filecount; | ||
144 | int enable_log_linecount; | ||
145 | int max_commit_count; | ||
146 | int max_lock_attempts; | ||
147 | int max_msg_len; | ||
148 | int max_repodesc_len; | ||
149 | int nocache; | ||
150 | int renamelimit; | ||
151 | int snapshots; | ||
152 | int summary_branches; | ||
153 | int summary_log; | ||
154 | int summary_tags; | ||
155 | }; | ||
156 | |||
157 | struct cgit_page { | ||
158 | time_t modified; | ||
159 | time_t expires; | ||
160 | char *mimetype; | ||
161 | char *charset; | ||
162 | char *filename; | ||
163 | char *title; | ||
164 | }; | ||
165 | |||
166 | struct cgit_context { | ||
167 | struct cgit_query qry; | ||
168 | struct cgit_config cfg; | ||
169 | struct cgit_repo *repo; | ||
170 | struct cgit_page page; | ||
171 | }; | ||
172 | |||
173 | struct cgit_snapshot_format { | ||
174 | const char *suffix; | ||
175 | const char *mimetype; | ||
176 | write_archive_fn_t write_func; | ||
177 | int bit; | ||
178 | }; | ||
179 | |||
126 | extern const char *cgit_version; | 180 | extern const char *cgit_version; |
127 | 181 | ||
128 | extern struct repolist cgit_repolist; | 182 | extern struct cgit_repolist cgit_repolist; |
129 | extern struct repoinfo *cgit_repo; | 183 | extern struct cgit_context ctx; |
130 | extern int cgit_cmd; | 184 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; |
131 | 185 | ||
132 | extern char *cgit_root_title; | 186 | extern struct cgit_repo *cgit_add_repo(const char *url); |
133 | extern char *cgit_css; | 187 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); |
134 | extern char *cgit_logo; | ||
135 | extern char *cgit_index_header; | ||
136 | extern char *cgit_index_info; | ||
137 | extern char *cgit_logo_link; | ||
138 | extern char *cgit_module_link; | ||
139 | extern char *cgit_agefile; | ||
140 | extern char *cgit_virtual_root; | ||
141 | extern char *cgit_script_name; | ||
142 | extern char *cgit_cache_root; | ||
143 | extern char *cgit_repo_group; | ||
144 | extern char *cgit_robots; | ||
145 | extern char *cgit_clone_prefix; | ||
146 | |||
147 | extern int cgit_nocache; | ||
148 | extern int cgit_snapshots; | ||
149 | extern int cgit_enable_index_links; | ||
150 | extern int cgit_enable_log_filecount; | ||
151 | extern int cgit_enable_log_linecount; | ||
152 | extern int cgit_max_lock_attempts; | ||
153 | extern int cgit_cache_root_ttl; | ||
154 | extern int cgit_cache_repo_ttl; | ||
155 | extern int cgit_cache_dynamic_ttl; | ||
156 | extern int cgit_cache_static_ttl; | ||
157 | extern int cgit_cache_max_create_time; | ||
158 | extern int cgit_summary_log; | ||
159 | extern int cgit_summary_tags; | ||
160 | extern int cgit_summary_branches; | ||
161 | |||
162 | extern int cgit_max_msg_len; | ||
163 | extern int cgit_max_repodesc_len; | ||
164 | extern int cgit_max_commit_count; | ||
165 | |||
166 | extern int cgit_query_has_symref; | ||
167 | extern int cgit_query_has_sha1; | ||
168 | |||
169 | extern char *cgit_querystring; | ||
170 | extern char *cgit_query_repo; | ||
171 | extern char *cgit_query_page; | ||
172 | extern char *cgit_query_search; | ||
173 | extern char *cgit_query_grep; | ||
174 | extern char *cgit_query_head; | ||
175 | extern char *cgit_query_sha1; | ||
176 | extern char *cgit_query_sha2; | ||
177 | extern char *cgit_query_path; | ||
178 | extern char *cgit_query_name; | ||
179 | extern int cgit_query_ofs; | ||
180 | |||
181 | extern int htmlfd; | ||
182 | |||
183 | extern int cgit_get_cmd_index(const char *cmd); | ||
184 | extern struct repoinfo *cgit_get_repoinfo(const char *url); | ||
185 | extern void cgit_global_config_cb(const char *name, const char *value); | ||
186 | extern void cgit_repo_config_cb(const char *name, const char *value); | 188 | extern void cgit_repo_config_cb(const char *name, const char *value); |
187 | extern void cgit_querystring_cb(const char *name, const char *value); | ||
188 | 189 | ||
189 | extern int chk_zero(int result, char *msg); | 190 | extern int chk_zero(int result, char *msg); |
190 | extern int chk_positive(int result, char *msg); | 191 | extern int chk_positive(int result, char *msg); |
191 | extern int chk_non_negative(int result, char *msg); | 192 | extern int chk_non_negative(int result, char *msg); |
192 | 193 | ||
193 | extern int hextoint(char c); | ||
194 | extern char *trim_end(const char *str, char c); | 194 | extern char *trim_end(const char *str, char c); |
195 | extern char *strlpart(char *txt, int maxlen); | 195 | extern char *strlpart(char *txt, int maxlen); |
196 | extern char *strrpart(char *txt, int maxlen); | 196 | extern char *strrpart(char *txt, int maxlen); |
197 | 197 | ||
198 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 198 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
199 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 199 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
200 | int flags, void *cb_data); | 200 | int flags, void *cb_data); |
201 | 201 | ||
202 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 202 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
203 | 203 | ||
204 | extern int cgit_diff_files(const unsigned char *old_sha1, | 204 | extern int cgit_diff_files(const unsigned char *old_sha1, |
205 | const unsigned char *new_sha1, | 205 | const unsigned char *new_sha1, |
206 | linediff_fn fn); | 206 | linediff_fn fn); |
207 | 207 | ||
208 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 208 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
209 | const unsigned char *new_sha1, | 209 | const unsigned char *new_sha1, |
210 | filepair_fn fn, const char *prefix); | 210 | filepair_fn fn, const char *prefix); |
211 | 211 | ||
212 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 212 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
213 | 213 | ||
214 | extern char *fmt(const char *format,...); | 214 | extern char *fmt(const char *format,...); |
215 | 215 | ||
216 | extern void html(const char *txt); | ||
217 | extern void htmlf(const char *format,...); | ||
218 | extern void html_txt(char *txt); | ||
219 | extern void html_ntxt(int len, char *txt); | ||
220 | extern void html_attr(char *txt); | ||
221 | extern void html_hidden(char *name, char *value); | ||
222 | extern void html_option(char *value, char *text, char *selected_value); | ||
223 | extern void html_link_open(char *url, char *title, char *class); | ||
224 | extern void html_link_close(void); | ||
225 | extern void html_filemode(unsigned short mode); | ||
226 | extern int html_include(const char *filename); | ||
227 | |||
228 | extern int cgit_read_config(const char *filename, configfn fn); | ||
229 | extern int cgit_parse_query(char *txt, configfn fn); | ||
230 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 216 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
231 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 217 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
232 | extern void cgit_parse_url(const char *url); | 218 | extern void cgit_parse_url(const char *url); |
233 | 219 | ||
234 | extern char *cache_safe_filename(const char *unsafe); | ||
235 | extern int cache_lock(struct cacheitem *item); | ||
236 | extern int cache_unlock(struct cacheitem *item); | ||
237 | extern int cache_cancel_lock(struct cacheitem *item); | ||
238 | extern int cache_exist(struct cacheitem *item); | ||
239 | extern int cache_expired(struct cacheitem *item); | ||
240 | |||
241 | extern char *cgit_repourl(const char *reponame); | ||
242 | extern char *cgit_fileurl(const char *reponame, const char *pagename, | ||
243 | const char *filename, const char *query); | ||
244 | extern char *cgit_pageurl(const char *reponame, const char *pagename, | ||
245 | const char *query); | ||
246 | |||
247 | extern const char *cgit_repobasename(const char *reponame); | 220 | extern const char *cgit_repobasename(const char *reponame); |
248 | 221 | ||
249 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, | ||
250 | char *rev, char *path); | ||
251 | extern void cgit_log_link(char *name, char *title, char *class, char *head, | ||
252 | char *rev, char *path, int ofs, char *grep, | ||
253 | char *pattern); | ||
254 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, | ||
255 | char *rev); | ||
256 | extern void cgit_refs_link(char *name, char *title, char *class, char *head, | ||
257 | char *rev, char *path); | ||
258 | extern void cgit_snapshot_link(char *name, char *title, char *class, | ||
259 | char *head, char *rev, char *archivename); | ||
260 | extern void cgit_diff_link(char *name, char *title, char *class, char *head, | ||
261 | char *new_rev, char *old_rev, char *path); | ||
262 | |||
263 | extern void cgit_object_link(struct object *obj); | ||
264 | |||
265 | extern void cgit_print_error(char *msg); | ||
266 | extern void cgit_print_date(time_t secs, char *format); | ||
267 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); | ||
268 | extern void cgit_print_docstart(char *title, struct cacheitem *item); | ||
269 | extern void cgit_print_docend(); | ||
270 | extern void cgit_print_pageheader(char *title, int show_search); | ||
271 | extern void cgit_print_snapshot_start(const char *mimetype, | ||
272 | const char *filename, | ||
273 | struct cacheitem *item); | ||
274 | extern void cgit_print_branches(int maxcount); | ||
275 | extern void cgit_print_tags(int maxcount); | ||
276 | |||
277 | extern void cgit_print_repolist(struct cacheitem *item); | ||
278 | extern void cgit_print_summary(); | ||
279 | extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, | ||
280 | char *pattern, char *path, int pager); | ||
281 | extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); | ||
282 | extern void cgit_print_tree(const char *rev, char *path); | ||
283 | extern void cgit_print_commit(char *hex); | ||
284 | extern void cgit_print_refs(); | ||
285 | extern void cgit_print_tag(char *revname); | ||
286 | extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); | ||
287 | extern void cgit_print_patch(char *hex, struct cacheitem *item); | ||
288 | extern void cgit_print_snapshot(struct cacheitem *item, const char *head, | ||
289 | const char *hex, const char *prefix, | ||
290 | const char *filename, int snapshot); | ||
291 | extern void cgit_print_snapshot_links(const char *repo, const char *head, | ||
292 | const char *hex, int snapshots); | ||
293 | extern int cgit_parse_snapshots_mask(const char *str); | 222 | extern int cgit_parse_snapshots_mask(const char *str); |
294 | 223 | ||
295 | #endif /* CGIT_H */ | 224 | #endif /* CGIT_H */ |
@@ -0,0 +1,112 @@ | |||
1 | /* cmd.c: the cgit command dispatcher | ||
2 | * | ||
3 | * Copyright (C) 2008 Lars Hjemli | ||
4 | * | ||
5 | * Licensed under GNU General Public License v2 | ||
6 | * (see COPYING for full license text) | ||
7 | */ | ||
8 | |||
9 | #include "cgit.h" | ||
10 | #include "cmd.h" | ||
11 | #include "ui-blob.h" | ||
12 | #include "ui-commit.h" | ||
13 | #include "ui-diff.h" | ||
14 | #include "ui-log.h" | ||
15 | #include "ui-patch.h" | ||
16 | #include "ui-refs.h" | ||
17 | #include "ui-repolist.h" | ||
18 | #include "ui-snapshot.h" | ||
19 | #include "ui-summary.h" | ||
20 | #include "ui-tag.h" | ||
21 | #include "ui-tree.h" | ||
22 | |||
23 | static void blob_fn(struct cgit_context *ctx) | ||
24 | { | ||
25 | cgit_print_blob(ctx->qry.sha1, ctx->qry.path); | ||
26 | } | ||
27 | |||
28 | static void commit_fn(struct cgit_context *ctx) | ||
29 | { | ||
30 | cgit_print_commit(ctx->qry.sha1); | ||
31 | } | ||
32 | |||
33 | static void diff_fn(struct cgit_context *ctx) | ||
34 | { | ||
35 | cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); | ||
36 | } | ||
37 | |||
38 | static void repolist_fn(struct cgit_context *ctx) | ||
39 | { | ||
40 | cgit_print_repolist(); | ||
41 | } | ||
42 | |||
43 | static void log_fn(struct cgit_context *ctx) | ||
44 | { | ||
45 | cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, | ||
46 | ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); | ||
47 | } | ||
48 | |||
49 | static void patch_fn(struct cgit_context *ctx) | ||
50 | { | ||
51 | cgit_print_patch(ctx->qry.sha1); | ||
52 | } | ||
53 | |||
54 | static void refs_fn(struct cgit_context *ctx) | ||
55 | { | ||
56 | cgit_print_refs(); | ||
57 | } | ||
58 | |||
59 | static void snapshot_fn(struct cgit_context *ctx) | ||
60 | { | ||
61 | cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, | ||
62 | cgit_repobasename(ctx->repo->url), ctx->qry.path, | ||
63 | ctx->repo->snapshots); | ||
64 | } | ||
65 | |||
66 | static void summary_fn(struct cgit_context *ctx) | ||
67 | { | ||
68 | cgit_print_summary(); | ||
69 | } | ||
70 | |||
71 | static void tag_fn(struct cgit_context *ctx) | ||
72 | { | ||
73 | cgit_print_tag(ctx->qry.sha1); | ||
74 | } | ||
75 | |||
76 | static void tree_fn(struct cgit_context *ctx) | ||
77 | { | ||
78 | cgit_print_tree(ctx->qry.sha1, ctx->qry.path); | ||
79 | } | ||
80 | |||
81 | #define def_cmd(name, want_repo, want_layout) \ | ||
82 | {#name, name##_fn, want_repo, want_layout} | ||
83 | |||
84 | struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) | ||
85 | { | ||
86 | static struct cgit_cmd cmds[] = { | ||
87 | def_cmd(blob, 1, 0), | ||
88 | def_cmd(commit, 1, 1), | ||
89 | def_cmd(diff, 1, 1), | ||
90 | def_cmd(log, 1, 1), | ||
91 | def_cmd(patch, 1, 0), | ||
92 | def_cmd(refs, 1, 1), | ||
93 | def_cmd(repolist, 0, 0), | ||
94 | def_cmd(snapshot, 1, 0), | ||
95 | def_cmd(summary, 1, 1), | ||
96 | def_cmd(tag, 1, 1), | ||
97 | def_cmd(tree, 1, 1), | ||
98 | }; | ||
99 | int i; | ||
100 | |||
101 | if (ctx->qry.page == NULL) { | ||
102 | if (ctx->repo) | ||
103 | ctx->qry.page = "summary"; | ||
104 | else | ||
105 | ctx->qry.page = "repolist"; | ||
106 | } | ||
107 | |||
108 | for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) | ||
109 | if (!strcmp(ctx->qry.page, cmds[i].name)) | ||
110 | return &cmds[i]; | ||
111 | return NULL; | ||
112 | } | ||
@@ -0,0 +1,15 @@ | |||
1 | #ifndef CMD_H | ||
2 | #define CMD_H | ||
3 | |||
4 | typedef void (*cgit_cmd_fn)(struct cgit_context *ctx); | ||
5 | |||
6 | struct cgit_cmd { | ||
7 | const char *name; | ||
8 | cgit_cmd_fn fn; | ||
9 | unsigned int want_repo:1, | ||
10 | want_layout:1; | ||
11 | }; | ||
12 | |||
13 | extern struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx); | ||
14 | |||
15 | #endif /* CMD_H */ | ||
diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..4908058 --- a/dev/null +++ b/configfile.c | |||
@@ -0,0 +1,87 @@ | |||
1 | /* configfile.c: parsing of config files | ||
2 | * | ||
3 | * Copyright (C) 2008 Lars Hjemli | ||
4 | * | ||
5 | * Licensed under GNU General Public License v2 | ||
6 | * (see COPYING for full license text) | ||
7 | */ | ||
8 | |||
9 | #include <ctype.h> | ||
10 | #include <stdio.h> | ||
11 | #include "configfile.h" | ||
12 | |||
13 | int next_char(FILE *f) | ||
14 | { | ||
15 | int c = fgetc(f); | ||
16 | if (c=='\r') { | ||
17 | c = fgetc(f); | ||
18 | if (c!='\n') { | ||
19 | ungetc(c, f); | ||
20 | c = '\r'; | ||
21 | } | ||
22 | } | ||
23 | return c; | ||
24 | } | ||
25 | |||
26 | void skip_line(FILE *f) | ||
27 | { | ||
28 | int c; | ||
29 | |||
30 | while((c=next_char(f)) && c!='\n' && c!=EOF) | ||
31 | ; | ||
32 | } | ||
33 | |||
34 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) | ||
35 | { | ||
36 | int i = 0, isname = 0; | ||
37 | |||
38 | *value = NULL; | ||
39 | while(i<bufsize-1) { | ||
40 | int c = next_char(f); | ||
41 | if (!isname && (c=='#' || c==';')) { | ||
42 | skip_line(f); | ||
43 | continue; | ||
44 | } | ||
45 | if (!isname && isspace(c)) | ||
46 | continue; | ||
47 | |||
48 | if (c=='=' && !*value) { | ||
49 | line[i] = 0; | ||
50 | *value = &line[i+1]; | ||
51 | } else if (c=='\n' && !isname) { | ||
52 | i = 0; | ||
53 | continue; | ||
54 | } else if (c=='\n' || c==EOF) { | ||
55 | line[i] = 0; | ||
56 | break; | ||
57 | } else { | ||
58 | line[i]=c; | ||
59 | } | ||
60 | isname = 1; | ||
61 | i++; | ||
62 | } | ||
63 | line[i+1] = 0; | ||
64 | return i; | ||
65 | } | ||
66 | |||
67 | int parse_configfile(const char *filename, configfile_value_fn fn) | ||
68 | { | ||
69 | static int nesting; | ||
70 | int len; | ||
71 | char line[256]; | ||
72 | const char *value; | ||
73 | FILE *f; | ||
74 | |||
75 | /* cancel deeply nested include-commands */ | ||
76 | if (nesting > 8) | ||
77 | return -1; | ||
78 | if (!(f = fopen(filename, "r"))) | ||
79 | return -1; | ||
80 | nesting++; | ||
81 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | ||
82 | fn(line, value); | ||
83 | nesting--; | ||
84 | fclose(f); | ||
85 | return 0; | ||
86 | } | ||
87 | |||
diff --git a/configfile.h b/configfile.h new file mode 100644 index 0000000..04235e5 --- a/dev/null +++ b/configfile.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef CONFIGFILE_H | ||
2 | #define CONFIGFILE_H | ||
3 | |||
4 | typedef void (*configfile_value_fn)(const char *name, const char *value); | ||
5 | |||
6 | extern int parse_configfile(const char *filename, configfile_value_fn fn); | ||
7 | |||
8 | #endif /* CONFIGFILE_H */ | ||
@@ -1,184 +1,241 @@ | |||
1 | /* html.c: helper functions for html output | 1 | /* html.c: helper functions for html output |
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 <unistd.h> |
10 | #include <stdio.h> | ||
11 | #include <stdlib.h> | ||
12 | #include <stdarg.h> | ||
13 | #include <string.h> | ||
14 | |||
15 | int htmlfd = STDOUT_FILENO; | ||
10 | 16 | ||
11 | char *fmt(const char *format, ...) | 17 | char *fmt(const char *format, ...) |
12 | { | 18 | { |
13 | static char buf[8][1024]; | 19 | static char buf[8][1024]; |
14 | static int bufidx; | 20 | static int bufidx; |
15 | int len; | 21 | int len; |
16 | va_list args; | 22 | va_list args; |
17 | 23 | ||
18 | bufidx++; | 24 | bufidx++; |
19 | bufidx &= 7; | 25 | bufidx &= 7; |
20 | 26 | ||
21 | va_start(args, format); | 27 | va_start(args, format); |
22 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); | 28 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
23 | va_end(args); | 29 | va_end(args); |
24 | if (len>sizeof(buf[bufidx])) | 30 | if (len>sizeof(buf[bufidx])) { |
25 | die("[html.c] string truncated: %s", format); | 31 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
32 | exit(1); | ||
33 | } | ||
26 | return buf[bufidx]; | 34 | return buf[bufidx]; |
27 | } | 35 | } |
28 | 36 | ||
29 | void html(const char *txt) | 37 | void html(const char *txt) |
30 | { | 38 | { |
31 | write(htmlfd, txt, strlen(txt)); | 39 | write(htmlfd, txt, strlen(txt)); |
32 | } | 40 | } |
33 | 41 | ||
34 | void htmlf(const char *format, ...) | 42 | void htmlf(const char *format, ...) |
35 | { | 43 | { |
36 | static char buf[65536]; | 44 | static char buf[65536]; |
37 | va_list args; | 45 | va_list args; |
38 | 46 | ||
39 | va_start(args, format); | 47 | va_start(args, format); |
40 | vsnprintf(buf, sizeof(buf), format, args); | 48 | vsnprintf(buf, sizeof(buf), format, args); |
41 | va_end(args); | 49 | va_end(args); |
42 | html(buf); | 50 | html(buf); |
43 | } | 51 | } |
44 | 52 | ||
45 | void html_txt(char *txt) | 53 | void html_txt(char *txt) |
46 | { | 54 | { |
47 | char *t = txt; | 55 | char *t = txt; |
48 | while(t && *t){ | 56 | while(t && *t){ |
49 | int c = *t; | 57 | int c = *t; |
50 | if (c=='<' || c=='>' || c=='&') { | 58 | if (c=='<' || c=='>' || c=='&') { |
51 | write(htmlfd, txt, t - txt); | 59 | write(htmlfd, txt, t - txt); |
52 | if (c=='>') | 60 | if (c=='>') |
53 | html(">"); | 61 | html(">"); |
54 | else if (c=='<') | 62 | else if (c=='<') |
55 | html("<"); | 63 | html("<"); |
56 | else if (c=='&') | 64 | else if (c=='&') |
57 | html("&"); | 65 | html("&"); |
58 | txt = t+1; | 66 | txt = t+1; |
59 | } | 67 | } |
60 | t++; | 68 | t++; |
61 | } | 69 | } |
62 | if (t!=txt) | 70 | if (t!=txt) |
63 | html(txt); | 71 | html(txt); |
64 | } | 72 | } |
65 | 73 | ||
66 | void html_ntxt(int len, char *txt) | 74 | void html_ntxt(int len, char *txt) |
67 | { | 75 | { |
68 | char *t = txt; | 76 | char *t = txt; |
69 | while(t && *t && len--){ | 77 | while(t && *t && len--){ |
70 | int c = *t; | 78 | int c = *t; |
71 | if (c=='<' || c=='>' || c=='&') { | 79 | if (c=='<' || c=='>' || c=='&') { |
72 | write(htmlfd, txt, t - txt); | 80 | write(htmlfd, txt, t - txt); |
73 | if (c=='>') | 81 | if (c=='>') |
74 | html(">"); | 82 | html(">"); |
75 | else if (c=='<') | 83 | else if (c=='<') |
76 | html("<"); | 84 | html("<"); |
77 | else if (c=='&') | 85 | else if (c=='&') |
78 | html("&"); | 86 | html("&"); |
79 | txt = t+1; | 87 | txt = t+1; |
80 | } | 88 | } |
81 | t++; | 89 | t++; |
82 | } | 90 | } |
83 | if (t!=txt) | 91 | if (t!=txt) |
84 | write(htmlfd, txt, t - txt); | 92 | write(htmlfd, txt, t - txt); |
85 | if (len<0) | 93 | if (len<0) |
86 | html("..."); | 94 | html("..."); |
87 | } | 95 | } |
88 | 96 | ||
89 | void html_attr(char *txt) | 97 | void html_attr(char *txt) |
90 | { | 98 | { |
91 | char *t = txt; | 99 | char *t = txt; |
92 | while(t && *t){ | 100 | while(t && *t){ |
93 | int c = *t; | 101 | int c = *t; |
94 | if (c=='<' || c=='>' || c=='\'') { | 102 | if (c=='<' || c=='>' || c=='\'') { |
95 | write(htmlfd, txt, t - txt); | 103 | write(htmlfd, txt, t - txt); |
96 | if (c=='>') | 104 | if (c=='>') |
97 | html(">"); | 105 | html(">"); |
98 | else if (c=='<') | 106 | else if (c=='<') |
99 | html("<"); | 107 | html("<"); |
100 | else if (c=='\'') | 108 | else if (c=='\'') |
101 | html(""e;"); | 109 | html(""e;"); |
102 | txt = t+1; | 110 | txt = t+1; |
103 | } | 111 | } |
104 | t++; | 112 | t++; |
105 | } | 113 | } |
106 | if (t!=txt) | 114 | if (t!=txt) |
107 | html(txt); | 115 | html(txt); |
108 | } | 116 | } |
109 | 117 | ||
110 | void html_hidden(char *name, char *value) | 118 | void html_hidden(char *name, char *value) |
111 | { | 119 | { |
112 | html("<input type='hidden' name='"); | 120 | html("<input type='hidden' name='"); |
113 | html_attr(name); | 121 | html_attr(name); |
114 | html("' value='"); | 122 | html("' value='"); |
115 | html_attr(value); | 123 | html_attr(value); |
116 | html("'/>"); | 124 | html("'/>"); |
117 | } | 125 | } |
118 | 126 | ||
119 | void html_option(char *value, char *text, char *selected_value) | 127 | void html_option(char *value, char *text, char *selected_value) |
120 | { | 128 | { |
121 | html("<option value='"); | 129 | html("<option value='"); |
122 | html_attr(value); | 130 | html_attr(value); |
123 | html("'"); | 131 | html("'"); |
124 | if (selected_value && !strcmp(selected_value, value)) | 132 | if (selected_value && !strcmp(selected_value, value)) |
125 | html(" selected='selected'"); | 133 | html(" selected='selected'"); |
126 | html(">"); | 134 | html(">"); |
127 | html_txt(text); | 135 | html_txt(text); |
128 | html("</option>\n"); | 136 | html("</option>\n"); |
129 | } | 137 | } |
130 | 138 | ||
131 | void html_link_open(char *url, char *title, char *class) | 139 | void html_link_open(char *url, char *title, char *class) |
132 | { | 140 | { |
133 | html("<a href='"); | 141 | html("<a href='"); |
134 | html_attr(url); | 142 | html_attr(url); |
135 | if (title) { | 143 | if (title) { |
136 | html("' title='"); | 144 | html("' title='"); |
137 | html_attr(title); | 145 | html_attr(title); |
138 | } | 146 | } |
139 | if (class) { | 147 | if (class) { |
140 | html("' class='"); | 148 | html("' class='"); |
141 | html_attr(class); | 149 | html_attr(class); |
142 | } | 150 | } |
143 | html("'>"); | 151 | html("'>"); |
144 | } | 152 | } |
145 | 153 | ||
146 | void html_link_close(void) | 154 | void html_link_close(void) |
147 | { | 155 | { |
148 | html("</a>"); | 156 | html("</a>"); |
149 | } | 157 | } |
150 | 158 | ||
151 | void html_fileperm(unsigned short mode) | 159 | void html_fileperm(unsigned short mode) |
152 | { | 160 | { |
153 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), | 161 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
154 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); | 162 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
155 | } | 163 | } |
156 | 164 | ||
157 | void html_filemode(unsigned short mode) | ||
158 | { | ||
159 | if (S_ISDIR(mode)) | ||
160 | html("d"); | ||
161 | else if (S_ISLNK(mode)) | ||
162 | html("l"); | ||
163 | else if (S_ISGITLINK(mode)) | ||
164 | html("m"); | ||
165 | else | ||
166 | html("-"); | ||
167 | html_fileperm(mode >> 6); | ||
168 | html_fileperm(mode >> 3); | ||
169 | html_fileperm(mode); | ||
170 | } | ||
171 | |||
172 | int html_include(const char *filename) | 165 | int html_include(const char *filename) |
173 | { | 166 | { |
174 | FILE *f; | 167 | FILE *f; |
175 | char buf[4096]; | 168 | char buf[4096]; |
176 | size_t len; | 169 | size_t len; |
177 | 170 | ||
178 | if (!(f = fopen(filename, "r"))) | 171 | if (!(f = fopen(filename, "r"))) |
179 | return -1; | 172 | return -1; |
180 | while((len = fread(buf, 1, 4096, f)) > 0) | 173 | while((len = fread(buf, 1, 4096, f)) > 0) |
181 | write(htmlfd, buf, len); | 174 | write(htmlfd, buf, len); |
182 | fclose(f); | 175 | fclose(f); |
183 | return 0; | 176 | return 0; |
184 | } | 177 | } |
178 | |||
179 | int hextoint(char c) | ||
180 | { | ||
181 | if (c >= 'a' && c <= 'f') | ||
182 | return 10 + c - 'a'; | ||
183 | else if (c >= 'A' && c <= 'F') | ||
184 | return 10 + c - 'A'; | ||
185 | else if (c >= '0' && c <= '9') | ||
186 | return c - '0'; | ||
187 | else | ||
188 | return -1; | ||
189 | } | ||
190 | |||
191 | char *convert_query_hexchar(char *txt) | ||
192 | { | ||
193 | int d1, d2; | ||
194 | if (strlen(txt) < 3) { | ||
195 | *txt = '\0'; | ||
196 | return txt-1; | ||
197 | } | ||
198 | d1 = hextoint(*(txt+1)); | ||
199 | d2 = hextoint(*(txt+2)); | ||
200 | if (d1<0 || d2<0) { | ||
201 | strcpy(txt, txt+3); | ||
202 | return txt-1; | ||
203 | } else { | ||
204 | *txt = d1 * 16 + d2; | ||
205 | strcpy(txt+1, txt+3); | ||
206 | return txt; | ||
207 | } | ||
208 | } | ||
209 | |||
210 | int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) | ||
211 | { | ||
212 | char *t, *value = NULL, c; | ||
213 | |||
214 | if (!txt) | ||
215 | return 0; | ||
216 | |||
217 | t = txt = strdup(txt); | ||
218 | if (t == NULL) { | ||
219 | printf("Out of memory\n"); | ||
220 | exit(1); | ||
221 | } | ||
222 | while((c=*t) != '\0') { | ||
223 | if (c=='=') { | ||
224 | *t = '\0'; | ||
225 | value = t+1; | ||
226 | } else if (c=='+') { | ||
227 | *t = ' '; | ||
228 | } else if (c=='%') { | ||
229 | t = convert_query_hexchar(t); | ||
230 | } else if (c=='&') { | ||
231 | *t = '\0'; | ||
232 | (*fn)(txt, value); | ||
233 | txt = t+1; | ||
234 | value = NULL; | ||
235 | } | ||
236 | t++; | ||
237 | } | ||
238 | if (t!=txt) | ||
239 | (*fn)(txt, value); | ||
240 | return 0; | ||
241 | } | ||
@@ -0,0 +1,20 @@ | |||
1 | #ifndef HTML_H | ||
2 | #define HTML_H | ||
3 | |||
4 | extern int htmlfd; | ||
5 | |||
6 | extern void html(const char *txt); | ||
7 | extern void htmlf(const char *format,...); | ||
8 | extern void html_txt(char *txt); | ||
9 | extern void html_ntxt(int len, char *txt); | ||
10 | extern void html_attr(char *txt); | ||
11 | extern void html_hidden(char *name, char *value); | ||
12 | extern void html_option(char *value, char *text, char *selected_value); | ||
13 | extern void html_link_open(char *url, char *title, char *class); | ||
14 | extern void html_link_close(void); | ||
15 | extern void html_fileperm(unsigned short mode); | ||
16 | extern int html_include(const char *filename); | ||
17 | |||
18 | extern int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)); | ||
19 | |||
20 | #endif /* HTML_H */ | ||
@@ -1,332 +1,208 @@ | |||
1 | /* config.c: parsing of config files | 1 | /* config.c: parsing of config files |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | int next_char(FILE *f) | ||
12 | { | ||
13 | int c = fgetc(f); | ||
14 | if (c=='\r') { | ||
15 | c = fgetc(f); | ||
16 | if (c!='\n') { | ||
17 | ungetc(c, f); | ||
18 | c = '\r'; | ||
19 | } | ||
20 | } | ||
21 | return c; | ||
22 | } | ||
23 | |||
24 | void skip_line(FILE *f) | ||
25 | { | ||
26 | int c; | ||
27 | |||
28 | while((c=next_char(f)) && c!='\n' && c!=EOF) | ||
29 | ; | ||
30 | } | ||
31 | |||
32 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) | ||
33 | { | ||
34 | int i = 0, isname = 0; | ||
35 | |||
36 | *value = NULL; | ||
37 | while(i<bufsize-1) { | ||
38 | int c = next_char(f); | ||
39 | if (!isname && (c=='#' || c==';')) { | ||
40 | skip_line(f); | ||
41 | continue; | ||
42 | } | ||
43 | if (!isname && isspace(c)) | ||
44 | continue; | ||
45 | |||
46 | if (c=='=' && !*value) { | ||
47 | line[i] = 0; | ||
48 | *value = &line[i+1]; | ||
49 | } else if (c=='\n' && !isname) { | ||
50 | i = 0; | ||
51 | continue; | ||
52 | } else if (c=='\n' || c==EOF) { | ||
53 | line[i] = 0; | ||
54 | break; | ||
55 | } else { | ||
56 | line[i]=c; | ||
57 | } | ||
58 | isname = 1; | ||
59 | i++; | ||
60 | } | ||
61 | line[i+1] = 0; | ||
62 | return i; | ||
63 | } | ||
64 | |||
65 | int cgit_read_config(const char *filename, configfn fn) | ||
66 | { | ||
67 | static int nesting; | ||
68 | int len; | ||
69 | char line[256]; | ||
70 | const char *value; | ||
71 | FILE *f; | ||
72 | |||
73 | /* cancel deeply nested include-commands */ | ||
74 | if (nesting > 8) | ||
75 | return -1; | ||
76 | if (!(f = fopen(filename, "r"))) | ||
77 | return -1; | ||
78 | nesting++; | ||
79 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | ||
80 | (*fn)(line, value); | ||
81 | nesting--; | ||
82 | fclose(f); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | char *convert_query_hexchar(char *txt) | ||
87 | { | ||
88 | int d1, d2; | ||
89 | if (strlen(txt) < 3) { | ||
90 | *txt = '\0'; | ||
91 | return txt-1; | ||
92 | } | ||
93 | d1 = hextoint(*(txt+1)); | ||
94 | d2 = hextoint(*(txt+2)); | ||
95 | if (d1<0 || d2<0) { | ||
96 | strcpy(txt, txt+3); | ||
97 | return txt-1; | ||
98 | } else { | ||
99 | *txt = d1 * 16 + d2; | ||
100 | strcpy(txt+1, txt+3); | ||
101 | return txt; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | int cgit_parse_query(char *txt, configfn fn) | ||
106 | { | ||
107 | char *t, *value = NULL, c; | ||
108 | |||
109 | if (!txt) | ||
110 | return 0; | ||
111 | |||
112 | t = txt = xstrdup(txt); | ||
113 | |||
114 | while((c=*t) != '\0') { | ||
115 | if (c=='=') { | ||
116 | *t = '\0'; | ||
117 | value = t+1; | ||
118 | } else if (c=='+') { | ||
119 | *t = ' '; | ||
120 | } else if (c=='%') { | ||
121 | t = convert_query_hexchar(t); | ||
122 | } else if (c=='&') { | ||
123 | *t = '\0'; | ||
124 | (*fn)(txt, value); | ||
125 | txt = t+1; | ||
126 | value = NULL; | ||
127 | } | ||
128 | t++; | ||
129 | } | ||
130 | if (t!=txt) | ||
131 | (*fn)(txt, value); | ||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | /* | 11 | /* |
136 | * url syntax: [repo ['/' cmd [ '/' path]]] | 12 | * url syntax: [repo ['/' cmd [ '/' path]]] |
137 | * repo: any valid repo url, may contain '/' | 13 | * repo: any valid repo url, may contain '/' |
138 | * cmd: log | commit | diff | tree | view | blob | snapshot | 14 | * cmd: log | commit | diff | tree | view | blob | snapshot |
139 | * path: any valid path, may contain '/' | 15 | * path: any valid path, may contain '/' |
140 | * | 16 | * |
141 | */ | 17 | */ |
142 | void cgit_parse_url(const char *url) | 18 | void cgit_parse_url(const char *url) |
143 | { | 19 | { |
144 | char *cmd, *p; | 20 | char *cmd, *p; |
145 | 21 | ||
146 | cgit_repo = NULL; | 22 | ctx.repo = NULL; |
147 | if (!url || url[0] == '\0') | 23 | if (!url || url[0] == '\0') |
148 | return; | 24 | return; |
149 | 25 | ||
150 | cgit_repo = cgit_get_repoinfo(url); | 26 | ctx.repo = cgit_get_repoinfo(url); |
151 | if (cgit_repo) { | 27 | if (ctx.repo) { |
152 | cgit_query_repo = cgit_repo->url; | 28 | ctx.qry.repo = ctx.repo->url; |
153 | return; | 29 | return; |
154 | } | 30 | } |
155 | 31 | ||
156 | cmd = strchr(url, '/'); | 32 | cmd = strchr(url, '/'); |
157 | while (!cgit_repo && cmd) { | 33 | while (!ctx.repo && cmd) { |
158 | cmd[0] = '\0'; | 34 | cmd[0] = '\0'; |
159 | cgit_repo = cgit_get_repoinfo(url); | 35 | ctx.repo = cgit_get_repoinfo(url); |
160 | if (cgit_repo == NULL) { | 36 | if (ctx.repo == NULL) { |
161 | cmd[0] = '/'; | 37 | cmd[0] = '/'; |
162 | cmd = strchr(cmd + 1, '/'); | 38 | cmd = strchr(cmd + 1, '/'); |
163 | continue; | 39 | continue; |
164 | } | 40 | } |
165 | 41 | ||
166 | cgit_query_repo = cgit_repo->url; | 42 | ctx.qry.repo = ctx.repo->url; |
167 | p = strchr(cmd + 1, '/'); | 43 | p = strchr(cmd + 1, '/'); |
168 | if (p) { | 44 | if (p) { |
169 | p[0] = '\0'; | 45 | p[0] = '\0'; |
170 | if (p[1]) | 46 | if (p[1]) |
171 | cgit_query_path = trim_end(p + 1, '/'); | 47 | ctx.qry.path = trim_end(p + 1, '/'); |
172 | } | 48 | } |
173 | cgit_cmd = cgit_get_cmd_index(cmd + 1); | 49 | if (cmd[1]) |
174 | cgit_query_page = xstrdup(cmd + 1); | 50 | ctx.qry.page = xstrdup(cmd + 1); |
175 | return; | 51 | return; |
176 | } | 52 | } |
177 | } | 53 | } |
178 | 54 | ||
179 | char *substr(const char *head, const char *tail) | 55 | char *substr(const char *head, const char *tail) |
180 | { | 56 | { |
181 | char *buf; | 57 | char *buf; |
182 | 58 | ||
183 | buf = xmalloc(tail - head + 1); | 59 | buf = xmalloc(tail - head + 1); |
184 | strncpy(buf, head, tail - head); | 60 | strncpy(buf, head, tail - head); |
185 | buf[tail - head] = '\0'; | 61 | buf[tail - head] = '\0'; |
186 | return buf; | 62 | return buf; |
187 | } | 63 | } |
188 | 64 | ||
189 | struct commitinfo *cgit_parse_commit(struct commit *commit) | 65 | struct commitinfo *cgit_parse_commit(struct commit *commit) |
190 | { | 66 | { |
191 | struct commitinfo *ret; | 67 | struct commitinfo *ret; |
192 | char *p = commit->buffer, *t = commit->buffer; | 68 | char *p = commit->buffer, *t = commit->buffer; |
193 | 69 | ||
194 | ret = xmalloc(sizeof(*ret)); | 70 | ret = xmalloc(sizeof(*ret)); |
195 | ret->commit = commit; | 71 | ret->commit = commit; |
196 | ret->author = NULL; | 72 | ret->author = NULL; |
197 | ret->author_email = NULL; | 73 | ret->author_email = NULL; |
198 | ret->committer = NULL; | 74 | ret->committer = NULL; |
199 | ret->committer_email = NULL; | 75 | ret->committer_email = NULL; |
200 | ret->subject = NULL; | 76 | ret->subject = NULL; |
201 | ret->msg = NULL; | 77 | ret->msg = NULL; |
202 | ret->msg_encoding = NULL; | 78 | ret->msg_encoding = NULL; |
203 | 79 | ||
204 | if (p == NULL) | 80 | if (p == NULL) |
205 | return ret; | 81 | return ret; |
206 | 82 | ||
207 | if (strncmp(p, "tree ", 5)) | 83 | if (strncmp(p, "tree ", 5)) |
208 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); | 84 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); |
209 | else | 85 | else |
210 | p += 46; // "tree " + hex[40] + "\n" | 86 | p += 46; // "tree " + hex[40] + "\n" |
211 | 87 | ||
212 | while (!strncmp(p, "parent ", 7)) | 88 | while (!strncmp(p, "parent ", 7)) |
213 | p += 48; // "parent " + hex[40] + "\n" | 89 | p += 48; // "parent " + hex[40] + "\n" |
214 | 90 | ||
215 | if (!strncmp(p, "author ", 7)) { | 91 | if (!strncmp(p, "author ", 7)) { |
216 | p += 7; | 92 | p += 7; |
217 | t = strchr(p, '<') - 1; | 93 | t = strchr(p, '<') - 1; |
218 | ret->author = substr(p, t); | 94 | ret->author = substr(p, t); |
219 | p = t; | 95 | p = t; |
220 | t = strchr(t, '>') + 1; | 96 | t = strchr(t, '>') + 1; |
221 | ret->author_email = substr(p, t); | 97 | ret->author_email = substr(p, t); |
222 | ret->author_date = atol(t+1); | 98 | ret->author_date = atol(t+1); |
223 | p = strchr(t, '\n') + 1; | 99 | p = strchr(t, '\n') + 1; |
224 | } | 100 | } |
225 | 101 | ||
226 | if (!strncmp(p, "committer ", 9)) { | 102 | if (!strncmp(p, "committer ", 9)) { |
227 | p += 9; | 103 | p += 9; |
228 | t = strchr(p, '<') - 1; | 104 | t = strchr(p, '<') - 1; |
229 | ret->committer = substr(p, t); | 105 | ret->committer = substr(p, t); |
230 | p = t; | 106 | p = t; |
231 | t = strchr(t, '>') + 1; | 107 | t = strchr(t, '>') + 1; |
232 | ret->committer_email = substr(p, t); | 108 | ret->committer_email = substr(p, t); |
233 | ret->committer_date = atol(t+1); | 109 | ret->committer_date = atol(t+1); |
234 | p = strchr(t, '\n') + 1; | 110 | p = strchr(t, '\n') + 1; |
235 | } | 111 | } |
236 | 112 | ||
237 | if (!strncmp(p, "encoding ", 9)) { | 113 | if (!strncmp(p, "encoding ", 9)) { |
238 | p += 9; | 114 | p += 9; |
239 | t = strchr(p, '\n') + 1; | 115 | t = strchr(p, '\n') + 1; |
240 | ret->msg_encoding = substr(p, t); | 116 | ret->msg_encoding = substr(p, t); |
241 | p = t; | 117 | p = t; |
242 | } else | 118 | } else |
243 | ret->msg_encoding = xstrdup(PAGE_ENCODING); | 119 | ret->msg_encoding = xstrdup(PAGE_ENCODING); |
244 | 120 | ||
245 | while (*p && (*p != '\n')) | 121 | while (*p && (*p != '\n')) |
246 | p = strchr(p, '\n') + 1; // skip unknown header fields | 122 | p = strchr(p, '\n') + 1; // skip unknown header fields |
247 | 123 | ||
248 | while (*p == '\n') | 124 | while (*p == '\n') |
249 | p = strchr(p, '\n') + 1; | 125 | p = strchr(p, '\n') + 1; |
250 | 126 | ||
251 | t = strchr(p, '\n'); | 127 | t = strchr(p, '\n'); |
252 | if (t) { | 128 | if (t) { |
253 | if (*t == '\0') | 129 | if (*t == '\0') |
254 | ret->subject = "** empty **"; | 130 | ret->subject = "** empty **"; |
255 | else | 131 | else |
256 | ret->subject = substr(p, t); | 132 | ret->subject = substr(p, t); |
257 | p = t + 1; | 133 | p = t + 1; |
258 | 134 | ||
259 | while (*p == '\n') | 135 | while (*p == '\n') |
260 | p = strchr(p, '\n') + 1; | 136 | p = strchr(p, '\n') + 1; |
261 | ret->msg = xstrdup(p); | 137 | ret->msg = xstrdup(p); |
262 | } else | 138 | } else |
263 | ret->subject = substr(p, p+strlen(p)); | 139 | ret->subject = substr(p, p+strlen(p)); |
264 | 140 | ||
265 | if(strcmp(ret->msg_encoding, PAGE_ENCODING)) { | 141 | if(strcmp(ret->msg_encoding, PAGE_ENCODING)) { |
266 | t = reencode_string(ret->subject, PAGE_ENCODING, | 142 | t = reencode_string(ret->subject, PAGE_ENCODING, |
267 | ret->msg_encoding); | 143 | ret->msg_encoding); |
268 | if(t) { | 144 | if(t) { |
269 | free(ret->subject); | 145 | free(ret->subject); |
270 | ret->subject = t; | 146 | ret->subject = t; |
271 | } | 147 | } |
272 | 148 | ||
273 | t = reencode_string(ret->msg, PAGE_ENCODING, | 149 | t = reencode_string(ret->msg, PAGE_ENCODING, |
274 | ret->msg_encoding); | 150 | ret->msg_encoding); |
275 | if(t) { | 151 | if(t) { |
276 | free(ret->msg); | 152 | free(ret->msg); |
277 | ret->msg = t; | 153 | ret->msg = t; |
278 | } | 154 | } |
279 | } | 155 | } |
280 | 156 | ||
281 | return ret; | 157 | return ret; |
282 | } | 158 | } |
283 | 159 | ||
284 | 160 | ||
285 | struct taginfo *cgit_parse_tag(struct tag *tag) | 161 | struct taginfo *cgit_parse_tag(struct tag *tag) |
286 | { | 162 | { |
287 | void *data; | 163 | void *data; |
288 | enum object_type type; | 164 | enum object_type type; |
289 | unsigned long size; | 165 | unsigned long size; |
290 | char *p, *t; | 166 | char *p, *t; |
291 | struct taginfo *ret; | 167 | struct taginfo *ret; |
292 | 168 | ||
293 | data = read_sha1_file(tag->object.sha1, &type, &size); | 169 | data = read_sha1_file(tag->object.sha1, &type, &size); |
294 | if (!data || type != OBJ_TAG) { | 170 | if (!data || type != OBJ_TAG) { |
295 | free(data); | 171 | free(data); |
296 | return 0; | 172 | return 0; |
297 | } | 173 | } |
298 | 174 | ||
299 | ret = xmalloc(sizeof(*ret)); | 175 | ret = xmalloc(sizeof(*ret)); |
300 | ret->tagger = NULL; | 176 | ret->tagger = NULL; |
301 | ret->tagger_email = NULL; | 177 | ret->tagger_email = NULL; |
302 | ret->tagger_date = 0; | 178 | ret->tagger_date = 0; |
303 | ret->msg = NULL; | 179 | ret->msg = NULL; |
304 | 180 | ||
305 | p = data; | 181 | p = data; |
306 | 182 | ||
307 | while (p && *p) { | 183 | while (p && *p) { |
308 | if (*p == '\n') | 184 | if (*p == '\n') |
309 | break; | 185 | break; |
310 | 186 | ||
311 | if (!strncmp(p, "tagger ", 7)) { | 187 | if (!strncmp(p, "tagger ", 7)) { |
312 | p += 7; | 188 | p += 7; |
313 | t = strchr(p, '<') - 1; | 189 | t = strchr(p, '<') - 1; |
314 | ret->tagger = substr(p, t); | 190 | ret->tagger = substr(p, t); |
315 | p = t; | 191 | p = t; |
316 | t = strchr(t, '>') + 1; | 192 | t = strchr(t, '>') + 1; |
317 | ret->tagger_email = substr(p, t); | 193 | ret->tagger_email = substr(p, t); |
318 | ret->tagger_date = atol(t+1); | 194 | ret->tagger_date = atol(t+1); |
319 | } | 195 | } |
320 | p = strchr(p, '\n') + 1; | 196 | p = strchr(p, '\n') + 1; |
321 | } | 197 | } |
322 | 198 | ||
323 | while (p && *p && (*p != '\n')) | 199 | while (p && *p && (*p != '\n')) |
324 | p = strchr(p, '\n') + 1; // skip unknown tag fields | 200 | p = strchr(p, '\n') + 1; // skip unknown tag fields |
325 | 201 | ||
326 | while (p && (*p == '\n')) | 202 | while (p && (*p == '\n')) |
327 | p = strchr(p, '\n') + 1; | 203 | p = strchr(p, '\n') + 1; |
328 | if (p && *p) | 204 | if (p && *p) |
329 | ret->msg = xstrdup(p); | 205 | ret->msg = xstrdup(p); |
330 | free(data); | 206 | free(data); |
331 | return ret; | 207 | return ret; |
332 | } | 208 | } |
@@ -1,521 +1,344 @@ | |||
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 cgit_repolist cgit_repolist; |
12 | struct repoinfo *cgit_repo; | 12 | struct cgit_context ctx; |
13 | int cgit_cmd; | 13 | int cgit_cmd; |
14 | 14 | ||
15 | const char *cgit_version = CGIT_VERSION; | ||
16 | |||
17 | char *cgit_root_title = "Git repository browser"; | ||
18 | char *cgit_css = "/cgit.css"; | ||
19 | char *cgit_logo = "/git-logo.png"; | ||
20 | char *cgit_index_header = NULL; | ||
21 | char *cgit_index_info = NULL; | ||
22 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; | ||
23 | char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; | ||
24 | char *cgit_agefile = "info/web/last-modified"; | ||
25 | char *cgit_virtual_root = NULL; | ||
26 | char *cgit_script_name = CGIT_SCRIPT_NAME; | ||
27 | char *cgit_cache_root = CGIT_CACHE_ROOT; | ||
28 | char *cgit_repo_group = NULL; | ||
29 | char *cgit_robots = "index, nofollow"; | ||
30 | char *cgit_clone_prefix = NULL; | ||
31 | |||
32 | int cgit_nocache = 0; | ||
33 | int cgit_snapshots = 0; | ||
34 | int cgit_enable_index_links = 0; | ||
35 | int cgit_enable_log_filecount = 0; | ||
36 | int cgit_enable_log_linecount = 0; | ||
37 | int cgit_max_lock_attempts = 5; | ||
38 | int cgit_cache_root_ttl = 5; | ||
39 | int cgit_cache_repo_ttl = 5; | ||
40 | int cgit_cache_dynamic_ttl = 5; | ||
41 | int cgit_cache_static_ttl = -1; | ||
42 | int cgit_cache_max_create_time = 5; | ||
43 | int cgit_summary_log = 0; | ||
44 | int cgit_summary_tags = 0; | ||
45 | int cgit_summary_branches = 0; | ||
46 | int cgit_renamelimit = -1; | ||
47 | |||
48 | int cgit_max_msg_len = 60; | ||
49 | int cgit_max_repodesc_len = 60; | ||
50 | int cgit_max_commit_count = 50; | ||
51 | |||
52 | int cgit_query_has_symref = 0; | ||
53 | int cgit_query_has_sha1 = 0; | ||
54 | |||
55 | char *cgit_querystring = NULL; | ||
56 | char *cgit_query_repo = NULL; | ||
57 | char *cgit_query_page = NULL; | ||
58 | char *cgit_query_head = NULL; | ||
59 | char *cgit_query_search = NULL; | ||
60 | char *cgit_query_grep = NULL; | ||
61 | char *cgit_query_sha1 = NULL; | ||
62 | char *cgit_query_sha2 = NULL; | ||
63 | char *cgit_query_path = NULL; | ||
64 | char *cgit_query_name = NULL; | ||
65 | int cgit_query_ofs = 0; | ||
66 | |||
67 | int htmlfd = 0; | ||
68 | |||
69 | |||
70 | int cgit_get_cmd_index(const char *cmd) | ||
71 | { | ||
72 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", | ||
73 | "snapshot", "tag", "refs", "patch", NULL}; | ||
74 | int i; | ||
75 | |||
76 | for(i = 0; cmds[i]; i++) | ||
77 | if (!strcmp(cmd, cmds[i])) | ||
78 | return i + 1; | ||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | int chk_zero(int result, char *msg) | 15 | int chk_zero(int result, char *msg) |
83 | { | 16 | { |
84 | if (result != 0) | 17 | if (result != 0) |
85 | die("%s: %s", msg, strerror(errno)); | 18 | die("%s: %s", msg, strerror(errno)); |
86 | return result; | 19 | return result; |
87 | } | 20 | } |
88 | 21 | ||
89 | int chk_positive(int result, char *msg) | 22 | int chk_positive(int result, char *msg) |
90 | { | 23 | { |
91 | if (result <= 0) | 24 | if (result <= 0) |
92 | die("%s: %s", msg, strerror(errno)); | 25 | die("%s: %s", msg, strerror(errno)); |
93 | return result; | 26 | return result; |
94 | } | 27 | } |
95 | 28 | ||
96 | int chk_non_negative(int result, char *msg) | 29 | int chk_non_negative(int result, char *msg) |
97 | { | 30 | { |
98 | if (result < 0) | 31 | if (result < 0) |
99 | die("%s: %s",msg, strerror(errno)); | 32 | die("%s: %s",msg, strerror(errno)); |
100 | return result; | 33 | return result; |
101 | } | 34 | } |
102 | 35 | ||
103 | struct repoinfo *add_repo(const char *url) | 36 | struct cgit_repo *cgit_add_repo(const char *url) |
104 | { | 37 | { |
105 | struct repoinfo *ret; | 38 | struct cgit_repo *ret; |
106 | 39 | ||
107 | if (++cgit_repolist.count > cgit_repolist.length) { | 40 | if (++cgit_repolist.count > cgit_repolist.length) { |
108 | if (cgit_repolist.length == 0) | 41 | if (cgit_repolist.length == 0) |
109 | cgit_repolist.length = 8; | 42 | cgit_repolist.length = 8; |
110 | else | 43 | else |
111 | cgit_repolist.length *= 2; | 44 | cgit_repolist.length *= 2; |
112 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 45 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
113 | cgit_repolist.length * | 46 | cgit_repolist.length * |
114 | sizeof(struct repoinfo)); | 47 | sizeof(struct cgit_repo)); |
115 | } | 48 | } |
116 | 49 | ||
117 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 50 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
118 | ret->url = trim_end(url, '/'); | 51 | ret->url = trim_end(url, '/'); |
119 | ret->name = ret->url; | 52 | ret->name = ret->url; |
120 | ret->path = NULL; | 53 | ret->path = NULL; |
121 | ret->desc = "[no description]"; | 54 | ret->desc = "[no description]"; |
122 | ret->owner = NULL; | 55 | ret->owner = NULL; |
123 | ret->group = cgit_repo_group; | 56 | ret->group = ctx.cfg.repo_group; |
124 | ret->defbranch = "master"; | 57 | ret->defbranch = "master"; |
125 | ret->snapshots = cgit_snapshots; | 58 | ret->snapshots = ctx.cfg.snapshots; |
126 | ret->enable_log_filecount = cgit_enable_log_filecount; | 59 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
127 | ret->enable_log_linecount = cgit_enable_log_linecount; | 60 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
128 | ret->module_link = cgit_module_link; | 61 | ret->module_link = ctx.cfg.module_link; |
129 | ret->readme = NULL; | 62 | ret->readme = NULL; |
130 | return ret; | 63 | return ret; |
131 | } | 64 | } |
132 | 65 | ||
133 | struct repoinfo *cgit_get_repoinfo(const char *url) | 66 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
134 | { | 67 | { |
135 | int i; | 68 | int i; |
136 | struct repoinfo *repo; | 69 | struct cgit_repo *repo; |
137 | 70 | ||
138 | for (i=0; i<cgit_repolist.count; i++) { | 71 | for (i=0; i<cgit_repolist.count; i++) { |
139 | repo = &cgit_repolist.repos[i]; | 72 | repo = &cgit_repolist.repos[i]; |
140 | if (!strcmp(repo->url, url)) | 73 | if (!strcmp(repo->url, url)) |
141 | return repo; | 74 | return repo; |
142 | } | 75 | } |
143 | return NULL; | 76 | return NULL; |
144 | } | 77 | } |
145 | 78 | ||
146 | void cgit_global_config_cb(const char *name, const char *value) | ||
147 | { | ||
148 | if (!strcmp(name, "root-title")) | ||
149 | cgit_root_title = xstrdup(value); | ||
150 | else if (!strcmp(name, "css")) | ||
151 | cgit_css = xstrdup(value); | ||
152 | else if (!strcmp(name, "logo")) | ||
153 | cgit_logo = xstrdup(value); | ||
154 | else if (!strcmp(name, "index-header")) | ||
155 | cgit_index_header = xstrdup(value); | ||
156 | else if (!strcmp(name, "index-info")) | ||
157 | cgit_index_info = xstrdup(value); | ||
158 | else if (!strcmp(name, "logo-link")) | ||
159 | cgit_logo_link = xstrdup(value); | ||
160 | else if (!strcmp(name, "module-link")) | ||
161 | cgit_module_link = xstrdup(value); | ||
162 | else if (!strcmp(name, "virtual-root")) { | ||
163 | cgit_virtual_root = trim_end(value, '/'); | ||
164 | if (!cgit_virtual_root && (!strcmp(value, "/"))) | ||
165 | cgit_virtual_root = ""; | ||
166 | } else if (!strcmp(name, "nocache")) | ||
167 | cgit_nocache = atoi(value); | ||
168 | else if (!strcmp(name, "snapshots")) | ||
169 | cgit_snapshots = cgit_parse_snapshots_mask(value); | ||
170 | else if (!strcmp(name, "enable-index-links")) | ||
171 | cgit_enable_index_links = atoi(value); | ||
172 | else if (!strcmp(name, "enable-log-filecount")) | ||
173 | cgit_enable_log_filecount = atoi(value); | ||
174 | else if (!strcmp(name, "enable-log-linecount")) | ||
175 | cgit_enable_log_linecount = atoi(value); | ||
176 | else if (!strcmp(name, "cache-root")) | ||
177 | cgit_cache_root = xstrdup(value); | ||
178 | else if (!strcmp(name, "cache-root-ttl")) | ||
179 | cgit_cache_root_ttl = atoi(value); | ||
180 | else if (!strcmp(name, "cache-repo-ttl")) | ||
181 | cgit_cache_repo_ttl = atoi(value); | ||
182 | else if (!strcmp(name, "cache-static-ttl")) | ||
183 | cgit_cache_static_ttl = atoi(value); | ||
184 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
185 | cgit_cache_dynamic_ttl = atoi(value); | ||
186 | else if (!strcmp(name, "max-message-length")) | ||
187 | cgit_max_msg_len = atoi(value); | ||
188 | else if (!strcmp(name, "max-repodesc-length")) | ||
189 | cgit_max_repodesc_len = atoi(value); | ||
190 | else if (!strcmp(name, "max-commit-count")) | ||
191 | cgit_max_commit_count = atoi(value); | ||
192 | else if (!strcmp(name, "summary-log")) | ||
193 | cgit_summary_log = atoi(value); | ||
194 | else if (!strcmp(name, "summary-branches")) | ||
195 | cgit_summary_branches = atoi(value); | ||
196 | else if (!strcmp(name, "summary-tags")) | ||
197 | cgit_summary_tags = atoi(value); | ||
198 | else if (!strcmp(name, "agefile")) | ||
199 | cgit_agefile = xstrdup(value); | ||
200 | else if (!strcmp(name, "renamelimit")) | ||
201 | cgit_renamelimit = atoi(value); | ||
202 | else if (!strcmp(name, "robots")) | ||
203 | cgit_robots = xstrdup(value); | ||
204 | else if (!strcmp(name, "clone-prefix")) | ||
205 | cgit_clone_prefix = xstrdup(value); | ||
206 | else if (!strcmp(name, "repo.group")) | ||
207 | cgit_repo_group = xstrdup(value); | ||
208 | else if (!strcmp(name, "repo.url")) | ||
209 | cgit_repo = add_repo(value); | ||
210 | else if (!strcmp(name, "repo.name")) | ||
211 | cgit_repo->name = xstrdup(value); | ||
212 | else if (cgit_repo && !strcmp(name, "repo.path")) | ||
213 | cgit_repo->path = trim_end(value, '/'); | ||
214 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) | ||
215 | cgit_repo->clone_url = xstrdup(value); | ||
216 | else if (cgit_repo && !strcmp(name, "repo.desc")) | ||
217 | cgit_repo->desc = xstrdup(value); | ||
218 | else if (cgit_repo && !strcmp(name, "repo.owner")) | ||
219 | cgit_repo->owner = xstrdup(value); | ||
220 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) | ||
221 | cgit_repo->defbranch = xstrdup(value); | ||
222 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) | ||
223 | cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
224 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) | ||
225 | cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); | ||
226 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) | ||
227 | cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); | ||
228 | else if (cgit_repo && !strcmp(name, "repo.module-link")) | ||
229 | cgit_repo->module_link= xstrdup(value); | ||
230 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
231 | if (*value == '/') | ||
232 | cgit_repo->readme = xstrdup(value); | ||
233 | else | ||
234 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); | ||
235 | } else if (!strcmp(name, "include")) | ||
236 | cgit_read_config(value, cgit_global_config_cb); | ||
237 | } | ||
238 | |||
239 | void cgit_querystring_cb(const char *name, const char *value) | ||
240 | { | ||
241 | if (!strcmp(name,"r")) { | ||
242 | cgit_query_repo = xstrdup(value); | ||
243 | cgit_repo = cgit_get_repoinfo(value); | ||
244 | } else if (!strcmp(name, "p")) { | ||
245 | cgit_query_page = xstrdup(value); | ||
246 | cgit_cmd = cgit_get_cmd_index(value); | ||
247 | } else if (!strcmp(name, "url")) { | ||
248 | cgit_parse_url(value); | ||
249 | } else if (!strcmp(name, "qt")) { | ||
250 | cgit_query_grep = xstrdup(value); | ||
251 | } else if (!strcmp(name, "q")) { | ||
252 | cgit_query_search = xstrdup(value); | ||
253 | } else if (!strcmp(name, "h")) { | ||
254 | cgit_query_head = xstrdup(value); | ||
255 | cgit_query_has_symref = 1; | ||
256 | } else if (!strcmp(name, "id")) { | ||
257 | cgit_query_sha1 = xstrdup(value); | ||
258 | cgit_query_has_sha1 = 1; | ||
259 | } else if (!strcmp(name, "id2")) { | ||
260 | cgit_query_sha2 = xstrdup(value); | ||
261 | cgit_query_has_sha1 = 1; | ||
262 | } else if (!strcmp(name, "ofs")) { | ||
263 | cgit_query_ofs = atoi(value); | ||
264 | } else if (!strcmp(name, "path")) { | ||
265 | cgit_query_path = trim_end(value, '/'); | ||
266 | } else if (!strcmp(name, "name")) { | ||
267 | cgit_query_name = xstrdup(value); | ||
268 | } | ||
269 | } | ||
270 | |||
271 | void *cgit_free_commitinfo(struct commitinfo *info) | 79 | void *cgit_free_commitinfo(struct commitinfo *info) |
272 | { | 80 | { |
273 | free(info->author); | 81 | free(info->author); |
274 | free(info->author_email); | 82 | free(info->author_email); |
275 | free(info->committer); | 83 | free(info->committer); |
276 | free(info->committer_email); | 84 | free(info->committer_email); |
277 | free(info->subject); | 85 | free(info->subject); |
278 | free(info->msg); | 86 | free(info->msg); |
279 | free(info->msg_encoding); | 87 | free(info->msg_encoding); |
280 | free(info); | 88 | free(info); |
281 | return NULL; | 89 | return NULL; |
282 | } | 90 | } |
283 | 91 | ||
284 | int hextoint(char c) | ||
285 | { | ||
286 | if (c >= 'a' && c <= 'f') | ||
287 | return 10 + c - 'a'; | ||
288 | else if (c >= 'A' && c <= 'F') | ||
289 | return 10 + c - 'A'; | ||
290 | else if (c >= '0' && c <= '9') | ||
291 | return c - '0'; | ||
292 | else | ||
293 | return -1; | ||
294 | } | ||
295 | |||
296 | char *trim_end(const char *str, char c) | 92 | char *trim_end(const char *str, char c) |
297 | { | 93 | { |
298 | int len; | 94 | int len; |
299 | char *s, *t; | 95 | char *s, *t; |
300 | 96 | ||
301 | if (str == NULL) | 97 | if (str == NULL) |
302 | return NULL; | 98 | return NULL; |
303 | t = (char *)str; | 99 | t = (char *)str; |
304 | len = strlen(t); | 100 | len = strlen(t); |
305 | while(len > 0 && t[len - 1] == c) | 101 | while(len > 0 && t[len - 1] == c) |
306 | len--; | 102 | len--; |
307 | 103 | ||
308 | if (len == 0) | 104 | if (len == 0) |
309 | return NULL; | 105 | return NULL; |
310 | 106 | ||
311 | c = t[len]; | 107 | c = t[len]; |
312 | t[len] = '\0'; | 108 | t[len] = '\0'; |
313 | s = xstrdup(t); | 109 | s = xstrdup(t); |
314 | t[len] = c; | 110 | t[len] = c; |
315 | return s; | 111 | return s; |
316 | } | 112 | } |
317 | 113 | ||
318 | char *strlpart(char *txt, int maxlen) | 114 | char *strlpart(char *txt, int maxlen) |
319 | { | 115 | { |
320 | char *result; | 116 | char *result; |
321 | 117 | ||
322 | if (!txt) | 118 | if (!txt) |
323 | return txt; | 119 | return txt; |
324 | 120 | ||
325 | if (strlen(txt) <= maxlen) | 121 | if (strlen(txt) <= maxlen) |
326 | return txt; | 122 | return txt; |
327 | result = xmalloc(maxlen + 1); | 123 | result = xmalloc(maxlen + 1); |
328 | memcpy(result, txt, maxlen - 3); | 124 | memcpy(result, txt, maxlen - 3); |
329 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 125 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
330 | result[maxlen] = '\0'; | 126 | result[maxlen] = '\0'; |
331 | return result; | 127 | return result; |
332 | } | 128 | } |
333 | 129 | ||
334 | char *strrpart(char *txt, int maxlen) | 130 | char *strrpart(char *txt, int maxlen) |
335 | { | 131 | { |
336 | char *result; | 132 | char *result; |
337 | 133 | ||
338 | if (!txt) | 134 | if (!txt) |
339 | return txt; | 135 | return txt; |
340 | 136 | ||
341 | if (strlen(txt) <= maxlen) | 137 | if (strlen(txt) <= maxlen) |
342 | return txt; | 138 | return txt; |
343 | result = xmalloc(maxlen + 1); | 139 | result = xmalloc(maxlen + 1); |
344 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); | 140 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
345 | result[0] = result[1] = result[2] = '.'; | 141 | result[0] = result[1] = result[2] = '.'; |
346 | return result; | 142 | return result; |
347 | } | 143 | } |
348 | 144 | ||
349 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) | 145 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
350 | { | 146 | { |
351 | size_t size; | 147 | size_t size; |
352 | 148 | ||
353 | if (list->count >= list->alloc) { | 149 | if (list->count >= list->alloc) { |
354 | list->alloc += (list->alloc ? list->alloc : 4); | 150 | list->alloc += (list->alloc ? list->alloc : 4); |
355 | size = list->alloc * sizeof(struct refinfo *); | 151 | size = list->alloc * sizeof(struct refinfo *); |
356 | list->refs = xrealloc(list->refs, size); | 152 | list->refs = xrealloc(list->refs, size); |
357 | } | 153 | } |
358 | list->refs[list->count++] = ref; | 154 | list->refs[list->count++] = ref; |
359 | } | 155 | } |
360 | 156 | ||
361 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) | 157 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
362 | { | 158 | { |
363 | struct refinfo *ref; | 159 | struct refinfo *ref; |
364 | 160 | ||
365 | ref = xmalloc(sizeof (struct refinfo)); | 161 | ref = xmalloc(sizeof (struct refinfo)); |
366 | ref->refname = xstrdup(refname); | 162 | ref->refname = xstrdup(refname); |
367 | ref->object = parse_object(sha1); | 163 | ref->object = parse_object(sha1); |
368 | switch (ref->object->type) { | 164 | switch (ref->object->type) { |
369 | case OBJ_TAG: | 165 | case OBJ_TAG: |
370 | ref->tag = cgit_parse_tag((struct tag *)ref->object); | 166 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
371 | break; | 167 | break; |
372 | case OBJ_COMMIT: | 168 | case OBJ_COMMIT: |
373 | ref->commit = cgit_parse_commit((struct commit *)ref->object); | 169 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
374 | break; | 170 | break; |
375 | } | 171 | } |
376 | return ref; | 172 | return ref; |
377 | } | 173 | } |
378 | 174 | ||
379 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, | 175 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
380 | void *cb_data) | 176 | void *cb_data) |
381 | { | 177 | { |
382 | struct reflist *list = (struct reflist *)cb_data; | 178 | struct reflist *list = (struct reflist *)cb_data; |
383 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); | 179 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
384 | 180 | ||
385 | if (info) | 181 | if (info) |
386 | cgit_add_ref(list, info); | 182 | cgit_add_ref(list, info); |
387 | return 0; | 183 | return 0; |
388 | } | 184 | } |
389 | 185 | ||
390 | void cgit_diff_tree_cb(struct diff_queue_struct *q, | 186 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
391 | struct diff_options *options, void *data) | 187 | struct diff_options *options, void *data) |
392 | { | 188 | { |
393 | int i; | 189 | int i; |
394 | 190 | ||
395 | for (i = 0; i < q->nr; i++) { | 191 | for (i = 0; i < q->nr; i++) { |
396 | if (q->queue[i]->status == 'U') | 192 | if (q->queue[i]->status == 'U') |
397 | continue; | 193 | continue; |
398 | ((filepair_fn)data)(q->queue[i]); | 194 | ((filepair_fn)data)(q->queue[i]); |
399 | } | 195 | } |
400 | } | 196 | } |
401 | 197 | ||
402 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) | 198 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
403 | { | 199 | { |
404 | enum object_type type; | 200 | enum object_type type; |
405 | 201 | ||
406 | if (is_null_sha1(sha1)) { | 202 | if (is_null_sha1(sha1)) { |
407 | file->ptr = (char *)""; | 203 | file->ptr = (char *)""; |
408 | file->size = 0; | 204 | file->size = 0; |
409 | } else { | 205 | } else { |
410 | file->ptr = read_sha1_file(sha1, &type, | 206 | file->ptr = read_sha1_file(sha1, &type, |
411 | (unsigned long *)&file->size); | 207 | (unsigned long *)&file->size); |
412 | } | 208 | } |
413 | return 1; | 209 | return 1; |
414 | } | 210 | } |
415 | 211 | ||
416 | /* | 212 | /* |
417 | * Receive diff-buffers from xdiff and concatenate them as | 213 | * Receive diff-buffers from xdiff and concatenate them as |
418 | * needed across multiple callbacks. | 214 | * needed across multiple callbacks. |
419 | * | 215 | * |
420 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), | 216 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
421 | * ripped from git and modified to use globals instead of | 217 | * ripped from git and modified to use globals instead of |
422 | * a special callback-struct. | 218 | * a special callback-struct. |
423 | */ | 219 | */ |
424 | char *diffbuf = NULL; | 220 | char *diffbuf = NULL; |
425 | int buflen = 0; | 221 | int buflen = 0; |
426 | 222 | ||
427 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) | 223 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
428 | { | 224 | { |
429 | int i; | 225 | int i; |
430 | 226 | ||
431 | for (i = 0; i < nbuf; i++) { | 227 | for (i = 0; i < nbuf; i++) { |
432 | if (mb[i].ptr[mb[i].size-1] != '\n') { | 228 | if (mb[i].ptr[mb[i].size-1] != '\n') { |
433 | /* Incomplete line */ | 229 | /* Incomplete line */ |
434 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 230 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
435 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 231 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
436 | buflen += mb[i].size; | 232 | buflen += mb[i].size; |
437 | continue; | 233 | continue; |
438 | } | 234 | } |
439 | 235 | ||
440 | /* we have a complete line */ | 236 | /* we have a complete line */ |
441 | if (!diffbuf) { | 237 | if (!diffbuf) { |
442 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); | 238 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); |
443 | continue; | 239 | continue; |
444 | } | 240 | } |
445 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 241 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
446 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 242 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
447 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); | 243 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); |
448 | free(diffbuf); | 244 | free(diffbuf); |
449 | diffbuf = NULL; | 245 | diffbuf = NULL; |
450 | buflen = 0; | 246 | buflen = 0; |
451 | } | 247 | } |
452 | if (diffbuf) { | 248 | if (diffbuf) { |
453 | ((linediff_fn)priv)(diffbuf, buflen); | 249 | ((linediff_fn)priv)(diffbuf, buflen); |
454 | free(diffbuf); | 250 | free(diffbuf); |
455 | diffbuf = NULL; | 251 | diffbuf = NULL; |
456 | buflen = 0; | 252 | buflen = 0; |
457 | } | 253 | } |
458 | return 0; | 254 | return 0; |
459 | } | 255 | } |
460 | 256 | ||
461 | int cgit_diff_files(const unsigned char *old_sha1, | 257 | int cgit_diff_files(const unsigned char *old_sha1, |
462 | const unsigned char *new_sha1, | 258 | const unsigned char *new_sha1, |
463 | linediff_fn fn) | 259 | linediff_fn fn) |
464 | { | 260 | { |
465 | mmfile_t file1, file2; | 261 | mmfile_t file1, file2; |
466 | xpparam_t diff_params; | 262 | xpparam_t diff_params; |
467 | xdemitconf_t emit_params; | 263 | xdemitconf_t emit_params; |
468 | xdemitcb_t emit_cb; | 264 | xdemitcb_t emit_cb; |
469 | 265 | ||
470 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) | 266 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) |
471 | return 1; | 267 | return 1; |
472 | 268 | ||
473 | diff_params.flags = XDF_NEED_MINIMAL; | 269 | diff_params.flags = XDF_NEED_MINIMAL; |
474 | emit_params.ctxlen = 3; | 270 | emit_params.ctxlen = 3; |
475 | emit_params.flags = XDL_EMIT_FUNCNAMES; | 271 | emit_params.flags = XDL_EMIT_FUNCNAMES; |
476 | emit_params.find_func = NULL; | 272 | emit_params.find_func = NULL; |
477 | emit_cb.outf = filediff_cb; | 273 | emit_cb.outf = filediff_cb; |
478 | emit_cb.priv = fn; | 274 | emit_cb.priv = fn; |
479 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); | 275 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); |
480 | return 0; | 276 | return 0; |
481 | } | 277 | } |
482 | 278 | ||
483 | void cgit_diff_tree(const unsigned char *old_sha1, | 279 | void cgit_diff_tree(const unsigned char *old_sha1, |
484 | const unsigned char *new_sha1, | 280 | const unsigned char *new_sha1, |
485 | filepair_fn fn, const char *prefix) | 281 | filepair_fn fn, const char *prefix) |
486 | { | 282 | { |
487 | struct diff_options opt; | 283 | struct diff_options opt; |
488 | int ret; | 284 | int ret; |
489 | int prefixlen; | 285 | int prefixlen; |
490 | 286 | ||
491 | diff_setup(&opt); | 287 | diff_setup(&opt); |
492 | opt.output_format = DIFF_FORMAT_CALLBACK; | 288 | opt.output_format = DIFF_FORMAT_CALLBACK; |
493 | opt.detect_rename = 1; | 289 | opt.detect_rename = 1; |
494 | opt.rename_limit = cgit_renamelimit; | 290 | opt.rename_limit = ctx.cfg.renamelimit; |
495 | DIFF_OPT_SET(&opt, RECURSIVE); | 291 | DIFF_OPT_SET(&opt, RECURSIVE); |
496 | opt.format_callback = cgit_diff_tree_cb; | 292 | opt.format_callback = cgit_diff_tree_cb; |
497 | opt.format_callback_data = fn; | 293 | opt.format_callback_data = fn; |
498 | if (prefix) { | 294 | if (prefix) { |
499 | opt.nr_paths = 1; | 295 | opt.nr_paths = 1; |
500 | opt.paths = &prefix; | 296 | opt.paths = &prefix; |
501 | prefixlen = strlen(prefix); | 297 | prefixlen = strlen(prefix); |
502 | opt.pathlens = &prefixlen; | 298 | opt.pathlens = &prefixlen; |
503 | } | 299 | } |
504 | diff_setup_done(&opt); | 300 | diff_setup_done(&opt); |
505 | 301 | ||
506 | if (old_sha1 && !is_null_sha1(old_sha1)) | 302 | if (old_sha1 && !is_null_sha1(old_sha1)) |
507 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); | 303 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); |
508 | else | 304 | else |
509 | ret = diff_root_tree_sha1(new_sha1, "", &opt); | 305 | ret = diff_root_tree_sha1(new_sha1, "", &opt); |
510 | diffcore_std(&opt); | 306 | diffcore_std(&opt); |
511 | diff_flush(&opt); | 307 | diff_flush(&opt); |
512 | } | 308 | } |
513 | 309 | ||
514 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) | 310 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) |
515 | { | 311 | { |
516 | unsigned char *old_sha1 = NULL; | 312 | unsigned char *old_sha1 = NULL; |
517 | 313 | ||
518 | if (commit->parents) | 314 | if (commit->parents) |
519 | old_sha1 = commit->parents->item->object.sha1; | 315 | old_sha1 = commit->parents->item->object.sha1; |
520 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); | 316 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); |
521 | } | 317 | } |
318 | |||
319 | int cgit_parse_snapshots_mask(const char *str) | ||
320 | { | ||
321 | const struct cgit_snapshot_format *f; | ||
322 | static const char *delim = " \t,:/|;"; | ||
323 | int tl, sl, rv = 0; | ||
324 | |||
325 | /* favor legacy setting */ | ||
326 | if(atoi(str)) | ||
327 | return 1; | ||
328 | for(;;) { | ||
329 | str += strspn(str,delim); | ||
330 | tl = strcspn(str,delim); | ||
331 | if (!tl) | ||
332 | break; | ||
333 | for (f = cgit_snapshot_formats; f->suffix; f++) { | ||
334 | sl = strlen(f->suffix); | ||
335 | if((tl == sl && !strncmp(f->suffix, str, tl)) || | ||
336 | (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { | ||
337 | rv |= f->bit; | ||
338 | break; | ||
339 | } | ||
340 | } | ||
341 | str += tl; | ||
342 | } | ||
343 | return rv; | ||
344 | } | ||
@@ -1,31 +1,43 @@ | |||
1 | /* ui-blob.c: show blob content | ||
2 | * | ||
3 | * Copyright (C) 2008 Lars Hjemli | ||
4 | * | ||
5 | * Licensed under GNU General Public License v2 | ||
6 | * (see COPYING for full license text) | ||
7 | */ | ||
8 | |||
1 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "html.h" | ||
11 | #include "ui-shared.h" | ||
2 | 12 | ||
3 | void cgit_print_blob(struct cacheitem *item, const char *hex, char *path) | 13 | void cgit_print_blob(const char *hex, char *path) |
4 | { | 14 | { |
5 | 15 | ||
6 | unsigned char sha1[20]; | 16 | unsigned char sha1[20]; |
7 | enum object_type type; | 17 | enum object_type type; |
8 | unsigned char *buf; | 18 | unsigned char *buf; |
9 | unsigned long size; | 19 | unsigned long size; |
10 | 20 | ||
11 | if (get_sha1_hex(hex, sha1)){ | 21 | if (get_sha1_hex(hex, sha1)){ |
12 | cgit_print_error(fmt("Bad hex value: %s", hex)); | 22 | cgit_print_error(fmt("Bad hex value: %s", hex)); |
13 | return; | 23 | return; |
14 | } | 24 | } |
15 | 25 | ||
16 | type = sha1_object_info(sha1, &size); | 26 | type = sha1_object_info(sha1, &size); |
17 | if (type == OBJ_BAD) { | 27 | if (type == OBJ_BAD) { |
18 | cgit_print_error(fmt("Bad object name: %s", hex)); | 28 | cgit_print_error(fmt("Bad object name: %s", hex)); |
19 | return; | 29 | return; |
20 | } | 30 | } |
21 | 31 | ||
22 | buf = read_sha1_file(sha1, &type, &size); | 32 | buf = read_sha1_file(sha1, &type, &size); |
23 | if (!buf) { | 33 | if (!buf) { |
24 | cgit_print_error(fmt("Error reading object %s", hex)); | 34 | cgit_print_error(fmt("Error reading object %s", hex)); |
25 | return; | 35 | return; |
26 | } | 36 | } |
27 | 37 | ||
28 | buf[size] = '\0'; | 38 | buf[size] = '\0'; |
29 | cgit_print_snapshot_start("text/plain", path, item); | 39 | ctx.page.mimetype = "text/plain"; |
40 | ctx.page.filename = path; | ||
41 | cgit_print_http_headers(&ctx); | ||
30 | write(htmlfd, buf, size); | 42 | write(htmlfd, buf, size); |
31 | } | 43 | } |
diff --git a/ui-blob.h b/ui-blob.h new file mode 100644 index 0000000..5a920a8 --- a/dev/null +++ b/ui-blob.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_BLOB_H | ||
2 | #define UI_BLOB_H | ||
3 | |||
4 | extern void cgit_print_blob(const char *hex, char *path); | ||
5 | |||
6 | #endif /* UI_BLOB_H */ | ||
diff --git a/ui-commit.c b/ui-commit.c index bd55a33..8019e36 100644 --- a/ui-commit.c +++ b/ui-commit.c | |||
@@ -1,226 +1,228 @@ | |||
1 | /* ui-commit.c: generate commit view | 1 | /* ui-commit.c: generate commit view |
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 "html.h" | ||
11 | #include "ui-shared.h" | ||
10 | 12 | ||
11 | static int files, slots; | 13 | static int files, slots; |
12 | static int total_adds, total_rems, max_changes; | 14 | static int total_adds, total_rems, max_changes; |
13 | static int lines_added, lines_removed; | 15 | static int lines_added, lines_removed; |
14 | static char *curr_rev; | 16 | static char *curr_rev; |
15 | 17 | ||
16 | static struct fileinfo { | 18 | static struct fileinfo { |
17 | char status; | 19 | char status; |
18 | unsigned char old_sha1[20]; | 20 | unsigned char old_sha1[20]; |
19 | unsigned char new_sha1[20]; | 21 | unsigned char new_sha1[20]; |
20 | unsigned short old_mode; | 22 | unsigned short old_mode; |
21 | unsigned short new_mode; | 23 | unsigned short new_mode; |
22 | char *old_path; | 24 | char *old_path; |
23 | char *new_path; | 25 | char *new_path; |
24 | unsigned int added; | 26 | unsigned int added; |
25 | unsigned int removed; | 27 | unsigned int removed; |
26 | } *items; | 28 | } *items; |
27 | 29 | ||
28 | 30 | ||
29 | void print_fileinfo(struct fileinfo *info) | 31 | void print_fileinfo(struct fileinfo *info) |
30 | { | 32 | { |
31 | char *class; | 33 | char *class; |
32 | 34 | ||
33 | switch (info->status) { | 35 | switch (info->status) { |
34 | case DIFF_STATUS_ADDED: | 36 | case DIFF_STATUS_ADDED: |
35 | class = "add"; | 37 | class = "add"; |
36 | break; | 38 | break; |
37 | case DIFF_STATUS_COPIED: | 39 | case DIFF_STATUS_COPIED: |
38 | class = "cpy"; | 40 | class = "cpy"; |
39 | break; | 41 | break; |
40 | case DIFF_STATUS_DELETED: | 42 | case DIFF_STATUS_DELETED: |
41 | class = "del"; | 43 | class = "del"; |
42 | break; | 44 | break; |
43 | case DIFF_STATUS_MODIFIED: | 45 | case DIFF_STATUS_MODIFIED: |
44 | class = "upd"; | 46 | class = "upd"; |
45 | break; | 47 | break; |
46 | case DIFF_STATUS_RENAMED: | 48 | case DIFF_STATUS_RENAMED: |
47 | class = "mov"; | 49 | class = "mov"; |
48 | break; | 50 | break; |
49 | case DIFF_STATUS_TYPE_CHANGED: | 51 | case DIFF_STATUS_TYPE_CHANGED: |
50 | class = "typ"; | 52 | class = "typ"; |
51 | break; | 53 | break; |
52 | case DIFF_STATUS_UNKNOWN: | 54 | case DIFF_STATUS_UNKNOWN: |
53 | class = "unk"; | 55 | class = "unk"; |
54 | break; | 56 | break; |
55 | case DIFF_STATUS_UNMERGED: | 57 | case DIFF_STATUS_UNMERGED: |
56 | class = "stg"; | 58 | class = "stg"; |
57 | break; | 59 | break; |
58 | default: | 60 | default: |
59 | die("bug: unhandled diff status %c", info->status); | 61 | die("bug: unhandled diff status %c", info->status); |
60 | } | 62 | } |
61 | 63 | ||
62 | html("<tr>"); | 64 | html("<tr>"); |
63 | htmlf("<td class='mode'>"); | 65 | htmlf("<td class='mode'>"); |
64 | if (is_null_sha1(info->new_sha1)) { | 66 | if (is_null_sha1(info->new_sha1)) { |
65 | html_filemode(info->old_mode); | 67 | cgit_print_filemode(info->old_mode); |
66 | } else { | 68 | } else { |
67 | html_filemode(info->new_mode); | 69 | cgit_print_filemode(info->new_mode); |
68 | } | 70 | } |
69 | 71 | ||
70 | if (info->old_mode != info->new_mode && | 72 | if (info->old_mode != info->new_mode && |
71 | !is_null_sha1(info->old_sha1) && | 73 | !is_null_sha1(info->old_sha1) && |
72 | !is_null_sha1(info->new_sha1)) { | 74 | !is_null_sha1(info->new_sha1)) { |
73 | html("<span class='modechange'>["); | 75 | html("<span class='modechange'>["); |
74 | html_filemode(info->old_mode); | 76 | cgit_print_filemode(info->old_mode); |
75 | html("]</span>"); | 77 | html("]</span>"); |
76 | } | 78 | } |
77 | htmlf("</td><td class='%s'>", class); | 79 | htmlf("</td><td class='%s'>", class); |
78 | cgit_diff_link(info->new_path, NULL, NULL, cgit_query_head, curr_rev, | 80 | cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, curr_rev, |
79 | NULL, info->new_path); | 81 | NULL, info->new_path); |
80 | if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) | 82 | if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) |
81 | htmlf(" (%s from %s)", | 83 | htmlf(" (%s from %s)", |
82 | info->status == DIFF_STATUS_COPIED ? "copied" : "renamed", | 84 | info->status == DIFF_STATUS_COPIED ? "copied" : "renamed", |
83 | info->old_path); | 85 | info->old_path); |
84 | html("</td><td class='right'>"); | 86 | html("</td><td class='right'>"); |
85 | htmlf("%d", info->added + info->removed); | 87 | htmlf("%d", info->added + info->removed); |
86 | html("</td><td class='graph'>"); | 88 | html("</td><td class='graph'>"); |
87 | htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes)); | 89 | htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes)); |
88 | htmlf("<td class='add' style='width: %.1f%%;'/>", | 90 | htmlf("<td class='add' style='width: %.1f%%;'/>", |
89 | info->added * 100.0 / max_changes); | 91 | info->added * 100.0 / max_changes); |
90 | htmlf("<td class='rem' style='width: %.1f%%;'/>", | 92 | htmlf("<td class='rem' style='width: %.1f%%;'/>", |
91 | info->removed * 100.0 / max_changes); | 93 | info->removed * 100.0 / max_changes); |
92 | htmlf("<td class='none' style='width: %.1f%%;'/>", | 94 | htmlf("<td class='none' style='width: %.1f%%;'/>", |
93 | (max_changes - info->removed - info->added) * 100.0 / max_changes); | 95 | (max_changes - info->removed - info->added) * 100.0 / max_changes); |
94 | html("</tr></table></td></tr>\n"); | 96 | html("</tr></table></td></tr>\n"); |
95 | } | 97 | } |
96 | 98 | ||
97 | void cgit_count_diff_lines(char *line, int len) | 99 | void cgit_count_diff_lines(char *line, int len) |
98 | { | 100 | { |
99 | if (line && (len > 0)) { | 101 | if (line && (len > 0)) { |
100 | if (line[0] == '+') | 102 | if (line[0] == '+') |
101 | lines_added++; | 103 | lines_added++; |
102 | else if (line[0] == '-') | 104 | else if (line[0] == '-') |
103 | lines_removed++; | 105 | lines_removed++; |
104 | } | 106 | } |
105 | } | 107 | } |
106 | 108 | ||
107 | void inspect_filepair(struct diff_filepair *pair) | 109 | void inspect_filepair(struct diff_filepair *pair) |
108 | { | 110 | { |
109 | files++; | 111 | files++; |
110 | lines_added = 0; | 112 | lines_added = 0; |
111 | lines_removed = 0; | 113 | lines_removed = 0; |
112 | cgit_diff_files(pair->one->sha1, pair->two->sha1, cgit_count_diff_lines); | 114 | cgit_diff_files(pair->one->sha1, pair->two->sha1, cgit_count_diff_lines); |
113 | if (files >= slots) { | 115 | if (files >= slots) { |
114 | if (slots == 0) | 116 | if (slots == 0) |
115 | slots = 4; | 117 | slots = 4; |
116 | else | 118 | else |
117 | slots = slots * 2; | 119 | slots = slots * 2; |
118 | items = xrealloc(items, slots * sizeof(struct fileinfo)); | 120 | items = xrealloc(items, slots * sizeof(struct fileinfo)); |
119 | } | 121 | } |
120 | items[files-1].status = pair->status; | 122 | items[files-1].status = pair->status; |
121 | hashcpy(items[files-1].old_sha1, pair->one->sha1); | 123 | hashcpy(items[files-1].old_sha1, pair->one->sha1); |
122 | hashcpy(items[files-1].new_sha1, pair->two->sha1); | 124 | hashcpy(items[files-1].new_sha1, pair->two->sha1); |
123 | items[files-1].old_mode = pair->one->mode; | 125 | items[files-1].old_mode = pair->one->mode; |
124 | items[files-1].new_mode = pair->two->mode; | 126 | items[files-1].new_mode = pair->two->mode; |
125 | items[files-1].old_path = xstrdup(pair->one->path); | 127 | items[files-1].old_path = xstrdup(pair->one->path); |
126 | items[files-1].new_path = xstrdup(pair->two->path); | 128 | items[files-1].new_path = xstrdup(pair->two->path); |
127 | items[files-1].added = lines_added; | 129 | items[files-1].added = lines_added; |
128 | items[files-1].removed = lines_removed; | 130 | items[files-1].removed = lines_removed; |
129 | if (lines_added + lines_removed > max_changes) | 131 | if (lines_added + lines_removed > max_changes) |
130 | max_changes = lines_added + lines_removed; | 132 | max_changes = lines_added + lines_removed; |
131 | total_adds += lines_added; | 133 | total_adds += lines_added; |
132 | total_rems += lines_removed; | 134 | total_rems += lines_removed; |
133 | } | 135 | } |
134 | 136 | ||
135 | 137 | ||
136 | void cgit_print_commit(char *hex) | 138 | void cgit_print_commit(char *hex) |
137 | { | 139 | { |
138 | struct commit *commit, *parent; | 140 | struct commit *commit, *parent; |
139 | struct commitinfo *info; | 141 | struct commitinfo *info; |
140 | struct commit_list *p; | 142 | struct commit_list *p; |
141 | unsigned char sha1[20]; | 143 | unsigned char sha1[20]; |
142 | char *tmp; | 144 | char *tmp; |
143 | int i; | 145 | int i; |
144 | 146 | ||
145 | if (!hex) | 147 | if (!hex) |
146 | hex = cgit_query_head; | 148 | hex = ctx.qry.head; |
147 | curr_rev = hex; | 149 | curr_rev = hex; |
148 | 150 | ||
149 | if (get_sha1(hex, sha1)) { | 151 | if (get_sha1(hex, sha1)) { |
150 | cgit_print_error(fmt("Bad object id: %s", hex)); | 152 | cgit_print_error(fmt("Bad object id: %s", hex)); |
151 | return; | 153 | return; |
152 | } | 154 | } |
153 | commit = lookup_commit_reference(sha1); | 155 | commit = lookup_commit_reference(sha1); |
154 | if (!commit) { | 156 | if (!commit) { |
155 | cgit_print_error(fmt("Bad commit reference: %s", hex)); | 157 | cgit_print_error(fmt("Bad commit reference: %s", hex)); |
156 | return; | 158 | return; |
157 | } | 159 | } |
158 | info = cgit_parse_commit(commit); | 160 | info = cgit_parse_commit(commit); |
159 | 161 | ||
160 | html("<table summary='commit info' class='commit-info'>\n"); | 162 | html("<table summary='commit info' class='commit-info'>\n"); |
161 | html("<tr><th>author</th><td>"); | 163 | html("<tr><th>author</th><td>"); |
162 | html_txt(info->author); | 164 | html_txt(info->author); |
163 | html(" "); | 165 | html(" "); |
164 | html_txt(info->author_email); | 166 | html_txt(info->author_email); |
165 | html("</td><td class='right'>"); | 167 | html("</td><td class='right'>"); |
166 | cgit_print_date(info->author_date, FMT_LONGDATE); | 168 | cgit_print_date(info->author_date, FMT_LONGDATE); |
167 | html("</td></tr>\n"); | 169 | html("</td></tr>\n"); |
168 | html("<tr><th>committer</th><td>"); | 170 | html("<tr><th>committer</th><td>"); |
169 | html_txt(info->committer); | 171 | html_txt(info->committer); |
170 | html(" "); | 172 | html(" "); |
171 | html_txt(info->committer_email); | 173 | html_txt(info->committer_email); |
172 | html("</td><td class='right'>"); | 174 | html("</td><td class='right'>"); |
173 | cgit_print_date(info->committer_date, FMT_LONGDATE); | 175 | cgit_print_date(info->committer_date, FMT_LONGDATE); |
174 | html("</td></tr>\n"); | 176 | html("</td></tr>\n"); |
175 | html("<tr><th>tree</th><td colspan='2' class='sha1'>"); | 177 | html("<tr><th>tree</th><td colspan='2' class='sha1'>"); |
176 | tmp = xstrdup(hex); | 178 | tmp = xstrdup(hex); |
177 | cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL, | 179 | cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL, |
178 | cgit_query_head, tmp, NULL); | 180 | ctx.qry.head, tmp, NULL); |
179 | html("</td></tr>\n"); | 181 | html("</td></tr>\n"); |
180 | for (p = commit->parents; p ; p = p->next) { | 182 | for (p = commit->parents; p ; p = p->next) { |
181 | parent = lookup_commit_reference(p->item->object.sha1); | 183 | parent = lookup_commit_reference(p->item->object.sha1); |
182 | if (!parent) { | 184 | if (!parent) { |
183 | html("<tr><td colspan='3'>"); | 185 | html("<tr><td colspan='3'>"); |
184 | cgit_print_error("Error reading parent commit"); | 186 | cgit_print_error("Error reading parent commit"); |
185 | html("</td></tr>"); | 187 | html("</td></tr>"); |
186 | continue; | 188 | continue; |
187 | } | 189 | } |
188 | html("<tr><th>parent</th>" | 190 | html("<tr><th>parent</th>" |
189 | "<td colspan='2' class='sha1'>"); | 191 | "<td colspan='2' class='sha1'>"); |
190 | cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL, | 192 | cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL, |
191 | cgit_query_head, sha1_to_hex(p->item->object.sha1)); | 193 | ctx.qry.head, sha1_to_hex(p->item->object.sha1)); |
192 | html(" ("); | 194 | html(" ("); |
193 | cgit_diff_link("diff", NULL, NULL, cgit_query_head, hex, | 195 | cgit_diff_link("diff", NULL, NULL, ctx.qry.head, hex, |
194 | sha1_to_hex(p->item->object.sha1), NULL); | 196 | sha1_to_hex(p->item->object.sha1), NULL); |
195 | html(")</td></tr>"); | 197 | html(")</td></tr>"); |
196 | } | 198 | } |
197 | if (cgit_repo->snapshots) { | 199 | if (ctx.repo->snapshots) { |
198 | html("<tr><th>download</th><td colspan='2' class='sha1'>"); | 200 | html("<tr><th>download</th><td colspan='2' class='sha1'>"); |
199 | cgit_print_snapshot_links(cgit_query_repo, cgit_query_head, | 201 | cgit_print_snapshot_links(ctx.qry.repo, ctx.qry.head, |
200 | hex, cgit_repo->snapshots); | 202 | hex, ctx.repo->snapshots); |
201 | html("</td></tr>"); | 203 | html("</td></tr>"); |
202 | } | 204 | } |
203 | html("</table>\n"); | 205 | html("</table>\n"); |
204 | html("<div class='commit-subject'>"); | 206 | html("<div class='commit-subject'>"); |
205 | html_txt(info->subject); | 207 | html_txt(info->subject); |
206 | html("</div>"); | 208 | html("</div>"); |
207 | html("<div class='commit-msg'>"); | 209 | html("<div class='commit-msg'>"); |
208 | html_txt(info->msg); | 210 | html_txt(info->msg); |
209 | html("</div>"); | 211 | html("</div>"); |
210 | if (!(commit->parents && commit->parents->next && commit->parents->next->next)) { | 212 | if (!(commit->parents && commit->parents->next && commit->parents->next->next)) { |
211 | html("<div class='diffstat-header'>Diffstat</div>"); | 213 | html("<div class='diffstat-header'>Diffstat</div>"); |
212 | html("<table summary='diffstat' class='diffstat'>"); | 214 | html("<table summary='diffstat' class='diffstat'>"); |
213 | max_changes = 0; | 215 | max_changes = 0; |
214 | cgit_diff_commit(commit, inspect_filepair); | 216 | cgit_diff_commit(commit, inspect_filepair); |
215 | for(i = 0; i<files; i++) | 217 | for(i = 0; i<files; i++) |
216 | print_fileinfo(&items[i]); | 218 | print_fileinfo(&items[i]); |
217 | html("</table>"); | 219 | html("</table>"); |
218 | html("<div class='diffstat-summary'>"); | 220 | html("<div class='diffstat-summary'>"); |
219 | htmlf("%d files changed, %d insertions, %d deletions (", | 221 | htmlf("%d files changed, %d insertions, %d deletions (", |
220 | files, total_adds, total_rems); | 222 | files, total_adds, total_rems); |
221 | cgit_diff_link("show diff", NULL, NULL, cgit_query_head, hex, | 223 | cgit_diff_link("show diff", NULL, NULL, ctx.qry.head, hex, |
222 | NULL, NULL); | 224 | NULL, NULL); |
223 | html(")</div>"); | 225 | html(")</div>"); |
224 | } | 226 | } |
225 | cgit_free_commitinfo(info); | 227 | cgit_free_commitinfo(info); |
226 | } | 228 | } |
diff --git a/ui-commit.h b/ui-commit.h new file mode 100644 index 0000000..40bcb31 --- a/dev/null +++ b/ui-commit.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_COMMIT_H | ||
2 | #define UI_COMMIT_H | ||
3 | |||
4 | extern void cgit_print_commit(char *hex); | ||
5 | |||
6 | #endif /* UI_COMMIT_H */ | ||
@@ -1,149 +1,150 @@ | |||
1 | /* ui-diff.c: show diff between two blobs | 1 | /* ui-diff.c: show diff between two blobs |
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 | #include "html.h" | |
11 | #include "ui-shared.h" | ||
11 | 12 | ||
12 | unsigned char old_rev_sha1[20]; | 13 | unsigned char old_rev_sha1[20]; |
13 | unsigned char new_rev_sha1[20]; | 14 | unsigned char new_rev_sha1[20]; |
14 | 15 | ||
15 | /* | 16 | /* |
16 | * print a single line returned from xdiff | 17 | * print a single line returned from xdiff |
17 | */ | 18 | */ |
18 | static void print_line(char *line, int len) | 19 | static void print_line(char *line, int len) |
19 | { | 20 | { |
20 | char *class = "ctx"; | 21 | char *class = "ctx"; |
21 | char c = line[len-1]; | 22 | char c = line[len-1]; |
22 | 23 | ||
23 | if (line[0] == '+') | 24 | if (line[0] == '+') |
24 | class = "add"; | 25 | class = "add"; |
25 | else if (line[0] == '-') | 26 | else if (line[0] == '-') |
26 | class = "del"; | 27 | class = "del"; |
27 | else if (line[0] == '@') | 28 | else if (line[0] == '@') |
28 | class = "hunk"; | 29 | class = "hunk"; |
29 | 30 | ||
30 | htmlf("<div class='%s'>", class); | 31 | htmlf("<div class='%s'>", class); |
31 | line[len-1] = '\0'; | 32 | line[len-1] = '\0'; |
32 | html_txt(line); | 33 | html_txt(line); |
33 | html("</div>"); | 34 | html("</div>"); |
34 | line[len-1] = c; | 35 | line[len-1] = c; |
35 | } | 36 | } |
36 | 37 | ||
37 | static void header(unsigned char *sha1, char *path1, int mode1, | 38 | static void header(unsigned char *sha1, char *path1, int mode1, |
38 | unsigned char *sha2, char *path2, int mode2) | 39 | unsigned char *sha2, char *path2, int mode2) |
39 | { | 40 | { |
40 | char *abbrev1, *abbrev2; | 41 | char *abbrev1, *abbrev2; |
41 | int subproject; | 42 | int subproject; |
42 | 43 | ||
43 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); | 44 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
44 | html("<div class='head'>"); | 45 | html("<div class='head'>"); |
45 | html("diff --git a/"); | 46 | html("diff --git a/"); |
46 | html_txt(path1); | 47 | html_txt(path1); |
47 | html(" b/"); | 48 | html(" b/"); |
48 | html_txt(path2); | 49 | html_txt(path2); |
49 | 50 | ||
50 | if (is_null_sha1(sha1)) | 51 | if (is_null_sha1(sha1)) |
51 | path1 = "dev/null"; | 52 | path1 = "dev/null"; |
52 | if (is_null_sha1(sha2)) | 53 | if (is_null_sha1(sha2)) |
53 | path2 = "dev/null"; | 54 | path2 = "dev/null"; |
54 | 55 | ||
55 | if (mode1 == 0) | 56 | if (mode1 == 0) |
56 | htmlf("<br/>new file mode %.6o", mode2); | 57 | htmlf("<br/>new file mode %.6o", mode2); |
57 | 58 | ||
58 | if (mode2 == 0) | 59 | if (mode2 == 0) |
59 | htmlf("<br/>deleted file mode %.6o", mode1); | 60 | htmlf("<br/>deleted file mode %.6o", mode1); |
60 | 61 | ||
61 | if (!subproject) { | 62 | if (!subproject) { |
62 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); | 63 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
63 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); | 64 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); |
64 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); | 65 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); |
65 | free(abbrev1); | 66 | free(abbrev1); |
66 | free(abbrev2); | 67 | free(abbrev2); |
67 | if (mode1 != 0 && mode2 != 0) { | 68 | if (mode1 != 0 && mode2 != 0) { |
68 | htmlf(" %.6o", mode1); | 69 | htmlf(" %.6o", mode1); |
69 | if (mode2 != mode1) | 70 | if (mode2 != mode1) |
70 | htmlf("..%.6o", mode2); | 71 | htmlf("..%.6o", mode2); |
71 | } | 72 | } |
72 | html("<br/>--- a/"); | 73 | html("<br/>--- a/"); |
73 | if (mode1 != 0) | 74 | if (mode1 != 0) |
74 | cgit_tree_link(path1, NULL, NULL, cgit_query_head, | 75 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
75 | sha1_to_hex(old_rev_sha1), path1); | 76 | sha1_to_hex(old_rev_sha1), path1); |
76 | else | 77 | else |
77 | html_txt(path1); | 78 | html_txt(path1); |
78 | html("<br/>+++ b/"); | 79 | html("<br/>+++ b/"); |
79 | if (mode2 != 0) | 80 | if (mode2 != 0) |
80 | cgit_tree_link(path2, NULL, NULL, cgit_query_head, | 81 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
81 | sha1_to_hex(new_rev_sha1), path2); | 82 | sha1_to_hex(new_rev_sha1), path2); |
82 | else | 83 | else |
83 | html_txt(path2); | 84 | html_txt(path2); |
84 | } | 85 | } |
85 | html("</div>"); | 86 | html("</div>"); |
86 | } | 87 | } |
87 | 88 | ||
88 | static void filepair_cb(struct diff_filepair *pair) | 89 | static void filepair_cb(struct diff_filepair *pair) |
89 | { | 90 | { |
90 | header(pair->one->sha1, pair->one->path, pair->one->mode, | 91 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
91 | pair->two->sha1, pair->two->path, pair->two->mode); | 92 | pair->two->sha1, pair->two->path, pair->two->mode); |
92 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { | 93 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
93 | if (S_ISGITLINK(pair->one->mode)) | 94 | if (S_ISGITLINK(pair->one->mode)) |
94 | print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); | 95 | print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); |
95 | if (S_ISGITLINK(pair->two->mode)) | 96 | if (S_ISGITLINK(pair->two->mode)) |
96 | print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); | 97 | print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); |
97 | return; | 98 | return; |
98 | } | 99 | } |
99 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) | 100 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) |
100 | cgit_print_error("Error running diff"); | 101 | cgit_print_error("Error running diff"); |
101 | } | 102 | } |
102 | 103 | ||
103 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) | 104 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) |
104 | { | 105 | { |
105 | enum object_type type; | 106 | enum object_type type; |
106 | unsigned long size; | 107 | unsigned long size; |
107 | struct commit *commit, *commit2; | 108 | struct commit *commit, *commit2; |
108 | 109 | ||
109 | if (!new_rev) | 110 | if (!new_rev) |
110 | new_rev = cgit_query_head; | 111 | new_rev = ctx.qry.head; |
111 | get_sha1(new_rev, new_rev_sha1); | 112 | get_sha1(new_rev, new_rev_sha1); |
112 | type = sha1_object_info(new_rev_sha1, &size); | 113 | type = sha1_object_info(new_rev_sha1, &size); |
113 | if (type == OBJ_BAD) { | 114 | if (type == OBJ_BAD) { |
114 | cgit_print_error(fmt("Bad object name: %s", new_rev)); | 115 | cgit_print_error(fmt("Bad object name: %s", new_rev)); |
115 | return; | 116 | return; |
116 | } | 117 | } |
117 | if (type != OBJ_COMMIT) { | 118 | if (type != OBJ_COMMIT) { |
118 | cgit_print_error(fmt("Unhandled object type: %s", | 119 | cgit_print_error(fmt("Unhandled object type: %s", |
119 | typename(type))); | 120 | typename(type))); |
120 | return; | 121 | return; |
121 | } | 122 | } |
122 | 123 | ||
123 | commit = lookup_commit_reference(new_rev_sha1); | 124 | commit = lookup_commit_reference(new_rev_sha1); |
124 | if (!commit || parse_commit(commit)) | 125 | if (!commit || parse_commit(commit)) |
125 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); | 126 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); |
126 | 127 | ||
127 | if (old_rev) | 128 | if (old_rev) |
128 | get_sha1(old_rev, old_rev_sha1); | 129 | get_sha1(old_rev, old_rev_sha1); |
129 | else if (commit->parents && commit->parents->item) | 130 | else if (commit->parents && commit->parents->item) |
130 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); | 131 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); |
131 | else | 132 | else |
132 | hashclr(old_rev_sha1); | 133 | hashclr(old_rev_sha1); |
133 | 134 | ||
134 | if (!is_null_sha1(old_rev_sha1)) { | 135 | if (!is_null_sha1(old_rev_sha1)) { |
135 | type = sha1_object_info(old_rev_sha1, &size); | 136 | type = sha1_object_info(old_rev_sha1, &size); |
136 | if (type == OBJ_BAD) { | 137 | if (type == OBJ_BAD) { |
137 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); | 138 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); |
138 | return; | 139 | return; |
139 | } | 140 | } |
140 | commit2 = lookup_commit_reference(old_rev_sha1); | 141 | commit2 = lookup_commit_reference(old_rev_sha1); |
141 | if (!commit2 || parse_commit(commit2)) | 142 | if (!commit2 || parse_commit(commit2)) |
142 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); | 143 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); |
143 | } | 144 | } |
144 | html("<table summary='diff' class='diff'>"); | 145 | html("<table summary='diff' class='diff'>"); |
145 | html("<tr><td>"); | 146 | html("<tr><td>"); |
146 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix); | 147 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix); |
147 | html("</td></tr>"); | 148 | html("</td></tr>"); |
148 | html("</table>"); | 149 | html("</table>"); |
149 | } | 150 | } |
diff --git a/ui-diff.h b/ui-diff.h new file mode 100644 index 0000000..2307322 --- a/dev/null +++ b/ui-diff.h | |||
@@ -0,0 +1,7 @@ | |||
1 | #ifndef UI_DIFF_H | ||
2 | #define UI_DIFF_H | ||
3 | |||
4 | extern void cgit_print_diff(const char *new_hex, const char *old_hex, | ||
5 | const char *prefix); | ||
6 | |||
7 | #endif /* UI_DIFF_H */ | ||
@@ -1,140 +1,142 @@ | |||
1 | /* ui-log.c: functions for log output | 1 | /* ui-log.c: functions for log output |
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 "html.h" | ||
11 | #include "ui-shared.h" | ||
10 | 12 | ||
11 | int files, add_lines, rem_lines; | 13 | int files, add_lines, rem_lines; |
12 | 14 | ||
13 | void count_lines(char *line, int size) | 15 | void count_lines(char *line, int size) |
14 | { | 16 | { |
15 | if (size <= 0) | 17 | if (size <= 0) |
16 | return; | 18 | return; |
17 | 19 | ||
18 | if (line[0] == '+') | 20 | if (line[0] == '+') |
19 | add_lines++; | 21 | add_lines++; |
20 | 22 | ||
21 | else if (line[0] == '-') | 23 | else if (line[0] == '-') |
22 | rem_lines++; | 24 | rem_lines++; |
23 | } | 25 | } |
24 | 26 | ||
25 | void inspect_files(struct diff_filepair *pair) | 27 | void inspect_files(struct diff_filepair *pair) |
26 | { | 28 | { |
27 | files++; | 29 | files++; |
28 | if (cgit_repo->enable_log_linecount) | 30 | if (ctx.repo->enable_log_linecount) |
29 | cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines); | 31 | cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines); |
30 | } | 32 | } |
31 | 33 | ||
32 | void print_commit(struct commit *commit) | 34 | void print_commit(struct commit *commit) |
33 | { | 35 | { |
34 | struct commitinfo *info; | 36 | struct commitinfo *info; |
35 | 37 | ||
36 | info = cgit_parse_commit(commit); | 38 | info = cgit_parse_commit(commit); |
37 | html("<tr><td>"); | 39 | html("<tr><td>"); |
38 | cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE); | 40 | cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE); |
39 | html("</td><td>"); | 41 | html("</td><td>"); |
40 | cgit_commit_link(info->subject, NULL, NULL, cgit_query_head, | 42 | cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head, |
41 | sha1_to_hex(commit->object.sha1)); | 43 | sha1_to_hex(commit->object.sha1)); |
42 | if (cgit_repo->enable_log_filecount) { | 44 | if (ctx.repo->enable_log_filecount) { |
43 | files = 0; | 45 | files = 0; |
44 | add_lines = 0; | 46 | add_lines = 0; |
45 | rem_lines = 0; | 47 | rem_lines = 0; |
46 | cgit_diff_commit(commit, inspect_files); | 48 | cgit_diff_commit(commit, inspect_files); |
47 | html("</td><td class='right'>"); | 49 | html("</td><td class='right'>"); |
48 | htmlf("%d", files); | 50 | htmlf("%d", files); |
49 | if (cgit_repo->enable_log_linecount) { | 51 | if (ctx.repo->enable_log_linecount) { |
50 | html("</td><td class='right'>"); | 52 | html("</td><td class='right'>"); |
51 | htmlf("-%d/+%d", rem_lines, add_lines); | 53 | htmlf("-%d/+%d", rem_lines, add_lines); |
52 | } | 54 | } |
53 | } | 55 | } |
54 | html("</td><td>"); | 56 | html("</td><td>"); |
55 | html_txt(info->author); | 57 | html_txt(info->author); |
56 | html("</td></tr>\n"); | 58 | html("</td></tr>\n"); |
57 | cgit_free_commitinfo(info); | 59 | cgit_free_commitinfo(info); |
58 | } | 60 | } |
59 | 61 | ||
60 | 62 | ||
61 | void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern, char *path, int pager) | 63 | void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern, char *path, int pager) |
62 | { | 64 | { |
63 | struct rev_info rev; | 65 | struct rev_info rev; |
64 | struct commit *commit; | 66 | struct commit *commit; |
65 | const char *argv[] = {NULL, tip, NULL, NULL, NULL}; | 67 | const char *argv[] = {NULL, tip, NULL, NULL, NULL}; |
66 | int argc = 2; | 68 | int argc = 2; |
67 | int i; | 69 | int i; |
68 | 70 | ||
69 | if (!tip) | 71 | if (!tip) |
70 | argv[1] = cgit_query_head; | 72 | argv[1] = ctx.qry.head; |
71 | 73 | ||
72 | if (grep && pattern && (!strcmp(grep, "grep") || | 74 | if (grep && pattern && (!strcmp(grep, "grep") || |
73 | !strcmp(grep, "author") || | 75 | !strcmp(grep, "author") || |
74 | !strcmp(grep, "committer"))) | 76 | !strcmp(grep, "committer"))) |
75 | argv[argc++] = fmt("--%s=%s", grep, pattern); | 77 | argv[argc++] = fmt("--%s=%s", grep, pattern); |
76 | 78 | ||
77 | if (path) { | 79 | if (path) { |
78 | argv[argc++] = "--"; | 80 | argv[argc++] = "--"; |
79 | argv[argc++] = path; | 81 | argv[argc++] = path; |
80 | } | 82 | } |
81 | init_revisions(&rev, NULL); | 83 | init_revisions(&rev, NULL); |
82 | rev.abbrev = DEFAULT_ABBREV; | 84 | rev.abbrev = DEFAULT_ABBREV; |
83 | rev.commit_format = CMIT_FMT_DEFAULT; | 85 | rev.commit_format = CMIT_FMT_DEFAULT; |
84 | rev.verbose_header = 1; | 86 | rev.verbose_header = 1; |
85 | rev.show_root_diff = 0; | 87 | rev.show_root_diff = 0; |
86 | setup_revisions(argc, argv, &rev, NULL); | 88 | setup_revisions(argc, argv, &rev, NULL); |
87 | if (rev.grep_filter) { | 89 | if (rev.grep_filter) { |
88 | rev.grep_filter->regflags |= REG_ICASE; | 90 | rev.grep_filter->regflags |= REG_ICASE; |
89 | compile_grep_patterns(rev.grep_filter); | 91 | compile_grep_patterns(rev.grep_filter); |
90 | } | 92 | } |
91 | prepare_revision_walk(&rev); | 93 | prepare_revision_walk(&rev); |
92 | 94 | ||
93 | html("<table summary='log' class='list nowrap'>"); | 95 | html("<table summary='log' class='list nowrap'>"); |
94 | html("<tr class='nohover'><th class='left'>Age</th>" | 96 | html("<tr class='nohover'><th class='left'>Age</th>" |
95 | "<th class='left'>Message</th>"); | 97 | "<th class='left'>Message</th>"); |
96 | 98 | ||
97 | if (cgit_repo->enable_log_filecount) { | 99 | if (ctx.repo->enable_log_filecount) { |
98 | html("<th class='right'>Files</th>"); | 100 | html("<th class='right'>Files</th>"); |
99 | if (cgit_repo->enable_log_linecount) | 101 | if (ctx.repo->enable_log_linecount) |
100 | html("<th class='right'>Lines</th>"); | 102 | html("<th class='right'>Lines</th>"); |
101 | } | 103 | } |
102 | html("<th class='left'>Author</th></tr>\n"); | 104 | html("<th class='left'>Author</th></tr>\n"); |
103 | 105 | ||
104 | if (ofs<0) | 106 | if (ofs<0) |
105 | ofs = 0; | 107 | ofs = 0; |
106 | 108 | ||
107 | for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) { | 109 | for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) { |
108 | free(commit->buffer); | 110 | free(commit->buffer); |
109 | commit->buffer = NULL; | 111 | commit->buffer = NULL; |
110 | free_commit_list(commit->parents); | 112 | free_commit_list(commit->parents); |
111 | commit->parents = NULL; | 113 | commit->parents = NULL; |
112 | } | 114 | } |
113 | 115 | ||
114 | for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) { | 116 | for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) { |
115 | print_commit(commit); | 117 | print_commit(commit); |
116 | free(commit->buffer); | 118 | free(commit->buffer); |
117 | commit->buffer = NULL; | 119 | commit->buffer = NULL; |
118 | free_commit_list(commit->parents); | 120 | free_commit_list(commit->parents); |
119 | commit->parents = NULL; | 121 | commit->parents = NULL; |
120 | } | 122 | } |
121 | html("</table>\n"); | 123 | html("</table>\n"); |
122 | 124 | ||
123 | if (pager) { | 125 | if (pager) { |
124 | html("<div class='pager'>"); | 126 | html("<div class='pager'>"); |
125 | if (ofs > 0) { | 127 | if (ofs > 0) { |
126 | cgit_log_link("[prev]", NULL, NULL, cgit_query_head, | 128 | cgit_log_link("[prev]", NULL, NULL, ctx.qry.head, |
127 | cgit_query_sha1, cgit_query_path, | 129 | ctx.qry.sha1, ctx.qry.path, |
128 | ofs - cnt, cgit_query_grep, | 130 | ofs - cnt, ctx.qry.grep, |
129 | cgit_query_search); | 131 | ctx.qry.search); |
130 | html(" "); | 132 | html(" "); |
131 | } | 133 | } |
132 | if ((commit = get_revision(&rev)) != NULL) { | 134 | if ((commit = get_revision(&rev)) != NULL) { |
133 | cgit_log_link("[next]", NULL, NULL, cgit_query_head, | 135 | cgit_log_link("[next]", NULL, NULL, ctx.qry.head, |
134 | cgit_query_sha1, cgit_query_path, | 136 | ctx.qry.sha1, ctx.qry.path, |
135 | ofs + cnt, cgit_query_grep, | 137 | ofs + cnt, ctx.qry.grep, |
136 | cgit_query_search); | 138 | ctx.qry.search); |
137 | } | 139 | } |
138 | html("</div>"); | 140 | html("</div>"); |
139 | } | 141 | } |
140 | } | 142 | } |
diff --git a/ui-log.h b/ui-log.h new file mode 100644 index 0000000..877e40e --- a/dev/null +++ b/ui-log.h | |||
@@ -0,0 +1,7 @@ | |||
1 | #ifndef UI_LOG_H | ||
2 | #define UI_LOG_H | ||
3 | |||
4 | extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, | ||
5 | char *pattern, char *path, int pager); | ||
6 | |||
7 | #endif /* UI_LOG_H */ | ||
@@ -1,114 +1,118 @@ | |||
1 | /* ui-patch.c: generate patch view | 1 | /* ui-patch.c: generate patch view |
2 | * | 2 | * |
3 | * Copyright (C) 2007 Lars Hjemli | 3 | * Copyright (C) 2007 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 "html.h" | ||
11 | #include "ui-shared.h" | ||
10 | 12 | ||
11 | static void print_line(char *line, int len) | 13 | static void print_line(char *line, int len) |
12 | { | 14 | { |
13 | char c = line[len-1]; | 15 | char c = line[len-1]; |
14 | 16 | ||
15 | line[len-1] = '\0'; | 17 | line[len-1] = '\0'; |
16 | htmlf("%s\n", line); | 18 | htmlf("%s\n", line); |
17 | line[len-1] = c; | 19 | line[len-1] = c; |
18 | } | 20 | } |
19 | 21 | ||
20 | static void header(unsigned char *sha1, char *path1, int mode1, | 22 | static void header(unsigned char *sha1, char *path1, int mode1, |
21 | unsigned char *sha2, char *path2, int mode2) | 23 | unsigned char *sha2, char *path2, int mode2) |
22 | { | 24 | { |
23 | char *abbrev1, *abbrev2; | 25 | char *abbrev1, *abbrev2; |
24 | int subproject; | 26 | int subproject; |
25 | 27 | ||
26 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); | 28 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
27 | htmlf("diff --git a/%s b/%s\n", path1, path2); | 29 | htmlf("diff --git a/%s b/%s\n", path1, path2); |
28 | 30 | ||
29 | if (is_null_sha1(sha1)) | 31 | if (is_null_sha1(sha1)) |
30 | path1 = "dev/null"; | 32 | path1 = "dev/null"; |
31 | if (is_null_sha1(sha2)) | 33 | if (is_null_sha1(sha2)) |
32 | path2 = "dev/null"; | 34 | path2 = "dev/null"; |
33 | 35 | ||
34 | if (mode1 == 0) | 36 | if (mode1 == 0) |
35 | htmlf("new file mode %.6o\n", mode2); | 37 | htmlf("new file mode %.6o\n", mode2); |
36 | 38 | ||
37 | if (mode2 == 0) | 39 | if (mode2 == 0) |
38 | htmlf("deleted file mode %.6o\n", mode1); | 40 | htmlf("deleted file mode %.6o\n", mode1); |
39 | 41 | ||
40 | if (!subproject) { | 42 | if (!subproject) { |
41 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); | 43 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
42 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); | 44 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); |
43 | htmlf("index %s..%s", abbrev1, abbrev2); | 45 | htmlf("index %s..%s", abbrev1, abbrev2); |
44 | free(abbrev1); | 46 | free(abbrev1); |
45 | free(abbrev2); | 47 | free(abbrev2); |
46 | if (mode1 != 0 && mode2 != 0) { | 48 | if (mode1 != 0 && mode2 != 0) { |
47 | htmlf(" %.6o", mode1); | 49 | htmlf(" %.6o", mode1); |
48 | if (mode2 != mode1) | 50 | if (mode2 != mode1) |
49 | htmlf("..%.6o", mode2); | 51 | htmlf("..%.6o", mode2); |
50 | } | 52 | } |
51 | htmlf("\n--- a/%s\n", path1); | 53 | htmlf("\n--- a/%s\n", path1); |
52 | htmlf("+++ b/%s\n", path2); | 54 | htmlf("+++ b/%s\n", path2); |
53 | } | 55 | } |
54 | } | 56 | } |
55 | 57 | ||
56 | static void filepair_cb(struct diff_filepair *pair) | 58 | static void filepair_cb(struct diff_filepair *pair) |
57 | { | 59 | { |
58 | header(pair->one->sha1, pair->one->path, pair->one->mode, | 60 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
59 | pair->two->sha1, pair->two->path, pair->two->mode); | 61 | pair->two->sha1, pair->two->path, pair->two->mode); |
60 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { | 62 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
61 | if (S_ISGITLINK(pair->one->mode)) | 63 | if (S_ISGITLINK(pair->one->mode)) |
62 | print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); | 64 | print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); |
63 | if (S_ISGITLINK(pair->two->mode)) | 65 | if (S_ISGITLINK(pair->two->mode)) |
64 | print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); | 66 | print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); |
65 | return; | 67 | return; |
66 | } | 68 | } |
67 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) | 69 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) |
68 | html("Error running diff"); | 70 | html("Error running diff"); |
69 | } | 71 | } |
70 | 72 | ||
71 | void cgit_print_patch(char *hex, struct cacheitem *item) | 73 | void cgit_print_patch(char *hex) |
72 | { | 74 | { |
73 | struct commit *commit; | 75 | struct commit *commit; |
74 | struct commitinfo *info; | 76 | struct commitinfo *info; |
75 | unsigned char sha1[20], old_sha1[20]; | 77 | unsigned char sha1[20], old_sha1[20]; |
76 | char *patchname; | 78 | char *patchname; |
77 | 79 | ||
78 | if (!hex) | 80 | if (!hex) |
79 | hex = cgit_query_head; | 81 | hex = ctx.qry.head; |
80 | 82 | ||
81 | if (get_sha1(hex, sha1)) { | 83 | if (get_sha1(hex, sha1)) { |
82 | cgit_print_error(fmt("Bad object id: %s", hex)); | 84 | cgit_print_error(fmt("Bad object id: %s", hex)); |
83 | return; | 85 | return; |
84 | } | 86 | } |
85 | commit = lookup_commit_reference(sha1); | 87 | commit = lookup_commit_reference(sha1); |
86 | if (!commit) { | 88 | if (!commit) { |
87 | cgit_print_error(fmt("Bad commit reference: %s", hex)); | 89 | cgit_print_error(fmt("Bad commit reference: %s", hex)); |
88 | return; | 90 | return; |
89 | } | 91 | } |
90 | info = cgit_parse_commit(commit); | 92 | info = cgit_parse_commit(commit); |
91 | 93 | ||
92 | if (commit->parents && commit->parents->item) | 94 | if (commit->parents && commit->parents->item) |
93 | hashcpy(old_sha1, commit->parents->item->object.sha1); | 95 | hashcpy(old_sha1, commit->parents->item->object.sha1); |
94 | else | 96 | else |
95 | hashclr(old_sha1); | 97 | hashclr(old_sha1); |
96 | 98 | ||
97 | patchname = fmt("%s.patch", sha1_to_hex(sha1)); | 99 | patchname = fmt("%s.patch", sha1_to_hex(sha1)); |
98 | cgit_print_snapshot_start("text/plain", patchname, item); | 100 | ctx.page.mimetype = "text/plain"; |
101 | ctx.page.filename = patchname; | ||
102 | cgit_print_http_headers(&ctx); | ||
99 | htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); | 103 | htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); |
100 | htmlf("From: %s%s\n", info->author, info->author_email); | 104 | htmlf("From: %s%s\n", info->author, info->author_email); |
101 | html("Date: "); | 105 | html("Date: "); |
102 | cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); | 106 | cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); |
103 | htmlf("Subject: %s\n\n", info->subject); | 107 | htmlf("Subject: %s\n\n", info->subject); |
104 | if (info->msg && *info->msg) { | 108 | if (info->msg && *info->msg) { |
105 | htmlf("%s", info->msg); | 109 | htmlf("%s", info->msg); |
106 | if (info->msg[strlen(info->msg) - 1] != '\n') | 110 | if (info->msg[strlen(info->msg) - 1] != '\n') |
107 | html("\n"); | 111 | html("\n"); |
108 | } | 112 | } |
109 | html("---\n"); | 113 | html("---\n"); |
110 | cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); | 114 | cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); |
111 | html("--\n"); | 115 | html("--\n"); |
112 | htmlf("cgit %s\n", CGIT_VERSION); | 116 | htmlf("cgit %s\n", CGIT_VERSION); |
113 | cgit_free_commitinfo(info); | 117 | cgit_free_commitinfo(info); |
114 | } | 118 | } |
diff --git a/ui-patch.h b/ui-patch.h new file mode 100644 index 0000000..9f68212 --- a/dev/null +++ b/ui-patch.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_PATCH_H | ||
2 | #define UI_PATCH_H | ||
3 | |||
4 | extern void cgit_print_patch(char *hex); | ||
5 | |||
6 | #endif /* UI_PATCH_H */ | ||
@@ -1,30 +1,201 @@ | |||
1 | /* ui-refs.c: browse symbolic refs | 1 | /* ui-refs.c: browse symbolic refs |
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 "html.h" | ||
11 | #include "ui-shared.h" | ||
10 | 12 | ||
13 | static int header; | ||
11 | 14 | ||
15 | static int cmp_age(int age1, int age2) | ||
16 | { | ||
17 | if (age1 != 0 && age2 != 0) | ||
18 | return age2 - age1; | ||
19 | |||
20 | if (age1 == 0 && age2 == 0) | ||
21 | return 0; | ||
22 | |||
23 | if (age1 == 0) | ||
24 | return +1; | ||
25 | |||
26 | return -1; | ||
27 | } | ||
28 | |||
29 | static int cmp_ref_name(const void *a, const void *b) | ||
30 | { | ||
31 | struct refinfo *r1 = *(struct refinfo **)a; | ||
32 | struct refinfo *r2 = *(struct refinfo **)b; | ||
33 | |||
34 | return strcmp(r1->refname, r2->refname); | ||
35 | } | ||
36 | |||
37 | static int cmp_branch_age(const void *a, const void *b) | ||
38 | { | ||
39 | struct refinfo *r1 = *(struct refinfo **)a; | ||
40 | struct refinfo *r2 = *(struct refinfo **)b; | ||
41 | |||
42 | return cmp_age(r1->commit->committer_date, r2->commit->committer_date); | ||
43 | } | ||
44 | |||
45 | static int cmp_tag_age(const void *a, const void *b) | ||
46 | { | ||
47 | struct refinfo *r1 = *(struct refinfo **)a; | ||
48 | struct refinfo *r2 = *(struct refinfo **)b; | ||
49 | |||
50 | return cmp_age(r1->tag->tagger_date, r2->tag->tagger_date); | ||
51 | } | ||
52 | |||
53 | static int print_branch(struct refinfo *ref) | ||
54 | { | ||
55 | struct commitinfo *info = ref->commit; | ||
56 | char *name = (char *)ref->refname; | ||
57 | |||
58 | if (!info) | ||
59 | return 1; | ||
60 | html("<tr><td>"); | ||
61 | cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL); | ||
62 | html("</td><td>"); | ||
63 | |||
64 | if (ref->object->type == OBJ_COMMIT) { | ||
65 | cgit_print_age(info->commit->date, -1, NULL); | ||
66 | html("</td><td>"); | ||
67 | html_txt(info->author); | ||
68 | html("</td><td>"); | ||
69 | cgit_commit_link(info->subject, NULL, NULL, name, NULL); | ||
70 | } else { | ||
71 | html("</td><td></td><td>"); | ||
72 | cgit_object_link(ref->object); | ||
73 | } | ||
74 | html("</td></tr>\n"); | ||
75 | return 0; | ||
76 | } | ||
77 | |||
78 | static void print_tag_header() | ||
79 | { | ||
80 | html("<tr class='nohover'><th class='left'>Tag</th>" | ||
81 | "<th class='left'>Age</th>" | ||
82 | "<th class='left'>Author</th>" | ||
83 | "<th class='left'>Reference</th></tr>\n"); | ||
84 | header = 1; | ||
85 | } | ||
86 | |||
87 | static int print_tag(struct refinfo *ref) | ||
88 | { | ||
89 | struct tag *tag; | ||
90 | struct taginfo *info; | ||
91 | char *url, *name = (char *)ref->refname; | ||
92 | |||
93 | if (ref->object->type == OBJ_TAG) { | ||
94 | tag = (struct tag *)ref->object; | ||
95 | info = ref->tag; | ||
96 | if (!tag || !info) | ||
97 | return 1; | ||
98 | html("<tr><td>"); | ||
99 | url = cgit_pageurl(ctx.qry.repo, "tag", | ||
100 | fmt("id=%s", name)); | ||
101 | html_link_open(url, NULL, NULL); | ||
102 | html_txt(name); | ||
103 | html_link_close(); | ||
104 | html("</td><td>"); | ||
105 | if (info->tagger_date > 0) | ||
106 | cgit_print_age(info->tagger_date, -1, NULL); | ||
107 | html("</td><td>"); | ||
108 | if (info->tagger) | ||
109 | html(info->tagger); | ||
110 | html("</td><td>"); | ||
111 | cgit_object_link(tag->tagged); | ||
112 | html("</td></tr>\n"); | ||
113 | } else { | ||
114 | if (!header) | ||
115 | print_tag_header(); | ||
116 | html("<tr><td>"); | ||
117 | html_txt(name); | ||
118 | html("</td><td colspan='2'/><td>"); | ||
119 | cgit_object_link(ref->object); | ||
120 | html("</td></tr>\n"); | ||
121 | } | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | static void print_refs_link(char *path) | ||
126 | { | ||
127 | html("<tr class='nohover'><td colspan='4'>"); | ||
128 | cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path); | ||
129 | html("</td></tr>"); | ||
130 | } | ||
131 | |||
132 | void cgit_print_branches(int maxcount) | ||
133 | { | ||
134 | struct reflist list; | ||
135 | int i; | ||
12 | 136 | ||
137 | html("<tr class='nohover'><th class='left'>Branch</th>" | ||
138 | "<th class='left'>Idle</th>" | ||
139 | "<th class='left'>Author</th>" | ||
140 | "<th class='left'>Head commit</th></tr>\n"); | ||
141 | |||
142 | list.refs = NULL; | ||
143 | list.alloc = list.count = 0; | ||
144 | for_each_branch_ref(cgit_refs_cb, &list); | ||
145 | |||
146 | if (maxcount == 0 || maxcount > list.count) | ||
147 | maxcount = list.count; | ||
148 | |||
149 | if (maxcount < list.count) { | ||
150 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age); | ||
151 | qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name); | ||
152 | } | ||
153 | |||
154 | for(i=0; i<maxcount; i++) | ||
155 | print_branch(list.refs[i]); | ||
156 | |||
157 | if (maxcount < list.count) | ||
158 | print_refs_link("heads"); | ||
159 | } | ||
160 | |||
161 | void cgit_print_tags(int maxcount) | ||
162 | { | ||
163 | struct reflist list; | ||
164 | int i; | ||
165 | |||
166 | header = 0; | ||
167 | list.refs = NULL; | ||
168 | list.alloc = list.count = 0; | ||
169 | for_each_tag_ref(cgit_refs_cb, &list); | ||
170 | if (list.count == 0) | ||
171 | return; | ||
172 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age); | ||
173 | if (!maxcount) | ||
174 | maxcount = list.count; | ||
175 | else if (maxcount > list.count) | ||
176 | maxcount = list.count; | ||
177 | print_tag_header(); | ||
178 | for(i=0; i<maxcount; i++) | ||
179 | print_tag(list.refs[i]); | ||
180 | |||
181 | if (maxcount < list.count) | ||
182 | print_refs_link("tags"); | ||
183 | } | ||
13 | 184 | ||
14 | void cgit_print_refs() | 185 | void cgit_print_refs() |
15 | { | 186 | { |
16 | 187 | ||
17 | html("<table class='list nowrap'>"); | 188 | html("<table class='list nowrap'>"); |
18 | 189 | ||
19 | if (cgit_query_path && !strncmp(cgit_query_path, "heads", 5)) | 190 | if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5)) |
20 | cgit_print_branches(0); | 191 | cgit_print_branches(0); |
21 | else if (cgit_query_path && !strncmp(cgit_query_path, "tags", 4)) | 192 | else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4)) |
22 | cgit_print_tags(0); | 193 | cgit_print_tags(0); |
23 | else { | 194 | else { |
24 | cgit_print_branches(0); | 195 | cgit_print_branches(0); |
25 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); | 196 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); |
26 | cgit_print_tags(0); | 197 | cgit_print_tags(0); |
27 | } | 198 | } |
28 | 199 | ||
29 | html("</table>"); | 200 | html("</table>"); |
30 | } | 201 | } |
diff --git a/ui-refs.h b/ui-refs.h new file mode 100644 index 0000000..b35c04a --- a/dev/null +++ b/ui-refs.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef UI_REFS_H | ||
2 | #define UI_REFS_H | ||
3 | |||
4 | extern void cgit_print_branches(int maxcount); | ||
5 | extern void cgit_print_tags(int maxcount); | ||
6 | extern void cgit_print_refs(); | ||
7 | |||
8 | #endif /* UI_REFS_H */ | ||
diff --git a/ui-repolist.c b/ui-repolist.c index 3e97ca9..eeeaf3d 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -1,110 +1,114 @@ | |||
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" | ||
10 | #include <time.h> | 9 | #include <time.h> |
11 | 10 | ||
11 | #include "cgit.h" | ||
12 | #include "html.h" | ||
13 | #include "ui-shared.h" | ||
12 | 14 | ||
13 | time_t read_agefile(char *path) | 15 | time_t read_agefile(char *path) |
14 | { | 16 | { |
15 | FILE *f; | 17 | FILE *f; |
16 | static char buf[64], buf2[64]; | 18 | static char buf[64], buf2[64]; |
17 | 19 | ||
18 | if (!(f = fopen(path, "r"))) | 20 | if (!(f = fopen(path, "r"))) |
19 | return -1; | 21 | return -1; |
20 | fgets(buf, sizeof(buf), f); | 22 | fgets(buf, sizeof(buf), f); |
21 | fclose(f); | 23 | fclose(f); |
22 | if (parse_date(buf, buf2, sizeof(buf2))) | 24 | if (parse_date(buf, buf2, sizeof(buf2))) |
23 | return strtoul(buf2, NULL, 10); | 25 | return strtoul(buf2, NULL, 10); |
24 | else | 26 | else |
25 | return 0; | 27 | return 0; |
26 | } | 28 | } |
27 | 29 | ||
28 | static void print_modtime(struct repoinfo *repo) | 30 | static void print_modtime(struct cgit_repo *repo) |
29 | { | 31 | { |
30 | char *path; | 32 | char *path; |
31 | struct stat s; | 33 | struct stat s; |
32 | 34 | ||
33 | path = fmt("%s/%s", repo->path, cgit_agefile); | 35 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
34 | if (stat(path, &s) == 0) { | 36 | if (stat(path, &s) == 0) { |
35 | cgit_print_age(read_agefile(path), -1, NULL); | 37 | cgit_print_age(read_agefile(path), -1, NULL); |
36 | return; | 38 | return; |
37 | } | 39 | } |
38 | 40 | ||
39 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); | 41 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); |
40 | if (stat(path, &s) != 0) | 42 | if (stat(path, &s) != 0) |
41 | return; | 43 | return; |
42 | cgit_print_age(s.st_mtime, -1, NULL); | 44 | cgit_print_age(s.st_mtime, -1, NULL); |
43 | } | 45 | } |
44 | 46 | ||
45 | void cgit_print_repolist(struct cacheitem *item) | 47 | void cgit_print_repolist() |
46 | { | 48 | { |
47 | int i, columns = 4; | 49 | int i, columns = 4; |
48 | char *last_group = NULL; | 50 | char *last_group = NULL; |
49 | 51 | ||
50 | if (cgit_enable_index_links) | 52 | if (ctx.cfg.enable_index_links) |
51 | columns++; | 53 | columns++; |
52 | 54 | ||
53 | cgit_print_docstart(cgit_root_title, item); | 55 | ctx.page.title = ctx.cfg.root_title; |
54 | cgit_print_pageheader(cgit_root_title, 0); | 56 | cgit_print_http_headers(&ctx); |
57 | cgit_print_docstart(&ctx); | ||
58 | cgit_print_pageheader(&ctx); | ||
55 | 59 | ||
56 | html("<table summary='repository list' class='list nowrap'>"); | 60 | html("<table summary='repository list' class='list nowrap'>"); |
57 | if (cgit_index_header) { | 61 | if (ctx.cfg.index_header) { |
58 | htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", | 62 | htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", |
59 | columns); | 63 | columns); |
60 | html_include(cgit_index_header); | 64 | html_include(ctx.cfg.index_header); |
61 | html("</td></tr>"); | 65 | html("</td></tr>"); |
62 | } | 66 | } |
63 | html("<tr class='nohover'>" | 67 | html("<tr class='nohover'>" |
64 | "<th class='left'>Name</th>" | 68 | "<th class='left'>Name</th>" |
65 | "<th class='left'>Description</th>" | 69 | "<th class='left'>Description</th>" |
66 | "<th class='left'>Owner</th>" | 70 | "<th class='left'>Owner</th>" |
67 | "<th class='left'>Idle</th>"); | 71 | "<th class='left'>Idle</th>"); |
68 | if (cgit_enable_index_links) | 72 | if (ctx.cfg.enable_index_links) |
69 | html("<th>Links</th>"); | 73 | html("<th>Links</th>"); |
70 | html("</tr>\n"); | 74 | html("</tr>\n"); |
71 | 75 | ||
72 | for (i=0; i<cgit_repolist.count; i++) { | 76 | for (i=0; i<cgit_repolist.count; i++) { |
73 | cgit_repo = &cgit_repolist.repos[i]; | 77 | ctx.repo = &cgit_repolist.repos[i]; |
74 | if ((last_group == NULL && cgit_repo->group != NULL) || | 78 | if ((last_group == NULL && ctx.repo->group != NULL) || |
75 | (last_group != NULL && cgit_repo->group == NULL) || | 79 | (last_group != NULL && ctx.repo->group == NULL) || |
76 | (last_group != NULL && cgit_repo->group != NULL && | 80 | (last_group != NULL && ctx.repo->group != NULL && |
77 | strcmp(cgit_repo->group, last_group))) { | 81 | strcmp(ctx.repo->group, last_group))) { |
78 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", | 82 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", |
79 | columns); | 83 | columns); |
80 | html_txt(cgit_repo->group); | 84 | html_txt(ctx.repo->group); |
81 | html("</td></tr>"); | 85 | html("</td></tr>"); |
82 | last_group = cgit_repo->group; | 86 | last_group = ctx.repo->group; |
83 | } | 87 | } |
84 | htmlf("<tr><td class='%s'>", | 88 | htmlf("<tr><td class='%s'>", |
85 | cgit_repo->group ? "sublevel-repo" : "toplevel-repo"); | 89 | ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); |
86 | html_link_open(cgit_repourl(cgit_repo->url), NULL, NULL); | 90 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); |
87 | html_txt(cgit_repo->name); | 91 | html_txt(ctx.repo->name); |
88 | html_link_close(); | 92 | html_link_close(); |
89 | html("</td><td>"); | 93 | html("</td><td>"); |
90 | html_ntxt(cgit_max_repodesc_len, cgit_repo->desc); | 94 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); |
91 | html("</td><td>"); | 95 | html("</td><td>"); |
92 | html_txt(cgit_repo->owner); | 96 | html_txt(ctx.repo->owner); |
93 | html("</td><td>"); | 97 | html("</td><td>"); |
94 | print_modtime(cgit_repo); | 98 | print_modtime(ctx.repo); |
95 | html("</td>"); | 99 | html("</td>"); |
96 | if (cgit_enable_index_links) { | 100 | if (ctx.cfg.enable_index_links) { |
97 | html("<td>"); | 101 | html("<td>"); |
98 | html_link_open(cgit_repourl(cgit_repo->url), | 102 | html_link_open(cgit_repourl(ctx.repo->url), |
99 | NULL, "button"); | 103 | NULL, "button"); |
100 | html("summary</a>"); | 104 | html("summary</a>"); |
101 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, | 105 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, |
102 | 0, NULL, NULL); | 106 | 0, NULL, NULL); |
103 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); | 107 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); |
104 | html("</td>"); | 108 | html("</td>"); |
105 | } | 109 | } |
106 | html("</tr>\n"); | 110 | html("</tr>\n"); |
107 | } | 111 | } |
108 | html("</table>"); | 112 | html("</table>"); |
109 | cgit_print_docend(); | 113 | cgit_print_docend(); |
110 | } | 114 | } |
diff --git a/ui-repolist.h b/ui-repolist.h new file mode 100644 index 0000000..c23e5d2 --- a/dev/null +++ b/ui-repolist.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_REPOLIST_H | ||
2 | #define UI_REPOLIST_H | ||
3 | |||
4 | extern void cgit_print_repolist(); | ||
5 | |||
6 | #endif /* UI_REPOLIST_H */ | ||
diff --git a/ui-shared.c b/ui-shared.c index 60aa2e3..aa65988 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,570 +1,591 @@ | |||
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 | #include "html.h" | ||
10 | 11 | ||
11 | const char cgit_doctype[] = | 12 | const char cgit_doctype[] = |
12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 13 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 14 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
14 | 15 | ||
15 | static char *http_date(time_t t) | 16 | static char *http_date(time_t t) |
16 | { | 17 | { |
17 | static char day[][4] = | 18 | static char day[][4] = |
18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 19 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
19 | static char month[][4] = | 20 | static char month[][4] = |
20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 21 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 22 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
22 | struct tm *tm = gmtime(&t); | 23 | struct tm *tm = gmtime(&t); |
23 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 24 | 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, | 25 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
25 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 26 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
26 | } | 27 | } |
27 | 28 | ||
28 | static long ttl_seconds(long ttl) | ||
29 | { | ||
30 | if (ttl<0) | ||
31 | return 60 * 60 * 24 * 365; | ||
32 | else | ||
33 | return ttl * 60; | ||
34 | } | ||
35 | |||
36 | void cgit_print_error(char *msg) | 29 | void cgit_print_error(char *msg) |
37 | { | 30 | { |
38 | html("<div class='error'>"); | 31 | html("<div class='error'>"); |
39 | html_txt(msg); | 32 | html_txt(msg); |
40 | html("</div>\n"); | 33 | html("</div>\n"); |
41 | } | 34 | } |
42 | 35 | ||
43 | char *cgit_rooturl() | 36 | char *cgit_rooturl() |
44 | { | 37 | { |
45 | if (cgit_virtual_root) | 38 | if (ctx.cfg.virtual_root) |
46 | return fmt("%s/", cgit_virtual_root); | 39 | return fmt("%s/", ctx.cfg.virtual_root); |
47 | else | 40 | else |
48 | return cgit_script_name; | 41 | return ctx.cfg.script_name; |
49 | } | 42 | } |
50 | 43 | ||
51 | char *cgit_repourl(const char *reponame) | 44 | char *cgit_repourl(const char *reponame) |
52 | { | 45 | { |
53 | if (cgit_virtual_root) { | 46 | if (ctx.cfg.virtual_root) { |
54 | return fmt("%s/%s/", cgit_virtual_root, reponame); | 47 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
55 | } else { | 48 | } else { |
56 | return fmt("?r=%s", reponame); | 49 | return fmt("?r=%s", reponame); |
57 | } | 50 | } |
58 | } | 51 | } |
59 | 52 | ||
60 | char *cgit_fileurl(const char *reponame, const char *pagename, | 53 | char *cgit_fileurl(const char *reponame, const char *pagename, |
61 | const char *filename, const char *query) | 54 | const char *filename, const char *query) |
62 | { | 55 | { |
63 | char *tmp; | 56 | char *tmp; |
64 | char *delim; | 57 | char *delim; |
65 | 58 | ||
66 | if (cgit_virtual_root) { | 59 | if (ctx.cfg.virtual_root) { |
67 | tmp = fmt("%s/%s/%s/%s", cgit_virtual_root, reponame, | 60 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
68 | pagename, (filename ? filename:"")); | 61 | pagename, (filename ? filename:"")); |
69 | delim = "?"; | 62 | delim = "?"; |
70 | } else { | 63 | } else { |
71 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 64 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
72 | (filename ? filename : "")); | 65 | (filename ? filename : "")); |
73 | delim = "&"; | 66 | delim = "&"; |
74 | } | 67 | } |
75 | if (query) | 68 | if (query) |
76 | tmp = fmt("%s%s%s", tmp, delim, query); | 69 | tmp = fmt("%s%s%s", tmp, delim, query); |
77 | return tmp; | 70 | return tmp; |
78 | } | 71 | } |
79 | 72 | ||
80 | char *cgit_pageurl(const char *reponame, const char *pagename, | 73 | char *cgit_pageurl(const char *reponame, const char *pagename, |
81 | const char *query) | 74 | const char *query) |
82 | { | 75 | { |
83 | return cgit_fileurl(reponame,pagename,0,query); | 76 | return cgit_fileurl(reponame,pagename,0,query); |
84 | } | 77 | } |
85 | 78 | ||
86 | const char *cgit_repobasename(const char *reponame) | 79 | const char *cgit_repobasename(const char *reponame) |
87 | { | 80 | { |
88 | /* I assume we don't need to store more than one repo basename */ | 81 | /* I assume we don't need to store more than one repo basename */ |
89 | static char rvbuf[1024]; | 82 | static char rvbuf[1024]; |
90 | int p; | 83 | int p; |
91 | const char *rv; | 84 | const char *rv; |
92 | strncpy(rvbuf,reponame,sizeof(rvbuf)); | 85 | strncpy(rvbuf,reponame,sizeof(rvbuf)); |
93 | if(rvbuf[sizeof(rvbuf)-1]) | 86 | if(rvbuf[sizeof(rvbuf)-1]) |
94 | die("cgit_repobasename: truncated repository name '%s'", reponame); | 87 | die("cgit_repobasename: truncated repository name '%s'", reponame); |
95 | p = strlen(rvbuf)-1; | 88 | p = strlen(rvbuf)-1; |
96 | /* strip trailing slashes */ | 89 | /* strip trailing slashes */ |
97 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; | 90 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; |
98 | /* strip trailing .git */ | 91 | /* strip trailing .git */ |
99 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { | 92 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { |
100 | p -= 3; rvbuf[p--] = 0; | 93 | p -= 3; rvbuf[p--] = 0; |
101 | } | 94 | } |
102 | /* strip more trailing slashes if any */ | 95 | /* strip more trailing slashes if any */ |
103 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; | 96 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; |
104 | /* find last slash in the remaining string */ | 97 | /* find last slash in the remaining string */ |
105 | rv = strrchr(rvbuf,'/'); | 98 | rv = strrchr(rvbuf,'/'); |
106 | if(rv) | 99 | if(rv) |
107 | return ++rv; | 100 | return ++rv; |
108 | return rvbuf; | 101 | return rvbuf; |
109 | } | 102 | } |
110 | 103 | ||
111 | char *cgit_currurl() | 104 | char *cgit_currurl() |
112 | { | 105 | { |
113 | if (!cgit_virtual_root) | 106 | if (!ctx.cfg.virtual_root) |
114 | return cgit_script_name; | 107 | return ctx.cfg.script_name; |
115 | else if (cgit_query_page) | 108 | else if (ctx.qry.page) |
116 | return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); | 109 | return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); |
117 | else if (cgit_query_repo) | 110 | else if (ctx.qry.repo) |
118 | return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); | 111 | return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); |
119 | else | 112 | else |
120 | return fmt("%s/", cgit_virtual_root); | 113 | return fmt("%s/", ctx.cfg.virtual_root); |
121 | } | 114 | } |
122 | 115 | ||
123 | static char *repolink(char *title, char *class, char *page, char *head, | 116 | static char *repolink(char *title, char *class, char *page, char *head, |
124 | char *path) | 117 | char *path) |
125 | { | 118 | { |
126 | char *delim = "?"; | 119 | char *delim = "?"; |
127 | 120 | ||
128 | html("<a"); | 121 | html("<a"); |
129 | if (title) { | 122 | if (title) { |
130 | html(" title='"); | 123 | html(" title='"); |
131 | html_attr(title); | 124 | html_attr(title); |
132 | html("'"); | 125 | html("'"); |
133 | } | 126 | } |
134 | if (class) { | 127 | if (class) { |
135 | html(" class='"); | 128 | html(" class='"); |
136 | html_attr(class); | 129 | html_attr(class); |
137 | html("'"); | 130 | html("'"); |
138 | } | 131 | } |
139 | html(" href='"); | 132 | html(" href='"); |
140 | if (cgit_virtual_root) { | 133 | if (ctx.cfg.virtual_root) { |
141 | html_attr(cgit_virtual_root); | 134 | html_attr(ctx.cfg.virtual_root); |
142 | if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') | 135 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') |
143 | html("/"); | 136 | html("/"); |
144 | html_attr(cgit_repo->url); | 137 | html_attr(ctx.repo->url); |
145 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 138 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
146 | html("/"); | 139 | html("/"); |
147 | if (page) { | 140 | if (page) { |
148 | html(page); | 141 | html(page); |
149 | html("/"); | 142 | html("/"); |
150 | if (path) | 143 | if (path) |
151 | html_attr(path); | 144 | html_attr(path); |
152 | } | 145 | } |
153 | } else { | 146 | } else { |
154 | html(cgit_script_name); | 147 | html(ctx.cfg.script_name); |
155 | html("?url="); | 148 | html("?url="); |
156 | html_attr(cgit_repo->url); | 149 | html_attr(ctx.repo->url); |
157 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 150 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
158 | html("/"); | 151 | html("/"); |
159 | if (page) { | 152 | if (page) { |
160 | html(page); | 153 | html(page); |
161 | html("/"); | 154 | html("/"); |
162 | if (path) | 155 | if (path) |
163 | html_attr(path); | 156 | html_attr(path); |
164 | } | 157 | } |
165 | delim = "&"; | 158 | delim = "&"; |
166 | } | 159 | } |
167 | if (head && strcmp(head, cgit_repo->defbranch)) { | 160 | if (head && strcmp(head, ctx.repo->defbranch)) { |
168 | html(delim); | 161 | html(delim); |
169 | html("h="); | 162 | html("h="); |
170 | html_attr(head); | 163 | html_attr(head); |
171 | delim = "&"; | 164 | delim = "&"; |
172 | } | 165 | } |
173 | return fmt("%s", delim); | 166 | return fmt("%s", delim); |
174 | } | 167 | } |
175 | 168 | ||
176 | static void reporevlink(char *page, char *name, char *title, char *class, | 169 | static void reporevlink(char *page, char *name, char *title, char *class, |
177 | char *head, char *rev, char *path) | 170 | char *head, char *rev, char *path) |
178 | { | 171 | { |
179 | char *delim; | 172 | char *delim; |
180 | 173 | ||
181 | delim = repolink(title, class, page, head, path); | 174 | delim = repolink(title, class, page, head, path); |
182 | if (rev && strcmp(rev, cgit_query_head)) { | 175 | if (rev && strcmp(rev, ctx.qry.head)) { |
183 | html(delim); | 176 | html(delim); |
184 | html("id="); | 177 | html("id="); |
185 | html_attr(rev); | 178 | html_attr(rev); |
186 | } | 179 | } |
187 | html("'>"); | 180 | html("'>"); |
188 | html_txt(name); | 181 | html_txt(name); |
189 | html("</a>"); | 182 | html("</a>"); |
190 | } | 183 | } |
191 | 184 | ||
192 | void cgit_tree_link(char *name, char *title, char *class, char *head, | 185 | void cgit_tree_link(char *name, char *title, char *class, char *head, |
193 | char *rev, char *path) | 186 | char *rev, char *path) |
194 | { | 187 | { |
195 | reporevlink("tree", name, title, class, head, rev, path); | 188 | reporevlink("tree", name, title, class, head, rev, path); |
196 | } | 189 | } |
197 | 190 | ||
198 | void cgit_log_link(char *name, char *title, char *class, char *head, | 191 | void cgit_log_link(char *name, char *title, char *class, char *head, |
199 | char *rev, char *path, int ofs, char *grep, char *pattern) | 192 | char *rev, char *path, int ofs, char *grep, char *pattern) |
200 | { | 193 | { |
201 | char *delim; | 194 | char *delim; |
202 | 195 | ||
203 | delim = repolink(title, class, "log", head, path); | 196 | delim = repolink(title, class, "log", head, path); |
204 | if (rev && strcmp(rev, cgit_query_head)) { | 197 | if (rev && strcmp(rev, ctx.qry.head)) { |
205 | html(delim); | 198 | html(delim); |
206 | html("id="); | 199 | html("id="); |
207 | html_attr(rev); | 200 | html_attr(rev); |
208 | delim = "&"; | 201 | delim = "&"; |
209 | } | 202 | } |
210 | if (grep && pattern) { | 203 | if (grep && pattern) { |
211 | html(delim); | 204 | html(delim); |
212 | html("qt="); | 205 | html("qt="); |
213 | html_attr(grep); | 206 | html_attr(grep); |
214 | delim = "&"; | 207 | delim = "&"; |
215 | html(delim); | 208 | html(delim); |
216 | html("q="); | 209 | html("q="); |
217 | html_attr(pattern); | 210 | html_attr(pattern); |
218 | } | 211 | } |
219 | if (ofs > 0) { | 212 | if (ofs > 0) { |
220 | html(delim); | 213 | html(delim); |
221 | html("ofs="); | 214 | html("ofs="); |
222 | htmlf("%d", ofs); | 215 | htmlf("%d", ofs); |
223 | } | 216 | } |
224 | html("'>"); | 217 | html("'>"); |
225 | html_txt(name); | 218 | html_txt(name); |
226 | html("</a>"); | 219 | html("</a>"); |
227 | } | 220 | } |
228 | 221 | ||
229 | void cgit_commit_link(char *name, char *title, char *class, char *head, | 222 | void cgit_commit_link(char *name, char *title, char *class, char *head, |
230 | char *rev) | 223 | char *rev) |
231 | { | 224 | { |
232 | if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { | 225 | if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { |
233 | name[cgit_max_msg_len] = '\0'; | 226 | name[ctx.cfg.max_msg_len] = '\0'; |
234 | name[cgit_max_msg_len - 1] = '.'; | 227 | name[ctx.cfg.max_msg_len - 1] = '.'; |
235 | name[cgit_max_msg_len - 2] = '.'; | 228 | name[ctx.cfg.max_msg_len - 2] = '.'; |
236 | name[cgit_max_msg_len - 3] = '.'; | 229 | name[ctx.cfg.max_msg_len - 3] = '.'; |
237 | } | 230 | } |
238 | reporevlink("commit", name, title, class, head, rev, NULL); | 231 | reporevlink("commit", name, title, class, head, rev, NULL); |
239 | } | 232 | } |
240 | 233 | ||
241 | void cgit_refs_link(char *name, char *title, char *class, char *head, | 234 | void cgit_refs_link(char *name, char *title, char *class, char *head, |
242 | char *rev, char *path) | 235 | char *rev, char *path) |
243 | { | 236 | { |
244 | reporevlink("refs", name, title, class, head, rev, path); | 237 | reporevlink("refs", name, title, class, head, rev, path); |
245 | } | 238 | } |
246 | 239 | ||
247 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, | 240 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, |
248 | char *rev, char *archivename) | 241 | char *rev, char *archivename) |
249 | { | 242 | { |
250 | reporevlink("snapshot", name, title, class, head, rev, archivename); | 243 | reporevlink("snapshot", name, title, class, head, rev, archivename); |
251 | } | 244 | } |
252 | 245 | ||
253 | void cgit_diff_link(char *name, char *title, char *class, char *head, | 246 | void cgit_diff_link(char *name, char *title, char *class, char *head, |
254 | char *new_rev, char *old_rev, char *path) | 247 | char *new_rev, char *old_rev, char *path) |
255 | { | 248 | { |
256 | char *delim; | 249 | char *delim; |
257 | 250 | ||
258 | delim = repolink(title, class, "diff", head, path); | 251 | delim = repolink(title, class, "diff", head, path); |
259 | if (new_rev && strcmp(new_rev, cgit_query_head)) { | 252 | if (new_rev && strcmp(new_rev, ctx.qry.head)) { |
260 | html(delim); | 253 | html(delim); |
261 | html("id="); | 254 | html("id="); |
262 | html_attr(new_rev); | 255 | html_attr(new_rev); |
263 | delim = "&"; | 256 | delim = "&"; |
264 | } | 257 | } |
265 | if (old_rev) { | 258 | if (old_rev) { |
266 | html(delim); | 259 | html(delim); |
267 | html("id2="); | 260 | html("id2="); |
268 | html_attr(old_rev); | 261 | html_attr(old_rev); |
269 | } | 262 | } |
270 | html("'>"); | 263 | html("'>"); |
271 | html_txt(name); | 264 | html_txt(name); |
272 | html("</a>"); | 265 | html("</a>"); |
273 | } | 266 | } |
274 | 267 | ||
275 | void cgit_patch_link(char *name, char *title, char *class, char *head, | 268 | void cgit_patch_link(char *name, char *title, char *class, char *head, |
276 | char *rev) | 269 | char *rev) |
277 | { | 270 | { |
278 | reporevlink("patch", name, title, class, head, rev, NULL); | 271 | reporevlink("patch", name, title, class, head, rev, NULL); |
279 | } | 272 | } |
280 | 273 | ||
281 | void cgit_object_link(struct object *obj) | 274 | void cgit_object_link(struct object *obj) |
282 | { | 275 | { |
283 | char *page, *arg, *url; | 276 | char *page, *arg, *url; |
284 | 277 | ||
285 | if (obj->type == OBJ_COMMIT) { | 278 | if (obj->type == OBJ_COMMIT) { |
286 | cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, | 279 | cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, |
287 | cgit_query_head, sha1_to_hex(obj->sha1)); | 280 | ctx.qry.head, sha1_to_hex(obj->sha1)); |
288 | return; | 281 | return; |
289 | } else if (obj->type == OBJ_TREE) { | 282 | } else if (obj->type == OBJ_TREE) { |
290 | page = "tree"; | 283 | page = "tree"; |
291 | arg = "id"; | 284 | arg = "id"; |
292 | } else if (obj->type == OBJ_TAG) { | 285 | } else if (obj->type == OBJ_TAG) { |
293 | page = "tag"; | 286 | page = "tag"; |
294 | arg = "id"; | 287 | arg = "id"; |
295 | } else { | 288 | } else { |
296 | page = "blob"; | 289 | page = "blob"; |
297 | arg = "id"; | 290 | arg = "id"; |
298 | } | 291 | } |
299 | 292 | ||
300 | url = cgit_pageurl(cgit_query_repo, page, | 293 | url = cgit_pageurl(ctx.qry.repo, page, |
301 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); | 294 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); |
302 | html_link_open(url, NULL, NULL); | 295 | html_link_open(url, NULL, NULL); |
303 | htmlf("%s %s", typename(obj->type), | 296 | htmlf("%s %s", typename(obj->type), |
304 | sha1_to_hex(obj->sha1)); | 297 | sha1_to_hex(obj->sha1)); |
305 | html_link_close(); | 298 | html_link_close(); |
306 | } | 299 | } |
307 | 300 | ||
308 | void cgit_print_date(time_t secs, char *format) | 301 | void cgit_print_date(time_t secs, char *format) |
309 | { | 302 | { |
310 | char buf[64]; | 303 | char buf[64]; |
311 | struct tm *time; | 304 | struct tm *time; |
312 | 305 | ||
313 | if (!secs) | 306 | if (!secs) |
314 | return; | 307 | return; |
315 | time = gmtime(&secs); | 308 | time = gmtime(&secs); |
316 | strftime(buf, sizeof(buf)-1, format, time); | 309 | strftime(buf, sizeof(buf)-1, format, time); |
317 | html_txt(buf); | 310 | html_txt(buf); |
318 | } | 311 | } |
319 | 312 | ||
320 | void cgit_print_age(time_t t, time_t max_relative, char *format) | 313 | void cgit_print_age(time_t t, time_t max_relative, char *format) |
321 | { | 314 | { |
322 | time_t now, secs; | 315 | time_t now, secs; |
323 | 316 | ||
324 | if (!t) | 317 | if (!t) |
325 | return; | 318 | return; |
326 | time(&now); | 319 | time(&now); |
327 | secs = now - t; | 320 | secs = now - t; |
328 | 321 | ||
329 | if (secs > max_relative && max_relative >= 0) { | 322 | if (secs > max_relative && max_relative >= 0) { |
330 | cgit_print_date(t, format); | 323 | cgit_print_date(t, format); |
331 | return; | 324 | return; |
332 | } | 325 | } |
333 | 326 | ||
334 | if (secs < TM_HOUR * 2) { | 327 | if (secs < TM_HOUR * 2) { |
335 | htmlf("<span class='age-mins'>%.0f min.</span>", | 328 | htmlf("<span class='age-mins'>%.0f min.</span>", |
336 | secs * 1.0 / TM_MIN); | 329 | secs * 1.0 / TM_MIN); |
337 | return; | 330 | return; |
338 | } | 331 | } |
339 | if (secs < TM_DAY * 2) { | 332 | if (secs < TM_DAY * 2) { |
340 | htmlf("<span class='age-hours'>%.0f hours</span>", | 333 | htmlf("<span class='age-hours'>%.0f hours</span>", |
341 | secs * 1.0 / TM_HOUR); | 334 | secs * 1.0 / TM_HOUR); |
342 | return; | 335 | return; |
343 | } | 336 | } |
344 | if (secs < TM_WEEK * 2) { | 337 | if (secs < TM_WEEK * 2) { |
345 | htmlf("<span class='age-days'>%.0f days</span>", | 338 | htmlf("<span class='age-days'>%.0f days</span>", |
346 | secs * 1.0 / TM_DAY); | 339 | secs * 1.0 / TM_DAY); |
347 | return; | 340 | return; |
348 | } | 341 | } |
349 | if (secs < TM_MONTH * 2) { | 342 | if (secs < TM_MONTH * 2) { |
350 | htmlf("<span class='age-weeks'>%.0f weeks</span>", | 343 | htmlf("<span class='age-weeks'>%.0f weeks</span>", |
351 | secs * 1.0 / TM_WEEK); | 344 | secs * 1.0 / TM_WEEK); |
352 | return; | 345 | return; |
353 | } | 346 | } |
354 | if (secs < TM_YEAR * 2) { | 347 | if (secs < TM_YEAR * 2) { |
355 | htmlf("<span class='age-months'>%.0f months</span>", | 348 | htmlf("<span class='age-months'>%.0f months</span>", |
356 | secs * 1.0 / TM_MONTH); | 349 | secs * 1.0 / TM_MONTH); |
357 | return; | 350 | return; |
358 | } | 351 | } |
359 | htmlf("<span class='age-years'>%.0f years</span>", | 352 | htmlf("<span class='age-years'>%.0f years</span>", |
360 | secs * 1.0 / TM_YEAR); | 353 | secs * 1.0 / TM_YEAR); |
361 | } | 354 | } |
362 | 355 | ||
363 | void cgit_print_docstart(char *title, struct cacheitem *item) | 356 | void cgit_print_http_headers(struct cgit_context *ctx) |
364 | { | 357 | { |
365 | html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); | 358 | if (ctx->page.mimetype && ctx->page.charset) |
366 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 359 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, |
367 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 360 | ctx->page.charset); |
368 | ttl_seconds(item->ttl))); | 361 | else if (ctx->page.mimetype) |
362 | htmlf("Content-Type: %s\n", ctx->page.mimetype); | ||
363 | if (ctx->page.filename) | ||
364 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", | ||
365 | ctx->page.filename); | ||
366 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); | ||
367 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); | ||
369 | html("\n"); | 368 | html("\n"); |
369 | } | ||
370 | |||
371 | void cgit_print_docstart(struct cgit_context *ctx) | ||
372 | { | ||
370 | html(cgit_doctype); | 373 | html(cgit_doctype); |
371 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); | 374 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); |
372 | html("<head>\n"); | 375 | html("<head>\n"); |
373 | html("<title>"); | 376 | html("<title>"); |
374 | html_txt(title); | 377 | html_txt(ctx->page.title); |
375 | html("</title>\n"); | 378 | html("</title>\n"); |
376 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 379 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
377 | if (cgit_robots && *cgit_robots) | 380 | if (ctx->cfg.robots && *ctx->cfg.robots) |
378 | htmlf("<meta name='robots' content='%s'/>\n", cgit_robots); | 381 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); |
379 | html("<link rel='stylesheet' type='text/css' href='"); | 382 | html("<link rel='stylesheet' type='text/css' href='"); |
380 | html_attr(cgit_css); | 383 | html_attr(ctx->cfg.css); |
381 | html("'/>\n"); | 384 | html("'/>\n"); |
382 | html("</head>\n"); | 385 | html("</head>\n"); |
383 | html("<body>\n"); | 386 | html("<body>\n"); |
384 | } | 387 | } |
385 | 388 | ||
386 | void cgit_print_docend() | 389 | void cgit_print_docend() |
387 | { | 390 | { |
388 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); | 391 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); |
389 | } | 392 | } |
390 | 393 | ||
391 | int print_branch_option(const char *refname, const unsigned char *sha1, | 394 | int print_branch_option(const char *refname, const unsigned char *sha1, |
392 | int flags, void *cb_data) | 395 | int flags, void *cb_data) |
393 | { | 396 | { |
394 | char *name = (char *)refname; | 397 | char *name = (char *)refname; |
395 | html_option(name, name, cgit_query_head); | 398 | html_option(name, name, ctx.qry.head); |
396 | return 0; | 399 | return 0; |
397 | } | 400 | } |
398 | 401 | ||
399 | int print_archive_ref(const char *refname, const unsigned char *sha1, | 402 | int print_archive_ref(const char *refname, const unsigned char *sha1, |
400 | int flags, void *cb_data) | 403 | int flags, void *cb_data) |
401 | { | 404 | { |
402 | struct tag *tag; | 405 | struct tag *tag; |
403 | struct taginfo *info; | 406 | struct taginfo *info; |
404 | struct object *obj; | 407 | struct object *obj; |
405 | char buf[256], *url; | 408 | char buf[256], *url; |
406 | unsigned char fileid[20]; | 409 | unsigned char fileid[20]; |
407 | int *header = (int *)cb_data; | 410 | int *header = (int *)cb_data; |
408 | 411 | ||
409 | if (prefixcmp(refname, "refs/archives")) | 412 | if (prefixcmp(refname, "refs/archives")) |
410 | return 0; | 413 | return 0; |
411 | strncpy(buf, refname+14, sizeof(buf)); | 414 | strncpy(buf, refname+14, sizeof(buf)); |
412 | obj = parse_object(sha1); | 415 | obj = parse_object(sha1); |
413 | if (!obj) | 416 | if (!obj) |
414 | return 1; | 417 | return 1; |
415 | if (obj->type == OBJ_TAG) { | 418 | if (obj->type == OBJ_TAG) { |
416 | tag = lookup_tag(sha1); | 419 | tag = lookup_tag(sha1); |
417 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) | 420 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) |
418 | return 0; | 421 | return 0; |
419 | hashcpy(fileid, tag->tagged->sha1); | 422 | hashcpy(fileid, tag->tagged->sha1); |
420 | } else if (obj->type != OBJ_BLOB) { | 423 | } else if (obj->type != OBJ_BLOB) { |
421 | return 0; | 424 | return 0; |
422 | } else { | 425 | } else { |
423 | hashcpy(fileid, sha1); | 426 | hashcpy(fileid, sha1); |
424 | } | 427 | } |
425 | if (!*header) { | 428 | if (!*header) { |
426 | html("<h1>download</h1>\n"); | 429 | html("<h1>download</h1>\n"); |
427 | *header = 1; | 430 | *header = 1; |
428 | } | 431 | } |
429 | url = cgit_pageurl(cgit_query_repo, "blob", | 432 | url = cgit_pageurl(ctx.qry.repo, "blob", |
430 | fmt("id=%s&path=%s", sha1_to_hex(fileid), | 433 | fmt("id=%s&path=%s", sha1_to_hex(fileid), |
431 | buf)); | 434 | buf)); |
432 | html_link_open(url, NULL, "menu"); | 435 | html_link_open(url, NULL, "menu"); |
433 | html_txt(strlpart(buf, 20)); | 436 | html_txt(strlpart(buf, 20)); |
434 | html_link_close(); | 437 | html_link_close(); |
435 | return 0; | 438 | return 0; |
436 | } | 439 | } |
437 | 440 | ||
438 | void add_hidden_formfields(int incl_head, int incl_search, char *page) | 441 | void add_hidden_formfields(int incl_head, int incl_search, char *page) |
439 | { | 442 | { |
440 | char *url; | 443 | char *url; |
441 | 444 | ||
442 | if (!cgit_virtual_root) { | 445 | if (!ctx.cfg.virtual_root) { |
443 | url = fmt("%s/%s", cgit_query_repo, page); | 446 | url = fmt("%s/%s", ctx.qry.repo, page); |
444 | if (cgit_query_path) | 447 | if (ctx.qry.path) |
445 | url = fmt("%s/%s", url, cgit_query_path); | 448 | url = fmt("%s/%s", url, ctx.qry.path); |
446 | html_hidden("url", url); | 449 | html_hidden("url", url); |
447 | } | 450 | } |
448 | 451 | ||
449 | if (incl_head && strcmp(cgit_query_head, cgit_repo->defbranch)) | 452 | if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) |
450 | html_hidden("h", cgit_query_head); | 453 | html_hidden("h", ctx.qry.head); |
451 | 454 | ||
452 | if (cgit_query_sha1) | 455 | if (ctx.qry.sha1) |
453 | html_hidden("id", cgit_query_sha1); | 456 | html_hidden("id", ctx.qry.sha1); |
454 | if (cgit_query_sha2) | 457 | if (ctx.qry.sha2) |
455 | html_hidden("id2", cgit_query_sha2); | 458 | html_hidden("id2", ctx.qry.sha2); |
456 | 459 | ||
457 | if (incl_search) { | 460 | if (incl_search) { |
458 | if (cgit_query_grep) | 461 | if (ctx.qry.grep) |
459 | html_hidden("qt", cgit_query_grep); | 462 | html_hidden("qt", ctx.qry.grep); |
460 | if (cgit_query_search) | 463 | if (ctx.qry.search) |
461 | html_hidden("q", cgit_query_search); | 464 | html_hidden("q", ctx.qry.search); |
462 | } | 465 | } |
463 | } | 466 | } |
464 | 467 | ||
465 | void cgit_print_pageheader(char *title, int show_search) | 468 | void cgit_print_pageheader(struct cgit_context *ctx) |
466 | { | 469 | { |
467 | static const char *default_info = "This is cgit, a fast webinterface for git repositories"; | 470 | static const char *default_info = "This is cgit, a fast webinterface for git repositories"; |
468 | int header = 0; | 471 | int header = 0; |
469 | char *url; | 472 | char *url; |
470 | 473 | ||
471 | html("<table id='layout' summary=''>\n"); | 474 | html("<table id='layout' summary=''>\n"); |
472 | html("<tr><td id='sidebar'>\n"); | 475 | html("<tr><td id='sidebar'>\n"); |
473 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); | 476 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); |
474 | html("<tr><td class='sidebar'>\n<a href='"); | 477 | html("<tr><td class='sidebar'>\n<a href='"); |
475 | html_attr(cgit_rooturl()); | 478 | html_attr(cgit_rooturl()); |
476 | htmlf("'><img src='%s' alt='cgit'/></a>\n", | 479 | htmlf("'><img src='%s' alt='cgit'/></a>\n", |
477 | cgit_logo); | 480 | ctx->cfg.logo); |
478 | html("</td></tr>\n<tr><td class='sidebar'>\n"); | 481 | html("</td></tr>\n<tr><td class='sidebar'>\n"); |
479 | if (cgit_query_repo) { | 482 | if (ctx->repo) { |
480 | html("<h1 class='first'>"); | 483 | html("<h1 class='first'>"); |
481 | html_txt(strrpart(cgit_repo->name, 20)); | 484 | html_txt(strrpart(ctx->repo->name, 20)); |
482 | html("</h1>\n"); | 485 | html("</h1>\n"); |
483 | html_txt(cgit_repo->desc); | 486 | html_txt(ctx->repo->desc); |
484 | if (cgit_repo->owner) { | 487 | if (ctx->repo->owner) { |
485 | html("<h1>owner</h1>\n"); | 488 | html("<h1>owner</h1>\n"); |
486 | html_txt(cgit_repo->owner); | 489 | html_txt(ctx->repo->owner); |
487 | } | 490 | } |
488 | html("<h1>navigate</h1>\n"); | 491 | html("<h1>navigate</h1>\n"); |
489 | reporevlink(NULL, "summary", NULL, "menu", cgit_query_head, | 492 | reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head, |
490 | NULL, NULL); | 493 | NULL, NULL); |
491 | cgit_log_link("log", NULL, "menu", cgit_query_head, NULL, NULL, | 494 | cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL, |
492 | 0, NULL, NULL); | 495 | 0, NULL, NULL); |
493 | cgit_tree_link("tree", NULL, "menu", cgit_query_head, | 496 | cgit_tree_link("tree", NULL, "menu", ctx->qry.head, |
494 | cgit_query_sha1, NULL); | 497 | ctx->qry.sha1, NULL); |
495 | cgit_commit_link("commit", NULL, "menu", cgit_query_head, | 498 | cgit_commit_link("commit", NULL, "menu", ctx->qry.head, |
496 | cgit_query_sha1); | 499 | ctx->qry.sha1); |
497 | cgit_diff_link("diff", NULL, "menu", cgit_query_head, | 500 | cgit_diff_link("diff", NULL, "menu", ctx->qry.head, |
498 | cgit_query_sha1, cgit_query_sha2, NULL); | 501 | ctx->qry.sha1, ctx->qry.sha2, NULL); |
499 | cgit_patch_link("patch", NULL, "menu", cgit_query_head, | 502 | cgit_patch_link("patch", NULL, "menu", ctx->qry.head, |
500 | cgit_query_sha1); | 503 | ctx->qry.sha1); |
501 | 504 | ||
502 | for_each_ref(print_archive_ref, &header); | 505 | for_each_ref(print_archive_ref, &header); |
503 | 506 | ||
504 | if (cgit_repo->clone_url || cgit_clone_prefix) { | 507 | if (ctx->repo->clone_url || ctx->cfg.clone_prefix) { |
505 | html("<h1>clone</h1>\n"); | 508 | html("<h1>clone</h1>\n"); |
506 | if (cgit_repo->clone_url) | 509 | if (ctx->repo->clone_url) |
507 | url = cgit_repo->clone_url; | 510 | url = ctx->repo->clone_url; |
508 | else | 511 | else |
509 | url = fmt("%s%s", cgit_clone_prefix, | 512 | url = fmt("%s%s", ctx->cfg.clone_prefix, |
510 | cgit_repo->url); | 513 | ctx->repo->url); |
511 | html("<a class='menu' href='"); | 514 | html("<a class='menu' href='"); |
512 | html_attr(url); | 515 | html_attr(url); |
513 | html("' title='"); | 516 | html("' title='"); |
514 | html_attr(url); | 517 | html_attr(url); |
515 | html("'>\n"); | 518 | html("'>\n"); |
516 | html_txt(strrpart(url, 20)); | 519 | html_txt(strrpart(url, 20)); |
517 | html("</a>\n"); | 520 | html("</a>\n"); |
518 | } | 521 | } |
519 | 522 | ||
520 | html("<h1>branch</h1>\n"); | 523 | html("<h1>branch</h1>\n"); |
521 | html("<form method='get' action=''>\n"); | 524 | html("<form method='get' action=''>\n"); |
522 | add_hidden_formfields(0, 1, cgit_query_page); | 525 | add_hidden_formfields(0, 1, ctx->qry.page); |
523 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); | 526 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); |
524 | html("<select name='h' onchange='this.form.submit();'>\n"); | 527 | html("<select name='h' onchange='this.form.submit();'>\n"); |
525 | for_each_branch_ref(print_branch_option, cgit_query_head); | 528 | for_each_branch_ref(print_branch_option, ctx->qry.head); |
526 | html("</select>\n"); | 529 | html("</select>\n"); |
527 | // html("</td><td>"); | 530 | // html("</td><td>"); |
528 | html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); | 531 | html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); |
529 | // html("</td></tr></table>"); | 532 | // html("</td></tr></table>"); |
530 | html("</form>\n"); | 533 | html("</form>\n"); |
531 | 534 | ||
532 | html("<h1>search</h1>\n"); | 535 | html("<h1>search</h1>\n"); |
533 | html("<form method='get' action='"); | 536 | html("<form method='get' action='"); |
534 | if (cgit_virtual_root) | 537 | if (ctx->cfg.virtual_root) |
535 | html_attr(cgit_fileurl(cgit_query_repo, "log", | 538 | html_attr(cgit_fileurl(ctx->qry.repo, "log", |
536 | cgit_query_path, NULL)); | 539 | ctx->qry.path, NULL)); |
537 | html("'>\n"); | 540 | html("'>\n"); |
538 | add_hidden_formfields(1, 0, "log"); | 541 | add_hidden_formfields(1, 0, "log"); |
539 | html("<select name='qt'>\n"); | 542 | html("<select name='qt'>\n"); |
540 | html_option("grep", "log msg", cgit_query_grep); | 543 | html_option("grep", "log msg", ctx->qry.grep); |
541 | html_option("author", "author", cgit_query_grep); | 544 | html_option("author", "author", ctx->qry.grep); |
542 | html_option("committer", "committer", cgit_query_grep); | 545 | html_option("committer", "committer", ctx->qry.grep); |
543 | html("</select>\n"); | 546 | html("</select>\n"); |
544 | html("<input class='txt' type='text' name='q' value='"); | 547 | html("<input class='txt' type='text' name='q' value='"); |
545 | html_attr(cgit_query_search); | 548 | html_attr(ctx->qry.search); |
546 | html("'/>\n"); | 549 | html("'/>\n"); |
547 | html("</form>\n"); | 550 | html("</form>\n"); |
548 | } else { | 551 | } else { |
549 | if (!cgit_index_info || html_include(cgit_index_info)) | 552 | if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info)) |
550 | html(default_info); | 553 | html(default_info); |
551 | } | 554 | } |
552 | 555 | ||
553 | html("</td></tr></table></td>\n"); | 556 | html("</td></tr></table></td>\n"); |
554 | 557 | ||
555 | html("<td id='content'>\n"); | 558 | html("<td id='content'>\n"); |
556 | } | 559 | } |
557 | 560 | ||
558 | 561 | void cgit_print_filemode(unsigned short mode) | |
559 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, | ||
560 | struct cacheitem *item) | ||
561 | { | 562 | { |
562 | htmlf("Content-Type: %s\n", mimetype); | 563 | if (S_ISDIR(mode)) |
563 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); | 564 | html("d"); |
564 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 565 | else if (S_ISLNK(mode)) |
565 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 566 | html("l"); |
566 | ttl_seconds(item->ttl))); | 567 | else if (S_ISGITLINK(mode)) |
567 | html("\n"); | 568 | html("m"); |
569 | else | ||
570 | html("-"); | ||
571 | html_fileperm(mode >> 6); | ||
572 | html_fileperm(mode >> 3); | ||
573 | html_fileperm(mode); | ||
568 | } | 574 | } |
569 | 575 | ||
570 | /* vim:set sw=8: */ | 576 | void cgit_print_snapshot_links(const char *repo, const char *head, |
577 | const char *hex, int snapshots) | ||
578 | { | ||
579 | const struct cgit_snapshot_format* f; | ||
580 | char *filename; | ||
581 | |||
582 | for (f = cgit_snapshot_formats; f->suffix; f++) { | ||
583 | if (!(snapshots & f->bit)) | ||
584 | continue; | ||
585 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, | ||
586 | f->suffix); | ||
587 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, | ||
588 | (char *)hex, filename); | ||
589 | html("<br/>"); | ||
590 | } | ||
591 | } | ||
diff --git a/ui-shared.h b/ui-shared.h new file mode 100644 index 0000000..94de884 --- a/dev/null +++ b/ui-shared.h | |||
@@ -0,0 +1,36 @@ | |||
1 | #ifndef UI_SHARED_H | ||
2 | #define UI_SHARED_H | ||
3 | |||
4 | extern char *cgit_repourl(const char *reponame); | ||
5 | extern char *cgit_fileurl(const char *reponame, const char *pagename, | ||
6 | const char *filename, const char *query); | ||
7 | extern char *cgit_pageurl(const char *reponame, const char *pagename, | ||
8 | const char *query); | ||
9 | |||
10 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, | ||
11 | char *rev, char *path); | ||
12 | extern void cgit_log_link(char *name, char *title, char *class, char *head, | ||
13 | char *rev, char *path, int ofs, char *grep, | ||
14 | char *pattern); | ||
15 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, | ||
16 | char *rev); | ||
17 | extern void cgit_refs_link(char *name, char *title, char *class, char *head, | ||
18 | char *rev, char *path); | ||
19 | extern void cgit_snapshot_link(char *name, char *title, char *class, | ||
20 | char *head, char *rev, char *archivename); | ||
21 | extern void cgit_diff_link(char *name, char *title, char *class, char *head, | ||
22 | char *new_rev, char *old_rev, char *path); | ||
23 | extern void cgit_object_link(struct object *obj); | ||
24 | |||
25 | extern void cgit_print_error(char *msg); | ||
26 | extern void cgit_print_date(time_t secs, char *format); | ||
27 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); | ||
28 | extern void cgit_print_http_headers(struct cgit_context *ctx); | ||
29 | extern void cgit_print_docstart(struct cgit_context *ctx); | ||
30 | extern void cgit_print_docend(); | ||
31 | extern void cgit_print_pageheader(struct cgit_context *ctx); | ||
32 | extern void cgit_print_filemode(unsigned short mode); | ||
33 | extern void cgit_print_snapshot_links(const char *repo, const char *head, | ||
34 | const char *hex, int snapshots); | ||
35 | |||
36 | #endif /* UI_SHARED_H */ | ||
diff --git a/ui-snapshot.c b/ui-snapshot.c index dfedd8f..966a140 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c | |||
@@ -1,157 +1,114 @@ | |||
1 | /* ui-snapshot.c: generate snapshot of a commit | 1 | /* ui-snapshot.c: generate snapshot of a commit |
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 "html.h" | ||
11 | #include "ui-shared.h" | ||
10 | 12 | ||
11 | static int write_compressed_tar_archive(struct archiver_args *args,const char *filter) | 13 | static int write_compressed_tar_archive(struct archiver_args *args,const char *filter) |
12 | { | 14 | { |
13 | int rw[2]; | 15 | int rw[2]; |
14 | pid_t gzpid; | 16 | pid_t gzpid; |
15 | int stdout2; | 17 | int stdout2; |
16 | int status; | 18 | int status; |
17 | int rv; | 19 | int rv; |
18 | 20 | ||
19 | stdout2 = chk_non_negative(dup(STDIN_FILENO), "Preserving STDOUT before compressing"); | 21 | stdout2 = chk_non_negative(dup(STDIN_FILENO), "Preserving STDOUT before compressing"); |
20 | chk_zero(pipe(rw), "Opening pipe from compressor subprocess"); | 22 | chk_zero(pipe(rw), "Opening pipe from compressor subprocess"); |
21 | gzpid = chk_non_negative(fork(), "Forking compressor subprocess"); | 23 | gzpid = chk_non_negative(fork(), "Forking compressor subprocess"); |
22 | if(gzpid==0) { | 24 | if(gzpid==0) { |
23 | /* child */ | 25 | /* child */ |
24 | chk_zero(close(rw[1]), "Closing write end of pipe in child"); | 26 | chk_zero(close(rw[1]), "Closing write end of pipe in child"); |
25 | chk_zero(close(STDIN_FILENO), "Closing STDIN"); | 27 | chk_zero(close(STDIN_FILENO), "Closing STDIN"); |
26 | chk_non_negative(dup2(rw[0],STDIN_FILENO), "Redirecting compressor input to stdin"); | 28 | chk_non_negative(dup2(rw[0],STDIN_FILENO), "Redirecting compressor input to stdin"); |
27 | execlp(filter,filter,NULL); | 29 | execlp(filter,filter,NULL); |
28 | _exit(-1); | 30 | _exit(-1); |
29 | } | 31 | } |
30 | /* parent */ | 32 | /* parent */ |
31 | chk_zero(close(rw[0]), "Closing read end of pipe"); | 33 | chk_zero(close(rw[0]), "Closing read end of pipe"); |
32 | chk_non_negative(dup2(rw[1],STDOUT_FILENO), "Redirecting output to compressor"); | 34 | chk_non_negative(dup2(rw[1],STDOUT_FILENO), "Redirecting output to compressor"); |
33 | 35 | ||
34 | rv = write_tar_archive(args); | 36 | rv = write_tar_archive(args); |
35 | 37 | ||
36 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT redirected to compressor"); | 38 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT redirected to compressor"); |
37 | chk_non_negative(dup2(stdout2,STDOUT_FILENO), "Restoring uncompressed STDOUT"); | 39 | chk_non_negative(dup2(stdout2,STDOUT_FILENO), "Restoring uncompressed STDOUT"); |
38 | chk_zero(close(stdout2), "Closing uncompressed STDOUT"); | 40 | chk_zero(close(stdout2), "Closing uncompressed STDOUT"); |
39 | chk_zero(close(rw[1]), "Closing write end of pipe in parent"); | 41 | chk_zero(close(rw[1]), "Closing write end of pipe in parent"); |
40 | chk_positive(waitpid(gzpid,&status,0), "Waiting on compressor process"); | 42 | chk_positive(waitpid(gzpid,&status,0), "Waiting on compressor process"); |
41 | if(! ( WIFEXITED(status) && WEXITSTATUS(status)==0 ) ) | 43 | if(! ( WIFEXITED(status) && WEXITSTATUS(status)==0 ) ) |
42 | cgit_print_error("Failed to compress archive"); | 44 | cgit_print_error("Failed to compress archive"); |
43 | 45 | ||
44 | return rv; | 46 | return rv; |
45 | } | 47 | } |
46 | 48 | ||
47 | static int write_tar_gzip_archive(struct archiver_args *args) | 49 | static int write_tar_gzip_archive(struct archiver_args *args) |
48 | { | 50 | { |
49 | return write_compressed_tar_archive(args,"gzip"); | 51 | return write_compressed_tar_archive(args,"gzip"); |
50 | } | 52 | } |
51 | 53 | ||
52 | static int write_tar_bzip2_archive(struct archiver_args *args) | 54 | static int write_tar_bzip2_archive(struct archiver_args *args) |
53 | { | 55 | { |
54 | return write_compressed_tar_archive(args,"bzip2"); | 56 | return write_compressed_tar_archive(args,"bzip2"); |
55 | } | 57 | } |
56 | 58 | ||
57 | static const struct snapshot_archive_t { | 59 | const struct cgit_snapshot_format cgit_snapshot_formats[] = { |
58 | const char *suffix; | ||
59 | const char *mimetype; | ||
60 | write_archive_fn_t write_func; | ||
61 | int bit; | ||
62 | }snapshot_archives[] = { | ||
63 | { ".zip", "application/x-zip", write_zip_archive, 0x1 }, | 60 | { ".zip", "application/x-zip", write_zip_archive, 0x1 }, |
64 | { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, | 61 | { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, |
65 | { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, | 62 | { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, |
66 | { ".tar", "application/x-tar", write_tar_archive, 0x8 } | 63 | { ".tar", "application/x-tar", write_tar_archive, 0x8 }, |
64 | {} | ||
67 | }; | 65 | }; |
68 | 66 | ||
69 | #define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives)) | 67 | static int make_snapshot(const struct cgit_snapshot_format *format, |
70 | |||
71 | void cgit_print_snapshot(struct cacheitem *item, const char *head, | ||
72 | const char *hex, const char *prefix, | 68 | const char *hex, const char *prefix, |
73 | const char *filename, int snapshots) | 69 | const char *filename) |
74 | { | 70 | { |
75 | const struct snapshot_archive_t* sat; | ||
76 | struct archiver_args args; | 71 | struct archiver_args args; |
77 | struct commit *commit; | 72 | struct commit *commit; |
78 | unsigned char sha1[20]; | 73 | unsigned char sha1[20]; |
79 | int f, sl, fnl = strlen(filename); | ||
80 | 74 | ||
81 | for(f=0; f<snapshot_archives_len; f++) { | 75 | if(get_sha1(hex, sha1)) { |
82 | sat = &snapshot_archives[f]; | 76 | cgit_print_error(fmt("Bad object id: %s", hex)); |
83 | if(!(snapshots & sat->bit)) | 77 | return 1; |
84 | continue; | ||
85 | sl = strlen(sat->suffix); | ||
86 | if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix)) | ||
87 | continue; | ||
88 | if (!hex) | ||
89 | hex = head; | ||
90 | if(get_sha1(hex, sha1)) { | ||
91 | cgit_print_error(fmt("Bad object id: %s", hex)); | ||
92 | return; | ||
93 | } | ||
94 | commit = lookup_commit_reference(sha1); | ||
95 | if(!commit) { | ||
96 | cgit_print_error(fmt("Not a commit reference: %s", hex)); | ||
97 | return;; | ||
98 | } | ||
99 | memset(&args,0,sizeof(args)); | ||
100 | args.base = fmt("%s/", prefix); | ||
101 | args.tree = commit->tree; | ||
102 | args.time = commit->date; | ||
103 | cgit_print_snapshot_start(sat->mimetype, filename, item); | ||
104 | (*sat->write_func)(&args); | ||
105 | return; | ||
106 | } | 78 | } |
107 | cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); | 79 | commit = lookup_commit_reference(sha1); |
108 | } | 80 | if(!commit) { |
109 | 81 | cgit_print_error(fmt("Not a commit reference: %s", hex)); | |
110 | void cgit_print_snapshot_links(const char *repo, const char *head, | 82 | return 1; |
111 | const char *hex, int snapshots) | ||
112 | { | ||
113 | const struct snapshot_archive_t* sat; | ||
114 | char *filename; | ||
115 | int f; | ||
116 | |||
117 | for(f=0; f<snapshot_archives_len; f++) { | ||
118 | sat = &snapshot_archives[f]; | ||
119 | if(!(snapshots & sat->bit)) | ||
120 | continue; | ||
121 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, | ||
122 | sat->suffix); | ||
123 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, | ||
124 | (char *)hex, filename); | ||
125 | html("<br/>"); | ||
126 | } | 83 | } |
84 | memset(&args, 0, sizeof(args)); | ||
85 | args.base = fmt("%s/", prefix); | ||
86 | args.tree = commit->tree; | ||
87 | args.time = commit->date; | ||
88 | ctx.page.mimetype = xstrdup(format->mimetype); | ||
89 | ctx.page.filename = xstrdup(filename); | ||
90 | cgit_print_http_headers(&ctx); | ||
91 | format->write_func(&args); | ||
92 | return 0; | ||
127 | } | 93 | } |
128 | 94 | ||
129 | int cgit_parse_snapshots_mask(const char *str) | 95 | void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, |
96 | const char *filename, int snapshots) | ||
130 | { | 97 | { |
131 | const struct snapshot_archive_t* sat; | 98 | const struct cgit_snapshot_format* f; |
132 | static const char *delim = " \t,:/|;"; | 99 | int sl, fnl; |
133 | int f, tl, sl, rv = 0; | 100 | |
134 | 101 | fnl = strlen(filename); | |
135 | /* favor legacy setting */ | 102 | if (!hex) |
136 | if(atoi(str)) | 103 | hex = head; |
137 | return 1; | 104 | for (f = cgit_snapshot_formats; f->suffix; f++) { |
138 | for(;;) { | 105 | if (!(snapshots & f->bit)) |
139 | str += strspn(str,delim); | 106 | continue; |
140 | tl = strcspn(str,delim); | 107 | sl = strlen(f->suffix); |
141 | if(!tl) | 108 | if(fnl < sl || strcmp(&filename[fnl-sl], f->suffix)) |
142 | break; | 109 | continue; |
143 | for(f=0; f<snapshot_archives_len; f++) { | 110 | make_snapshot(f, hex, prefix, filename); |
144 | sat = &snapshot_archives[f]; | 111 | return; |
145 | sl = strlen(sat->suffix); | ||
146 | if((tl == sl && !strncmp(sat->suffix, str, tl)) || | ||
147 | (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) { | ||
148 | rv |= sat->bit; | ||
149 | break; | ||
150 | } | ||
151 | } | ||
152 | str += tl; | ||
153 | } | 112 | } |
154 | return rv; | 113 | cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); |
155 | } | 114 | } |
156 | |||
157 | /* vim:set sw=8: */ | ||
diff --git a/ui-snapshot.h b/ui-snapshot.h new file mode 100644 index 0000000..6e03761 --- a/dev/null +++ b/ui-snapshot.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef UI_SNAPSHOT_H | ||
2 | #define UI_SNAPSHOT_H | ||
3 | |||
4 | extern void cgit_print_snapshot(const char *head, const char *hex, | ||
5 | const char *prefix, const char *filename, | ||
6 | int snapshot); | ||
7 | |||
8 | #endif /* UI_SNAPSHOT_H */ | ||
diff --git a/ui-summary.c b/ui-summary.c index b96414e..0b66b52 100644 --- a/ui-summary.c +++ b/ui-summary.c | |||
@@ -1,200 +1,31 @@ | |||
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 | #include "html.h" | |
11 | static int header; | 11 | #include "ui-log.h" |
12 | 12 | #include "ui-refs.h" | |
13 | static int cmp_age(int age1, int age2) | ||
14 | { | ||
15 | if (age1 != 0 && age2 != 0) | ||
16 | return age2 - age1; | ||
17 | |||
18 | if (age1 == 0 && age2 == 0) | ||
19 | return 0; | ||
20 | |||
21 | if (age1 == 0) | ||
22 | return +1; | ||
23 | |||
24 | return -1; | ||
25 | } | ||
26 | |||
27 | static int cmp_ref_name(const void *a, const void *b) | ||
28 | { | ||
29 | struct refinfo *r1 = *(struct refinfo **)a; | ||
30 | struct refinfo *r2 = *(struct refinfo **)b; | ||
31 | |||
32 | return strcmp(r1->refname, r2->refname); | ||
33 | } | ||
34 | |||
35 | static int cmp_branch_age(const void *a, const void *b) | ||
36 | { | ||
37 | struct refinfo *r1 = *(struct refinfo **)a; | ||
38 | struct refinfo *r2 = *(struct refinfo **)b; | ||
39 | |||
40 | return cmp_age(r1->commit->committer_date, r2->commit->committer_date); | ||
41 | } | ||
42 | |||
43 | static int cmp_tag_age(const void *a, const void *b) | ||
44 | { | ||
45 | struct refinfo *r1 = *(struct refinfo **)a; | ||
46 | struct refinfo *r2 = *(struct refinfo **)b; | ||
47 | |||
48 | return cmp_age(r1->tag->tagger_date, r2->tag->tagger_date); | ||
49 | } | ||
50 | |||
51 | static int print_branch(struct refinfo *ref) | ||
52 | { | ||
53 | struct commitinfo *info = ref->commit; | ||
54 | char *name = (char *)ref->refname; | ||
55 | |||
56 | if (!info) | ||
57 | return 1; | ||
58 | html("<tr><td>"); | ||
59 | cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL); | ||
60 | html("</td><td>"); | ||
61 | |||
62 | if (ref->object->type == OBJ_COMMIT) { | ||
63 | cgit_print_age(info->commit->date, -1, NULL); | ||
64 | html("</td><td>"); | ||
65 | html_txt(info->author); | ||
66 | html("</td><td>"); | ||
67 | cgit_commit_link(info->subject, NULL, NULL, name, NULL); | ||
68 | } else { | ||
69 | html("</td><td></td><td>"); | ||
70 | cgit_object_link(ref->object); | ||
71 | } | ||
72 | html("</td></tr>\n"); | ||
73 | return 0; | ||
74 | } | ||
75 | |||
76 | static void print_tag_header() | ||
77 | { | ||
78 | html("<tr class='nohover'><th class='left'>Tag</th>" | ||
79 | "<th class='left'>Age</th>" | ||
80 | "<th class='left'>Author</th>" | ||
81 | "<th class='left'>Reference</th></tr>\n"); | ||
82 | header = 1; | ||
83 | } | ||
84 | |||
85 | static int print_tag(struct refinfo *ref) | ||
86 | { | ||
87 | struct tag *tag; | ||
88 | struct taginfo *info; | ||
89 | char *url, *name = (char *)ref->refname; | ||
90 | |||
91 | if (ref->object->type == OBJ_TAG) { | ||
92 | tag = (struct tag *)ref->object; | ||
93 | info = ref->tag; | ||
94 | if (!tag || !info) | ||
95 | return 1; | ||
96 | html("<tr><td>"); | ||
97 | url = cgit_pageurl(cgit_query_repo, "tag", | ||
98 | fmt("id=%s", name)); | ||
99 | html_link_open(url, NULL, NULL); | ||
100 | html_txt(name); | ||
101 | html_link_close(); | ||
102 | html("</td><td>"); | ||
103 | if (info->tagger_date > 0) | ||
104 | cgit_print_age(info->tagger_date, -1, NULL); | ||
105 | html("</td><td>"); | ||
106 | if (info->tagger) | ||
107 | html(info->tagger); | ||
108 | html("</td><td>"); | ||
109 | cgit_object_link(tag->tagged); | ||
110 | html("</td></tr>\n"); | ||
111 | } else { | ||
112 | if (!header) | ||
113 | print_tag_header(); | ||
114 | html("<tr><td>"); | ||
115 | html_txt(name); | ||
116 | html("</td><td colspan='2'/><td>"); | ||
117 | cgit_object_link(ref->object); | ||
118 | html("</td></tr>\n"); | ||
119 | } | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | static void print_refs_link(char *path) | ||
124 | { | ||
125 | html("<tr class='nohover'><td colspan='4'>"); | ||
126 | cgit_refs_link("[...]", NULL, NULL, cgit_query_head, NULL, path); | ||
127 | html("</td></tr>"); | ||
128 | } | ||
129 | |||
130 | void cgit_print_branches(int maxcount) | ||
131 | { | ||
132 | struct reflist list; | ||
133 | int i; | ||
134 | |||
135 | html("<tr class='nohover'><th class='left'>Branch</th>" | ||
136 | "<th class='left'>Idle</th>" | ||
137 | "<th class='left'>Author</th>" | ||
138 | "<th class='left'>Head commit</th></tr>\n"); | ||
139 | |||
140 | list.refs = NULL; | ||
141 | list.alloc = list.count = 0; | ||
142 | for_each_branch_ref(cgit_refs_cb, &list); | ||
143 | |||
144 | if (maxcount == 0 || maxcount > list.count) | ||
145 | maxcount = list.count; | ||
146 | |||
147 | if (maxcount < list.count) { | ||
148 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age); | ||
149 | qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name); | ||
150 | } | ||
151 | |||
152 | for(i=0; i<maxcount; i++) | ||
153 | print_branch(list.refs[i]); | ||
154 | |||
155 | if (maxcount < list.count) | ||
156 | print_refs_link("heads"); | ||
157 | } | ||
158 | |||
159 | void cgit_print_tags(int maxcount) | ||
160 | { | ||
161 | struct reflist list; | ||
162 | int i; | ||
163 | |||
164 | header = 0; | ||
165 | list.refs = NULL; | ||
166 | list.alloc = list.count = 0; | ||
167 | for_each_tag_ref(cgit_refs_cb, &list); | ||
168 | if (list.count == 0) | ||
169 | return; | ||
170 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age); | ||
171 | if (!maxcount) | ||
172 | maxcount = list.count; | ||
173 | else if (maxcount > list.count) | ||
174 | maxcount = list.count; | ||
175 | print_tag_header(); | ||
176 | for(i=0; i<maxcount; i++) | ||
177 | print_tag(list.refs[i]); | ||
178 | |||
179 | if (maxcount < list.count) | ||
180 | print_refs_link("tags"); | ||
181 | } | ||
182 | 13 | ||
183 | void cgit_print_summary() | 14 | void cgit_print_summary() |
184 | { | 15 | { |
185 | if (cgit_repo->readme) { | 16 | if (ctx.repo->readme) { |
186 | html("<div id='summary'>"); | 17 | html("<div id='summary'>"); |
187 | html_include(cgit_repo->readme); | 18 | html_include(ctx.repo->readme); |
188 | html("</div>"); | 19 | html("</div>"); |
189 | } | 20 | } |
190 | if (cgit_summary_log > 0) | 21 | if (ctx.cfg.summary_log > 0) |
191 | cgit_print_log(cgit_query_head, 0, cgit_summary_log, NULL, | 22 | cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL, |
192 | NULL, NULL, 0); | 23 | NULL, NULL, 0); |
193 | html("<table summary='repository info' class='list nowrap'>"); | 24 | html("<table summary='repository info' class='list nowrap'>"); |
194 | if (cgit_summary_log > 0) | 25 | if (ctx.cfg.summary_log > 0) |
195 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); | 26 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); |
196 | cgit_print_branches(cgit_summary_branches); | 27 | cgit_print_branches(ctx.cfg.summary_branches); |
197 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); | 28 | html("<tr class='nohover'><td colspan='4'> </td></tr>"); |
198 | cgit_print_tags(cgit_summary_tags); | 29 | cgit_print_tags(ctx.cfg.summary_tags); |
199 | html("</table>"); | 30 | html("</table>"); |
200 | } | 31 | } |
diff --git a/ui-summary.h b/ui-summary.h new file mode 100644 index 0000000..37aedd2 --- a/dev/null +++ b/ui-summary.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_SUMMARY_H | ||
2 | #define UI_SUMMARY_H | ||
3 | |||
4 | extern void cgit_print_summary(); | ||
5 | |||
6 | #endif /* UI_SUMMARY_H */ | ||
@@ -1,74 +1,75 @@ | |||
1 | /* ui-tag.c: display a tag | 1 | /* ui-tag.c: display a tag |
2 | * | 2 | * |
3 | * Copyright (C) 2007 Lars Hjemli | 3 | * Copyright (C) 2007 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 | #include "html.h" | |
11 | #include "ui-shared.h" | ||
11 | 12 | ||
12 | static void print_tag_content(char *buf) | 13 | static void print_tag_content(char *buf) |
13 | { | 14 | { |
14 | char *p; | 15 | char *p; |
15 | 16 | ||
16 | if (!buf) | 17 | if (!buf) |
17 | return; | 18 | return; |
18 | 19 | ||
19 | html("<div class='commit-subject'>"); | 20 | html("<div class='commit-subject'>"); |
20 | p = strchr(buf, '\n'); | 21 | p = strchr(buf, '\n'); |
21 | if (p) | 22 | if (p) |
22 | *p = '\0'; | 23 | *p = '\0'; |
23 | html_txt(buf); | 24 | html_txt(buf); |
24 | html("</div>"); | 25 | html("</div>"); |
25 | if (p) { | 26 | if (p) { |
26 | html("<div class='commit-msg'>"); | 27 | html("<div class='commit-msg'>"); |
27 | html_txt(++p); | 28 | html_txt(++p); |
28 | html("</div>"); | 29 | html("</div>"); |
29 | } | 30 | } |
30 | } | 31 | } |
31 | 32 | ||
32 | void cgit_print_tag(char *revname) | 33 | void cgit_print_tag(char *revname) |
33 | { | 34 | { |
34 | unsigned char sha1[20]; | 35 | unsigned char sha1[20]; |
35 | struct object *obj; | 36 | struct object *obj; |
36 | struct tag *tag; | 37 | struct tag *tag; |
37 | struct taginfo *info; | 38 | struct taginfo *info; |
38 | 39 | ||
39 | if (get_sha1(revname, sha1)) { | 40 | if (get_sha1(revname, sha1)) { |
40 | cgit_print_error(fmt("Bad tag reference: %s", revname)); | 41 | cgit_print_error(fmt("Bad tag reference: %s", revname)); |
41 | return; | 42 | return; |
42 | } | 43 | } |
43 | obj = parse_object(sha1); | 44 | obj = parse_object(sha1); |
44 | if (!obj) { | 45 | if (!obj) { |
45 | cgit_print_error(fmt("Bad object id: %s", sha1_to_hex(sha1))); | 46 | cgit_print_error(fmt("Bad object id: %s", sha1_to_hex(sha1))); |
46 | return; | 47 | return; |
47 | } | 48 | } |
48 | if (obj->type == OBJ_TAG) { | 49 | if (obj->type == OBJ_TAG) { |
49 | tag = lookup_tag(sha1); | 50 | tag = lookup_tag(sha1); |
50 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) { | 51 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) { |
51 | cgit_print_error(fmt("Bad tag object: %s", revname)); | 52 | cgit_print_error(fmt("Bad tag object: %s", revname)); |
52 | return; | 53 | return; |
53 | } | 54 | } |
54 | html("<table class='commit-info'>\n"); | 55 | html("<table class='commit-info'>\n"); |
55 | htmlf("<tr><td>Tag name</td><td>%s (%s)</td></tr>\n", | 56 | htmlf("<tr><td>Tag name</td><td>%s (%s)</td></tr>\n", |
56 | revname, sha1_to_hex(sha1)); | 57 | revname, sha1_to_hex(sha1)); |
57 | if (info->tagger_date > 0) { | 58 | if (info->tagger_date > 0) { |
58 | html("<tr><td>Tag date</td><td>"); | 59 | html("<tr><td>Tag date</td><td>"); |
59 | cgit_print_date(info->tagger_date, FMT_LONGDATE); | 60 | cgit_print_date(info->tagger_date, FMT_LONGDATE); |
60 | html("</td></tr>\n"); | 61 | html("</td></tr>\n"); |
61 | } | 62 | } |
62 | if (info->tagger) { | 63 | if (info->tagger) { |
63 | html("<tr><td>Tagged by</td><td>"); | 64 | html("<tr><td>Tagged by</td><td>"); |
64 | html_txt(info->tagger); | 65 | html_txt(info->tagger); |
65 | html("</td></tr>\n"); | 66 | html("</td></tr>\n"); |
66 | } | 67 | } |
67 | html("<tr><td>Tagged object</td><td>"); | 68 | html("<tr><td>Tagged object</td><td>"); |
68 | cgit_object_link(tag->tagged); | 69 | cgit_object_link(tag->tagged); |
69 | html("</td></tr>\n"); | 70 | html("</td></tr>\n"); |
70 | html("</table>\n"); | 71 | html("</table>\n"); |
71 | print_tag_content(info->msg); | 72 | print_tag_content(info->msg); |
72 | } | 73 | } |
73 | return; | 74 | return; |
74 | } | 75 | } |
diff --git a/ui-tag.h b/ui-tag.h new file mode 100644 index 0000000..d295cdc --- a/dev/null +++ b/ui-tag.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_TAG_H | ||
2 | #define UI_TAG_H | ||
3 | |||
4 | extern void cgit_print_tag(char *revname); | ||
5 | |||
6 | #endif /* UI_TAG_H */ | ||
@@ -1,216 +1,218 @@ | |||
1 | /* ui-tree.c: functions for tree output | 1 | /* ui-tree.c: functions for tree output |
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 "html.h" | ||
11 | #include "ui-shared.h" | ||
10 | 12 | ||
11 | char *curr_rev; | 13 | char *curr_rev; |
12 | char *match_path; | 14 | char *match_path; |
13 | int header = 0; | 15 | int header = 0; |
14 | 16 | ||
15 | static void print_object(const unsigned char *sha1, char *path) | 17 | static void print_object(const unsigned char *sha1, char *path) |
16 | { | 18 | { |
17 | enum object_type type; | 19 | enum object_type type; |
18 | char *buf; | 20 | char *buf; |
19 | unsigned long size, lineno, start, idx; | 21 | unsigned long size, lineno, start, idx; |
20 | const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; | 22 | const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; |
21 | 23 | ||
22 | type = sha1_object_info(sha1, &size); | 24 | type = sha1_object_info(sha1, &size); |
23 | if (type == OBJ_BAD) { | 25 | if (type == OBJ_BAD) { |
24 | cgit_print_error(fmt("Bad object name: %s", | 26 | cgit_print_error(fmt("Bad object name: %s", |
25 | sha1_to_hex(sha1))); | 27 | sha1_to_hex(sha1))); |
26 | return; | 28 | return; |
27 | } | 29 | } |
28 | 30 | ||
29 | buf = read_sha1_file(sha1, &type, &size); | 31 | buf = read_sha1_file(sha1, &type, &size); |
30 | if (!buf) { | 32 | if (!buf) { |
31 | cgit_print_error(fmt("Error reading object %s", | 33 | cgit_print_error(fmt("Error reading object %s", |
32 | sha1_to_hex(sha1))); | 34 | sha1_to_hex(sha1))); |
33 | return; | 35 | return; |
34 | } | 36 | } |
35 | 37 | ||
36 | html(" blob: <a href='"); | 38 | html(" blob: <a href='"); |
37 | html_attr(cgit_pageurl(cgit_query_repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); | 39 | html_attr(cgit_pageurl(ctx.qry.repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); |
38 | htmlf("'>%s</a>",sha1_to_hex(sha1)); | 40 | htmlf("'>%s</a>",sha1_to_hex(sha1)); |
39 | 41 | ||
40 | html("<table summary='blob content' class='blob'>\n"); | 42 | html("<table summary='blob content' class='blob'>\n"); |
41 | idx = 0; | 43 | idx = 0; |
42 | start = 0; | 44 | start = 0; |
43 | lineno = 0; | 45 | lineno = 0; |
44 | while(idx < size) { | 46 | while(idx < size) { |
45 | if (buf[idx] == '\n') { | 47 | if (buf[idx] == '\n') { |
46 | buf[idx] = '\0'; | 48 | buf[idx] = '\0'; |
47 | htmlf(linefmt, ++lineno); | 49 | htmlf(linefmt, ++lineno); |
48 | html_txt(buf + start); | 50 | html_txt(buf + start); |
49 | html("</td></tr>\n"); | 51 | html("</td></tr>\n"); |
50 | start = idx + 1; | 52 | start = idx + 1; |
51 | } | 53 | } |
52 | idx++; | 54 | idx++; |
53 | } | 55 | } |
54 | htmlf(linefmt, ++lineno); | 56 | htmlf(linefmt, ++lineno); |
55 | html_txt(buf + start); | 57 | html_txt(buf + start); |
56 | html("</td></tr>\n"); | 58 | html("</td></tr>\n"); |
57 | html("</table>\n"); | 59 | html("</table>\n"); |
58 | } | 60 | } |
59 | 61 | ||
60 | 62 | ||
61 | static int ls_item(const unsigned char *sha1, const char *base, int baselen, | 63 | static int ls_item(const unsigned char *sha1, const char *base, int baselen, |
62 | const char *pathname, unsigned int mode, int stage) | 64 | const char *pathname, unsigned int mode, int stage) |
63 | { | 65 | { |
64 | char *name; | 66 | char *name; |
65 | char *fullpath; | 67 | char *fullpath; |
66 | enum object_type type; | 68 | enum object_type type; |
67 | unsigned long size = 0; | 69 | unsigned long size = 0; |
68 | 70 | ||
69 | name = xstrdup(pathname); | 71 | name = xstrdup(pathname); |
70 | fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "", | 72 | fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "", |
71 | cgit_query_path ? "/" : "", name); | 73 | ctx.qry.path ? "/" : "", name); |
72 | 74 | ||
73 | type = sha1_object_info(sha1, &size); | 75 | type = sha1_object_info(sha1, &size); |
74 | if (type == OBJ_BAD && !S_ISGITLINK(mode)) { | 76 | if (type == OBJ_BAD && !S_ISGITLINK(mode)) { |
75 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", | 77 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
76 | name, | 78 | name, |
77 | sha1_to_hex(sha1)); | 79 | sha1_to_hex(sha1)); |
78 | return 0; | 80 | return 0; |
79 | } | 81 | } |
80 | 82 | ||
81 | html("<tr><td class='ls-mode'>"); | 83 | html("<tr><td class='ls-mode'>"); |
82 | html_filemode(mode); | 84 | cgit_print_filemode(mode); |
83 | html("</td><td>"); | 85 | html("</td><td>"); |
84 | if (S_ISGITLINK(mode)) { | 86 | if (S_ISGITLINK(mode)) { |
85 | htmlf("<a class='ls-mod' href='"); | 87 | htmlf("<a class='ls-mod' href='"); |
86 | html_attr(fmt(cgit_repo->module_link, | 88 | html_attr(fmt(ctx.repo->module_link, |
87 | name, | 89 | name, |
88 | sha1_to_hex(sha1))); | 90 | sha1_to_hex(sha1))); |
89 | html("'>"); | 91 | html("'>"); |
90 | html_txt(name); | 92 | html_txt(name); |
91 | html("</a>"); | 93 | html("</a>"); |
92 | } else if (S_ISDIR(mode)) { | 94 | } else if (S_ISDIR(mode)) { |
93 | cgit_tree_link(name, NULL, "ls-dir", cgit_query_head, | 95 | cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, |
94 | curr_rev, fullpath); | 96 | curr_rev, fullpath); |
95 | } else { | 97 | } else { |
96 | cgit_tree_link(name, NULL, "ls-blob", cgit_query_head, | 98 | cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head, |
97 | curr_rev, fullpath); | 99 | curr_rev, fullpath); |
98 | } | 100 | } |
99 | htmlf("</td><td class='ls-size'>%li</td>", size); | 101 | htmlf("</td><td class='ls-size'>%li</td>", size); |
100 | 102 | ||
101 | html("<td>"); | 103 | html("<td>"); |
102 | cgit_log_link("log", NULL, "button", cgit_query_head, curr_rev, | 104 | cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev, |
103 | fullpath, 0, NULL, NULL); | 105 | fullpath, 0, NULL, NULL); |
104 | html("</td></tr>\n"); | 106 | html("</td></tr>\n"); |
105 | free(name); | 107 | free(name); |
106 | return 0; | 108 | return 0; |
107 | } | 109 | } |
108 | 110 | ||
109 | static void ls_head() | 111 | static void ls_head() |
110 | { | 112 | { |
111 | html("<table summary='tree listing' class='list'>\n"); | 113 | html("<table summary='tree listing' class='list'>\n"); |
112 | html("<tr class='nohover'>"); | 114 | html("<tr class='nohover'>"); |
113 | html("<th class='left'>Mode</th>"); | 115 | html("<th class='left'>Mode</th>"); |
114 | html("<th class='left'>Name</th>"); | 116 | html("<th class='left'>Name</th>"); |
115 | html("<th class='right'>Size</th>"); | 117 | html("<th class='right'>Size</th>"); |
116 | html("<th/>"); | 118 | html("<th/>"); |
117 | html("</tr>\n"); | 119 | html("</tr>\n"); |
118 | header = 1; | 120 | header = 1; |
119 | } | 121 | } |
120 | 122 | ||
121 | static void ls_tail() | 123 | static void ls_tail() |
122 | { | 124 | { |
123 | if (!header) | 125 | if (!header) |
124 | return; | 126 | return; |
125 | html("</table>\n"); | 127 | html("</table>\n"); |
126 | header = 0; | 128 | header = 0; |
127 | } | 129 | } |
128 | 130 | ||
129 | static void ls_tree(const unsigned char *sha1, char *path) | 131 | static void ls_tree(const unsigned char *sha1, char *path) |
130 | { | 132 | { |
131 | struct tree *tree; | 133 | struct tree *tree; |
132 | 134 | ||
133 | tree = parse_tree_indirect(sha1); | 135 | tree = parse_tree_indirect(sha1); |
134 | if (!tree) { | 136 | if (!tree) { |
135 | cgit_print_error(fmt("Not a tree object: %s", | 137 | cgit_print_error(fmt("Not a tree object: %s", |
136 | sha1_to_hex(sha1))); | 138 | sha1_to_hex(sha1))); |
137 | return; | 139 | return; |
138 | } | 140 | } |
139 | 141 | ||
140 | ls_head(); | 142 | ls_head(); |
141 | read_tree_recursive(tree, "", 0, 1, NULL, ls_item); | 143 | read_tree_recursive(tree, "", 0, 1, NULL, ls_item); |
142 | ls_tail(); | 144 | ls_tail(); |
143 | } | 145 | } |
144 | 146 | ||
145 | 147 | ||
146 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, | 148 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
147 | const char *pathname, unsigned mode, int stage) | 149 | const char *pathname, unsigned mode, int stage) |
148 | { | 150 | { |
149 | static int state; | 151 | static int state; |
150 | static char buffer[PATH_MAX]; | 152 | static char buffer[PATH_MAX]; |
151 | char *url; | 153 | char *url; |
152 | 154 | ||
153 | if (state == 0) { | 155 | if (state == 0) { |
154 | memcpy(buffer, base, baselen); | 156 | memcpy(buffer, base, baselen); |
155 | strcpy(buffer+baselen, pathname); | 157 | strcpy(buffer+baselen, pathname); |
156 | url = cgit_pageurl(cgit_query_repo, "tree", | 158 | url = cgit_pageurl(ctx.qry.repo, "tree", |
157 | fmt("h=%s&path=%s", curr_rev, buffer)); | 159 | fmt("h=%s&path=%s", curr_rev, buffer)); |
158 | html("/"); | 160 | html("/"); |
159 | cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head, | 161 | cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head, |
160 | curr_rev, buffer); | 162 | curr_rev, buffer); |
161 | 163 | ||
162 | if (strcmp(match_path, buffer)) | 164 | if (strcmp(match_path, buffer)) |
163 | return READ_TREE_RECURSIVE; | 165 | return READ_TREE_RECURSIVE; |
164 | 166 | ||
165 | if (S_ISDIR(mode)) { | 167 | if (S_ISDIR(mode)) { |
166 | state = 1; | 168 | state = 1; |
167 | ls_head(); | 169 | ls_head(); |
168 | return READ_TREE_RECURSIVE; | 170 | return READ_TREE_RECURSIVE; |
169 | } else { | 171 | } else { |
170 | print_object(sha1, buffer); | 172 | print_object(sha1, buffer); |
171 | return 0; | 173 | return 0; |
172 | } | 174 | } |
173 | } | 175 | } |
174 | ls_item(sha1, base, baselen, pathname, mode, stage); | 176 | ls_item(sha1, base, baselen, pathname, mode, stage); |
175 | return 0; | 177 | return 0; |
176 | } | 178 | } |
177 | 179 | ||
178 | 180 | ||
179 | /* | 181 | /* |
180 | * Show a tree or a blob | 182 | * Show a tree or a blob |
181 | * rev: the commit pointing at the root tree object | 183 | * rev: the commit pointing at the root tree object |
182 | * path: path to tree or blob | 184 | * path: path to tree or blob |
183 | */ | 185 | */ |
184 | void cgit_print_tree(const char *rev, char *path) | 186 | void cgit_print_tree(const char *rev, char *path) |
185 | { | 187 | { |
186 | unsigned char sha1[20]; | 188 | unsigned char sha1[20]; |
187 | struct commit *commit; | 189 | struct commit *commit; |
188 | const char *paths[] = {path, NULL}; | 190 | const char *paths[] = {path, NULL}; |
189 | 191 | ||
190 | if (!rev) | 192 | if (!rev) |
191 | rev = cgit_query_head; | 193 | rev = ctx.qry.head; |
192 | 194 | ||
193 | curr_rev = xstrdup(rev); | 195 | curr_rev = xstrdup(rev); |
194 | if (get_sha1(rev, sha1)) { | 196 | if (get_sha1(rev, sha1)) { |
195 | cgit_print_error(fmt("Invalid revision name: %s", rev)); | 197 | cgit_print_error(fmt("Invalid revision name: %s", rev)); |
196 | return; | 198 | return; |
197 | } | 199 | } |
198 | commit = lookup_commit_reference(sha1); | 200 | commit = lookup_commit_reference(sha1); |
199 | if (!commit || parse_commit(commit)) { | 201 | if (!commit || parse_commit(commit)) { |
200 | cgit_print_error(fmt("Invalid commit reference: %s", rev)); | 202 | cgit_print_error(fmt("Invalid commit reference: %s", rev)); |
201 | return; | 203 | return; |
202 | } | 204 | } |
203 | 205 | ||
204 | html("path: <a href='"); | 206 | html("path: <a href='"); |
205 | html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev))); | 207 | html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev))); |
206 | html("'>root</a>"); | 208 | html("'>root</a>"); |
207 | 209 | ||
208 | if (path == NULL) { | 210 | if (path == NULL) { |
209 | ls_tree(commit->tree->object.sha1, NULL); | 211 | ls_tree(commit->tree->object.sha1, NULL); |
210 | return; | 212 | return; |
211 | } | 213 | } |
212 | 214 | ||
213 | match_path = path; | 215 | match_path = path; |
214 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); | 216 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); |
215 | ls_tail(); | 217 | ls_tail(); |
216 | } | 218 | } |
diff --git a/ui-tree.h b/ui-tree.h new file mode 100644 index 0000000..bbd34e3 --- a/dev/null +++ b/ui-tree.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef UI_TREE_H | ||
2 | #define UI_TREE_H | ||
3 | |||
4 | extern void cgit_print_tree(const char *rev, char *path); | ||
5 | |||
6 | #endif /* UI_TREE_H */ | ||