summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2007-06-16 18:20:42 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2007-06-16 18:20:42 (UTC)
commitffc69736a606fd8f9a10468cce48b9a37b428944 (patch) (unidiff)
treeb080c702391244a525fa0e7c97758f7f7fa0c383
parent849f0f0f02f858e21fbe59295846e51628f82915 (diff)
downloadcgit-ffc69736a606fd8f9a10468cce48b9a37b428944.zip
cgit-ffc69736a606fd8f9a10468cce48b9a37b428944.tar.gz
cgit-ffc69736a606fd8f9a10468cce48b9a37b428944.tar.bz2
ui-tree: unify with ui-view, use path to select tree/blob
This teaches ui-tree to show both trees and blobs, thereby making ui-view superfluous. At the same time, ui-tree is extended to honour the specified path instead of requiering a tree/blob sha1.
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--Makefile2
-rw-r--r--cgit.c5
-rw-r--r--cgit.css16
-rw-r--r--cgit.h8
-rw-r--r--shared.c3
-rw-r--r--ui-commit.c3
-rw-r--r--ui-tree.c192
-rw-r--r--ui-view.c55
8 files changed, 173 insertions, 111 deletions
diff --git a/Makefile b/Makefile
index 57f80f8..c2a5736 100644
--- a/Makefile
+++ b/Makefile
@@ -1,41 +1,41 @@
1CGIT_VERSION = 0.5 1CGIT_VERSION = 0.5
2 2
3prefix = /var/www/htdocs/cgit 3prefix = /var/www/htdocs/cgit
4 4
5SHA1_HEADER = <openssl/sha.h> 5SHA1_HEADER = <openssl/sha.h>
6CACHE_ROOT = /var/cache/cgit 6CACHE_ROOT = /var/cache/cgit
7CGIT_CONFIG = /etc/cgitrc 7CGIT_CONFIG = /etc/cgitrc
8CGIT_SCRIPT_NAME = cgit.cgi 8CGIT_SCRIPT_NAME = cgit.cgi
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
15EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto 15EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
16OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ 16OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \
17 ui-summary.o ui-log.o ui-view.o ui-tree.o ui-commit.o ui-diff.o \ 17 ui-summary.o ui-log.o ui-tree.o ui-commit.o ui-diff.o \
18 ui-snapshot.o ui-blob.o 18 ui-snapshot.o ui-blob.o
19 19
20CFLAGS += -Wall 20CFLAGS += -Wall
21 21
22ifdef DEBUG 22ifdef DEBUG
23 CFLAGS += -g 23 CFLAGS += -g
24endif 24endif
25 25
26CFLAGS += -Igit 26CFLAGS += -Igit
27CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' 27CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)'
28CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' 28CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"'
29CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' 29CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
30CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' 30CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
31 31
32 32
33# 33#
34# If make is run on a nongit platform, get the git sources as a tarball. 34# If make is run on a nongit platform, get the git sources as a tarball.
35# 35#
36GITVER = $(shell git version 2>/dev/null || echo nogit) 36GITVER = $(shell git version 2>/dev/null || echo nogit)
37ifeq ($(GITVER),nogit) 37ifeq ($(GITVER),nogit)
38GITURL = http://www.kernel.org/pub/software/scm/git/git-1.5.2.tar.bz2 38GITURL = http://www.kernel.org/pub/software/scm/git/git-1.5.2.tar.bz2
39INITGIT = test -e git/git.c || ((curl "$(GITURL)" | tar -xj) && mv git-1.5.2 git) 39INITGIT = test -e git/git.c || ((curl "$(GITURL)" | tar -xj) && mv git-1.5.2 git)
40else 40else
41INITGIT = ./submodules.sh -i 41INITGIT = ./submodules.sh -i
diff --git a/cgit.c b/cgit.c
index 34e590e..1c213c7 100644
--- a/cgit.c
+++ b/cgit.c
@@ -76,56 +76,53 @@ static void cgit_print_repo_page(struct cacheitem *item)
76 76
77 if (cgit_cmd == CMD_BLOB) { 77 if (cgit_cmd == CMD_BLOB) {
78 cgit_print_blob(item, cgit_query_sha1, cgit_query_path); 78 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
79 return; 79 return;
80 } 80 }
81 81
82 show_search = (cgit_cmd == CMD_LOG); 82 show_search = (cgit_cmd == CMD_LOG);
83 cgit_print_docstart(title, item); 83 cgit_print_docstart(title, item);
84 if (!cgit_cmd) { 84 if (!cgit_cmd) {
85 cgit_print_pageheader("summary", show_search); 85 cgit_print_pageheader("summary", show_search);
86 cgit_print_summary(); 86 cgit_print_summary();
87 cgit_print_docend(); 87 cgit_print_docend();
88 return; 88 return;
89 } 89 }
90 90
91 cgit_print_pageheader(cgit_query_page, show_search); 91 cgit_print_pageheader(cgit_query_page, show_search);
92 92
93 switch(cgit_cmd) { 93 switch(cgit_cmd) {
94 case CMD_LOG: 94 case CMD_LOG:
95 cgit_print_log(cgit_query_head, cgit_query_ofs, 95 cgit_print_log(cgit_query_head, cgit_query_ofs,
96 cgit_max_commit_count, cgit_query_search, 96 cgit_max_commit_count, cgit_query_search,
97 cgit_query_path, 1); 97 cgit_query_path, 1);
98 break; 98 break;
99 case CMD_TREE: 99 case CMD_TREE:
100 cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path); 100 cgit_print_tree(cgit_query_sha1, cgit_query_path);
101 break; 101 break;
102 case CMD_COMMIT: 102 case CMD_COMMIT:
103 cgit_print_commit(cgit_query_head); 103 cgit_print_commit(cgit_query_head);
104 break; 104 break;
105 case CMD_VIEW:
106 cgit_print_view(cgit_query_sha1, cgit_query_path);
107 break;
108 case CMD_DIFF: 105 case CMD_DIFF:
109 cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2, 106 cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2,
110 cgit_query_path); 107 cgit_query_path);
111 break; 108 break;
112 default: 109 default:
113 cgit_print_error("Invalid request"); 110 cgit_print_error("Invalid request");
114 } 111 }
115 cgit_print_docend(); 112 cgit_print_docend();
116} 113}
117 114
118static void cgit_fill_cache(struct cacheitem *item, int use_cache) 115static void cgit_fill_cache(struct cacheitem *item, int use_cache)
119{ 116{
120 static char buf[PATH_MAX]; 117 static char buf[PATH_MAX];
121 int stdout2; 118 int stdout2;
122 119
123 getcwd(buf, sizeof(buf)); 120 getcwd(buf, sizeof(buf));
124 item->st.st_mtime = time(NULL); 121 item->st.st_mtime = time(NULL);
125 122
126 if (use_cache) { 123 if (use_cache) {
127 stdout2 = chk_positive(dup(STDOUT_FILENO), 124 stdout2 = chk_positive(dup(STDOUT_FILENO),
128 "Preserving STDOUT"); 125 "Preserving STDOUT");
129 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 126 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
130 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 127 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
131 } 128 }
diff --git a/cgit.css b/cgit.css
index 8bd6bf4..30fde03 100644
--- a/cgit.css
+++ b/cgit.css
@@ -178,52 +178,64 @@ td.ls-blob, td.ls-dir, td.ls-mod {
178 font-family: monospace; 178 font-family: monospace;
179} 179}
180 180
181div.ls-dir a { 181div.ls-dir a {
182 font-weight: bold; 182 font-weight: bold;
183} 183}
184 184
185th.filesize, td.filesize { 185th.filesize, td.filesize {
186 text-align: right; 186 text-align: right;
187} 187}
188 188
189td.filesize { 189td.filesize {
190 font-family: monospace; 190 font-family: monospace;
191} 191}
192 192
193td.links { 193td.links {
194 font-size: 80%; 194 font-size: 80%;
195 padding-left: 2em; 195 padding-left: 2em;
196} 196}
197 197
198td.filemode { 198td.filemode {
199 font-family: monospace; 199 font-family: monospace;
200} 200}
201 201
202td.blob { 202table.blob {
203 margin-top: 0.5em;
204 border-top: solid 1px black;
205}
206
207table.blob td.no {
208 border-right: solid 1px black;
209 color: black;
210 background-color: #eee;
211 text-align: right;
212}
213
214table.blob td.txt {
203 white-space: pre; 215 white-space: pre;
204 font-family: monospace; 216 font-family: monospace;
205 background-color: white; 217 padding-left: 0.5em;
206} 218}
207 219
208table.nowrap td { 220table.nowrap td {
209 white-space: nowrap; 221 white-space: nowrap;
210} 222}
211 223
212table.commit-info { 224table.commit-info {
213 border-collapse: collapse; 225 border-collapse: collapse;
214 margin-top: 1.5em; 226 margin-top: 1.5em;
215} 227}
216 228
217table.commit-info th { 229table.commit-info th {
218 text-align: left; 230 text-align: left;
219 font-weight: normal; 231 font-weight: normal;
220 padding: 0.1em 1em 0.1em 0.1em; 232 padding: 0.1em 1em 0.1em 0.1em;
221} 233}
222 234
223table.commit-info td { 235table.commit-info td {
224 font-weight: normal; 236 font-weight: normal;
225 padding: 0.1em 1em 0.1em 0.1em; 237 padding: 0.1em 1em 0.1em 0.1em;
226} 238}
227 239
228div.commit-subject { 240div.commit-subject {
229 font-weight: bold; 241 font-weight: bold;
diff --git a/cgit.h b/cgit.h
index 2f3fca1..b93e2e8 100644
--- a/cgit.h
+++ b/cgit.h
@@ -4,51 +4,50 @@
4 4
5#include <git-compat-util.h> 5#include <git-compat-util.h>
6#include <cache.h> 6#include <cache.h>
7#include <grep.h> 7#include <grep.h>
8#include <object.h> 8#include <object.h>
9#include <tree.h> 9#include <tree.h>
10#include <commit.h> 10#include <commit.h>
11#include <tag.h> 11#include <tag.h>
12#include <diff.h> 12#include <diff.h>
13#include <diffcore.h> 13#include <diffcore.h>
14#include <refs.h> 14#include <refs.h>
15#include <revision.h> 15#include <revision.h>
16#include <log-tree.h> 16#include <log-tree.h>
17#include <archive.h> 17#include <archive.h>
18#include <xdiff/xdiff.h> 18#include <xdiff/xdiff.h>
19 19
20 20
21/* 21/*
22 * The valid cgit repo-commands 22 * The valid cgit repo-commands
23 */ 23 */
24#define CMD_LOG 1 24#define CMD_LOG 1
25#define CMD_COMMIT 2 25#define CMD_COMMIT 2
26#define CMD_DIFF 3 26#define CMD_DIFF 3
27#define CMD_TREE 4 27#define CMD_TREE 4
28#define CMD_VIEW 5 28#define CMD_BLOB 5
29#define CMD_BLOB 6 29#define CMD_SNAPSHOT 6
30#define CMD_SNAPSHOT 7
31 30
32 31
33/* 32/*
34 * Dateformats used on misc. pages 33 * Dateformats used on misc. pages
35 */ 34 */
36#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" 35#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S"
37#define FMT_SHORTDATE "%Y-%m-%d" 36#define FMT_SHORTDATE "%Y-%m-%d"
38 37
39 38
40/* 39/*
41 * Limits used for relative dates 40 * Limits used for relative dates
42 */ 41 */
43#define TM_MIN 60 42#define TM_MIN 60
44#define TM_HOUR (TM_MIN * 60) 43#define TM_HOUR (TM_MIN * 60)
45#define TM_DAY (TM_HOUR * 24) 44#define TM_DAY (TM_HOUR * 24)
46#define TM_WEEK (TM_DAY * 7) 45#define TM_WEEK (TM_DAY * 7)
47#define TM_YEAR (TM_DAY * 365) 46#define TM_YEAR (TM_DAY * 365)
48#define TM_MONTH (TM_YEAR / 12.0) 47#define TM_MONTH (TM_YEAR / 12.0)
49 48
50 49
51typedef void (*configfn)(const char *name, const char *value); 50typedef void (*configfn)(const char *name, const char *value);
52typedef void (*filepair_fn)(struct diff_filepair *pair); 51typedef void (*filepair_fn)(struct diff_filepair *pair);
53typedef void (*linediff_fn)(char *line, int len); 52typedef void (*linediff_fn)(char *line, int len);
54 53
@@ -194,35 +193,34 @@ extern void cgit_parse_url(const char *url);
194extern char *cache_safe_filename(const char *unsafe); 193extern char *cache_safe_filename(const char *unsafe);
195extern int cache_lock(struct cacheitem *item); 194extern int cache_lock(struct cacheitem *item);
196extern int cache_unlock(struct cacheitem *item); 195extern int cache_unlock(struct cacheitem *item);
197extern int cache_cancel_lock(struct cacheitem *item); 196extern int cache_cancel_lock(struct cacheitem *item);
198extern int cache_exist(struct cacheitem *item); 197extern int cache_exist(struct cacheitem *item);
199extern int cache_expired(struct cacheitem *item); 198extern int cache_expired(struct cacheitem *item);
200 199
201extern char *cgit_repourl(const char *reponame); 200extern char *cgit_repourl(const char *reponame);
202extern char *cgit_pageurl(const char *reponame, const char *pagename, 201extern char *cgit_pageurl(const char *reponame, const char *pagename,
203 const char *query); 202 const char *query);
204 203
205extern void cgit_print_error(char *msg); 204extern void cgit_print_error(char *msg);
206extern void cgit_print_date(time_t secs, char *format); 205extern void cgit_print_date(time_t secs, char *format);
207extern void cgit_print_age(time_t t, time_t max_relative, char *format); 206extern void cgit_print_age(time_t t, time_t max_relative, char *format);
208extern void cgit_print_docstart(char *title, struct cacheitem *item); 207extern void cgit_print_docstart(char *title, struct cacheitem *item);
209extern void cgit_print_docend(); 208extern void cgit_print_docend();
210extern void cgit_print_pageheader(char *title, int show_search); 209extern void cgit_print_pageheader(char *title, int show_search);
211extern void cgit_print_snapshot_start(const char *mimetype, 210extern void cgit_print_snapshot_start(const char *mimetype,
212 const char *filename, 211 const char *filename,
213 struct cacheitem *item); 212 struct cacheitem *item);
214 213
215extern void cgit_print_repolist(struct cacheitem *item); 214extern void cgit_print_repolist(struct cacheitem *item);
216extern void cgit_print_summary(); 215extern void cgit_print_summary();
217extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path, int pager); 216extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path, int pager);
218extern void cgit_print_view(const char *hex, char *path);
219extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); 217extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
220extern void cgit_print_tree(const char *rev, const char *hex, char *path); 218extern void cgit_print_tree(const char *rev, char *path);
221extern void cgit_print_commit(const char *hex); 219extern void cgit_print_commit(const char *hex);
222extern void cgit_print_diff(const char *head, const char *old_hex, const char *new_hex, 220extern void cgit_print_diff(const char *head, const char *old_hex, const char *new_hex,
223 char *path); 221 char *path);
224extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, 222extern void cgit_print_snapshot(struct cacheitem *item, const char *hex,
225 const char *format, const char *prefix, 223 const char *format, const char *prefix,
226 const char *filename); 224 const char *filename);
227 225
228#endif /* CGIT_H */ 226#endif /* CGIT_H */
diff --git a/shared.c b/shared.c
index b6d2fa1..c7cd8a5 100644
--- a/shared.c
+++ b/shared.c
@@ -38,49 +38,50 @@ int cgit_summary_log = 0;
38 38
39int cgit_max_msg_len = 60; 39int cgit_max_msg_len = 60;
40int cgit_max_repodesc_len = 60; 40int cgit_max_repodesc_len = 60;
41int cgit_max_commit_count = 50; 41int cgit_max_commit_count = 50;
42 42
43int cgit_query_has_symref = 0; 43int cgit_query_has_symref = 0;
44int cgit_query_has_sha1 = 0; 44int cgit_query_has_sha1 = 0;
45 45
46char *cgit_querystring = NULL; 46char *cgit_querystring = NULL;
47char *cgit_query_repo = NULL; 47char *cgit_query_repo = NULL;
48char *cgit_query_page = NULL; 48char *cgit_query_page = NULL;
49char *cgit_query_head = NULL; 49char *cgit_query_head = NULL;
50char *cgit_query_search = NULL; 50char *cgit_query_search = NULL;
51char *cgit_query_sha1 = NULL; 51char *cgit_query_sha1 = NULL;
52char *cgit_query_sha2 = NULL; 52char *cgit_query_sha2 = NULL;
53char *cgit_query_path = NULL; 53char *cgit_query_path = NULL;
54char *cgit_query_name = NULL; 54char *cgit_query_name = NULL;
55int cgit_query_ofs = 0; 55int cgit_query_ofs = 0;
56 56
57int htmlfd = 0; 57int htmlfd = 0;
58 58
59 59
60int cgit_get_cmd_index(const char *cmd) 60int cgit_get_cmd_index(const char *cmd)
61{ 61{
62 static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", NULL}; 62 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
63 "snapshot", NULL};
63 int i; 64 int i;
64 65
65 for(i = 0; cmds[i]; i++) 66 for(i = 0; cmds[i]; i++)
66 if (!strcmp(cmd, cmds[i])) 67 if (!strcmp(cmd, cmds[i]))
67 return i + 1; 68 return i + 1;
68 return 0; 69 return 0;
69} 70}
70 71
71int chk_zero(int result, char *msg) 72int chk_zero(int result, char *msg)
72{ 73{
73 if (result != 0) 74 if (result != 0)
74 die("%s: %s", msg, strerror(errno)); 75 die("%s: %s", msg, strerror(errno));
75 return result; 76 return result;
76} 77}
77 78
78int chk_positive(int result, char *msg) 79int chk_positive(int result, char *msg)
79{ 80{
80 if (result <= 0) 81 if (result <= 0)
81 die("%s: %s", msg, strerror(errno)); 82 die("%s: %s", msg, strerror(errno));
82 return result; 83 return result;
83} 84}
84 85
85struct repoinfo *add_repo(const char *url) 86struct repoinfo *add_repo(const char *url)
86{ 87{
diff --git a/ui-commit.c b/ui-commit.c
index 1d12bbb..ed5384d 100644
--- a/ui-commit.c
+++ b/ui-commit.c
@@ -161,50 +161,49 @@ void cgit_print_commit(const char *hex)
161 } 161 }
162 commit = lookup_commit_reference(sha1); 162 commit = lookup_commit_reference(sha1);
163 if (!commit) { 163 if (!commit) {
164 cgit_print_error(fmt("Bad commit reference: %s", hex)); 164 cgit_print_error(fmt("Bad commit reference: %s", hex));
165 return; 165 return;
166 } 166 }
167 info = cgit_parse_commit(commit); 167 info = cgit_parse_commit(commit);
168 168
169 html("<table class='commit-info'>\n"); 169 html("<table class='commit-info'>\n");
170 html("<tr><th>author</th><td>"); 170 html("<tr><th>author</th><td>");
171 html_txt(info->author); 171 html_txt(info->author);
172 html(" "); 172 html(" ");
173 html_txt(info->author_email); 173 html_txt(info->author_email);
174 html("</td><td class='right'>"); 174 html("</td><td class='right'>");
175 cgit_print_date(info->author_date, FMT_LONGDATE); 175 cgit_print_date(info->author_date, FMT_LONGDATE);
176 html("</td></tr>\n"); 176 html("</td></tr>\n");
177 html("<tr><th>committer</th><td>"); 177 html("<tr><th>committer</th><td>");
178 html_txt(info->committer); 178 html_txt(info->committer);
179 html(" "); 179 html(" ");
180 html_txt(info->committer_email); 180 html_txt(info->committer_email);
181 html("</td><td class='right'>"); 181 html("</td><td class='right'>");
182 cgit_print_date(info->committer_date, FMT_LONGDATE); 182 cgit_print_date(info->committer_date, FMT_LONGDATE);
183 html("</td></tr>\n"); 183 html("</td></tr>\n");
184 html("<tr><th>tree</th><td colspan='2' class='sha1'><a href='"); 184 html("<tr><th>tree</th><td colspan='2' class='sha1'><a href='");
185 query = fmt("h=%s&amp;id=%s", sha1_to_hex(commit->object.sha1), 185 query = fmt("h=%s", sha1_to_hex(commit->object.sha1));
186 sha1_to_hex(commit->tree->object.sha1));
187 html_attr(cgit_pageurl(cgit_query_repo, "tree", query)); 186 html_attr(cgit_pageurl(cgit_query_repo, "tree", query));
188 htmlf("'>%s</a></td></tr>\n", sha1_to_hex(commit->tree->object.sha1)); 187 htmlf("'>%s</a></td></tr>\n", sha1_to_hex(commit->tree->object.sha1));
189 for (p = commit->parents; p ; p = p->next) { 188 for (p = commit->parents; p ; p = p->next) {
190 parent = lookup_commit_reference(p->item->object.sha1); 189 parent = lookup_commit_reference(p->item->object.sha1);
191 if (!parent) { 190 if (!parent) {
192 html("<tr><td colspan='3'>"); 191 html("<tr><td colspan='3'>");
193 cgit_print_error("Error reading parent commit"); 192 cgit_print_error("Error reading parent commit");
194 html("</td></tr>"); 193 html("</td></tr>");
195 continue; 194 continue;
196 } 195 }
197 html("<tr><th>parent</th>" 196 html("<tr><th>parent</th>"
198 "<td colspan='2' class='sha1'>" 197 "<td colspan='2' class='sha1'>"
199 "<a href='"); 198 "<a href='");
200 query = fmt("h=%s", sha1_to_hex(p->item->object.sha1)); 199 query = fmt("h=%s", sha1_to_hex(p->item->object.sha1));
201 html_attr(cgit_pageurl(cgit_query_repo, "commit", query)); 200 html_attr(cgit_pageurl(cgit_query_repo, "commit", query));
202 htmlf("'>%s</a> (<a href='", 201 htmlf("'>%s</a> (<a href='",
203 sha1_to_hex(p->item->object.sha1)); 202 sha1_to_hex(p->item->object.sha1));
204 query = fmt("id=%s&amp;id2=%s", sha1_to_hex(parent->tree->object.sha1), 203 query = fmt("id=%s&amp;id2=%s", sha1_to_hex(parent->tree->object.sha1),
205 sha1_to_hex(commit->tree->object.sha1)); 204 sha1_to_hex(commit->tree->object.sha1));
206 html_attr(cgit_pageurl(cgit_query_repo, "diff", query)); 205 html_attr(cgit_pageurl(cgit_query_repo, "diff", query));
207 html("'>diff</a>)</td></tr>"); 206 html("'>diff</a>)</td></tr>");
208 } 207 }
209 if (cgit_repo->snapshots) { 208 if (cgit_repo->snapshots) {
210 htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='"); 209 htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='");
diff --git a/ui-tree.c b/ui-tree.c
index cb57d8d..eb3d3ac 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -1,104 +1,214 @@
1/* ui-tree.c: functions for tree output 1/* ui-tree.c: functions for tree 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 "cgit.h" 9#include "cgit.h"
10 10
11char *curr_rev; 11char *curr_rev;
12char *match_path;
13int header = 0;
12 14
13static int print_entry(const unsigned char *sha1, const char *base, 15static void print_object(const unsigned char *sha1, char *path)
14 int baselen, const char *pathname, unsigned int mode, 16{
15 int stage) 17 enum object_type type;
18 unsigned char *buf;
19 unsigned long size, lineno, start, idx;
20
21 type = sha1_object_info(sha1, &size);
22 if (type == OBJ_BAD) {
23 cgit_print_error(fmt("Bad object name: %s",
24 sha1_to_hex(sha1)));
25 return;
26 }
27
28 buf = read_sha1_file(sha1, &type, &size);
29 if (!buf) {
30 cgit_print_error(fmt("Error reading object %s",
31 sha1_to_hex(sha1)));
32 return;
33 }
34
35 html("<table class='blob'>\n");
36 idx = 0;
37 start = 0;
38 lineno = 0;
39 while(idx < size) {
40 if (buf[idx] == '\n') {
41 buf[idx] = '\0';
42 htmlf("<tr><td class='no'>%d</td><td class='txt'>",
43 ++lineno);
44 html_txt(buf + start);
45 html("</td></tr>\n");
46 start = idx + 1;
47 }
48 idx++;
49 }
50 html("\n</td></tr>\n");
51 html("</table>\n");
52}
53
54
55static int ls_item(const unsigned char *sha1, const char *base, int baselen,
56 const char *pathname, unsigned int mode, int stage)
16{ 57{
17 char *name; 58 char *name;
18 enum object_type type; 59 enum object_type type;
19 unsigned long size = 0; 60 unsigned long size = 0;
61 char *url, *qry;
20 62
21 name = xstrdup(pathname); 63 name = xstrdup(pathname);
22 type = sha1_object_info(sha1, &size); 64 type = sha1_object_info(sha1, &size);
23 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) { 65 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
24 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", 66 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
25 name, 67 name,
26 sha1_to_hex(sha1)); 68 sha1_to_hex(sha1));
27 return 0; 69 return 0;
28 } 70 }
71 qry = fmt("h=%s&amp;path=%s%s%s", curr_rev,
72 cgit_query_path ? cgit_query_path : "",
73 cgit_query_path ? "/" : "", pathname);
74 url = cgit_pageurl(cgit_query_repo, "tree", qry);
29 html("<tr><td class='filemode'>"); 75 html("<tr><td class='filemode'>");
30 html_filemode(mode); 76 html_filemode(mode);
31 html("</td><td "); 77 html("</td><td ");
32 if (S_ISDIRLNK(mode)) { 78 if (S_ISDIRLNK(mode)) {
33 htmlf("class='ls-mod'><a href='"); 79 htmlf("class='ls-mod'><a href='");
34 html_attr(fmt(cgit_repo->module_link, 80 html_attr(fmt(cgit_repo->module_link,
35 name, 81 name,
36 sha1_to_hex(sha1))); 82 sha1_to_hex(sha1)));
37 } else if (S_ISDIR(mode)) { 83 } else if (S_ISDIR(mode)) {
38 html("class='ls-dir'><a href='"); 84 html("class='ls-dir'><a href='");
39 html_attr(cgit_pageurl(cgit_query_repo, "tree", 85 html_attr(url);
40 fmt("h=%s&amp;id=%s&amp;path=%s%s/",
41 curr_rev,
42 sha1_to_hex(sha1),
43 cgit_query_path ? cgit_query_path : "",
44 pathname)));
45 } else { 86 } else {
46 html("class='ls-blob'><a href='"); 87 html("class='ls-blob'><a href='");
47 html_attr(cgit_pageurl(cgit_query_repo, "view", 88 html_attr(url);
48 fmt("h=%s&amp;id=%s&amp;path=%s%s", curr_rev,
49 sha1_to_hex(sha1),
50 cgit_query_path ? cgit_query_path : "",
51 pathname)));
52 } 89 }
53 htmlf("'>%s</a></td>", name); 90 htmlf("'>%s</a></td>", name);
54 htmlf("<td class='filesize'>%li</td>", size); 91 htmlf("<td class='filesize'>%li</td>", size);
55 92
56 html("<td class='links'><a href='"); 93 html("<td class='links'><a href='");
57 html_attr(cgit_pageurl(cgit_query_repo, "log", 94 qry = fmt("h=%s&amp;path=%s%s%s", curr_rev,
58 fmt("h=%s&amp;path=%s%s",
59 curr_rev,
60 cgit_query_path ? cgit_query_path : "", 95 cgit_query_path ? cgit_query_path : "",
61 pathname))); 96 cgit_query_path ? "/" : "", pathname);
62 html("'>history</a></td>"); 97 url = cgit_pageurl(cgit_query_repo, "log", qry);
98 html_attr(url);
99 html("' class='button'>H</a></td>");
63 html("</tr>\n"); 100 html("</tr>\n");
64 free(name); 101 free(name);
65 return 0; 102 return 0;
66} 103}
67 104
68void cgit_print_tree(const char *rev, const char *hex, char *path) 105static void ls_head()
106{
107 html("<table class='list'>\n");
108 html("<tr class='nohover'>");
109 html("<th class='left'>Mode</th>");
110 html("<th class='left'>Name</th>");
111 html("<th class='right'>Size</th>");
112 html("<th/>");
113 html("</tr>\n");
114 header = 1;
115}
116
117static void ls_tail()
118{
119 if (!header)
120 return;
121 html("</table>\n");
122 header = 0;
123}
124
125static void ls_tree(const unsigned char *sha1, char *path)
69{ 126{
70 struct tree *tree; 127 struct tree *tree;
128
129 tree = parse_tree_indirect(sha1);
130 if (!tree) {
131 cgit_print_error(fmt("Not a tree object: %s",
132 sha1_to_hex(sha1)));
133 return;
134 }
135
136 ls_head();
137 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
138 ls_tail();
139}
140
141
142static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
143 const char *pathname, unsigned mode, int stage)
144{
145 static int state;
146 static char buffer[PATH_MAX];
147 char *url;
148
149 if (state == 0) {
150 memcpy(buffer, base, baselen);
151 strcpy(buffer+baselen, pathname);
152 url = cgit_pageurl(cgit_query_repo, "tree",
153 fmt("h=%s&amp;path=%s", curr_rev, buffer));
154 htmlf(" / <a href='");
155 html_attr(url);
156 html("'>");
157 html_txt(xstrdup(pathname));
158 html("</a>");
159
160 if (strcmp(match_path, buffer))
161 return READ_TREE_RECURSIVE;
162
163 if (S_ISDIR(mode)) {
164 state = 1;
165 ls_head();
166 return READ_TREE_RECURSIVE;
167 } else {
168 print_object(sha1, buffer);
169 return 0;
170 }
171 }
172 ls_item(sha1, base, baselen, pathname, mode, stage);
173 return 0;
174}
175
176
177/*
178 * Show a tree or a blob
179 * rev: the commit pointing at the root tree object
180 * path: path to tree or blob
181 */
182void cgit_print_tree(const char *rev, char *path)
183{
71 unsigned char sha1[20]; 184 unsigned char sha1[20];
72 struct commit *commit; 185 struct commit *commit;
186 const char *paths[] = {path, NULL};
187
188 if (!rev)
189 rev = cgit_query_head;
73 190
74 curr_rev = xstrdup(rev); 191 curr_rev = xstrdup(rev);
75 get_sha1(rev, sha1); 192 if (get_sha1(rev, sha1)) {
193 cgit_print_error(fmt("Invalid revision name: %s", rev));
194 return;
195 }
76 commit = lookup_commit_reference(sha1); 196 commit = lookup_commit_reference(sha1);
77 if (!commit || parse_commit(commit)) { 197 if (!commit || parse_commit(commit)) {
78 cgit_print_error(fmt("Invalid head: %s", rev)); 198 cgit_print_error(fmt("Invalid commit reference: %s", rev));
79 return; 199 return;
80 } 200 }
81 if (!hex)
82 hex = sha1_to_hex(commit->tree->object.sha1);
83 201
84 if (get_sha1_hex(hex, sha1)) { 202 html("path: <a href='");
85 cgit_print_error(fmt("Invalid object id: %s", hex)); 203 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev)));
86 return; 204 html("'>root</a>");
87 } 205
88 tree = parse_tree_indirect(sha1); 206 if (path == NULL) {
89 if (!tree) { 207 ls_tree(commit->tree->object.sha1, NULL);
90 cgit_print_error(fmt("Not a tree object: %s", hex));
91 return; 208 return;
92 } 209 }
93 210
94 html_txt(path); 211 match_path = path;
95 html("<table class='list'>\n"); 212 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);
96 html("<tr class='nohover'>"); 213 ls_tail();
97 html("<th class='left'>Mode</th>");
98 html("<th class='left'>Name</th>");
99 html("<th class='right'>Size</th>");
100 html("<th/>");
101 html("</tr>\n");
102 read_tree_recursive(tree, "", 0, 1, NULL, print_entry);
103 html("</table>\n");
104} 214}
diff --git a/ui-view.c b/ui-view.c
deleted file mode 100644
index 8873415..0000000
--- a/ui-view.c
+++ b/dev/null
@@ -1,55 +0,0 @@
1/* ui-view.c: functions to output _any_ object, given it's sha1
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10
11void cgit_print_view(const char *hex, char *path)
12{
13 unsigned char sha1[20];
14 enum object_type type;
15 unsigned char *buf;
16 unsigned long size;
17
18 if (get_sha1_hex(hex, sha1)){
19 cgit_print_error(fmt("Bad hex value: %s", hex));
20 return;
21 }
22
23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) {
25 cgit_print_error(fmt("Bad object name: %s", hex));
26 return;
27 }
28
29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) {
31 cgit_print_error(fmt("Error reading object %s", hex));
32 return;
33 }
34
35 buf[size] = '\0';
36 html("<table class='list'>\n");
37 html("<tr class='nohover'><th class='left'>");
38 if (path)
39 htmlf("%s (", path);
40 htmlf("%s %s, %li bytes", typename(type), hex, size);
41 if (path)
42 html(")");
43
44 html(" <a href='");
45 html_attr(cgit_pageurl(cgit_query_repo, "blob",
46 fmt("id=%s&amp;path=%s",
47 hex,
48 path)));
49 html("'>download</a>");
50 html("</th></tr>\n");
51 html("<tr><td class='blob'>\n");
52 html_txt(buf);
53 html("\n</td></tr>\n");
54 html("</table>\n");
55}