Merge branch 'sb/object-store-grafts' into sb/object-store-lookup
[git/git.git] / walker.c
CommitLineData
4250a5e5 1#include "cache.h"
30ae764b 2#include "walker.h"
cbd53a21 3#include "object-store.h"
4250a5e5
DB
4#include "commit.h"
5#include "tree.h"
1bc995a3 6#include "tree-walk.h"
3173bd49
DB
7#include "tag.h"
8#include "blob.h"
cd541a68
DB
9#include "refs.h"
10
94f5a121 11static struct object_id current_commit_oid;
4250a5e5 12
fa262cac 13void walker_say(struct walker *walker, const char *fmt, ...)
1e8be59d 14{
fa262cac
JK
15 if (walker->get_verbosely) {
16 va_list ap;
17 va_start(ap, fmt);
18 vfprintf(stderr, fmt, ap);
19 va_end(ap);
20 }
e78d9772
JH
21}
22
0d7a6e4e 23static void report_missing(const struct object *obj)
b2d62f16 24{
0d7a6e4e 25 fprintf(stderr, "Cannot obtain needed %s %s\n",
debca9d2 26 obj->type ? type_name(obj->type): "object",
f2fd0760 27 oid_to_hex(&obj->oid));
94f5a121 28 if (!is_null_oid(&current_commit_oid))
0d7a6e4e 29 fprintf(stderr, "while processing commit %s.\n",
94f5a121 30 oid_to_hex(&current_commit_oid));
b2d62f16
JH
31}
32
30ae764b 33static int process(struct walker *walker, struct object *obj);
3173bd49 34
30ae764b 35static int process_tree(struct walker *walker, struct tree *tree)
4250a5e5 36{
1bc995a3 37 struct tree_desc desc;
4c068a98 38 struct name_entry entry;
4250a5e5
DB
39
40 if (parse_tree(tree))
41 return -1;
42
6fda5e51 43 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 44 while (tree_entry(&desc, &entry)) {
6f9012b6
JH
45 struct object *obj = NULL;
46
582c7393 47 /* submodule commits are not stored in the superproject */
68fb4650 48 if (S_ISGITLINK(entry.mode))
582c7393 49 continue;
4c068a98 50 if (S_ISDIR(entry.mode)) {
740ee055 51 struct tree *tree = lookup_tree(entry.oid);
6f9012b6
JH
52 if (tree)
53 obj = &tree->object;
54 }
55 else {
3aca1fc6 56 struct blob *blob = lookup_blob(entry.oid);
6f9012b6
JH
57 if (blob)
58 obj = &blob->object;
2d9c58c6 59 }
30ae764b 60 if (!obj || process(walker, obj))
4250a5e5 61 return -1;
4250a5e5 62 }
6e454b9a 63 free_tree_buffer(tree);
4250a5e5
DB
64 return 0;
65}
66
208acbfb 67/* Remember to update object flag allocation in object.h */
24451c31
SV
68#define COMPLETE (1U << 0)
69#define SEEN (1U << 1)
70#define TO_SCAN (1U << 2)
85d106c2 71
d0ac30f2 72static struct commit_list *complete = NULL;
22c6e1d0 73
30ae764b 74static int process_commit(struct walker *walker, struct commit *commit)
4250a5e5 75{
0b6b3429
76 struct commit_list *parents;
77
1e8be59d 78 if (parse_commit(commit))
4250a5e5
DB
79 return -1;
80
22c6e1d0 81 while (complete && complete->item->date >= commit->date) {
d0ac30f2 82 pop_most_recent_commit(&complete, COMPLETE);
22c6e1d0 83 }
22c6e1d0 84
d0ac30f2 85 if (commit->object.flags & COMPLETE)
22c6e1d0
DB
86 return 0;
87
94f5a121 88 oidcpy(&current_commit_oid, &commit->object.oid);
4250a5e5 89
f2fd0760 90 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
85d106c2 91
c89b6e13 92 if (process(walker, &get_commit_tree(commit)->object))
0b6b3429
93 return -1;
94
95 for (parents = commit->parents; parents; parents = parents->next) {
96 if (process(walker, &parents->item->object))
4250a5e5 97 return -1;
4250a5e5 98 }
0b6b3429 99
4250a5e5
DB
100 return 0;
101}
102
30ae764b 103static int process_tag(struct walker *walker, struct tag *tag)
3173bd49 104{
1e8be59d 105 if (parse_tag(tag))
3173bd49 106 return -1;
30ae764b 107 return process(walker, tag->tagged);
3173bd49
DB
108}
109
1e8be59d
DB
110static struct object_list *process_queue = NULL;
111static struct object_list **process_queue_end = &process_queue;
112
30ae764b 113static int process_object(struct walker *walker, struct object *obj)
3173bd49 114{
1974632c 115 if (obj->type == OBJ_COMMIT) {
30ae764b 116 if (process_commit(walker, (struct commit *)obj))
f88fcf8b
DB
117 return -1;
118 return 0;
119 }
1974632c 120 if (obj->type == OBJ_TREE) {
30ae764b 121 if (process_tree(walker, (struct tree *)obj))
f88fcf8b
DB
122 return -1;
123 return 0;
124 }
1974632c 125 if (obj->type == OBJ_BLOB) {
f88fcf8b
DB
126 return 0;
127 }
1974632c 128 if (obj->type == OBJ_TAG) {
30ae764b 129 if (process_tag(walker, (struct tag *)obj))
f88fcf8b 130 return -1;
3173bd49 131 return 0;
f88fcf8b
DB
132 }
133 return error("Unable to determine requirements "
134 "of type %s for %s",
debca9d2 135 type_name(obj->type), oid_to_hex(&obj->oid));
f88fcf8b
DB
136}
137
30ae764b 138static int process(struct walker *walker, struct object *obj)
f88fcf8b 139{
a82d07e5
SV
140 if (obj->flags & SEEN)
141 return 0;
142 obj->flags |= SEEN;
143
f2fd0760 144 if (has_object_file(&obj->oid)) {
f88fcf8b 145 /* We already have it, so we should scan it now. */
85d106c2 146 obj->flags |= TO_SCAN;
e5f38ec3
JH
147 }
148 else {
7b64d06b
SV
149 if (obj->flags & COMPLETE)
150 return 0;
ed1c9977 151 walker->prefetch(walker, obj->oid.hash);
f88fcf8b 152 }
a6080a0a 153
1e8be59d
DB
154 object_list_insert(obj, process_queue_end);
155 process_queue_end = &(*process_queue_end)->next;
1e8be59d
DB
156 return 0;
157}
158
30ae764b 159static int loop(struct walker *walker)
1e8be59d 160{
85d106c2
JH
161 struct object_list *elem;
162
1e8be59d
DB
163 while (process_queue) {
164 struct object *obj = process_queue->item;
85d106c2
JH
165 elem = process_queue;
166 process_queue = elem->next;
167 free(elem);
1e8be59d
DB
168 if (!process_queue)
169 process_queue_end = &process_queue;
170
85d106c2
JH
171 /* If we are not scanning this object, we placed it in
172 * the queue because we needed to fetch it first.
173 */
174 if (! (obj->flags & TO_SCAN)) {
ed1c9977 175 if (walker->fetch(walker, obj->oid.hash)) {
0d7a6e4e 176 report_missing(obj);
85d106c2
JH
177 return -1;
178 }
179 }
1e8be59d 180 if (!obj->type)
c251c83d 181 parse_object(&obj->oid);
30ae764b 182 if (process_object(walker, obj))
f88fcf8b 183 return -1;
1e8be59d
DB
184 }
185 return 0;
3173bd49
DB
186}
187
94f5a121 188static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
cd541a68 189{
94f5a121 190 if (!get_oid_hex(target, oid))
cd541a68 191 return 0;
8d9c5010 192 if (!check_refname_format(target, 0)) {
59c69c0c 193 struct ref *ref = alloc_ref(target);
c13b2633 194 if (!walker->fetch_ref(walker, ref)) {
94f5a121 195 oidcpy(oid, &ref->old_oid);
c13b2633 196 free(ref);
cd541a68
DB
197 return 0;
198 }
c13b2633 199 free(ref);
cd541a68
DB
200 }
201 return -1;
202}
203
b4ebaf9e
MH
204static int mark_complete(const char *path, const struct object_id *oid,
205 int flag, void *cb_data)
22c6e1d0 206{
bc83266a 207 struct commit *commit = lookup_commit_reference_gently(oid, 1);
b4ebaf9e 208
d0ac30f2
JH
209 if (commit) {
210 commit->object.flags |= COMPLETE;
3bc7a05b 211 commit_list_insert(commit, &complete);
22c6e1d0
DB
212 }
213 return 0;
214}
cd541a68 215
30ae764b 216int walker_targets_stdin(char ***target, const char ***write_ref)
8e87ca66
PB
217{
218 int targets = 0, targets_alloc = 0;
f285a2d7 219 struct strbuf buf = STRBUF_INIT;
8e87ca66 220 *target = NULL; *write_ref = NULL;
8e87ca66
PB
221 while (1) {
222 char *rf_one = NULL;
223 char *tg_one;
224
8f309aeb 225 if (strbuf_getline_lf(&buf, stdin) == EOF)
8e87ca66
PB
226 break;
227 tg_one = buf.buf;
228 rf_one = strchr(tg_one, '\t');
229 if (rf_one)
230 *rf_one++ = 0;
231
232 if (targets >= targets_alloc) {
233 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
2756ca43
RS
234 REALLOC_ARRAY(*target, targets_alloc);
235 REALLOC_ARRAY(*write_ref, targets_alloc);
8e87ca66 236 }
9befac47 237 (*target)[targets] = xstrdup(tg_one);
8c53f071 238 (*write_ref)[targets] = xstrdup_or_null(rf_one);
8e87ca66
PB
239 targets++;
240 }
e6c019d0 241 strbuf_release(&buf);
8e87ca66
PB
242 return targets;
243}
244
30ae764b 245void walker_targets_free(int targets, char **target, const char **write_ref)
8e87ca66
PB
246{
247 while (targets--) {
248 free(target[targets]);
24deea52 249 if (write_ref)
8e87ca66
PB
250 free((char *) write_ref[targets]);
251 }
252}
253
30ae764b
DB
254int walker_fetch(struct walker *walker, int targets, char **target,
255 const char **write_ref, const char *write_ref_log_details)
4250a5e5 256{
b6b10bb4
RS
257 struct strbuf refname = STRBUF_INIT;
258 struct strbuf err = STRBUF_INIT;
259 struct ref_transaction *transaction = NULL;
94f5a121 260 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
b6b10bb4
RS
261 char *msg = NULL;
262 int i, ret = -1;
cd541a68 263
98533b90 264 save_commit_buffer = 0;
4211e4d1 265
b6b10bb4
RS
266 if (write_ref) {
267 transaction = ref_transaction_begin(&err);
268 if (!transaction) {
269 error("%s", err.buf);
270 goto done;
d0740d92 271 }
cd541a68
DB
272 }
273
3bc7a05b 274 if (!walker->get_recover) {
b4ebaf9e 275 for_each_ref(mark_complete, NULL);
3bc7a05b
RS
276 commit_list_sort_by_date(&complete);
277 }
22c6e1d0 278
4211e4d1 279 for (i = 0; i < targets; i++) {
94f5a121 280 if (interpret_target(walker, target[i], oids + i)) {
5f48741a 281 error("Could not interpret response from server '%s' as something to pull", target[i]);
b6b10bb4 282 goto done;
4211e4d1 283 }
94f5a121 284 if (process(walker, lookup_unknown_object(oids[i].hash)))
b6b10bb4 285 goto done;
4bd18c43 286 }
4211e4d1 287
30ae764b 288 if (loop(walker))
b6b10bb4
RS
289 goto done;
290 if (!write_ref) {
291 ret = 0;
292 goto done;
293 }
4211e4d1 294 if (write_ref_log_details) {
01d678a2 295 msg = xstrfmt("fetch from %s", write_ref_log_details);
4211e4d1
PB
296 } else {
297 msg = NULL;
4bd18c43 298 }
4211e4d1 299 for (i = 0; i < targets; i++) {
b6b10bb4 300 if (!write_ref[i])
4211e4d1 301 continue;
b6b10bb4
RS
302 strbuf_reset(&refname);
303 strbuf_addf(&refname, "refs/%s", write_ref[i]);
304 if (ref_transaction_update(transaction, refname.buf,
89f3bbdd 305 oids + i, NULL, 0,
db7516ab 306 msg ? msg : "fetch (unknown)",
b6b10bb4
RS
307 &err)) {
308 error("%s", err.buf);
309 goto done;
310 }
311 }
db7516ab 312 if (ref_transaction_commit(transaction, &err)) {
b6b10bb4
RS
313 error("%s", err.buf);
314 goto done;
4bd18c43 315 }
4211e4d1 316
b6b10bb4 317 ret = 0;
30ae764b 318
b6b10bb4
RS
319done:
320 ref_transaction_free(transaction);
321 free(msg);
94f5a121 322 free(oids);
b6b10bb4
RS
323 strbuf_release(&err);
324 strbuf_release(&refname);
325 return ret;
4250a5e5 326}
30ae764b
DB
327
328void walker_free(struct walker *walker)
329{
330 walker->cleanup(walker);
331 free(walker);
332}