list-objects.c 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "cache.h"
#include "tag.h"
#include "commit.h"
#include "tree.h"
#include "blob.h"
#include "diff.h"
#include "tree-walk.h"
#include "revision.h"
#include "list-objects.h"

static void process_blob(struct rev_info *revs,
			 struct blob *blob,
13
			 show_object_fn show,
14
			 struct name_path *path,
15 16
			 const char *name,
			 void *cb_data)
17 18 19 20 21
{
	struct object *obj = &blob->object;

	if (!revs->blob_objects)
		return;
22 23
	if (!obj)
		die("bad blob object");
24 25 26
	if (obj->flags & (UNINTERESTING | SEEN))
		return;
	obj->flags |= SEEN;
27
	show(obj, path, name, cb_data);
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
/*
 * Processing a gitlink entry currently does nothing, since
 * we do not recurse into the subproject.
 *
 * We *could* eventually add a flag that actually does that,
 * which would involve:
 *  - is the subproject actually checked out?
 *  - if so, see if the subproject has already been added
 *    to the alternates list, and add it if not.
 *  - process the commit (or tag) the gitlink points to
 *    recursively.
 *
 * However, it's unclear whether there is really ever any
 * reason to see superprojects and subprojects as such a
 * "unified" object pool (potentially resulting in a totally
 * humongous pack - avoiding which was the whole point of
 * having gitlinks in the first place!).
 *
 * So for now, there is just a note that we *could* follow
 * the link, and how to do it. Whether it necessarily makes
 * any sense what-so-ever to ever do that is another issue.
 */
static void process_gitlink(struct rev_info *revs,
			    const unsigned char *sha1,
54
			    show_object_fn show,
55
			    struct name_path *path,
56 57
			    const char *name,
			    void *cb_data)
58 59 60 61
{
	/* Nothing to do */
}

62 63
static void process_tree(struct rev_info *revs,
			 struct tree *tree,
64
			 show_object_fn show,
65
			 struct name_path *path,
66
			 struct strbuf *base,
67 68
			 const char *name,
			 void *cb_data)
69 70 71 72 73
{
	struct object *obj = &tree->object;
	struct tree_desc desc;
	struct name_entry entry;
	struct name_path me;
74 75
	enum interesting match = revs->diffopt.pathspec.nr == 0 ?
		all_entries_interesting: entry_not_interesting;
76
	int baselen = base->len;
77 78 79

	if (!revs->tree_objects)
		return;
80 81
	if (!obj)
		die("bad tree object");
82 83
	if (obj->flags & (UNINTERESTING | SEEN))
		return;
84 85 86
	if (parse_tree(tree) < 0) {
		if (revs->ignore_missing_links)
			return;
87
		die("bad tree object %s", sha1_to_hex(obj->sha1));
88
	}
89
	obj->flags |= SEEN;
90
	show(obj, path, name, cb_data);
91 92 93 94
	me.up = path;
	me.elem = name;
	me.elem_len = strlen(name);

95
	if (!match) {
96 97 98 99 100
		strbuf_addstr(base, name);
		if (base->len)
			strbuf_addch(base, '/');
	}

101
	init_tree_desc(&desc, tree->buffer, tree->size);
102 103

	while (tree_entry(&desc, &entry)) {
104
		if (match != all_entries_interesting) {
105 106
			match = tree_entry_interesting(&entry, base, 0,
						       &revs->diffopt.pathspec);
107
			if (match == all_entries_not_interesting)
108
				break;
109
			if (match == entry_not_interesting)
110 111 112
				continue;
		}

113 114 115
		if (S_ISDIR(entry.mode))
			process_tree(revs,
				     lookup_tree(entry.sha1),
116 117
				     show, &me, base, entry.path,
				     cb_data);
M
Martin Waitz 已提交
118
		else if (S_ISGITLINK(entry.mode))
119
			process_gitlink(revs, entry.sha1,
120 121
					show, &me, entry.path,
					cb_data);
122 123 124
		else
			process_blob(revs,
				     lookup_blob(entry.sha1),
125 126
				     show, &me, entry.path,
				     cb_data);
127
	}
128
	strbuf_setlen(base, baselen);
129
	free_tree_buffer(tree);
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static void mark_edge_parents_uninteresting(struct commit *commit,
					    struct rev_info *revs,
					    show_edge_fn show_edge)
{
	struct commit_list *parents;

	for (parents = commit->parents; parents; parents = parents->next) {
		struct commit *parent = parents->item;
		if (!(parent->object.flags & UNINTERESTING))
			continue;
		mark_tree_uninteresting(parent->tree);
		if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
			parent->object.flags |= SHOWN;
			show_edge(parent);
		}
	}
}

150
void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
151
{
152
	struct commit_list *list;
153 154
	int i;

155
	for (list = revs->commits; list; list = list->next) {
156 157 158 159
		struct commit *commit = list->item;

		if (commit->object.flags & UNINTERESTING) {
			mark_tree_uninteresting(commit->tree);
160 161 162 163
			if (revs->edge_hint && !(commit->object.flags & SHOWN)) {
				commit->object.flags |= SHOWN;
				show_edge(commit);
			}
164 165 166 167
			continue;
		}
		mark_edge_parents_uninteresting(commit, revs, show_edge);
	}
168 169 170 171 172 173 174 175 176 177 178
	if (revs->edge_hint) {
		for (i = 0; i < revs->cmdline.nr; i++) {
			struct object *obj = revs->cmdline.rev[i].item;
			struct commit *commit = (struct commit *)obj;
			if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
				continue;
			mark_tree_uninteresting(commit->tree);
			if (!(obj->flags & SHOWN)) {
				obj->flags |= SHOWN;
				show_edge(commit);
			}
179 180
		}
	}
181 182
}

183 184 185 186 187
static void add_pending_tree(struct rev_info *revs, struct tree *tree)
{
	add_pending_object(revs, &tree->object, "");
}

188
void traverse_commit_list(struct rev_info *revs,
189 190 191
			  show_commit_fn show_commit,
			  show_object_fn show_object,
			  void *data)
192 193 194
{
	int i;
	struct commit *commit;
195
	struct strbuf base;
196

197
	strbuf_init(&base, PATH_MAX);
198
	while ((commit = get_revision(revs)) != NULL) {
199 200 201 202 203 204
		/*
		 * an uninteresting boundary commit may not have its tree
		 * parsed yet, but we are not going to show them anyway
		 */
		if (commit->tree)
			add_pending_tree(revs, commit->tree);
205
		show_commit(commit, data);
206 207 208 209 210
	}
	for (i = 0; i < revs->pending.nr; i++) {
		struct object_array_entry *pending = revs->pending.objects + i;
		struct object *obj = pending->item;
		const char *name = pending->name;
211
		const char *path = pending->path;
212 213 214 215
		if (obj->flags & (UNINTERESTING | SEEN))
			continue;
		if (obj->type == OBJ_TAG) {
			obj->flags |= SEEN;
216
			show_object(obj, NULL, name, data);
217 218
			continue;
		}
219 220
		if (!path)
			path = "";
221
		if (obj->type == OBJ_TREE) {
222
			process_tree(revs, (struct tree *)obj, show_object,
223
				     NULL, &base, path, data);
224 225 226
			continue;
		}
		if (obj->type == OBJ_BLOB) {
227
			process_blob(revs, (struct blob *)obj, show_object,
228
				     NULL, path, data);
229 230 231 232 233
			continue;
		}
		die("unknown pending object %s (%s)",
		    sha1_to_hex(obj->sha1), name);
	}
J
Jeff King 已提交
234
	object_array_clear(&revs->pending);
235
	strbuf_release(&base);
236
}