summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--html.c2
-rw-r--r--ui-diff.c8
-rw-r--r--ui-tree.c4
3 files changed, 7 insertions, 7 deletions
diff --git a/html.c b/html.c
index 33a956f..76fa6c4 100644
--- a/html.c
+++ b/html.c
@@ -33,150 +33,150 @@ void html(const char *txt)
33 33
34void htmlf(const char *format, ...) 34void htmlf(const char *format, ...)
35{ 35{
36 static char buf[65536]; 36 static char buf[65536];
37 va_list args; 37 va_list args;
38 38
39 va_start(args, format); 39 va_start(args, format);
40 vsnprintf(buf, sizeof(buf), format, args); 40 vsnprintf(buf, sizeof(buf), format, args);
41 va_end(args); 41 va_end(args);
42 html(buf); 42 html(buf);
43} 43}
44 44
45void html_txt(char *txt) 45void html_txt(char *txt)
46{ 46{
47 char *t = txt; 47 char *t = txt;
48 while(t && *t){ 48 while(t && *t){
49 int c = *t; 49 int c = *t;
50 if (c=='<' || c=='>' || c=='&') { 50 if (c=='<' || c=='>' || c=='&') {
51 *t = '\0'; 51 *t = '\0';
52 html(txt); 52 html(txt);
53 *t = c; 53 *t = c;
54 if (c=='>') 54 if (c=='>')
55 html("&gt;"); 55 html("&gt;");
56 else if (c=='<') 56 else if (c=='<')
57 html("&lt;"); 57 html("&lt;");
58 else if (c=='&') 58 else if (c=='&')
59 html("&amp;"); 59 html("&amp;");
60 txt = t+1; 60 txt = t+1;
61 } 61 }
62 t++; 62 t++;
63 } 63 }
64 if (t!=txt) 64 if (t!=txt)
65 html(txt); 65 html(txt);
66} 66}
67 67
68void html_ntxt(int len, char *txt) 68void html_ntxt(int len, char *txt)
69{ 69{
70 char *t = txt; 70 char *t = txt;
71 while(t && *t && len--){ 71 while(t && *t && len--){
72 int c = *t; 72 int c = *t;
73 if (c=='<' || c=='>' || c=='&') { 73 if (c=='<' || c=='>' || c=='&') {
74 *t = '\0'; 74 *t = '\0';
75 html(txt); 75 html(txt);
76 *t = c; 76 *t = c;
77 if (c=='>') 77 if (c=='>')
78 html("&gt;"); 78 html("&gt;");
79 else if (c=='<') 79 else if (c=='<')
80 html("&lt;"); 80 html("&lt;");
81 else if (c=='&') 81 else if (c=='&')
82 html("&amp;"); 82 html("&amp;");
83 txt = t+1; 83 txt = t+1;
84 } 84 }
85 t++; 85 t++;
86 } 86 }
87 if (t!=txt) { 87 if (t!=txt) {
88 char c = *t; 88 char c = *t;
89 *t = '\0'; 89 *t = '\0';
90 html(txt); 90 html(txt);
91 *t = c; 91 *t = c;
92 } 92 }
93 if (len<0) 93 if (len<0)
94 html("..."); 94 html("...");
95} 95}
96 96
97void html_attr(char *txt) 97void html_attr(char *txt)
98{ 98{
99 char *t = txt; 99 char *t = txt;
100 while(t && *t){ 100 while(t && *t){
101 int c = *t; 101 int c = *t;
102 if (c=='<' || c=='>' || c=='\'') { 102 if (c=='<' || c=='>' || c=='\'') {
103 *t = '\0'; 103 *t = '\0';
104 html(txt); 104 html(txt);
105 *t = c; 105 *t = c;
106 if (c=='>') 106 if (c=='>')
107 html("&gt;"); 107 html("&gt;");
108 else if (c=='<') 108 else if (c=='<')
109 html("&lt;"); 109 html("&lt;");
110 else if (c=='\'') 110 else if (c=='\'')
111 html("&quote;"); 111 html("&quote;");
112 txt = t+1; 112 txt = t+1;
113 } 113 }
114 t++; 114 t++;
115 } 115 }
116 if (t!=txt) 116 if (t!=txt)
117 html(txt); 117 html(txt);
118} 118}
119 119
120void html_hidden(char *name, char *value) 120void html_hidden(char *name, char *value)
121{ 121{
122 html("<input type='hidden' name='"); 122 html("<input type='hidden' name='");
123 html_attr(name); 123 html_attr(name);
124 html("' value='"); 124 html("' value='");
125 html_attr(value); 125 html_attr(value);
126 html("'/>"); 126 html("'/>");
127} 127}
128 128
129void html_link_open(char *url, char *title, char *class) 129void html_link_open(char *url, char *title, char *class)
130{ 130{
131 html("<a href='"); 131 html("<a href='");
132 html_attr(url); 132 html_attr(url);
133 if (title) { 133 if (title) {
134 html("' title='"); 134 html("' title='");
135 html_attr(title); 135 html_attr(title);
136 } 136 }
137 if (class) { 137 if (class) {
138 html("' class='"); 138 html("' class='");
139 html_attr(class); 139 html_attr(class);
140 } 140 }
141 html("'>"); 141 html("'>");
142} 142}
143 143
144void html_link_close(void) 144void html_link_close(void)
145{ 145{
146 html("</a>"); 146 html("</a>");
147} 147}
148 148
149void html_fileperm(unsigned short mode) 149void html_fileperm(unsigned short mode)
150{ 150{
151 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), 151 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
152 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); 152 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
153} 153}
154 154
155void html_filemode(unsigned short mode) 155void html_filemode(unsigned short mode)
156{ 156{
157 if (S_ISDIR(mode)) 157 if (S_ISDIR(mode))
158 html("d"); 158 html("d");
159 else if (S_ISLNK(mode)) 159 else if (S_ISLNK(mode))
160 html("l"); 160 html("l");
161 else if (S_ISDIRLNK(mode)) 161 else if (S_ISGITLINK(mode))
162 html("m"); 162 html("m");
163 else 163 else
164 html("-"); 164 html("-");
165 html_fileperm(mode >> 6); 165 html_fileperm(mode >> 6);
166 html_fileperm(mode >> 3); 166 html_fileperm(mode >> 3);
167 html_fileperm(mode); 167 html_fileperm(mode);
168} 168}
169 169
170int html_include(const char *filename) 170int html_include(const char *filename)
171{ 171{
172 FILE *f; 172 FILE *f;
173 char buf[4096]; 173 char buf[4096];
174 size_t len; 174 size_t len;
175 175
176 if (!(f = fopen(filename, "r"))) 176 if (!(f = fopen(filename, "r")))
177 return -1; 177 return -1;
178 while((len = fread(buf, 1, 4096, f)) > 0) 178 while((len = fread(buf, 1, 4096, f)) > 0)
179 write(htmlfd, buf, len); 179 write(htmlfd, buf, len);
180 fclose(f); 180 fclose(f);
181 return 0; 181 return 0;
182} 182}
diff --git a/ui-diff.c b/ui-diff.c
index a76a234..0be845f 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,140 +1,140 @@
1/* ui-diff.c: show diff between two blobs 1/* ui-diff.c: show diff between two blobs
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
11 11
12/* 12/*
13 * print a single line returned from xdiff 13 * print a single line returned from xdiff
14 */ 14 */
15static void print_line(char *line, int len) 15static void print_line(char *line, int len)
16{ 16{
17 char *class = "ctx"; 17 char *class = "ctx";
18 char c = line[len-1]; 18 char c = line[len-1];
19 19
20 if (line[0] == '+') 20 if (line[0] == '+')
21 class = "add"; 21 class = "add";
22 else if (line[0] == '-') 22 else if (line[0] == '-')
23 class = "del"; 23 class = "del";
24 else if (line[0] == '@') 24 else if (line[0] == '@')
25 class = "hunk"; 25 class = "hunk";
26 26
27 htmlf("<div class='%s'>", class); 27 htmlf("<div class='%s'>", class);
28 line[len-1] = '\0'; 28 line[len-1] = '\0';
29 html_txt(line); 29 html_txt(line);
30 html("</div>"); 30 html("</div>");
31 line[len-1] = c; 31 line[len-1] = c;
32} 32}
33 33
34static void header(unsigned char *sha1, char *path1, int mode1, 34static void header(unsigned char *sha1, char *path1, int mode1,
35 unsigned char *sha2, char *path2, int mode2) 35 unsigned char *sha2, char *path2, int mode2)
36{ 36{
37 char *abbrev1, *abbrev2; 37 char *abbrev1, *abbrev2;
38 int subproject; 38 int subproject;
39 39
40 subproject = (S_ISDIRLNK(mode1) || S_ISDIRLNK(mode2)); 40 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
41 html("<div class='head'>"); 41 html("<div class='head'>");
42 html("diff --git a/"); 42 html("diff --git a/");
43 html_txt(path1); 43 html_txt(path1);
44 html(" b/"); 44 html(" b/");
45 html_txt(path2); 45 html_txt(path2);
46 46
47 if (is_null_sha1(sha1)) 47 if (is_null_sha1(sha1))
48 path1 = "dev/null"; 48 path1 = "dev/null";
49 if (is_null_sha1(sha2)) 49 if (is_null_sha1(sha2))
50 path2 = "dev/null"; 50 path2 = "dev/null";
51 51
52 if (mode1 == 0) 52 if (mode1 == 0)
53 htmlf("<br/>new file mode %.6o", mode2); 53 htmlf("<br/>new file mode %.6o", mode2);
54 54
55 if (mode2 == 0) 55 if (mode2 == 0)
56 htmlf("<br/>deleted file mode %.6o", mode1); 56 htmlf("<br/>deleted file mode %.6o", mode1);
57 57
58 if (!subproject) { 58 if (!subproject) {
59 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 59 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
60 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 60 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
61 htmlf("<br/>index %s..%s", abbrev1, abbrev2); 61 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
62 free(abbrev1); 62 free(abbrev1);
63 free(abbrev2); 63 free(abbrev2);
64 if (mode1 != 0 && mode2 != 0) { 64 if (mode1 != 0 && mode2 != 0) {
65 htmlf(" %.6o", mode1); 65 htmlf(" %.6o", mode1);
66 if (mode2 != mode1) 66 if (mode2 != mode1)
67 htmlf("..%.6o", mode2); 67 htmlf("..%.6o", mode2);
68 } 68 }
69 html("<br/>--- a/"); 69 html("<br/>--- a/");
70 html_txt(path1); 70 html_txt(path1);
71 html("<br/>+++ b/"); 71 html("<br/>+++ b/");
72 html_txt(path2); 72 html_txt(path2);
73 } 73 }
74 html("</div>"); 74 html("</div>");
75} 75}
76 76
77static void filepair_cb(struct diff_filepair *pair) 77static void filepair_cb(struct diff_filepair *pair)
78{ 78{
79 header(pair->one->sha1, pair->one->path, pair->one->mode, 79 header(pair->one->sha1, pair->one->path, pair->one->mode,
80 pair->two->sha1, pair->two->path, pair->two->mode); 80 pair->two->sha1, pair->two->path, pair->two->mode);
81 if (S_ISDIRLNK(pair->one->mode) || S_ISDIRLNK(pair->two->mode)) { 81 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
82 if (S_ISDIRLNK(pair->one->mode)) 82 if (S_ISGITLINK(pair->one->mode))
83 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 83 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
84 if (S_ISDIRLNK(pair->two->mode)) 84 if (S_ISGITLINK(pair->two->mode))
85 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 85 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
86 return; 86 return;
87 } 87 }
88 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) 88 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
89 cgit_print_error("Error running diff"); 89 cgit_print_error("Error running diff");
90} 90}
91 91
92void cgit_print_diff(const char *new_rev, const char *old_rev) 92void cgit_print_diff(const char *new_rev, const char *old_rev)
93{ 93{
94 unsigned char sha1[20], sha2[20]; 94 unsigned char sha1[20], sha2[20];
95 enum object_type type; 95 enum object_type type;
96 unsigned long size; 96 unsigned long size;
97 struct commit *commit, *commit2; 97 struct commit *commit, *commit2;
98 98
99 if (!new_rev) 99 if (!new_rev)
100 new_rev = cgit_query_head; 100 new_rev = cgit_query_head;
101 get_sha1(new_rev, sha1); 101 get_sha1(new_rev, sha1);
102 type = sha1_object_info(sha1, &size); 102 type = sha1_object_info(sha1, &size);
103 if (type == OBJ_BAD) { 103 if (type == OBJ_BAD) {
104 cgit_print_error(fmt("Bad object name: %s", new_rev)); 104 cgit_print_error(fmt("Bad object name: %s", new_rev));
105 return; 105 return;
106 } 106 }
107 if (type != OBJ_COMMIT) { 107 if (type != OBJ_COMMIT) {
108 cgit_print_error(fmt("Unhandled object type: %s", 108 cgit_print_error(fmt("Unhandled object type: %s",
109 typename(type))); 109 typename(type)));
110 return; 110 return;
111 } 111 }
112 112
113 commit = lookup_commit_reference(sha1); 113 commit = lookup_commit_reference(sha1);
114 if (!commit || parse_commit(commit)) 114 if (!commit || parse_commit(commit))
115 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(sha1))); 115 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(sha1)));
116 116
117 if (old_rev) 117 if (old_rev)
118 get_sha1(old_rev, sha2); 118 get_sha1(old_rev, sha2);
119 else if (commit->parents && commit->parents->item) 119 else if (commit->parents && commit->parents->item)
120 hashcpy(sha2, commit->parents->item->object.sha1); 120 hashcpy(sha2, commit->parents->item->object.sha1);
121 else 121 else
122 hashclr(sha2); 122 hashclr(sha2);
123 123
124 if (!is_null_sha1(sha2)) { 124 if (!is_null_sha1(sha2)) {
125 type = sha1_object_info(sha2, &size); 125 type = sha1_object_info(sha2, &size);
126 if (type == OBJ_BAD) { 126 if (type == OBJ_BAD) {
127 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(sha2))); 127 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(sha2)));
128 return; 128 return;
129 } 129 }
130 commit2 = lookup_commit_reference(sha2); 130 commit2 = lookup_commit_reference(sha2);
131 if (!commit2 || parse_commit(commit2)) 131 if (!commit2 || parse_commit(commit2))
132 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(sha2))); 132 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(sha2)));
133 } 133 }
134 134
135 html("<table class='diff'>"); 135 html("<table class='diff'>");
136 html("<tr><td>"); 136 html("<tr><td>");
137 cgit_diff_tree(sha2, sha1, filepair_cb); 137 cgit_diff_tree(sha2, sha1, filepair_cb);
138 html("</td></tr>"); 138 html("</td></tr>");
139 html("</table>"); 139 html("</table>");
140} 140}
diff --git a/ui-tree.c b/ui-tree.c
index 634132f..1cb09f7 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -1,209 +1,209 @@
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; 12char *match_path;
13int header = 0; 13int header = 0;
14 14
15static void print_object(const unsigned char *sha1, char *path) 15static void print_object(const unsigned char *sha1, char *path)
16{ 16{
17 enum object_type type; 17 enum object_type type;
18 unsigned char *buf; 18 unsigned char *buf;
19 unsigned long size, lineno, start, idx; 19 unsigned long size, lineno, start, idx;
20 20
21 type = sha1_object_info(sha1, &size); 21 type = sha1_object_info(sha1, &size);
22 if (type == OBJ_BAD) { 22 if (type == OBJ_BAD) {
23 cgit_print_error(fmt("Bad object name: %s", 23 cgit_print_error(fmt("Bad object name: %s",
24 sha1_to_hex(sha1))); 24 sha1_to_hex(sha1)));
25 return; 25 return;
26 } 26 }
27 27
28 buf = read_sha1_file(sha1, &type, &size); 28 buf = read_sha1_file(sha1, &type, &size);
29 if (!buf) { 29 if (!buf) {
30 cgit_print_error(fmt("Error reading object %s", 30 cgit_print_error(fmt("Error reading object %s",
31 sha1_to_hex(sha1))); 31 sha1_to_hex(sha1)));
32 return; 32 return;
33 } 33 }
34 34
35 html(" blob: <a href='"); 35 html(" blob: <a href='");
36 html_attr(cgit_pageurl(cgit_query_repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); 36 html_attr(cgit_pageurl(cgit_query_repo, "blob", fmt("id=%s", sha1_to_hex(sha1))));
37 htmlf("'>%s</a>",sha1_to_hex(sha1)); 37 htmlf("'>%s</a>",sha1_to_hex(sha1));
38 38
39 html("<table class='blob'>\n"); 39 html("<table class='blob'>\n");
40 idx = 0; 40 idx = 0;
41 start = 0; 41 start = 0;
42 lineno = 0; 42 lineno = 0;
43 while(idx < size) { 43 while(idx < size) {
44 if (buf[idx] == '\n') { 44 if (buf[idx] == '\n') {
45 buf[idx] = '\0'; 45 buf[idx] = '\0';
46 htmlf("<tr><td class='no'><a name='%d'>%1$d</a></td><td class='txt'>", 46 htmlf("<tr><td class='no'><a name='%d'>%1$d</a></td><td class='txt'>",
47 ++lineno); 47 ++lineno);
48 html_txt(buf + start); 48 html_txt(buf + start);
49 html("</td></tr>\n"); 49 html("</td></tr>\n");
50 start = idx + 1; 50 start = idx + 1;
51 } 51 }
52 idx++; 52 idx++;
53 } 53 }
54 html("</table>\n"); 54 html("</table>\n");
55} 55}
56 56
57 57
58static int ls_item(const unsigned char *sha1, const char *base, int baselen, 58static int ls_item(const unsigned char *sha1, const char *base, int baselen,
59 const char *pathname, unsigned int mode, int stage) 59 const char *pathname, unsigned int mode, int stage)
60{ 60{
61 char *name; 61 char *name;
62 char *fullpath; 62 char *fullpath;
63 enum object_type type; 63 enum object_type type;
64 unsigned long size = 0; 64 unsigned long size = 0;
65 65
66 name = xstrdup(pathname); 66 name = xstrdup(pathname);
67 fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "", 67 fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "",
68 cgit_query_path ? "/" : "", name); 68 cgit_query_path ? "/" : "", name);
69 69
70 type = sha1_object_info(sha1, &size); 70 type = sha1_object_info(sha1, &size);
71 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) { 71 if (type == OBJ_BAD && !S_ISGITLINK(mode)) {
72 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", 72 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
73 name, 73 name,
74 sha1_to_hex(sha1)); 74 sha1_to_hex(sha1));
75 return 0; 75 return 0;
76 } 76 }
77 77
78 html("<tr><td class='ls-mode'>"); 78 html("<tr><td class='ls-mode'>");
79 html_filemode(mode); 79 html_filemode(mode);
80 html("</td><td>"); 80 html("</td><td>");
81 if (S_ISDIRLNK(mode)) { 81 if (S_ISGITLINK(mode)) {
82 htmlf("<a class='ls-mod' href='"); 82 htmlf("<a class='ls-mod' href='");
83 html_attr(fmt(cgit_repo->module_link, 83 html_attr(fmt(cgit_repo->module_link,
84 name, 84 name,
85 sha1_to_hex(sha1))); 85 sha1_to_hex(sha1)));
86 html("'>"); 86 html("'>");
87 html_txt(name); 87 html_txt(name);
88 html("</a>"); 88 html("</a>");
89 } else if (S_ISDIR(mode)) { 89 } else if (S_ISDIR(mode)) {
90 cgit_tree_link(name, NULL, "ls-dir", cgit_query_head, 90 cgit_tree_link(name, NULL, "ls-dir", cgit_query_head,
91 curr_rev, fullpath); 91 curr_rev, fullpath);
92 } else { 92 } else {
93 cgit_tree_link(name, NULL, "ls-blob", cgit_query_head, 93 cgit_tree_link(name, NULL, "ls-blob", cgit_query_head,
94 curr_rev, fullpath); 94 curr_rev, fullpath);
95 } 95 }
96 htmlf("</td><td class='ls-size'>%li</td>", size); 96 htmlf("</td><td class='ls-size'>%li</td>", size);
97 97
98 html("<td>"); 98 html("<td>");
99 cgit_log_link("log", NULL, "button", cgit_query_head, curr_rev, 99 cgit_log_link("log", NULL, "button", cgit_query_head, curr_rev,
100 fullpath, 0); 100 fullpath, 0);
101 html("</td></tr>\n"); 101 html("</td></tr>\n");
102 free(name); 102 free(name);
103 return 0; 103 return 0;
104} 104}
105 105
106static void ls_head() 106static void ls_head()
107{ 107{
108 html("<table class='list'>\n"); 108 html("<table class='list'>\n");
109 html("<tr class='nohover'>"); 109 html("<tr class='nohover'>");
110 html("<th class='left'>Mode</th>"); 110 html("<th class='left'>Mode</th>");
111 html("<th class='left'>Name</th>"); 111 html("<th class='left'>Name</th>");
112 html("<th class='right'>Size</th>"); 112 html("<th class='right'>Size</th>");
113 html("<th/>"); 113 html("<th/>");
114 html("</tr>\n"); 114 html("</tr>\n");
115 header = 1; 115 header = 1;
116} 116}
117 117
118static void ls_tail() 118static void ls_tail()
119{ 119{
120 if (!header) 120 if (!header)
121 return; 121 return;
122 html("</table>\n"); 122 html("</table>\n");
123 header = 0; 123 header = 0;
124} 124}
125 125
126static void ls_tree(const unsigned char *sha1, char *path) 126static void ls_tree(const unsigned char *sha1, char *path)
127{ 127{
128 struct tree *tree; 128 struct tree *tree;
129 129
130 tree = parse_tree_indirect(sha1); 130 tree = parse_tree_indirect(sha1);
131 if (!tree) { 131 if (!tree) {
132 cgit_print_error(fmt("Not a tree object: %s", 132 cgit_print_error(fmt("Not a tree object: %s",
133 sha1_to_hex(sha1))); 133 sha1_to_hex(sha1)));
134 return; 134 return;
135 } 135 }
136 136
137 ls_head(); 137 ls_head();
138 read_tree_recursive(tree, "", 0, 1, NULL, ls_item); 138 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
139 ls_tail(); 139 ls_tail();
140} 140}
141 141
142 142
143static int walk_tree(const unsigned char *sha1, const char *base, int baselen, 143static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
144 const char *pathname, unsigned mode, int stage) 144 const char *pathname, unsigned mode, int stage)
145{ 145{
146 static int state; 146 static int state;
147 static char buffer[PATH_MAX]; 147 static char buffer[PATH_MAX];
148 char *url; 148 char *url;
149 149
150 if (state == 0) { 150 if (state == 0) {
151 memcpy(buffer, base, baselen); 151 memcpy(buffer, base, baselen);
152 strcpy(buffer+baselen, pathname); 152 strcpy(buffer+baselen, pathname);
153 url = cgit_pageurl(cgit_query_repo, "tree", 153 url = cgit_pageurl(cgit_query_repo, "tree",
154 fmt("h=%s&amp;path=%s", curr_rev, buffer)); 154 fmt("h=%s&amp;path=%s", curr_rev, buffer));
155 html("/"); 155 html("/");
156 cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head, 156 cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head,
157 curr_rev, buffer); 157 curr_rev, buffer);
158 158
159 if (strcmp(match_path, buffer)) 159 if (strcmp(match_path, buffer))
160 return READ_TREE_RECURSIVE; 160 return READ_TREE_RECURSIVE;
161 161
162 if (S_ISDIR(mode)) { 162 if (S_ISDIR(mode)) {
163 state = 1; 163 state = 1;
164 ls_head(); 164 ls_head();
165 return READ_TREE_RECURSIVE; 165 return READ_TREE_RECURSIVE;
166 } else { 166 } else {
167 print_object(sha1, buffer); 167 print_object(sha1, buffer);
168 return 0; 168 return 0;
169 } 169 }
170 } 170 }
171 ls_item(sha1, base, baselen, pathname, mode, stage); 171 ls_item(sha1, base, baselen, pathname, mode, stage);
172 return 0; 172 return 0;
173} 173}
174 174
175 175
176/* 176/*
177 * Show a tree or a blob 177 * Show a tree or a blob
178 * rev: the commit pointing at the root tree object 178 * rev: the commit pointing at the root tree object
179 * path: path to tree or blob 179 * path: path to tree or blob
180 */ 180 */
181void cgit_print_tree(const char *rev, char *path) 181void cgit_print_tree(const char *rev, char *path)
182{ 182{
183 unsigned char sha1[20]; 183 unsigned char sha1[20];
184 struct commit *commit; 184 struct commit *commit;
185 const char *paths[] = {path, NULL}; 185 const char *paths[] = {path, NULL};
186 186
187 if (!rev) 187 if (!rev)
188 rev = cgit_query_head; 188 rev = cgit_query_head;
189 189
190 curr_rev = xstrdup(rev); 190 curr_rev = xstrdup(rev);
191 if (get_sha1(rev, sha1)) { 191 if (get_sha1(rev, sha1)) {
192 cgit_print_error(fmt("Invalid revision name: %s", rev)); 192 cgit_print_error(fmt("Invalid revision name: %s", rev));
193 return; 193 return;
194 } 194 }
195 commit = lookup_commit_reference(sha1); 195 commit = lookup_commit_reference(sha1);
196 if (!commit || parse_commit(commit)) { 196 if (!commit || parse_commit(commit)) {
197 cgit_print_error(fmt("Invalid commit reference: %s", rev)); 197 cgit_print_error(fmt("Invalid commit reference: %s", rev));
198 return; 198 return;
199 } 199 }
200 200
201 html("path: <a href='"); 201 html("path: <a href='");
202 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev))); 202 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev)));
203 html("'>root</a>"); 203 html("'>root</a>");
204 204
205 if (path == NULL) { 205 if (path == NULL) {
206 ls_tree(commit->tree->object.sha1, NULL); 206 ls_tree(commit->tree->object.sha1, NULL);
207 return; 207 return;
208 } 208 }
209 209