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 1163b02..b002d44 100644
--- a/Makefile
+++ b/Makefile
@@ -1,123 +1,124 @@
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.6.0.rc1 7GIT_VER = 1.6.0.rc1
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-atom.o 58OBJECTS += ui-atom.o
59OBJECTS += ui-blob.o 59OBJECTS += ui-blob.o
60OBJECTS += ui-clone.o
60OBJECTS += ui-commit.o 61OBJECTS += ui-commit.o
61OBJECTS += ui-diff.o 62OBJECTS += ui-diff.o
62OBJECTS += ui-log.o 63OBJECTS += ui-log.o
63OBJECTS += ui-patch.o 64OBJECTS += ui-patch.o
64OBJECTS += ui-refs.o 65OBJECTS += ui-refs.o
65OBJECTS += ui-repolist.o 66OBJECTS += ui-repolist.o
66OBJECTS += ui-shared.o 67OBJECTS += ui-shared.o
67OBJECTS += ui-snapshot.o 68OBJECTS += ui-snapshot.o
68OBJECTS += ui-summary.o 69OBJECTS += ui-summary.o
69OBJECTS += ui-tag.o 70OBJECTS += ui-tag.o
70OBJECTS += ui-tree.o 71OBJECTS += ui-tree.o
71 72
72ifdef NEEDS_LIBICONV 73ifdef NEEDS_LIBICONV
73 EXTLIBS += -liconv 74 EXTLIBS += -liconv
74endif 75endif
75 76
76 77
77.PHONY: all libgit test install uninstall clean force-version get-git 78.PHONY: all libgit test install uninstall clean force-version get-git
78 79
79all: cgit 80all: cgit
80 81
81VERSION: force-version 82VERSION: force-version
82 @./gen-version.sh "$(CGIT_VERSION)" 83 @./gen-version.sh "$(CGIT_VERSION)"
83-include VERSION 84-include VERSION
84 85
85 86
86CFLAGS += -g -Wall -Igit 87CFLAGS += -g -Wall -Igit
87CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' 88CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)'
88CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' 89CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"'
89CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' 90CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
90CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' 91CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
91CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' 92CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
92 93
93 94
94cgit: $(OBJECTS) libgit 95cgit: $(OBJECTS) libgit
95 $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) 96 $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
96 97
97cgit.o: VERSION 98cgit.o: VERSION
98 99
99-include $(OBJECTS:.o=.d) 100-include $(OBJECTS:.o=.d)
100 101
101libgit: 102libgit:
102 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a 103 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a
103 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a 104 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a
104 105
105test: all 106test: all
106 $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all 107 $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all
107 108
108install: all 109install: all
109 mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) 110 mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH)
110 install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) 111 install cgit $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME)
111 install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css 112 install cgit.css $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.css
112 install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png 113 install cgit.png $(DESTDIR)$(CGIT_SCRIPT_PATH)/cgit.png
113 114
114uninstall: 115uninstall:
115 rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) 116 rm -f $(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME)
116 rm -f $(CGIT_SCRIPT_PATH)/cgit.css 117 rm -f $(CGIT_SCRIPT_PATH)/cgit.css
117 rm -f $(CGIT_SCRIPT_PATH)/cgit.png 118 rm -f $(CGIT_SCRIPT_PATH)/cgit.png
118 119
119clean: 120clean:
120 rm -f cgit VERSION *.o *.d 121 rm -f cgit VERSION *.o *.d
121 122
122get-git: 123get-git:
123 curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git 124 curl $(GIT_URL) | tar -xj && rm -rf git && mv git-$(GIT_VER) git
diff --git a/cmd.c b/cmd.c
index c0e4db3..40ac53e 100644
--- a/cmd.c
+++ b/cmd.c
@@ -1,139 +1,158 @@
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-atom.h" 13#include "ui-atom.h"
14#include "ui-blob.h" 14#include "ui-blob.h"
15#include "ui-clone.h"
15#include "ui-commit.h" 16#include "ui-commit.h"
16#include "ui-diff.h" 17#include "ui-diff.h"
17#include "ui-log.h" 18#include "ui-log.h"
18#include "ui-patch.h" 19#include "ui-patch.h"
19#include "ui-refs.h" 20#include "ui-refs.h"
20#include "ui-repolist.h" 21#include "ui-repolist.h"
21#include "ui-snapshot.h" 22#include "ui-snapshot.h"
22#include "ui-summary.h" 23#include "ui-summary.h"
23#include "ui-tag.h" 24#include "ui-tag.h"
24#include "ui-tree.h" 25#include "ui-tree.h"
25 26
27static void HEAD_fn(struct cgit_context *ctx)
28{
29 cgit_clone_head(ctx);
30}
31
26static void atom_fn(struct cgit_context *ctx) 32static void atom_fn(struct cgit_context *ctx)
27{ 33{
28 cgit_print_atom(ctx->qry.head, ctx->qry.path, 10); 34 cgit_print_atom(ctx->qry.head, ctx->qry.path, 10);
29} 35}
30 36
31static void about_fn(struct cgit_context *ctx) 37static void about_fn(struct cgit_context *ctx)
32{ 38{
33 if (ctx->repo) 39 if (ctx->repo)
34 cgit_print_repo_readme(); 40 cgit_print_repo_readme();
35 else 41 else
36 cgit_print_site_readme(); 42 cgit_print_site_readme();
37} 43}
38 44
39static void blob_fn(struct cgit_context *ctx) 45static void blob_fn(struct cgit_context *ctx)
40{ 46{
41 cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head); 47 cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head);
42} 48}
43 49
44static void commit_fn(struct cgit_context *ctx) 50static void commit_fn(struct cgit_context *ctx)
45{ 51{
46 cgit_print_commit(ctx->qry.sha1); 52 cgit_print_commit(ctx->qry.sha1);
47} 53}
48 54
49static void diff_fn(struct cgit_context *ctx) 55static void diff_fn(struct cgit_context *ctx)
50{ 56{
51 cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); 57 cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path);
52} 58}
53 59
60static void info_fn(struct cgit_context *ctx)
61{
62 cgit_clone_info(ctx);
63}
64
54static void log_fn(struct cgit_context *ctx) 65static void log_fn(struct cgit_context *ctx)
55{ 66{
56 cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, 67 cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count,
57 ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); 68 ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1);
58} 69}
59 70
60static void ls_cache_fn(struct cgit_context *ctx) 71static void ls_cache_fn(struct cgit_context *ctx)
61{ 72{
62 ctx->page.mimetype = "text/plain"; 73 ctx->page.mimetype = "text/plain";
63 ctx->page.filename = "ls-cache.txt"; 74 ctx->page.filename = "ls-cache.txt";
64 cgit_print_http_headers(ctx); 75 cgit_print_http_headers(ctx);
65 cache_ls(ctx->cfg.cache_root); 76 cache_ls(ctx->cfg.cache_root);
66} 77}
67 78
79static void objects_fn(struct cgit_context *ctx)
80{
81 cgit_clone_objects(ctx);
82}
83
68static void repolist_fn(struct cgit_context *ctx) 84static void repolist_fn(struct cgit_context *ctx)
69{ 85{
70 cgit_print_repolist(); 86 cgit_print_repolist();
71} 87}
72 88
73static void patch_fn(struct cgit_context *ctx) 89static void patch_fn(struct cgit_context *ctx)
74{ 90{
75 cgit_print_patch(ctx->qry.sha1); 91 cgit_print_patch(ctx->qry.sha1);
76} 92}
77 93
78static void refs_fn(struct cgit_context *ctx) 94static void refs_fn(struct cgit_context *ctx)
79{ 95{
80 cgit_print_refs(); 96 cgit_print_refs();
81} 97}
82 98
83static void snapshot_fn(struct cgit_context *ctx) 99static void snapshot_fn(struct cgit_context *ctx)
84{ 100{
85 cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, 101 cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1,
86 cgit_repobasename(ctx->repo->url), ctx->qry.path, 102 cgit_repobasename(ctx->repo->url), ctx->qry.path,
87 ctx->repo->snapshots); 103 ctx->repo->snapshots);
88} 104}
89 105
90static void summary_fn(struct cgit_context *ctx) 106static void summary_fn(struct cgit_context *ctx)
91{ 107{
92 cgit_print_summary(); 108 cgit_print_summary();
93} 109}
94 110
95static void tag_fn(struct cgit_context *ctx) 111static void tag_fn(struct cgit_context *ctx)
96{ 112{
97 cgit_print_tag(ctx->qry.sha1); 113 cgit_print_tag(ctx->qry.sha1);
98} 114}
99 115
100static void tree_fn(struct cgit_context *ctx) 116static void tree_fn(struct cgit_context *ctx)
101{ 117{
102 cgit_print_tree(ctx->qry.sha1, ctx->qry.path); 118 cgit_print_tree(ctx->qry.sha1, ctx->qry.path);
103} 119}
104 120
105#define def_cmd(name, want_repo, want_layout) \ 121#define def_cmd(name, want_repo, want_layout) \
106 {#name, name##_fn, want_repo, want_layout} 122 {#name, name##_fn, want_repo, want_layout}
107 123
108struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) 124struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
109{ 125{
110 static struct cgit_cmd cmds[] = { 126 static struct cgit_cmd cmds[] = {
127 def_cmd(HEAD, 1, 0),
111 def_cmd(atom, 1, 0), 128 def_cmd(atom, 1, 0),
112 def_cmd(about, 0, 1), 129 def_cmd(about, 0, 1),
113 def_cmd(blob, 1, 0), 130 def_cmd(blob, 1, 0),
114 def_cmd(commit, 1, 1), 131 def_cmd(commit, 1, 1),
115 def_cmd(diff, 1, 1), 132 def_cmd(diff, 1, 1),
133 def_cmd(info, 1, 0),
116 def_cmd(log, 1, 1), 134 def_cmd(log, 1, 1),
117 def_cmd(ls_cache, 0, 0), 135 def_cmd(ls_cache, 0, 0),
136 def_cmd(objects, 1, 0),
118 def_cmd(patch, 1, 0), 137 def_cmd(patch, 1, 0),
119 def_cmd(refs, 1, 1), 138 def_cmd(refs, 1, 1),
120 def_cmd(repolist, 0, 0), 139 def_cmd(repolist, 0, 0),
121 def_cmd(snapshot, 1, 0), 140 def_cmd(snapshot, 1, 0),
122 def_cmd(summary, 1, 1), 141 def_cmd(summary, 1, 1),
123 def_cmd(tag, 1, 1), 142 def_cmd(tag, 1, 1),
124 def_cmd(tree, 1, 1), 143 def_cmd(tree, 1, 1),
125 }; 144 };
126 int i; 145 int i;
127 146
128 if (ctx->qry.page == NULL) { 147 if (ctx->qry.page == NULL) {
129 if (ctx->repo) 148 if (ctx->repo)
130 ctx->qry.page = "summary"; 149 ctx->qry.page = "summary";
131 else 150 else
132 ctx->qry.page = "repolist"; 151 ctx->qry.page = "repolist";
133 } 152 }
134 153
135 for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) 154 for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++)
136 if (!strcmp(ctx->qry.page, cmds[i].name)) 155 if (!strcmp(ctx->qry.page, cmds[i].name))
137 return &cmds[i]; 156 return &cmds[i];
138 return NULL; 157 return NULL;
139} 158}
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 */