summaryrefslogtreecommitdiffstatshomepage
path: root/ui-tree.c
blob: e4c3d2263cb3b85e84066ed4f0bfb6640bdcbe0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
generated by cgit v1.2.3-54-g00ecf (git 2.39.0) at 2025-01-14 10:06:08 +0000
 


));
	} else if (S_ISDIR(mode)) {
		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
			       walk_tree_ctx->curr_rev, fullpath.buf);
	} else {
		char *ext = strrchr(name, '.');
		strbuf_addstr(&class, "ls-blob");
		if (ext)
			strbuf_addf(&class, " %s", ext + 1);
		cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
			       walk_tree_ctx->curr_rev, fullpath.buf);
	}
	htmlf("</td><td class='ls-size'>%li</td>", size);

	html("<td>");
	cgit_log_link("log", NULL, "button", ctx.qry.head,
		      walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
		      ctx.qry.showmsg);
	if (ctx.repo->max_stats)
		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
				fullpath.buf);
	if (!S_ISGITLINK(mode))
		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
				walk_tree_ctx->curr_rev, fullpath.buf);
	html("</td></tr>\n");
	free(name);
	strbuf_release(&fullpath);
	strbuf_release(&class);
	return 0;
}

static void ls_head()
{
	html("<table summary='tree listing' class='list'>\n");
	html("<tr class='nohover'>");
	html("<th class='left'>Mode</th>");
	html("<th class='left'>Name</th>");
	html("<th class='right'>Size</th>");
	html("<th/>");
	html("</tr>\n");
}

static void ls_tail()
{
	html("</table>\n");
}

static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
{
	struct tree *tree;
	struct pathspec paths = {
		.nr = 0
	};

	tree = parse_tree_indirect(sha1);
	if (!tree) {
		cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
		return;
	}

	ls_head();
	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
	ls_tail();
}


static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
		     const char *pathname, unsigned mode, int stage,
		     void *cbdata)
{
	struct walk_tree_context *walk_tree_ctx = cbdata;
	static char buffer[PATH_MAX];

	if (walk_tree_ctx->state == 0) {
		memcpy(buffer, base, baselen);
		strcpy(buffer + baselen, pathname);
		if (strcmp(walk_tree_ctx->match_path, buffer))
			return READ_TREE_RECURSIVE;

		if (S_ISDIR(mode)) {
			walk_tree_ctx->state = 1;
			ls_head();
			return READ_TREE_RECURSIVE;
		} else {
			print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
			return 0;
		}
	}
	ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
	return 0;
}


/*
 * Show a tree or a blob
 *   rev:  the commit pointing at the root tree object
 *   path: path to tree or blob
 */
void cgit_print_tree(const char *rev, char *path)
{
	unsigned char sha1[20];
	struct commit *commit;
	struct pathspec_item path_items = {
		.match = path,
		.len = path ? strlen(path) : 0
	};
	struct pathspec paths = {
		.nr = path ? 1 : 0,
		.items = &path_items
	};
	struct walk_tree_context walk_tree_ctx = {
		.match_path = path,
		.state = 0
	};

	if (!rev)
		rev = ctx.qry.head;

	if (get_sha1(rev, sha1)) {
		cgit_print_error("Invalid revision name: %s", rev);
		return;
	}
	commit = lookup_commit_reference(sha1);
	if (!commit || parse_commit(commit)) {
		cgit_print_error("Invalid commit reference: %s", rev);
		return;
	}

	walk_tree_ctx.curr_rev = xstrdup(rev);

	if (path == NULL) {
		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
		goto cleanup;
	}

	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
	if (walk_tree_ctx.state == 1)
		ls_tail();

cleanup:
	free(walk_tree_ctx.curr_rev);
}