summaryrefslogtreecommitdiffabout
Unidiff
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,36 +1,36 @@
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
25 mkdir -p $(prefix) 25 mkdir -p $(prefix)
26 install cgit $(prefix)/cgit.cgi 26 install cgit $(prefix)/cgit.cgi
27 install cgit.css $(prefix)/cgit.css 27 install cgit.css $(prefix)/cgit.css
28 28
29cgit: cgit.c cgit.h $(OBJECTS) $(gitsrc)/libgit.a 29cgit: cgit.c cgit.h $(OBJECTS) $(gitsrc)/libgit.a
30 $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \ 30 $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \
31 $(OBJECTS) $(EXTLIBS) 31 $(OBJECTS) $(EXTLIBS)
32 32
33$(OBJECTS): cgit.h 33$(OBJECTS): cgit.h
34 34
35$(gitsrc)/libgit.a: 35$(gitsrc)/libgit.a:
36 $(MAKE) -C $(gitsrc) 36 $(MAKE) -C $(gitsrc)
diff --git a/cgit.c b/cgit.c
index 6dddcbb..28bab8d 100644
--- a/cgit.c
+++ b/cgit.c
@@ -58,54 +58,59 @@ static int cgit_prepare_cache(struct cacheitem *item)
58 item->ttl = cgit_cache_repo_ttl; 58 item->ttl = cgit_cache_repo_ttl;
59 } 59 }
60 return 1; 60 return 1;
61} 61}
62 62
63static void cgit_print_repo_page(struct cacheitem *item) 63static void cgit_print_repo_page(struct cacheitem *item)
64{ 64{
65 char *title; 65 char *title;
66 int show_search; 66 int show_search;
67 67
68 if (chdir(cgit_repo->path)) { 68 if (chdir(cgit_repo->path)) {
69 title = fmt("%s - %s", cgit_root_title, "Bad request"); 69 title = fmt("%s - %s", cgit_root_title, "Bad request");
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;
100 } 105 }
101 106
102 cgit_print_pageheader(cgit_query_page, show_search); 107 cgit_print_pageheader(cgit_query_page, show_search);
103 108
104 if (!strcmp(cgit_query_page, "log")) { 109 if (!strcmp(cgit_query_page, "log")) {
105 cgit_print_log(cgit_query_head, cgit_query_ofs, 100, 110 cgit_print_log(cgit_query_head, cgit_query_ofs, 100,
106 cgit_query_search); 111 cgit_query_search);
107 } else if (!strcmp(cgit_query_page, "tree")) { 112 } else if (!strcmp(cgit_query_page, "tree")) {
108 cgit_print_tree(cgit_query_sha1, cgit_query_path); 113 cgit_print_tree(cgit_query_sha1, cgit_query_path);
109 } else if (!strcmp(cgit_query_page, "commit")) { 114 } else if (!strcmp(cgit_query_page, "commit")) {
110 cgit_print_commit(cgit_query_sha1); 115 cgit_print_commit(cgit_query_sha1);
111 } else if (!strcmp(cgit_query_page, "view")) { 116 } else if (!strcmp(cgit_query_page, "view")) {
diff --git a/cgit.h b/cgit.h
index 2615231..292e488 100644
--- a/cgit.h
+++ b/cgit.h
@@ -135,32 +135,33 @@ extern struct taginfo *cgit_parse_tag(struct tag *tag);
135extern char *cache_safe_filename(const char *unsafe); 135extern char *cache_safe_filename(const char *unsafe);
136extern int cache_lock(struct cacheitem *item); 136extern int cache_lock(struct cacheitem *item);
137extern int cache_unlock(struct cacheitem *item); 137extern int cache_unlock(struct cacheitem *item);
138extern int cache_cancel_lock(struct cacheitem *item); 138extern int cache_cancel_lock(struct cacheitem *item);
139extern int cache_exist(struct cacheitem *item); 139extern int cache_exist(struct cacheitem *item);
140extern int cache_expired(struct cacheitem *item); 140extern int cache_expired(struct cacheitem *item);
141 141
142extern char *cgit_repourl(const char *reponame); 142extern char *cgit_repourl(const char *reponame);
143extern char *cgit_pageurl(const char *reponame, const char *pagename, 143extern char *cgit_pageurl(const char *reponame, const char *pagename,
144 const char *query); 144 const char *query);
145 145
146extern void cgit_print_error(char *msg); 146extern 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
@@ -19,30 +19,37 @@ void cgit_print_view(const char *hex, char *path)
19 cgit_print_error(fmt("Bad hex value: %s", hex)); 19 cgit_print_error(fmt("Bad hex value: %s", hex));
20 return; 20 return;
21 } 21 }
22 22
23 type = sha1_object_info(sha1, &size); 23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) { 24 if (type == OBJ_BAD) {
25 cgit_print_error(fmt("Bad object name: %s", hex)); 25 cgit_print_error(fmt("Bad object name: %s", hex));
26 return; 26 return;
27 } 27 }
28 28
29 buf = read_sha1_file(sha1, &type, &size); 29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) { 30 if (!buf) {
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}