merge-ort/merge-recursive: do report errors in `merge_submodule()`
[git/gitster.git] / walker.c
blob65002a7220adc2e60aafe6a29555df1cee167ad0
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "walker.h"
5 #include "repository.h"
6 #include "object-store-ll.h"
7 #include "commit.h"
8 #include "strbuf.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "tag.h"
12 #include "blob.h"
13 #include "refs.h"
14 #include "progress.h"
16 static struct object_id current_commit_oid;
18 void walker_say(struct walker *walker, const char *fmt, ...)
20 if (walker->get_verbosely) {
21 va_list ap;
22 va_start(ap, fmt);
23 vfprintf(stderr, fmt, ap);
24 va_end(ap);
28 static void report_missing(const struct object *obj)
30 fprintf(stderr, "Cannot obtain needed %s %s\n",
31 obj->type ? type_name(obj->type): "object",
32 oid_to_hex(&obj->oid));
33 if (!is_null_oid(&current_commit_oid))
34 fprintf(stderr, "while processing commit %s.\n",
35 oid_to_hex(&current_commit_oid));
38 static int process(struct walker *walker, struct object *obj);
40 static int process_tree(struct walker *walker, struct tree *tree)
42 struct tree_desc desc;
43 struct name_entry entry;
45 if (parse_tree(tree))
46 return -1;
48 init_tree_desc(&desc, tree->buffer, tree->size);
49 while (tree_entry(&desc, &entry)) {
50 struct object *obj = NULL;
52 /* submodule commits are not stored in the superproject */
53 if (S_ISGITLINK(entry.mode))
54 continue;
55 if (S_ISDIR(entry.mode)) {
56 struct tree *tree = lookup_tree(the_repository,
57 &entry.oid);
58 if (tree)
59 obj = &tree->object;
61 else {
62 struct blob *blob = lookup_blob(the_repository,
63 &entry.oid);
64 if (blob)
65 obj = &blob->object;
67 if (!obj || process(walker, obj))
68 return -1;
70 free_tree_buffer(tree);
71 return 0;
74 /* Remember to update object flag allocation in object.h */
75 #define COMPLETE (1U << 0)
76 #define SEEN (1U << 1)
77 #define TO_SCAN (1U << 2)
79 static struct commit_list *complete = NULL;
81 static int process_commit(struct walker *walker, struct commit *commit)
83 struct commit_list *parents;
85 if (repo_parse_commit(the_repository, commit))
86 return -1;
88 while (complete && complete->item->date >= commit->date) {
89 pop_most_recent_commit(&complete, COMPLETE);
92 if (commit->object.flags & COMPLETE)
93 return 0;
95 oidcpy(&current_commit_oid, &commit->object.oid);
97 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
99 if (process(walker, &repo_get_commit_tree(the_repository, commit)->object))
100 return -1;
102 for (parents = commit->parents; parents; parents = parents->next) {
103 if (process(walker, &parents->item->object))
104 return -1;
107 return 0;
110 static int process_tag(struct walker *walker, struct tag *tag)
112 if (parse_tag(tag))
113 return -1;
114 return process(walker, tag->tagged);
117 static struct object_list *process_queue = NULL;
118 static struct object_list **process_queue_end = &process_queue;
120 static int process_object(struct walker *walker, struct object *obj)
122 if (obj->type == OBJ_COMMIT) {
123 if (process_commit(walker, (struct commit *)obj))
124 return -1;
125 return 0;
127 if (obj->type == OBJ_TREE) {
128 if (process_tree(walker, (struct tree *)obj))
129 return -1;
130 return 0;
132 if (obj->type == OBJ_BLOB) {
133 return 0;
135 if (obj->type == OBJ_TAG) {
136 if (process_tag(walker, (struct tag *)obj))
137 return -1;
138 return 0;
140 return error("Unable to determine requirements "
141 "of type %s for %s",
142 type_name(obj->type), oid_to_hex(&obj->oid));
145 static int process(struct walker *walker, struct object *obj)
147 if (obj->flags & SEEN)
148 return 0;
149 obj->flags |= SEEN;
151 if (repo_has_object_file(the_repository, &obj->oid)) {
152 /* We already have it, so we should scan it now. */
153 obj->flags |= TO_SCAN;
155 else {
156 if (obj->flags & COMPLETE)
157 return 0;
158 walker->prefetch(walker, obj->oid.hash);
161 object_list_insert(obj, process_queue_end);
162 process_queue_end = &(*process_queue_end)->next;
163 return 0;
166 static int loop(struct walker *walker)
168 struct object_list *elem;
169 struct progress *progress = NULL;
170 uint64_t nr = 0;
172 if (walker->get_progress)
173 progress = start_delayed_progress(_("Fetching objects"), 0);
175 while (process_queue) {
176 struct object *obj = process_queue->item;
177 elem = process_queue;
178 process_queue = elem->next;
179 free(elem);
180 if (!process_queue)
181 process_queue_end = &process_queue;
183 /* If we are not scanning this object, we placed it in
184 * the queue because we needed to fetch it first.
186 if (! (obj->flags & TO_SCAN)) {
187 if (walker->fetch(walker, obj->oid.hash)) {
188 stop_progress(&progress);
189 report_missing(obj);
190 return -1;
193 if (!obj->type)
194 parse_object(the_repository, &obj->oid);
195 if (process_object(walker, obj)) {
196 stop_progress(&progress);
197 return -1;
199 display_progress(progress, ++nr);
201 stop_progress(&progress);
202 return 0;
205 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
207 if (!get_oid_hex(target, oid))
208 return 0;
209 if (!check_refname_format(target, 0)) {
210 struct ref *ref = alloc_ref(target);
211 if (!walker->fetch_ref(walker, ref)) {
212 oidcpy(oid, &ref->old_oid);
213 free(ref);
214 return 0;
216 free(ref);
218 return -1;
221 static int mark_complete(const char *path UNUSED,
222 const struct object_id *oid,
223 int flag UNUSED,
224 void *cb_data UNUSED)
226 struct commit *commit = lookup_commit_reference_gently(the_repository,
227 oid, 1);
229 if (commit) {
230 commit->object.flags |= COMPLETE;
231 commit_list_insert(commit, &complete);
233 return 0;
236 int walker_targets_stdin(char ***target, const char ***write_ref)
238 int targets = 0, targets_alloc = 0;
239 struct strbuf buf = STRBUF_INIT;
240 *target = NULL; *write_ref = NULL;
241 while (1) {
242 char *rf_one = NULL;
243 char *tg_one;
245 if (strbuf_getline_lf(&buf, stdin) == EOF)
246 break;
247 tg_one = buf.buf;
248 rf_one = strchr(tg_one, '\t');
249 if (rf_one)
250 *rf_one++ = 0;
252 if (targets >= targets_alloc) {
253 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
254 REALLOC_ARRAY(*target, targets_alloc);
255 REALLOC_ARRAY(*write_ref, targets_alloc);
257 (*target)[targets] = xstrdup(tg_one);
258 (*write_ref)[targets] = xstrdup_or_null(rf_one);
259 targets++;
261 strbuf_release(&buf);
262 return targets;
265 void walker_targets_free(int targets, char **target, const char **write_ref)
267 while (targets--) {
268 free(target[targets]);
269 if (write_ref)
270 free((char *) write_ref[targets]);
274 int walker_fetch(struct walker *walker, int targets, char **target,
275 const char **write_ref, const char *write_ref_log_details)
277 struct strbuf refname = STRBUF_INIT;
278 struct strbuf err = STRBUF_INIT;
279 struct ref_transaction *transaction = NULL;
280 struct object_id *oids;
281 char *msg = NULL;
282 int i, ret = -1;
284 save_commit_buffer = 0;
286 ALLOC_ARRAY(oids, targets);
288 if (write_ref) {
289 transaction = ref_transaction_begin(&err);
290 if (!transaction) {
291 error("%s", err.buf);
292 goto done;
296 if (!walker->get_recover) {
297 for_each_ref(mark_complete, NULL);
298 commit_list_sort_by_date(&complete);
301 for (i = 0; i < targets; i++) {
302 if (interpret_target(walker, target[i], oids + i)) {
303 error("Could not interpret response from server '%s' as something to pull", target[i]);
304 goto done;
306 if (process(walker, lookup_unknown_object(the_repository, &oids[i])))
307 goto done;
310 if (loop(walker))
311 goto done;
312 if (!write_ref) {
313 ret = 0;
314 goto done;
316 if (write_ref_log_details) {
317 msg = xstrfmt("fetch from %s", write_ref_log_details);
318 } else {
319 msg = NULL;
321 for (i = 0; i < targets; i++) {
322 if (!write_ref[i])
323 continue;
324 strbuf_reset(&refname);
325 strbuf_addf(&refname, "refs/%s", write_ref[i]);
326 if (ref_transaction_update(transaction, refname.buf,
327 oids + i, NULL, 0,
328 msg ? msg : "fetch (unknown)",
329 &err)) {
330 error("%s", err.buf);
331 goto done;
334 if (ref_transaction_commit(transaction, &err)) {
335 error("%s", err.buf);
336 goto done;
339 ret = 0;
341 done:
342 ref_transaction_free(transaction);
343 free(msg);
344 free(oids);
345 strbuf_release(&err);
346 strbuf_release(&refname);
347 return ret;
350 void walker_free(struct walker *walker)
352 walker->cleanup(walker);
353 free(walker);