summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile1
-rw-r--r--cmd.c19
-rw-r--r--html.c7
-rw-r--r--html.h1
-rw-r--r--ui-clone.c104
-rw-r--r--ui-clone.h8
6 files changed, 140 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index e1436a3..78aad10 100644
--- a/Makefile
+++ b/Makefile
@@ -1,124 +1,125 @@
1CGIT_VERSION = v0.7.2 1CGIT_VERSION = v0.7.2
2CGIT_SCRIPT_NAME = cgit.cgi 2CGIT_SCRIPT_NAME = cgit.cgi
3CGIT_SCRIPT_PATH = /var/www/htdocs/cgit 3CGIT_SCRIPT_PATH = /var/www/htdocs/cgit
4CGIT_CONFIG = /etc/cgitrc 4CGIT_CONFIG = /etc/cgitrc
5CACHE_ROOT = /var/cache/cgit 5CACHE_ROOT = /var/cache/cgit
6SHA1_HEADER = <openssl/sha.h> 6SHA1_HEADER = <openssl/sha.h>
7GIT_VER = 1.5.6 7GIT_VER = 1.5.6
8GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 8GIT_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#
19QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir 19QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
20QUIET_SUBDIR1 = 20QUIET_SUBDIR1 =
21 21
22ifneq ($(findstring $(MAKEFLAGS),w),w) 22ifneq ($(findstring $(MAKEFLAGS),w),w)
23PRINT_DIR = --no-print-directory 23PRINT_DIR = --no-print-directory
24else # "make -w" 24else # "make -w"
25NO_SUBDIR = : 25NO_SUBDIR = :
26endif 26endif
27 27
28ifndef V 28ifndef 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
34endif 34endif
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
49EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto 49EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
50OBJECTS = 50OBJECTS =
51OBJECTS += cache.o 51OBJECTS += cache.o
52OBJECTS += cgit.o 52OBJECTS += cgit.o
53OBJECTS += cmd.o 53OBJECTS += cmd.o
54OBJECTS += configfile.o 54OBJECTS += configfile.o
55OBJECTS += html.o 55OBJECTS += html.o
56OBJECTS += parsing.o 56OBJECTS += parsing.o
57OBJECTS += shared.o 57OBJECTS += shared.o
58OBJECTS += ui-blob.o 58OBJECTS += ui-blob.o
59OBJECTS += ui-clone.o
59OBJECTS += ui-commit.o 60OBJECTS += ui-commit.o
60OBJECTS += ui-diff.o 61OBJECTS += ui-diff.o
61OBJECTS += ui-log.o 62OBJECTS += ui-log.o
62OBJECTS += ui-patch.o 63OBJECTS += ui-patch.o
63OBJECTS += ui-refs.o 64OBJECTS += ui-refs.o
64OBJECTS += ui-repolist.o 65OBJECTS += ui-repolist.o
65OBJECTS += ui-shared.o 66OBJECTS += ui-shared.o
66OBJECTS += ui-snapshot.o 67OBJECTS += ui-snapshot.o
67OBJECTS += ui-summary.o 68OBJECTS += ui-summary.o
68OBJECTS += ui-tag.o 69OBJECTS += ui-tag.o
69OBJECTS += ui-tree.o 70OBJECTS += ui-tree.o
70 71
71ifdef NEEDS_LIBICONV 72ifdef NEEDS_LIBICONV
72 EXTLIBS += -liconv 73 EXTLIBS += -liconv
73endif 74endif
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
78all: cgit 79all: cgit
79 80
80VERSION: force-version 81VERSION: force-version
81 @./gen-version.sh "$(CGIT_VERSION)" 82 @./gen-version.sh "$(CGIT_VERSION)"
82-include VERSION 83-include VERSION
83 84
84 85
85CFLAGS += -g -Wall -Igit 86CFLAGS += -g -Wall -Igit
86CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' 87CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)'
87CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' 88CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"'
88CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' 89CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
89CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' 90CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
90CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' 91CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
91 92
92 93
93cgit: $(OBJECTS) git/libgit.a git/xdiff/lib.a 94cgit: $(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
96cgit.o: VERSION 97cgit.o: VERSION
97 98
98-include $(OBJECTS:.o=.d) 99-include $(OBJECTS:.o=.d)
99 100
100git/libgit.a: git 101git/libgit.a: git
101 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a 102 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a
102 103
103git/xdiff/lib.a: git 104git/xdiff/lib.a: git
104 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a 105 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a
105 106
106test: all 107test: all
107 $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all 108 $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all
108 109
109install: all 110install: 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
115uninstall: 116uninstall:
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
120clean: 121clean:
121 rm -f cgit VERSION *.o *.d 122 rm -f cgit VERSION *.o *.d
122 123
123get-git: 124get-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
diff --git a/cmd.c b/cmd.c
index fe0ea8f..03e165c 100644
--- a/cmd.c
+++ b/cmd.c
@@ -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
26static void HEAD_fn(struct cgit_context *ctx)
27{
28 cgit_clone_head(ctx);
29}
30
25static void about_fn(struct cgit_context *ctx) 31static 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
33static void blob_fn(struct cgit_context *ctx) 39static 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
38static void commit_fn(struct cgit_context *ctx) 44static 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
43static void diff_fn(struct cgit_context *ctx) 49static 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
54static void info_fn(struct cgit_context *ctx)
55{
56 cgit_clone_info(ctx);
57}
58
48static void log_fn(struct cgit_context *ctx) 59static 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
54static void ls_cache_fn(struct cgit_context *ctx) 65static 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
73static void objects_fn(struct cgit_context *ctx)
74{
75 cgit_clone_objects(ctx);
76}
77
62static void repolist_fn(struct cgit_context *ctx) 78static void repolist_fn(struct cgit_context *ctx)
63{ 79{
64 cgit_print_repolist(); 80 cgit_print_repolist();
65} 81}
66 82
67static void patch_fn(struct cgit_context *ctx) 83static 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
72static void refs_fn(struct cgit_context *ctx) 88static void refs_fn(struct cgit_context *ctx)
73{ 89{
74 cgit_print_refs(); 90 cgit_print_refs();
75} 91}
76 92
77static void snapshot_fn(struct cgit_context *ctx) 93static 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
84static void summary_fn(struct cgit_context *ctx) 100static void summary_fn(struct cgit_context *ctx)
85{ 101{
86 cgit_print_summary(); 102 cgit_print_summary();
87} 103}
88 104
89static void tag_fn(struct cgit_context *ctx) 105static 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
94static void tree_fn(struct cgit_context *ctx) 110static 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
102struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) 118struct 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}
diff --git a/html.c b/html.c
index bddb04d..1237076 100644
--- a/html.c
+++ b/html.c
@@ -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
16int htmlfd = STDOUT_FILENO; 16int htmlfd = STDOUT_FILENO;
17 17
18char *fmt(const char *format, ...) 18char *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
38void html(const char *txt) 38void html(const char *txt)
39{ 39{
40 write(htmlfd, txt, strlen(txt)); 40 write(htmlfd, txt, strlen(txt));
41} 41}
42 42
43void htmlf(const char *format, ...) 43void 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
54void html_status(int code, int more_headers)
55{
56 htmlf("Status: %d\n", code);
57 if (!more_headers)
58 html("\n");
59}
60
54void html_txt(char *txt) 61void 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("&gt;"); 69 html("&gt;");
63 else if (c=='<') 70 else if (c=='<')
64 html("&lt;"); 71 html("&lt;");
65 else if (c=='&') 72 else if (c=='&')
66 html("&amp;"); 73 html("&amp;");
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
75void html_ntxt(int len, char *txt) 82void 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("&gt;"); 90 html("&gt;");
84 else if (c=='<') 91 else if (c=='<')
85 html("&lt;"); 92 html("&lt;");
86 else if (c=='&') 93 else if (c=='&')
87 html("&amp;"); 94 html("&amp;");
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
98void html_attr(char *txt) 105void 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("&gt;"); 113 html("&gt;");
107 else if (c=='<') 114 else if (c=='<')
108 html("&lt;"); 115 html("&lt;");
109 else if (c=='\'') 116 else if (c=='\'')
110 html("&quote;"); 117 html("&quote;");
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
119void html_hidden(char *name, char *value) 126void 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
128void html_option(char *value, char *text, char *selected_value) 135void 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
140void html_link_open(char *url, char *title, char *class) 147void 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
155void html_link_close(void) 162void html_link_close(void)
156{ 163{
157 html("</a>"); 164 html("</a>");
158} 165}
159 166
160void html_fileperm(unsigned short mode) 167void 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
166int html_include(const char *filename) 173int 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
183int hextoint(char c) 190int 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
195char *convert_query_hexchar(char *txt) 202char *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
214int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) 221int 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}
diff --git a/html.h b/html.h
index e6fdc54..2bde28d 100644
--- a/html.h
+++ b/html.h
@@ -1,20 +1,21 @@
1#ifndef HTML_H 1#ifndef HTML_H
2#define HTML_H 2#define HTML_H
3 3
4extern int htmlfd; 4extern int htmlfd;
5 5
6extern void html(const char *txt); 6extern void html(const char *txt);
7extern void htmlf(const char *format,...); 7extern void htmlf(const char *format,...);
8extern void html_status(int code, int more_headers);
8extern void html_txt(char *txt); 9extern void html_txt(char *txt);
9extern void html_ntxt(int len, char *txt); 10extern void html_ntxt(int len, char *txt);
10extern void html_attr(char *txt); 11extern void html_attr(char *txt);
11extern void html_hidden(char *name, char *value); 12extern void html_hidden(char *name, char *value);
12extern void html_option(char *value, char *text, char *selected_value); 13extern void html_option(char *value, char *text, char *selected_value);
13extern void html_link_open(char *url, char *title, char *class); 14extern void html_link_open(char *url, char *title, char *class);
14extern void html_link_close(void); 15extern void html_link_close(void);
15extern void html_fileperm(unsigned short mode); 16extern void html_fileperm(unsigned short mode);
16extern int html_include(const char *filename); 17extern int html_include(const char *filename);
17 18
18extern int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)); 19extern 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
14static 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
33static 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
48static 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
75void 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
86void 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
101void 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
4void cgit_clone_info(struct cgit_context *ctx);
5void cgit_clone_objects(struct cgit_context *ctx);
6void cgit_clone_head(struct cgit_context *ctx);
7
8#endif /* UI_CLONE_H */