]>
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)
11 #include "ui-shared.h"
17 static void print_object(const unsigned char *sha1
, char *path
)
19 enum object_type type
;
21 unsigned long size
, lineno
, start
, idx
;
22 const char *linefmt
= "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>";
24 type
= sha1_object_info(sha1
, &size
);
25 if (type
== OBJ_BAD
) {
26 cgit_print_error(fmt("Bad object name: %s",
31 buf
= read_sha1_file(sha1
, &type
, &size
);
33 cgit_print_error(fmt("Error reading object %s",
39 cgit_plain_link("plain", NULL
, NULL
, ctx
.qry
.head
,
41 htmlf(")<br/>blob: %s", sha1_to_hex(sha1
));
43 html("<table summary='blob content' class='blob'>\n");
48 if (buf
[idx
] == '\n') {
50 htmlf(linefmt
, ++lineno
);
51 html_txt(buf
+ start
);
58 htmlf(linefmt
, ++lineno
);
59 html_txt(buf
+ start
);
66 static int ls_item(const unsigned char *sha1
, const char *base
, int baselen
,
67 const char *pathname
, unsigned int mode
, int stage
,
72 enum object_type type
;
73 unsigned long size
= 0;
75 name
= xstrdup(pathname
);
76 fullpath
= fmt("%s%s%s", ctx
.qry
.path
? ctx
.qry
.path
: "",
77 ctx
.qry
.path
? "/" : "", name
);
79 if (!S_ISGITLINK(mode
)) {
80 type
= sha1_object_info(sha1
, &size
);
81 if (type
== OBJ_BAD
) {
82 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
89 html("<tr><td class='ls-mode'>");
90 cgit_print_filemode(mode
);
92 if (S_ISGITLINK(mode
)) {
93 htmlf("<a class='ls-mod' href='");
94 html_attr(fmt(ctx
.repo
->module_link
,
100 } else if (S_ISDIR(mode
)) {
101 cgit_tree_link(name
, NULL
, "ls-dir", ctx
.qry
.head
,
104 cgit_tree_link(name
, NULL
, "ls-blob", ctx
.qry
.head
,
107 htmlf("</td><td class='ls-size'>%li</td>", size
);
110 cgit_log_link("log", NULL
, "button", ctx
.qry
.head
, curr_rev
,
111 fullpath
, 0, NULL
, NULL
, ctx
.qry
.showmsg
);
112 if (ctx
.repo
->max_stats
)
113 cgit_stats_link("stats", NULL
, "button", ctx
.qry
.head
,
115 html("</td></tr>\n");
120 static void ls_head()
122 html("<table summary='tree listing' class='list'>\n");
123 html("<tr class='nohover'>");
124 html("<th class='left'>Mode</th>");
125 html("<th class='left'>Name</th>");
126 html("<th class='right'>Size</th>");
132 static void ls_tail()
140 static void ls_tree(const unsigned char *sha1
, char *path
)
144 tree
= parse_tree_indirect(sha1
);
146 cgit_print_error(fmt("Not a tree object: %s",
152 read_tree_recursive(tree
, "", 0, 1, NULL
, ls_item
, NULL
);
157 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
158 const char *pathname
, unsigned mode
, int stage
,
162 static char buffer
[PATH_MAX
];
166 memcpy(buffer
, base
, baselen
);
167 strcpy(buffer
+baselen
, pathname
);
168 url
= cgit_pageurl(ctx
.qry
.repo
, "tree",
169 fmt("h=%s&path=%s", curr_rev
, buffer
));
171 cgit_tree_link(xstrdup(pathname
), NULL
, NULL
, ctx
.qry
.head
,
174 if (strcmp(match_path
, buffer
))
175 return READ_TREE_RECURSIVE
;
180 return READ_TREE_RECURSIVE
;
182 print_object(sha1
, buffer
);
186 ls_item(sha1
, base
, baselen
, pathname
, mode
, stage
, NULL
);
192 * Show a tree or a blob
193 * rev: the commit pointing at the root tree object
194 * path: path to tree or blob
196 void cgit_print_tree(const char *rev
, char *path
)
198 unsigned char sha1
[20];
199 struct commit
*commit
;
200 const char *paths
[] = {path
, NULL
};
205 curr_rev
= xstrdup(rev
);
206 if (get_sha1(rev
, sha1
)) {
207 cgit_print_error(fmt("Invalid revision name: %s", rev
));
210 commit
= lookup_commit_reference(sha1
);
211 if (!commit
|| parse_commit(commit
)) {
212 cgit_print_error(fmt("Invalid commit reference: %s", rev
));
216 html("path: <a href='");
217 html_attr(cgit_pageurl(ctx
.qry
.repo
, "tree", fmt("h=%s", rev
)));
221 ls_tree(commit
->tree
->object
.sha1
, NULL
);
226 read_tree_recursive(commit
->tree
, NULL
, 0, 0, paths
, walk_tree
, NULL
);