-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | cmd.c | 19 | ||||
-rw-r--r-- | html.c | 7 | ||||
-rw-r--r-- | html.h | 1 | ||||
-rw-r--r-- | ui-clone.c | 104 | ||||
-rw-r--r-- | ui-clone.h | 8 |
6 files changed, 140 insertions, 0 deletions
@@ -1,124 +1,125 @@ | |||
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.6 | 7 | GIT_VER = 1.5.6 |
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 | # | 15 | # |
16 | # Define a way to invoke make in subdirs quietly, shamelessly ripped | 16 | # Define a way to invoke make in subdirs quietly, shamelessly ripped |
17 | # from git.git | 17 | # from git.git |
18 | # | 18 | # |
19 | QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir | 19 | QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir |
20 | QUIET_SUBDIR1 = | 20 | QUIET_SUBDIR1 = |
21 | 21 | ||
22 | ifneq ($(findstring $(MAKEFLAGS),w),w) | 22 | ifneq ($(findstring $(MAKEFLAGS),w),w) |
23 | PRINT_DIR = --no-print-directory | 23 | PRINT_DIR = --no-print-directory |
24 | else # "make -w" | 24 | else # "make -w" |
25 | NO_SUBDIR = : | 25 | NO_SUBDIR = : |
26 | endif | 26 | endif |
27 | 27 | ||
28 | ifndef V | 28 | ifndef V |
29 | QUIET_CC = @echo ' ' CC $@; | 29 | QUIET_CC = @echo ' ' CC $@; |
30 | QUIET_MM = @echo ' ' MM $@; | 30 | QUIET_MM = @echo ' ' MM $@; |
31 | QUIET_SUBDIR0 = +@subdir= | 31 | QUIET_SUBDIR0 = +@subdir= |
32 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ | 32 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ |
33 | $(MAKE) $(PRINT_DIR) -C $$subdir | 33 | $(MAKE) $(PRINT_DIR) -C $$subdir |
34 | endif | 34 | endif |
35 | 35 | ||
36 | # | 36 | # |
37 | # Define a pattern rule for automatic dependency building | 37 | # Define a pattern rule for automatic dependency building |
38 | # | 38 | # |
39 | %.d: %.c | 39 | %.d: %.c |
40 | $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ | 40 | $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ |
41 | 41 | ||
42 | # | 42 | # |
43 | # Define a pattern rule for silent object building | 43 | # Define a pattern rule for silent object building |
44 | # | 44 | # |
45 | %.o: %.c | 45 | %.o: %.c |
46 | $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< | 46 | $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< |
47 | 47 | ||
48 | 48 | ||
49 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto | 49 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto |
50 | OBJECTS = | 50 | OBJECTS = |
51 | OBJECTS += cache.o | 51 | OBJECTS += cache.o |
52 | OBJECTS += cgit.o | 52 | OBJECTS += cgit.o |
53 | OBJECTS += cmd.o | 53 | OBJECTS += cmd.o |
54 | OBJECTS += configfile.o | 54 | OBJECTS += configfile.o |
55 | OBJECTS += html.o | 55 | OBJECTS += html.o |
56 | OBJECTS += parsing.o | 56 | OBJECTS += parsing.o |
57 | OBJECTS += shared.o | 57 | OBJECTS += shared.o |
58 | OBJECTS += ui-blob.o | 58 | OBJECTS += ui-blob.o |
59 | OBJECTS += ui-clone.o | ||
59 | OBJECTS += ui-commit.o | 60 | OBJECTS += ui-commit.o |
60 | OBJECTS += ui-diff.o | 61 | OBJECTS += ui-diff.o |
61 | OBJECTS += ui-log.o | 62 | OBJECTS += ui-log.o |
62 | OBJECTS += ui-patch.o | 63 | OBJECTS += ui-patch.o |
63 | OBJECTS += ui-refs.o | 64 | OBJECTS += ui-refs.o |
64 | OBJECTS += ui-repolist.o | 65 | OBJECTS += ui-repolist.o |
65 | OBJECTS += ui-shared.o | 66 | OBJECTS += ui-shared.o |
66 | OBJECTS += ui-snapshot.o | 67 | OBJECTS += ui-snapshot.o |
67 | OBJECTS += ui-summary.o | 68 | OBJECTS += ui-summary.o |
68 | OBJECTS += ui-tag.o | 69 | OBJECTS += ui-tag.o |
69 | OBJECTS += ui-tree.o | 70 | OBJECTS += ui-tree.o |
70 | 71 | ||
71 | ifdef NEEDS_LIBICONV | 72 | ifdef NEEDS_LIBICONV |
72 | EXTLIBS += -liconv | 73 | EXTLIBS += -liconv |
73 | endif | 74 | endif |
74 | 75 | ||
75 | 76 | ||
76 | .PHONY: all git test install uninstall clean force-version get-git | 77 | .PHONY: all git test install uninstall clean force-version get-git |
77 | 78 | ||
78 | all: cgit | 79 | all: cgit |
79 | 80 | ||
80 | VERSION: force-version | 81 | VERSION: force-version |
81 | @./gen-version.sh "$(CGIT_VERSION)" | 82 | @./gen-version.sh "$(CGIT_VERSION)" |
82 | -include VERSION | 83 | -include VERSION |
83 | 84 | ||
84 | 85 | ||
85 | CFLAGS += -g -Wall -Igit | 86 | CFLAGS += -g -Wall -Igit |
86 | CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' | 87 | CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' |
87 | CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' | 88 | CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' |
88 | CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' | 89 | CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' |
89 | CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' | 90 | CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' |
90 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' | 91 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' |
91 | 92 | ||
92 | 93 | ||
93 | cgit: $(OBJECTS) git/libgit.a git/xdiff/lib.a | 94 | cgit: $(OBJECTS) git/libgit.a git/xdiff/lib.a |
94 | $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) | 95 | $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) |
95 | 96 | ||
96 | cgit.o: VERSION | 97 | cgit.o: VERSION |
97 | 98 | ||
98 | -include $(OBJECTS:.o=.d) | 99 | -include $(OBJECTS:.o=.d) |
99 | 100 | ||
100 | git/libgit.a: git | 101 | git/libgit.a: git |
101 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a | 102 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a |
102 | 103 | ||
103 | git/xdiff/lib.a: git | 104 | git/xdiff/lib.a: git |
104 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a | 105 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a |
105 | 106 | ||
106 | test: all | 107 | test: all |
107 | $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all | 108 | $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all |
108 | 109 | ||
109 | install: all | 110 | install: all |
110 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) | 111 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) |
111 | install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) | 112 | install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) |
112 | install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css | 113 | install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css |
113 | install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png | 114 | install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png |
114 | 115 | ||
115 | uninstall: | 116 | uninstall: |
116 | rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) | 117 | rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) |
117 | rm -f $(CGIT_SCRIPT_PATH)/cgit.css | 118 | rm -f $(CGIT_SCRIPT_PATH)/cgit.css |
118 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png | 119 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png |
119 | 120 | ||
120 | clean: | 121 | clean: |
121 | rm -f cgit VERSION *.o *.d | 122 | rm -f cgit VERSION *.o *.d |
122 | 123 | ||
123 | get-git: | 124 | get-git: |
124 | curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git | 125 | curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git |
@@ -1,132 +1,151 @@ | |||
1 | /* cmd.c: the cgit command dispatcher | 1 | /* cmd.c: the cgit command dispatcher |
2 | * | 2 | * |
3 | * Copyright (C) 2008 Lars Hjemli | 3 | * Copyright (C) 2008 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cmd.h" | 10 | #include "cmd.h" |
11 | #include "cache.h" | 11 | #include "cache.h" |
12 | #include "ui-shared.h" | 12 | #include "ui-shared.h" |
13 | #include "ui-blob.h" | 13 | #include "ui-blob.h" |
14 | #include "ui-clone.h" | ||
14 | #include "ui-commit.h" | 15 | #include "ui-commit.h" |
15 | #include "ui-diff.h" | 16 | #include "ui-diff.h" |
16 | #include "ui-log.h" | 17 | #include "ui-log.h" |
17 | #include "ui-patch.h" | 18 | #include "ui-patch.h" |
18 | #include "ui-refs.h" | 19 | #include "ui-refs.h" |
19 | #include "ui-repolist.h" | 20 | #include "ui-repolist.h" |
20 | #include "ui-snapshot.h" | 21 | #include "ui-snapshot.h" |
21 | #include "ui-summary.h" | 22 | #include "ui-summary.h" |
22 | #include "ui-tag.h" | 23 | #include "ui-tag.h" |
23 | #include "ui-tree.h" | 24 | #include "ui-tree.h" |
24 | 25 | ||
26 | static void HEAD_fn(struct cgit_context *ctx) | ||
27 | { | ||
28 | cgit_clone_head(ctx); | ||
29 | } | ||
30 | |||
25 | static void about_fn(struct cgit_context *ctx) | 31 | static void about_fn(struct cgit_context *ctx) |
26 | { | 32 | { |
27 | if (ctx->repo) | 33 | if (ctx->repo) |
28 | cgit_print_repo_readme(); | 34 | cgit_print_repo_readme(); |
29 | else | 35 | else |
30 | cgit_print_site_readme(); | 36 | cgit_print_site_readme(); |
31 | } | 37 | } |
32 | 38 | ||
33 | static void blob_fn(struct cgit_context *ctx) | 39 | static void blob_fn(struct cgit_context *ctx) |
34 | { | 40 | { |
35 | cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head); | 41 | cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head); |
36 | } | 42 | } |
37 | 43 | ||
38 | static void commit_fn(struct cgit_context *ctx) | 44 | static void commit_fn(struct cgit_context *ctx) |
39 | { | 45 | { |
40 | cgit_print_commit(ctx->qry.sha1); | 46 | cgit_print_commit(ctx->qry.sha1); |
41 | } | 47 | } |
42 | 48 | ||
43 | static void diff_fn(struct cgit_context *ctx) | 49 | static void diff_fn(struct cgit_context *ctx) |
44 | { | 50 | { |
45 | cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); | 51 | cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); |
46 | } | 52 | } |
47 | 53 | ||
54 | static void info_fn(struct cgit_context *ctx) | ||
55 | { | ||
56 | cgit_clone_info(ctx); | ||
57 | } | ||
58 | |||
48 | static void log_fn(struct cgit_context *ctx) | 59 | static void log_fn(struct cgit_context *ctx) |
49 | { | 60 | { |
50 | cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, | 61 | cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, |
51 | ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); | 62 | ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); |
52 | } | 63 | } |
53 | 64 | ||
54 | static void ls_cache_fn(struct cgit_context *ctx) | 65 | static void ls_cache_fn(struct cgit_context *ctx) |
55 | { | 66 | { |
56 | ctx->page.mimetype = "text/plain"; | 67 | ctx->page.mimetype = "text/plain"; |
57 | ctx->page.filename = "ls-cache.txt"; | 68 | ctx->page.filename = "ls-cache.txt"; |
58 | cgit_print_http_headers(ctx); | 69 | cgit_print_http_headers(ctx); |
59 | cache_ls(ctx->cfg.cache_root); | 70 | cache_ls(ctx->cfg.cache_root); |
60 | } | 71 | } |
61 | 72 | ||
73 | static void objects_fn(struct cgit_context *ctx) | ||
74 | { | ||
75 | cgit_clone_objects(ctx); | ||
76 | } | ||
77 | |||
62 | static void repolist_fn(struct cgit_context *ctx) | 78 | static void repolist_fn(struct cgit_context *ctx) |
63 | { | 79 | { |
64 | cgit_print_repolist(); | 80 | cgit_print_repolist(); |
65 | } | 81 | } |
66 | 82 | ||
67 | static void patch_fn(struct cgit_context *ctx) | 83 | static void patch_fn(struct cgit_context *ctx) |
68 | { | 84 | { |
69 | cgit_print_patch(ctx->qry.sha1); | 85 | cgit_print_patch(ctx->qry.sha1); |
70 | } | 86 | } |
71 | 87 | ||
72 | static void refs_fn(struct cgit_context *ctx) | 88 | static void refs_fn(struct cgit_context *ctx) |
73 | { | 89 | { |
74 | cgit_print_refs(); | 90 | cgit_print_refs(); |
75 | } | 91 | } |
76 | 92 | ||
77 | static void snapshot_fn(struct cgit_context *ctx) | 93 | static void snapshot_fn(struct cgit_context *ctx) |
78 | { | 94 | { |
79 | cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, | 95 | cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, |
80 | cgit_repobasename(ctx->repo->url), ctx->qry.path, | 96 | cgit_repobasename(ctx->repo->url), ctx->qry.path, |
81 | ctx->repo->snapshots); | 97 | ctx->repo->snapshots); |
82 | } | 98 | } |
83 | 99 | ||
84 | static void summary_fn(struct cgit_context *ctx) | 100 | static void summary_fn(struct cgit_context *ctx) |
85 | { | 101 | { |
86 | cgit_print_summary(); | 102 | cgit_print_summary(); |
87 | } | 103 | } |
88 | 104 | ||
89 | static void tag_fn(struct cgit_context *ctx) | 105 | static void tag_fn(struct cgit_context *ctx) |
90 | { | 106 | { |
91 | cgit_print_tag(ctx->qry.sha1); | 107 | cgit_print_tag(ctx->qry.sha1); |
92 | } | 108 | } |
93 | 109 | ||
94 | static void tree_fn(struct cgit_context *ctx) | 110 | static void tree_fn(struct cgit_context *ctx) |
95 | { | 111 | { |
96 | cgit_print_tree(ctx->qry.sha1, ctx->qry.path); | 112 | cgit_print_tree(ctx->qry.sha1, ctx->qry.path); |
97 | } | 113 | } |
98 | 114 | ||
99 | #define def_cmd(name, want_repo, want_layout) \ | 115 | #define def_cmd(name, want_repo, want_layout) \ |
100 | {#name, name##_fn, want_repo, want_layout} | 116 | {#name, name##_fn, want_repo, want_layout} |
101 | 117 | ||
102 | struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) | 118 | struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) |
103 | { | 119 | { |
104 | static struct cgit_cmd cmds[] = { | 120 | static struct cgit_cmd cmds[] = { |
121 | def_cmd(HEAD, 1, 0), | ||
105 | def_cmd(about, 0, 1), | 122 | def_cmd(about, 0, 1), |
106 | def_cmd(blob, 1, 0), | 123 | def_cmd(blob, 1, 0), |
107 | def_cmd(commit, 1, 1), | 124 | def_cmd(commit, 1, 1), |
108 | def_cmd(diff, 1, 1), | 125 | def_cmd(diff, 1, 1), |
126 | def_cmd(info, 1, 0), | ||
109 | def_cmd(log, 1, 1), | 127 | def_cmd(log, 1, 1), |
110 | def_cmd(ls_cache, 0, 0), | 128 | def_cmd(ls_cache, 0, 0), |
129 | def_cmd(objects, 1, 0), | ||
111 | def_cmd(patch, 1, 0), | 130 | def_cmd(patch, 1, 0), |
112 | def_cmd(refs, 1, 1), | 131 | def_cmd(refs, 1, 1), |
113 | def_cmd(repolist, 0, 0), | 132 | def_cmd(repolist, 0, 0), |
114 | def_cmd(snapshot, 1, 0), | 133 | def_cmd(snapshot, 1, 0), |
115 | def_cmd(summary, 1, 1), | 134 | def_cmd(summary, 1, 1), |
116 | def_cmd(tag, 1, 1), | 135 | def_cmd(tag, 1, 1), |
117 | def_cmd(tree, 1, 1), | 136 | def_cmd(tree, 1, 1), |
118 | }; | 137 | }; |
119 | int i; | 138 | int i; |
120 | 139 | ||
121 | if (ctx->qry.page == NULL) { | 140 | if (ctx->qry.page == NULL) { |
122 | if (ctx->repo) | 141 | if (ctx->repo) |
123 | ctx->qry.page = "summary"; | 142 | ctx->qry.page = "summary"; |
124 | else | 143 | else |
125 | ctx->qry.page = "repolist"; | 144 | ctx->qry.page = "repolist"; |
126 | } | 145 | } |
127 | 146 | ||
128 | for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) | 147 | for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) |
129 | if (!strcmp(ctx->qry.page, cmds[i].name)) | 148 | if (!strcmp(ctx->qry.page, cmds[i].name)) |
130 | return &cmds[i]; | 149 | return &cmds[i]; |
131 | return NULL; | 150 | return NULL; |
132 | } | 151 | } |
@@ -1,245 +1,252 @@ | |||
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 <unistd.h> | 9 | #include <unistd.h> |
10 | #include <stdio.h> | 10 | #include <stdio.h> |
11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
12 | #include <stdarg.h> | 12 | #include <stdarg.h> |
13 | #include <string.h> | 13 | #include <string.h> |
14 | #include <errno.h> | 14 | #include <errno.h> |
15 | 15 | ||
16 | int htmlfd = STDOUT_FILENO; | 16 | int htmlfd = STDOUT_FILENO; |
17 | 17 | ||
18 | char *fmt(const char *format, ...) | 18 | char *fmt(const char *format, ...) |
19 | { | 19 | { |
20 | static char buf[8][1024]; | 20 | static char buf[8][1024]; |
21 | static int bufidx; | 21 | static int bufidx; |
22 | int len; | 22 | int len; |
23 | va_list args; | 23 | va_list args; |
24 | 24 | ||
25 | bufidx++; | 25 | bufidx++; |
26 | bufidx &= 7; | 26 | bufidx &= 7; |
27 | 27 | ||
28 | va_start(args, format); | 28 | va_start(args, format); |
29 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); | 29 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
30 | va_end(args); | 30 | va_end(args); |
31 | if (len>sizeof(buf[bufidx])) { | 31 | if (len>sizeof(buf[bufidx])) { |
32 | fprintf(stderr, "[html.c] string truncated: %s\n", format); | 32 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
33 | exit(1); | 33 | exit(1); |
34 | } | 34 | } |
35 | return buf[bufidx]; | 35 | return buf[bufidx]; |
36 | } | 36 | } |
37 | 37 | ||
38 | void html(const char *txt) | 38 | void html(const char *txt) |
39 | { | 39 | { |
40 | write(htmlfd, txt, strlen(txt)); | 40 | write(htmlfd, txt, strlen(txt)); |
41 | } | 41 | } |
42 | 42 | ||
43 | void htmlf(const char *format, ...) | 43 | void htmlf(const char *format, ...) |
44 | { | 44 | { |
45 | static char buf[65536]; | 45 | static char buf[65536]; |
46 | va_list args; | 46 | va_list args; |
47 | 47 | ||
48 | va_start(args, format); | 48 | va_start(args, format); |
49 | vsnprintf(buf, sizeof(buf), format, args); | 49 | vsnprintf(buf, sizeof(buf), format, args); |
50 | va_end(args); | 50 | va_end(args); |
51 | html(buf); | 51 | html(buf); |
52 | } | 52 | } |
53 | 53 | ||
54 | void html_status(int code, int more_headers) | ||
55 | { | ||
56 | htmlf("Status: %d\n", code); | ||
57 | if (!more_headers) | ||
58 | html("\n"); | ||
59 | } | ||
60 | |||
54 | void html_txt(char *txt) | 61 | void html_txt(char *txt) |
55 | { | 62 | { |
56 | char *t = txt; | 63 | char *t = txt; |
57 | while(t && *t){ | 64 | while(t && *t){ |
58 | int c = *t; | 65 | int c = *t; |
59 | if (c=='<' || c=='>' || c=='&') { | 66 | if (c=='<' || c=='>' || c=='&') { |
60 | write(htmlfd, txt, t - txt); | 67 | write(htmlfd, txt, t - txt); |
61 | if (c=='>') | 68 | if (c=='>') |
62 | html(">"); | 69 | html(">"); |
63 | else if (c=='<') | 70 | else if (c=='<') |
64 | html("<"); | 71 | html("<"); |
65 | else if (c=='&') | 72 | else if (c=='&') |
66 | html("&"); | 73 | html("&"); |
67 | txt = t+1; | 74 | txt = t+1; |
68 | } | 75 | } |
69 | t++; | 76 | t++; |
70 | } | 77 | } |
71 | if (t!=txt) | 78 | if (t!=txt) |
72 | html(txt); | 79 | html(txt); |
73 | } | 80 | } |
74 | 81 | ||
75 | void html_ntxt(int len, char *txt) | 82 | void html_ntxt(int len, char *txt) |
76 | { | 83 | { |
77 | char *t = txt; | 84 | char *t = txt; |
78 | while(t && *t && len--){ | 85 | while(t && *t && len--){ |
79 | int c = *t; | 86 | int c = *t; |
80 | if (c=='<' || c=='>' || c=='&') { | 87 | if (c=='<' || c=='>' || c=='&') { |
81 | write(htmlfd, txt, t - txt); | 88 | write(htmlfd, txt, t - txt); |
82 | if (c=='>') | 89 | if (c=='>') |
83 | html(">"); | 90 | html(">"); |
84 | else if (c=='<') | 91 | else if (c=='<') |
85 | html("<"); | 92 | html("<"); |
86 | else if (c=='&') | 93 | else if (c=='&') |
87 | html("&"); | 94 | html("&"); |
88 | txt = t+1; | 95 | txt = t+1; |
89 | } | 96 | } |
90 | t++; | 97 | t++; |
91 | } | 98 | } |
92 | if (t!=txt) | 99 | if (t!=txt) |
93 | write(htmlfd, txt, t - txt); | 100 | write(htmlfd, txt, t - txt); |
94 | if (len<0) | 101 | if (len<0) |
95 | html("..."); | 102 | html("..."); |
96 | } | 103 | } |
97 | 104 | ||
98 | void html_attr(char *txt) | 105 | void html_attr(char *txt) |
99 | { | 106 | { |
100 | char *t = txt; | 107 | char *t = txt; |
101 | while(t && *t){ | 108 | while(t && *t){ |
102 | int c = *t; | 109 | int c = *t; |
103 | if (c=='<' || c=='>' || c=='\'') { | 110 | if (c=='<' || c=='>' || c=='\'') { |
104 | write(htmlfd, txt, t - txt); | 111 | write(htmlfd, txt, t - txt); |
105 | if (c=='>') | 112 | if (c=='>') |
106 | html(">"); | 113 | html(">"); |
107 | else if (c=='<') | 114 | else if (c=='<') |
108 | html("<"); | 115 | html("<"); |
109 | else if (c=='\'') | 116 | else if (c=='\'') |
110 | html(""e;"); | 117 | html(""e;"); |
111 | txt = t+1; | 118 | txt = t+1; |
112 | } | 119 | } |
113 | t++; | 120 | t++; |
114 | } | 121 | } |
115 | if (t!=txt) | 122 | if (t!=txt) |
116 | html(txt); | 123 | html(txt); |
117 | } | 124 | } |
118 | 125 | ||
119 | void html_hidden(char *name, char *value) | 126 | void html_hidden(char *name, char *value) |
120 | { | 127 | { |
121 | html("<input type='hidden' name='"); | 128 | html("<input type='hidden' name='"); |
122 | html_attr(name); | 129 | html_attr(name); |
123 | html("' value='"); | 130 | html("' value='"); |
124 | html_attr(value); | 131 | html_attr(value); |
125 | html("'/>"); | 132 | html("'/>"); |
126 | } | 133 | } |
127 | 134 | ||
128 | void html_option(char *value, char *text, char *selected_value) | 135 | void html_option(char *value, char *text, char *selected_value) |
129 | { | 136 | { |
130 | html("<option value='"); | 137 | html("<option value='"); |
131 | html_attr(value); | 138 | html_attr(value); |
132 | html("'"); | 139 | html("'"); |
133 | if (selected_value && !strcmp(selected_value, value)) | 140 | if (selected_value && !strcmp(selected_value, value)) |
134 | html(" selected='selected'"); | 141 | html(" selected='selected'"); |
135 | html(">"); | 142 | html(">"); |
136 | html_txt(text); | 143 | html_txt(text); |
137 | html("</option>\n"); | 144 | html("</option>\n"); |
138 | } | 145 | } |
139 | 146 | ||
140 | void html_link_open(char *url, char *title, char *class) | 147 | void html_link_open(char *url, char *title, char *class) |
141 | { | 148 | { |
142 | html("<a href='"); | 149 | html("<a href='"); |
143 | html_attr(url); | 150 | html_attr(url); |
144 | if (title) { | 151 | if (title) { |
145 | html("' title='"); | 152 | html("' title='"); |
146 | html_attr(title); | 153 | html_attr(title); |
147 | } | 154 | } |
148 | if (class) { | 155 | if (class) { |
149 | html("' class='"); | 156 | html("' class='"); |
150 | html_attr(class); | 157 | html_attr(class); |
151 | } | 158 | } |
152 | html("'>"); | 159 | html("'>"); |
153 | } | 160 | } |
154 | 161 | ||
155 | void html_link_close(void) | 162 | void html_link_close(void) |
156 | { | 163 | { |
157 | html("</a>"); | 164 | html("</a>"); |
158 | } | 165 | } |
159 | 166 | ||
160 | void html_fileperm(unsigned short mode) | 167 | void html_fileperm(unsigned short mode) |
161 | { | 168 | { |
162 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), | 169 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
163 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); | 170 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
164 | } | 171 | } |
165 | 172 | ||
166 | int html_include(const char *filename) | 173 | int html_include(const char *filename) |
167 | { | 174 | { |
168 | FILE *f; | 175 | FILE *f; |
169 | char buf[4096]; | 176 | char buf[4096]; |
170 | size_t len; | 177 | size_t len; |
171 | 178 | ||
172 | if (!(f = fopen(filename, "r"))) { | 179 | if (!(f = fopen(filename, "r"))) { |
173 | fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", | 180 | fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", |
174 | filename, strerror(errno), errno); | 181 | filename, strerror(errno), errno); |
175 | return -1; | 182 | return -1; |
176 | } | 183 | } |
177 | while((len = fread(buf, 1, 4096, f)) > 0) | 184 | while((len = fread(buf, 1, 4096, f)) > 0) |
178 | write(htmlfd, buf, len); | 185 | write(htmlfd, buf, len); |
179 | fclose(f); | 186 | fclose(f); |
180 | return 0; | 187 | return 0; |
181 | } | 188 | } |
182 | 189 | ||
183 | int hextoint(char c) | 190 | int hextoint(char c) |
184 | { | 191 | { |
185 | if (c >= 'a' && c <= 'f') | 192 | if (c >= 'a' && c <= 'f') |
186 | return 10 + c - 'a'; | 193 | return 10 + c - 'a'; |
187 | else if (c >= 'A' && c <= 'F') | 194 | else if (c >= 'A' && c <= 'F') |
188 | return 10 + c - 'A'; | 195 | return 10 + c - 'A'; |
189 | else if (c >= '0' && c <= '9') | 196 | else if (c >= '0' && c <= '9') |
190 | return c - '0'; | 197 | return c - '0'; |
191 | else | 198 | else |
192 | return -1; | 199 | return -1; |
193 | } | 200 | } |
194 | 201 | ||
195 | char *convert_query_hexchar(char *txt) | 202 | char *convert_query_hexchar(char *txt) |
196 | { | 203 | { |
197 | int d1, d2; | 204 | int d1, d2; |
198 | if (strlen(txt) < 3) { | 205 | if (strlen(txt) < 3) { |
199 | *txt = '\0'; | 206 | *txt = '\0'; |
200 | return txt-1; | 207 | return txt-1; |
201 | } | 208 | } |
202 | d1 = hextoint(*(txt+1)); | 209 | d1 = hextoint(*(txt+1)); |
203 | d2 = hextoint(*(txt+2)); | 210 | d2 = hextoint(*(txt+2)); |
204 | if (d1<0 || d2<0) { | 211 | if (d1<0 || d2<0) { |
205 | strcpy(txt, txt+3); | 212 | strcpy(txt, txt+3); |
206 | return txt-1; | 213 | return txt-1; |
207 | } else { | 214 | } else { |
208 | *txt = d1 * 16 + d2; | 215 | *txt = d1 * 16 + d2; |
209 | strcpy(txt+1, txt+3); | 216 | strcpy(txt+1, txt+3); |
210 | return txt; | 217 | return txt; |
211 | } | 218 | } |
212 | } | 219 | } |
213 | 220 | ||
214 | int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) | 221 | int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) |
215 | { | 222 | { |
216 | char *t, *value = NULL, c; | 223 | char *t, *value = NULL, c; |
217 | 224 | ||
218 | if (!txt) | 225 | if (!txt) |
219 | return 0; | 226 | return 0; |
220 | 227 | ||
221 | t = txt = strdup(txt); | 228 | t = txt = strdup(txt); |
222 | if (t == NULL) { | 229 | if (t == NULL) { |
223 | printf("Out of memory\n"); | 230 | printf("Out of memory\n"); |
224 | exit(1); | 231 | exit(1); |
225 | } | 232 | } |
226 | while((c=*t) != '\0') { | 233 | while((c=*t) != '\0') { |
227 | if (c=='=') { | 234 | if (c=='=') { |
228 | *t = '\0'; | 235 | *t = '\0'; |
229 | value = t+1; | 236 | value = t+1; |
230 | } else if (c=='+') { | 237 | } else if (c=='+') { |
231 | *t = ' '; | 238 | *t = ' '; |
232 | } else if (c=='%') { | 239 | } else if (c=='%') { |
233 | t = convert_query_hexchar(t); | 240 | t = convert_query_hexchar(t); |
234 | } else if (c=='&') { | 241 | } else if (c=='&') { |
235 | *t = '\0'; | 242 | *t = '\0'; |
236 | (*fn)(txt, value); | 243 | (*fn)(txt, value); |
237 | txt = t+1; | 244 | txt = t+1; |
238 | value = NULL; | 245 | value = NULL; |
239 | } | 246 | } |
240 | t++; | 247 | t++; |
241 | } | 248 | } |
242 | if (t!=txt) | 249 | if (t!=txt) |
243 | (*fn)(txt, value); | 250 | (*fn)(txt, value); |
244 | return 0; | 251 | return 0; |
245 | } | 252 | } |
@@ -1,20 +1,21 @@ | |||
1 | #ifndef HTML_H | 1 | #ifndef HTML_H |
2 | #define HTML_H | 2 | #define HTML_H |
3 | 3 | ||
4 | extern int htmlfd; | 4 | extern int htmlfd; |
5 | 5 | ||
6 | extern void html(const char *txt); | 6 | extern void html(const char *txt); |
7 | extern void htmlf(const char *format,...); | 7 | extern void htmlf(const char *format,...); |
8 | extern void html_status(int code, int more_headers); | ||
8 | extern void html_txt(char *txt); | 9 | extern void html_txt(char *txt); |
9 | extern void html_ntxt(int len, char *txt); | 10 | extern void html_ntxt(int len, char *txt); |
10 | extern void html_attr(char *txt); | 11 | extern void html_attr(char *txt); |
11 | extern void html_hidden(char *name, char *value); | 12 | extern void html_hidden(char *name, char *value); |
12 | extern void html_option(char *value, char *text, char *selected_value); | 13 | 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_open(char *url, char *title, char *class); |
14 | extern void html_link_close(void); | 15 | extern void html_link_close(void); |
15 | extern void html_fileperm(unsigned short mode); | 16 | extern void html_fileperm(unsigned short mode); |
16 | extern int html_include(const char *filename); | 17 | extern int html_include(const char *filename); |
17 | 18 | ||
18 | extern int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)); | 19 | extern int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)); |
19 | 20 | ||
20 | #endif /* HTML_H */ | 21 | #endif /* HTML_H */ |
diff --git a/ui-clone.c b/ui-clone.c new file mode 100644 index 0000000..3a037ad --- a/dev/null +++ b/ui-clone.c | |||
@@ -0,0 +1,104 @@ | |||
1 | /* ui-clone.c: functions for http cloning, based on | ||
2 | * git's http-backend.c by Shawn O. Pearce | ||
3 | * | ||
4 | * Copyright (C) 2008 Lars Hjemli | ||
5 | * | ||
6 | * Licensed under GNU General Public License v2 | ||
7 | * (see COPYING for full license text) | ||
8 | */ | ||
9 | |||
10 | #include "cgit.h" | ||
11 | #include "html.h" | ||
12 | #include "ui-shared.h" | ||
13 | |||
14 | static int print_ref_info(const char *refname, const unsigned char *sha1, | ||
15 | int flags, void *cb_data) | ||
16 | { | ||
17 | struct object *obj; | ||
18 | |||
19 | if (!(obj = parse_object(sha1))) | ||
20 | return 0; | ||
21 | |||
22 | if (!strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/")) | ||
23 | htmlf("%s\t%s\n", sha1_to_hex(sha1), refname); | ||
24 | else if (!prefixcmp(refname, "refs/tags") && obj->type == OBJ_TAG) { | ||
25 | if (!(obj = deref_tag(obj, refname, 0))) | ||
26 | return 0; | ||
27 | htmlf("%s\t%s\n", sha1_to_hex(sha1), refname); | ||
28 | htmlf("%s\t%s^{}\n", sha1_to_hex(obj->sha1), refname); | ||
29 | } | ||
30 | return 0; | ||
31 | } | ||
32 | |||
33 | static void print_pack_info(struct cgit_context *ctx) | ||
34 | { | ||
35 | struct packed_git *pack; | ||
36 | int ofs; | ||
37 | |||
38 | ctx->page.mimetype = "text/plain"; | ||
39 | ctx->page.filename = "objects/info/packs"; | ||
40 | cgit_print_http_headers(ctx); | ||
41 | ofs = strlen(ctx->repo->path) + strlen("/objects/pack/"); | ||
42 | prepare_packed_git(); | ||
43 | for (pack = packed_git; pack; pack = pack->next) | ||
44 | if (pack->pack_local) | ||
45 | htmlf("P %s\n", pack->pack_name + ofs); | ||
46 | } | ||
47 | |||
48 | static void send_file(struct cgit_context *ctx, char *path) | ||
49 | { | ||
50 | struct stat st; | ||
51 | int err; | ||
52 | |||
53 | if (stat(path, &st)) { | ||
54 | switch (errno) { | ||
55 | case ENOENT: | ||
56 | err = 404; | ||
57 | break; | ||
58 | case EACCES: | ||
59 | err = 403; | ||
60 | break; | ||
61 | default: | ||
62 | err = 400; | ||
63 | } | ||
64 | html_status(err, 0); | ||
65 | return; | ||
66 | } | ||
67 | ctx->page.mimetype = "application/octet-stream"; | ||
68 | ctx->page.filename = path; | ||
69 | if (prefixcmp(ctx->repo->path, path)) | ||
70 | ctx->page.filename += strlen(ctx->repo->path) + 1; | ||
71 | cgit_print_http_headers(ctx); | ||
72 | html_include(path); | ||
73 | } | ||
74 | |||
75 | void cgit_clone_info(struct cgit_context *ctx) | ||
76 | { | ||
77 | if (!ctx->qry.path || strcmp(ctx->qry.path, "refs")) | ||
78 | return; | ||
79 | |||
80 | ctx->page.mimetype = "text/plain"; | ||
81 | ctx->page.filename = "info/refs"; | ||
82 | cgit_print_http_headers(ctx); | ||
83 | for_each_ref(print_ref_info, ctx); | ||
84 | } | ||
85 | |||
86 | void cgit_clone_objects(struct cgit_context *ctx) | ||
87 | { | ||
88 | if (!ctx->qry.path) { | ||
89 | html_status(400, 0); | ||
90 | return; | ||
91 | } | ||
92 | |||
93 | if (!strcmp(ctx->qry.path, "info/packs")) { | ||
94 | print_pack_info(ctx); | ||
95 | return; | ||
96 | } | ||
97 | |||
98 | send_file(ctx, git_path("objects/%s", ctx->qry.path)); | ||
99 | } | ||
100 | |||
101 | void cgit_clone_head(struct cgit_context *ctx) | ||
102 | { | ||
103 | send_file(ctx, git_path("%s", "HEAD")); | ||
104 | } | ||
diff --git a/ui-clone.h b/ui-clone.h new file mode 100644 index 0000000..89cd4f1 --- a/dev/null +++ b/ui-clone.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef UI_CLONE_H | ||
2 | #define UI_CLONE_H | ||
3 | |||
4 | void cgit_clone_info(struct cgit_context *ctx); | ||
5 | void cgit_clone_objects(struct cgit_context *ctx); | ||
6 | void cgit_clone_head(struct cgit_context *ctx); | ||
7 | |||
8 | #endif /* UI_CLONE_H */ | ||