summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2007-05-08 22:48:09 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2007-05-08 22:48:09 (UTC)
commitca8eb8fc8f71ee0a40015c323619f776840b6503 (patch) (unidiff)
treef74a114ba19c215bffe8362bf8e63495155ebef4
parent7250a154678477a1e8260efbc9810ec389754ef9 (diff)
downloadcgit-ca8eb8fc8f71ee0a40015c323619f776840b6503.zip
cgit-ca8eb8fc8f71ee0a40015c323619f776840b6503.tar.gz
cgit-ca8eb8fc8f71ee0a40015c323619f776840b6503.tar.bz2
Add support for downloading single blobs
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--Makefile2
-rw-r--r--cgit.c9
-rw-r--r--cgit.h1
-rw-r--r--ui-blob.c31
-rw-r--r--ui-view.c7
5 files changed, 47 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 24c3963..7c0527b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,24 +1,24 @@
1CGIT_VERSION = 0.2 1CGIT_VERSION = 0.2
2 2
3prefix = /var/www/htdocs/cgit 3prefix = /var/www/htdocs/cgit
4gitsrc = ../git 4gitsrc = ../git
5 5
6SHA1_HEADER = <openssl/sha.h> 6SHA1_HEADER = <openssl/sha.h>
7 7
8CACHE_ROOT = /var/cache/cgit 8CACHE_ROOT = /var/cache/cgit
9EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto 9EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto
10OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ 10OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \
11 ui-summary.o ui-log.o ui-view.o ui-tree.o ui-commit.o ui-diff.o \ 11 ui-summary.o ui-log.o ui-view.o ui-tree.o ui-commit.o ui-diff.o \
12 ui-snapshot.o 12 ui-snapshot.o ui-blob.o
13 13
14CFLAGS += -Wall 14CFLAGS += -Wall
15 15
16ifdef DEBUG 16ifdef DEBUG
17 CFLAGS += -g 17 CFLAGS += -g
18endif 18endif
19 19
20CFLAGS += -I$(gitsrc) -DSHA1_HEADER='$(SHA1_HEADER)' 20CFLAGS += -I$(gitsrc) -DSHA1_HEADER='$(SHA1_HEADER)'
21 21
22all: cgit 22all: cgit
23 23
24install: all clean-cache 24install: all clean-cache
diff --git a/cgit.c b/cgit.c
index 6dddcbb..28bab8d 100644
--- a/cgit.c
+++ b/cgit.c
@@ -70,30 +70,35 @@ static void cgit_print_repo_page(struct cacheitem *item)
70 cgit_print_docstart(title, item); 70 cgit_print_docstart(title, item);
71 cgit_print_pageheader(title, 0); 71 cgit_print_pageheader(title, 0);
72 cgit_print_error(fmt("Unable to scan repository: %s", 72 cgit_print_error(fmt("Unable to scan repository: %s",
73 strerror(errno))); 73 strerror(errno)));
74 cgit_print_docend(); 74 cgit_print_docend();
75 return; 75 return;
76 } 76 }
77 77
78 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 78 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
79 show_search = 0; 79 show_search = 0;
80 setenv("GIT_DIR", cgit_repo->path, 1); 80 setenv("GIT_DIR", cgit_repo->path, 1);
81 81
82 if (cgit_repo->snapshots && cgit_query_page && 82 if (cgit_query_page) {
83 !strcmp(cgit_query_page, "snapshot")) { 83 if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) {
84 cgit_print_snapshot(item, cgit_query_sha1, "zip", 84 cgit_print_snapshot(item, cgit_query_sha1, "zip",
85 cgit_repo->url, cgit_query_name); 85 cgit_repo->url, cgit_query_name);
86 return; 86 return;
87 } 87 }
88 if (!strcmp(cgit_query_page, "blob")) {
89 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
90 return;
91 }
92 }
88 93
89 if (cgit_query_page && !strcmp(cgit_query_page, "log")) 94 if (cgit_query_page && !strcmp(cgit_query_page, "log"))
90 show_search = 1; 95 show_search = 1;
91 96
92 cgit_print_docstart(title, item); 97 cgit_print_docstart(title, item);
93 98
94 99
95 if (!cgit_query_page) { 100 if (!cgit_query_page) {
96 cgit_print_pageheader("summary", show_search); 101 cgit_print_pageheader("summary", show_search);
97 cgit_print_summary(); 102 cgit_print_summary();
98 cgit_print_docend(); 103 cgit_print_docend();
99 return; 104 return;
diff --git a/cgit.h b/cgit.h
index 2615231..292e488 100644
--- a/cgit.h
+++ b/cgit.h
@@ -147,20 +147,21 @@ extern void cgit_print_error(char *msg);
147extern void cgit_print_date(unsigned long secs); 147extern void cgit_print_date(unsigned long secs);
148extern void cgit_print_docstart(char *title, struct cacheitem *item); 148extern void cgit_print_docstart(char *title, struct cacheitem *item);
149extern void cgit_print_docend(); 149extern void cgit_print_docend();
150extern void cgit_print_pageheader(char *title, int show_search); 150extern void cgit_print_pageheader(char *title, int show_search);
151extern void cgit_print_snapshot_start(const char *mimetype, 151extern void cgit_print_snapshot_start(const char *mimetype,
152 const char *filename, 152 const char *filename,
153 struct cacheitem *item); 153 struct cacheitem *item);
154 154
155extern void cgit_print_repolist(struct cacheitem *item); 155extern void cgit_print_repolist(struct cacheitem *item);
156extern void cgit_print_summary(); 156extern void cgit_print_summary();
157extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep); 157extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep);
158extern void cgit_print_view(const char *hex, char *path); 158extern void cgit_print_view(const char *hex, char *path);
159extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
159extern void cgit_print_tree(const char *hex, char *path); 160extern void cgit_print_tree(const char *hex, char *path);
160extern void cgit_print_commit(const char *hex); 161extern void cgit_print_commit(const char *hex);
161extern void cgit_print_diff(const char *old_hex, const char *new_hex); 162extern void cgit_print_diff(const char *old_hex, const char *new_hex);
162extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, 163extern void cgit_print_snapshot(struct cacheitem *item, const char *hex,
163 const char *format, const char *prefix, 164 const char *format, const char *prefix,
164 const char *filename); 165 const char *filename);
165 166
166#endif /* CGIT_H */ 167#endif /* CGIT_H */
diff --git a/ui-blob.c b/ui-blob.c
new file mode 100644
index 0000000..f5b844b
--- a/dev/null
+++ b/ui-blob.c
@@ -0,0 +1,31 @@
1#include "cgit.h"
2
3void cgit_print_blob(struct cacheitem *item, const char *hex, char *path)
4{
5
6 unsigned char sha1[20];
7 enum object_type type;
8 unsigned char *buf;
9 unsigned long size;
10
11 if (get_sha1_hex(hex, sha1)){
12 cgit_print_error(fmt("Bad hex value: %s", hex));
13 return;
14 }
15
16 type = sha1_object_info(sha1, &size);
17 if (type == OBJ_BAD) {
18 cgit_print_error(fmt("Bad object name: %s", hex));
19 return;
20 }
21
22 buf = read_sha1_file(sha1, &type, &size);
23 if (!buf) {
24 cgit_print_error(fmt("Error reading object %s", hex));
25 return;
26 }
27
28 buf[size] = '\0';
29 cgit_print_snapshot_start("text/plain", path, item);
30 write(htmlfd, buf, size);
31}
diff --git a/ui-view.c b/ui-view.c
index 7d022fd..dbe4b29 100644
--- a/ui-view.c
+++ b/ui-view.c
@@ -31,18 +31,25 @@ void cgit_print_view(const char *hex, char *path)
31 cgit_print_error(fmt("Error reading object %s", hex)); 31 cgit_print_error(fmt("Error reading object %s", hex));
32 return; 32 return;
33 } 33 }
34 34
35 buf[size] = '\0'; 35 buf[size] = '\0';
36 html("<table class='list'>\n"); 36 html("<table class='list'>\n");
37 html("<tr class='nohover'><th class='left'>"); 37 html("<tr class='nohover'><th class='left'>");
38 if (path) 38 if (path)
39 htmlf("%s (", path); 39 htmlf("%s (", path);
40 htmlf("%s %s, %li bytes", typename(type), hex, size); 40 htmlf("%s %s, %li bytes", typename(type), hex, size);
41 if (path) 41 if (path)
42 html(")"); 42 html(")");
43
44 html(" <a href='");
45 html_attr(cgit_pageurl(cgit_query_repo, "blob",
46 fmt("id=%s&path=%s",
47 hex,
48 path)));
49 html("'>download</a>");
43 html("</th></tr>\n"); 50 html("</th></tr>\n");
44 html("<tr><td class='blob'>\n"); 51 html("<tr><td class='blob'>\n");
45 html_txt(buf); 52 html_txt(buf);
46 html("\n</td></tr>\n"); 53 html("\n</td></tr>\n");
47 html("</table>\n"); 54 html("</table>\n");
48} 55}