]>
git.cameronkatri.com Git - cgit.git/blob - ui-tree.c
1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
15 static void print_object(const unsigned char *sha1
, char *path
)
17 enum object_type type
;
19 unsigned long size
, lineno
, start
, idx
;
21 type
= sha1_object_info(sha1
, &size
);
22 if (type
== OBJ_BAD
) {
23 cgit_print_error(fmt("Bad object name: %s",
28 buf
= read_sha1_file(sha1
, &type
, &size
);
30 cgit_print_error(fmt("Error reading object %s",
35 html("<table class='blob'>\n");
40 if (buf
[idx
] == '\n') {
42 htmlf("<tr><td class='no'>%d</td><td class='txt'>",
44 html_txt(buf
+ start
);
54 static int ls_item(const unsigned char *sha1
, const char *base
, int baselen
,
55 const char *pathname
, unsigned int mode
, int stage
)
59 enum object_type type
;
60 unsigned long size
= 0;
63 name
= xstrdup(pathname
);
64 fullpath
= fmt("%s%s%s", cgit_query_path
? cgit_query_path
: "",
65 cgit_query_path
? "/" : "", name
);
67 type
= sha1_object_info(sha1
, &size
);
68 if (type
== OBJ_BAD
&& !S_ISDIRLNK(mode
)) {
69 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
75 html("<tr><td class='filemode'>");
78 if (S_ISDIRLNK(mode
)) {
79 htmlf("class='ls-mod'><a href='");
80 html_attr(fmt(cgit_repo
->module_link
,
86 } else if (S_ISDIR(mode
)) {
87 html("class='ls-dir'>");
88 cgit_tree_link(name
, NULL
, NULL
, cgit_query_head
,
91 html("class='ls-blob'>");
92 cgit_tree_link(name
, NULL
, NULL
, cgit_query_head
,
95 htmlf("</td><td class='filesize'>%li</td>", size
);
97 html("<td class='links'><a href='");
98 qry
= fmt("h=%s&path=%s%s%s", curr_rev
,
99 cgit_query_path
? cgit_query_path
: "",
100 cgit_query_path
? "/" : "", pathname
);
101 url
= cgit_pageurl(cgit_query_repo
, "log", qry
);
103 html("' class='button'>H</a></td>");
109 static void ls_head()
111 html("<table class='list'>\n");
112 html("<tr class='nohover'>");
113 html("<th class='left'>Mode</th>");
114 html("<th class='left'>Name</th>");
115 html("<th class='right'>Size</th>");
121 static void ls_tail()
129 static void ls_tree(const unsigned char *sha1
, char *path
)
133 tree
= parse_tree_indirect(sha1
);
135 cgit_print_error(fmt("Not a tree object: %s",
141 read_tree_recursive(tree
, "", 0, 1, NULL
, ls_item
);
146 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
147 const char *pathname
, unsigned mode
, int stage
)
150 static char buffer
[PATH_MAX
];
154 memcpy(buffer
, base
, baselen
);
155 strcpy(buffer
+baselen
, pathname
);
156 url
= cgit_pageurl(cgit_query_repo
, "tree",
157 fmt("h=%s&path=%s", curr_rev
, buffer
));
159 cgit_tree_link(xstrdup(pathname
), NULL
, NULL
, cgit_query_head
,
162 if (strcmp(match_path
, buffer
))
163 return READ_TREE_RECURSIVE
;
168 return READ_TREE_RECURSIVE
;
170 print_object(sha1
, buffer
);
174 ls_item(sha1
, base
, baselen
, pathname
, mode
, stage
);
180 * Show a tree or a blob
181 * rev: the commit pointing at the root tree object
182 * path: path to tree or blob
184 void cgit_print_tree(const char *rev
, char *path
)
186 unsigned char sha1
[20];
187 struct commit
*commit
;
188 const char *paths
[] = {path
, NULL
};
191 rev
= cgit_query_head
;
193 curr_rev
= xstrdup(rev
);
194 if (get_sha1(rev
, sha1
)) {
195 cgit_print_error(fmt("Invalid revision name: %s", rev
));
198 commit
= lookup_commit_reference(sha1
);
199 if (!commit
|| parse_commit(commit
)) {
200 cgit_print_error(fmt("Invalid commit reference: %s", rev
));
204 html("path: <a href='");
205 html_attr(cgit_pageurl(cgit_query_repo
, "tree", fmt("h=%s", rev
)));
209 ls_tree(commit
->tree
->object
.sha1
, NULL
);
214 read_tree_recursive(commit
->tree
, NULL
, 0, 0, paths
, walk_tree
);