]>
git.cameronkatri.com Git - cgit.git/blob - ui-tree.c
79127840e0f029d3ae8c4ef2bf8c5b417f1cfda9
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)
16 static void print_object(const unsigned char *sha1
, char *path
)
18 enum object_type type
;
20 unsigned long size
, lineno
, start
, idx
;
21 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'>";
23 type
= sha1_object_info(sha1
, &size
);
24 if (type
== OBJ_BAD
) {
25 cgit_print_error(fmt("Bad object name: %s",
30 buf
= read_sha1_file(sha1
, &type
, &size
);
32 cgit_print_error(fmt("Error reading object %s",
37 html(" blob: <a href='");
38 html_attr(cgit_pageurl(ctx
.qry
.repo
, "blob", fmt("id=%s", sha1_to_hex(sha1
))));
39 htmlf("'>%s</a>",sha1_to_hex(sha1
));
41 html("<table summary='blob content' class='blob'>\n");
46 if (buf
[idx
] == '\n') {
48 htmlf(linefmt
, ++lineno
);
49 html_txt(buf
+ start
);
55 htmlf(linefmt
, ++lineno
);
56 html_txt(buf
+ start
);
62 static int ls_item(const unsigned char *sha1
, const char *base
, int baselen
,
63 const char *pathname
, unsigned int mode
, int stage
)
67 enum object_type type
;
68 unsigned long size
= 0;
70 name
= xstrdup(pathname
);
71 fullpath
= fmt("%s%s%s", ctx
.qry
.path
? ctx
.qry
.path
: "",
72 ctx
.qry
.path
? "/" : "", name
);
74 type
= sha1_object_info(sha1
, &size
);
75 if (type
== OBJ_BAD
&& !S_ISGITLINK(mode
)) {
76 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
82 html("<tr><td class='ls-mode'>");
83 cgit_print_filemode(mode
);
85 if (S_ISGITLINK(mode
)) {
86 htmlf("<a class='ls-mod' href='");
87 html_attr(fmt(ctx
.repo
->module_link
,
93 } else if (S_ISDIR(mode
)) {
94 cgit_tree_link(name
, NULL
, "ls-dir", ctx
.qry
.head
,
97 cgit_tree_link(name
, NULL
, "ls-blob", ctx
.qry
.head
,
100 htmlf("</td><td class='ls-size'>%li</td>", size
);
103 cgit_log_link("log", NULL
, "button", ctx
.qry
.head
, curr_rev
,
104 fullpath
, 0, NULL
, NULL
);
105 html("</td></tr>\n");
110 static void ls_head()
112 html("<table summary='tree listing' class='list'>\n");
113 html("<tr class='nohover'>");
114 html("<th class='left'>Mode</th>");
115 html("<th class='left'>Name</th>");
116 html("<th class='right'>Size</th>");
122 static void ls_tail()
130 static void ls_tree(const unsigned char *sha1
, char *path
)
134 tree
= parse_tree_indirect(sha1
);
136 cgit_print_error(fmt("Not a tree object: %s",
142 read_tree_recursive(tree
, "", 0, 1, NULL
, ls_item
);
147 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
148 const char *pathname
, unsigned mode
, int stage
)
151 static char buffer
[PATH_MAX
];
155 memcpy(buffer
, base
, baselen
);
156 strcpy(buffer
+baselen
, pathname
);
157 url
= cgit_pageurl(ctx
.qry
.repo
, "tree",
158 fmt("h=%s&path=%s", curr_rev
, buffer
));
160 cgit_tree_link(xstrdup(pathname
), NULL
, NULL
, ctx
.qry
.head
,
163 if (strcmp(match_path
, buffer
))
164 return READ_TREE_RECURSIVE
;
169 return READ_TREE_RECURSIVE
;
171 print_object(sha1
, buffer
);
175 ls_item(sha1
, base
, baselen
, pathname
, mode
, stage
);
181 * Show a tree or a blob
182 * rev: the commit pointing at the root tree object
183 * path: path to tree or blob
185 void cgit_print_tree(const char *rev
, char *path
)
187 unsigned char sha1
[20];
188 struct commit
*commit
;
189 const char *paths
[] = {path
, NULL
};
194 curr_rev
= xstrdup(rev
);
195 if (get_sha1(rev
, sha1
)) {
196 cgit_print_error(fmt("Invalid revision name: %s", rev
));
199 commit
= lookup_commit_reference(sha1
);
200 if (!commit
|| parse_commit(commit
)) {
201 cgit_print_error(fmt("Invalid commit reference: %s", rev
));
205 html("path: <a href='");
206 html_attr(cgit_pageurl(ctx
.qry
.repo
, "tree", fmt("h=%s", rev
)));
210 ls_tree(commit
->tree
->object
.sha1
, NULL
);
215 read_tree_recursive(commit
->tree
, NULL
, 0, 0, paths
, walk_tree
);