]>
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(" blob: <a href='");
36 html_attr(cgit_pageurl(cgit_query_repo
, "blob", fmt("id=%s", sha1_to_hex(sha1
))));
37 htmlf("'>%s</a>",sha1_to_hex(sha1
));
39 html("<table class='blob'>\n");
44 if (buf
[idx
] == '\n') {
46 htmlf("<tr><td class='no'>%d</td><td class='txt'>",
48 html_txt(buf
+ start
);
58 static int ls_item(const unsigned char *sha1
, const char *base
, int baselen
,
59 const char *pathname
, unsigned int mode
, int stage
)
63 enum object_type type
;
64 unsigned long size
= 0;
66 name
= xstrdup(pathname
);
67 fullpath
= fmt("%s%s%s", cgit_query_path
? cgit_query_path
: "",
68 cgit_query_path
? "/" : "", name
);
70 type
= sha1_object_info(sha1
, &size
);
71 if (type
== OBJ_BAD
&& !S_ISDIRLNK(mode
)) {
72 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
78 html("<tr><td class='ls-mode'>");
81 if (S_ISDIRLNK(mode
)) {
82 htmlf("<a class='ls-mod' href='");
83 html_attr(fmt(cgit_repo
->module_link
,
89 } else if (S_ISDIR(mode
)) {
90 cgit_tree_link(name
, NULL
, "ls-dir", cgit_query_head
,
93 cgit_tree_link(name
, NULL
, "ls-blob", cgit_query_head
,
96 htmlf("</td><td class='ls-size'>%li</td>", size
);
99 cgit_log_link("log", NULL
, "button", cgit_query_head
, curr_rev
,
101 html("</td></tr>\n");
106 static void ls_head()
108 html("<table class='list'>\n");
109 html("<tr class='nohover'>");
110 html("<th class='left'>Mode</th>");
111 html("<th class='left'>Name</th>");
112 html("<th class='right'>Size</th>");
118 static void ls_tail()
126 static void ls_tree(const unsigned char *sha1
, char *path
)
130 tree
= parse_tree_indirect(sha1
);
132 cgit_print_error(fmt("Not a tree object: %s",
138 read_tree_recursive(tree
, "", 0, 1, NULL
, ls_item
);
143 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
144 const char *pathname
, unsigned mode
, int stage
)
147 static char buffer
[PATH_MAX
];
151 memcpy(buffer
, base
, baselen
);
152 strcpy(buffer
+baselen
, pathname
);
153 url
= cgit_pageurl(cgit_query_repo
, "tree",
154 fmt("h=%s&path=%s", curr_rev
, buffer
));
156 cgit_tree_link(xstrdup(pathname
), NULL
, NULL
, cgit_query_head
,
159 if (strcmp(match_path
, buffer
))
160 return READ_TREE_RECURSIVE
;
165 return READ_TREE_RECURSIVE
;
167 print_object(sha1
, buffer
);
171 ls_item(sha1
, base
, baselen
, pathname
, mode
, stage
);
177 * Show a tree or a blob
178 * rev: the commit pointing at the root tree object
179 * path: path to tree or blob
181 void cgit_print_tree(const char *rev
, char *path
)
183 unsigned char sha1
[20];
184 struct commit
*commit
;
185 const char *paths
[] = {path
, NULL
};
188 rev
= cgit_query_head
;
190 curr_rev
= xstrdup(rev
);
191 if (get_sha1(rev
, sha1
)) {
192 cgit_print_error(fmt("Invalid revision name: %s", rev
));
195 commit
= lookup_commit_reference(sha1
);
196 if (!commit
|| parse_commit(commit
)) {
197 cgit_print_error(fmt("Invalid commit reference: %s", rev
));
201 html("path: <a href='");
202 html_attr(cgit_pageurl(cgit_query_repo
, "tree", fmt("h=%s", rev
)));
206 ls_tree(commit
->tree
->object
.sha1
, NULL
);
211 read_tree_recursive(commit
->tree
, NULL
, 0, 0, paths
, walk_tree
);