rebase -m: fix serialization of strategy options
[git/gitster.git] / tree.c
blob76a6534f67841026c675a36a55bcb62889d187ca
1 #include "cache.h"
2 #include "cache-tree.h"
3 #include "hex.h"
4 #include "tree.h"
5 #include "object-store.h"
6 #include "blob.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "alloc.h"
10 #include "tree-walk.h"
11 #include "repository.h"
13 const char *tree_type = "tree";
15 int read_tree_at(struct repository *r,
16 struct tree *tree, struct strbuf *base,
17 const struct pathspec *pathspec,
18 read_tree_fn_t fn, void *context)
20 struct tree_desc desc;
21 struct name_entry entry;
22 struct object_id oid;
23 int len, oldlen = base->len;
24 enum interesting retval = entry_not_interesting;
26 if (parse_tree(tree))
27 return -1;
29 init_tree_desc(&desc, tree->buffer, tree->size);
31 while (tree_entry(&desc, &entry)) {
32 if (retval != all_entries_interesting) {
33 retval = tree_entry_interesting(r->index, &entry,
34 base, 0, pathspec);
35 if (retval == all_entries_not_interesting)
36 break;
37 if (retval == entry_not_interesting)
38 continue;
41 switch (fn(&entry.oid, base,
42 entry.path, entry.mode, context)) {
43 case 0:
44 continue;
45 case READ_TREE_RECURSIVE:
46 break;
47 default:
48 return -1;
51 if (S_ISDIR(entry.mode))
52 oidcpy(&oid, &entry.oid);
53 else if (S_ISGITLINK(entry.mode)) {
54 struct commit *commit;
56 commit = lookup_commit(r, &entry.oid);
57 if (!commit)
58 die("Commit %s in submodule path %s%s not found",
59 oid_to_hex(&entry.oid),
60 base->buf, entry.path);
62 if (parse_commit(commit))
63 die("Invalid commit %s in submodule path %s%s",
64 oid_to_hex(&entry.oid),
65 base->buf, entry.path);
67 oidcpy(&oid, get_commit_tree_oid(commit));
69 else
70 continue;
72 len = tree_entry_len(&entry);
73 strbuf_add(base, entry.path, len);
74 strbuf_addch(base, '/');
75 retval = read_tree_at(r, lookup_tree(r, &oid),
76 base, pathspec,
77 fn, context);
78 strbuf_setlen(base, oldlen);
79 if (retval)
80 return -1;
82 return 0;
85 int read_tree(struct repository *r,
86 struct tree *tree,
87 const struct pathspec *pathspec,
88 read_tree_fn_t fn, void *context)
90 struct strbuf sb = STRBUF_INIT;
91 int ret = read_tree_at(r, tree, &sb, pathspec, fn, context);
92 strbuf_release(&sb);
93 return ret;
96 int cmp_cache_name_compare(const void *a_, const void *b_)
98 const struct cache_entry *ce1, *ce2;
100 ce1 = *((const struct cache_entry **)a_);
101 ce2 = *((const struct cache_entry **)b_);
102 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
103 ce2->name, ce2->ce_namelen, ce_stage(ce2));
106 struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
108 struct object *obj = lookup_object(r, oid);
109 if (!obj)
110 return create_object(r, oid, alloc_tree_node(r));
111 return object_as_type(obj, OBJ_TREE, 0);
114 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
116 if (item->object.parsed)
117 return 0;
118 item->object.parsed = 1;
119 item->buffer = buffer;
120 item->size = size;
122 return 0;
125 int parse_tree_gently(struct tree *item, int quiet_on_missing)
127 enum object_type type;
128 void *buffer;
129 unsigned long size;
131 if (item->object.parsed)
132 return 0;
133 buffer = read_object_file(&item->object.oid, &type, &size);
134 if (!buffer)
135 return quiet_on_missing ? -1 :
136 error("Could not read %s",
137 oid_to_hex(&item->object.oid));
138 if (type != OBJ_TREE) {
139 free(buffer);
140 return error("Object %s not a tree",
141 oid_to_hex(&item->object.oid));
143 return parse_tree_buffer(item, buffer, size);
146 void free_tree_buffer(struct tree *tree)
148 FREE_AND_NULL(tree->buffer);
149 tree->size = 0;
150 tree->object.parsed = 0;
153 struct tree *parse_tree_indirect(const struct object_id *oid)
155 struct repository *r = the_repository;
156 struct object *obj = parse_object(r, oid);
157 return (struct tree *)repo_peel_to_type(r, NULL, 0, obj, OBJ_TREE);