Sync with 2.40.2
[git.git] / walker.c
blob24ff7dfdc28a43a0f2b8535fd417b84eb0bede42
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.h"
7 #include "commit.h"
8 #include "tree.h"
9 #include "tree-walk.h"
10 #include "tag.h"
11 #include "blob.h"
12 #include "refs.h"
13 #include "progress.h"
15 static struct object_id current_commit_oid;
17 void walker_say(struct walker *walker, const char *fmt, ...)
19 if (walker->get_verbosely) {
20 va_list ap;
21 va_start(ap, fmt);
22 vfprintf(stderr, fmt, ap);
23 va_end(ap);
27 static void report_missing(const struct object *obj)
29 fprintf(stderr, "Cannot obtain needed %s %s\n",
30 obj->type ? type_name(obj->type): "object",
31 oid_to_hex(&obj->oid));
32 if (!is_null_oid(&current_commit_oid))
33 fprintf(stderr, "while processing commit %s.\n",
34 oid_to_hex(&current_commit_oid));
37 static int process(struct walker *walker, struct object *obj);
39 static int process_tree(struct walker *walker, struct tree *tree)
41 struct tree_desc desc;
42 struct name_entry entry;
44 if (parse_tree(tree))
45 return -1;
47 init_tree_desc(&desc, tree->buffer, tree->size);
48 while (tree_entry(&desc, &entry)) {
49 struct object *obj = NULL;
51 /* submodule commits are not stored in the superproject */
52 if (S_ISGITLINK(entry.mode))
53 continue;
54 if (S_ISDIR(entry.mode)) {
55 struct tree *tree = lookup_tree(the_repository,
56 &entry.oid);
57 if (tree)
58 obj = &tree->object;
60 else {
61 struct blob *blob = lookup_blob(the_repository,
62 &entry.oid);
63 if (blob)
64 obj = &blob->object;
66 if (!obj || process(walker, obj))
67 return -1;
69 free_tree_buffer(tree);
70 return 0;
73 /* Remember to update object flag allocation in object.h */
74 #define COMPLETE (1U << 0)
75 #define SEEN (1U << 1)
76 #define TO_SCAN (1U << 2)
78 static struct commit_list *complete = NULL;
80 static int process_commit(struct walker *walker, struct commit *commit)
82 struct commit_list *parents;
84 if (repo_parse_commit(the_repository, commit))
85 return -1;
87 while (complete && complete->item->date >= commit->date) {
88 pop_most_recent_commit(&complete, COMPLETE);
91 if (commit->object.flags & COMPLETE)
92 return 0;
94 oidcpy(&current_commit_oid, &commit->object.oid);
96 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
98 if (process(walker, &repo_get_commit_tree(the_repository, commit)->object))
99 return -1;
101 for (parents = commit->parents; parents; parents = parents->next) {
102 if (process(walker, &parents->item->object))
103 return -1;
106 return 0;
109 static int process_tag(struct walker *walker, struct tag *tag)
111 if (parse_tag(tag))
112 return -1;
113 return process(walker, tag->tagged);
116 static struct object_list *process_queue = NULL;
117 static struct object_list **process_queue_end = &process_queue;
119 static int process_object(struct walker *walker, struct object *obj)
121 if (obj->type == OBJ_COMMIT) {
122 if (process_commit(walker, (struct commit *)obj))
123 return -1;
124 return 0;
126 if (obj->type == OBJ_TREE) {
127 if (process_tree(walker, (struct tree *)obj))
128 return -1;
129 return 0;
131 if (obj->type == OBJ_BLOB) {
132 return 0;
134 if (obj->type == OBJ_TAG) {
135 if (process_tag(walker, (struct tag *)obj))
136 return -1;
137 return 0;
139 return error("Unable to determine requirements "
140 "of type %s for %s",
141 type_name(obj->type), oid_to_hex(&obj->oid));
144 static int process(struct walker *walker, struct object *obj)
146 if (obj->flags & SEEN)
147 return 0;
148 obj->flags |= SEEN;
150 if (repo_has_object_file(the_repository, &obj->oid)) {
151 /* We already have it, so we should scan it now. */
152 obj->flags |= TO_SCAN;
154 else {
155 if (obj->flags & COMPLETE)
156 return 0;
157 walker->prefetch(walker, obj->oid.hash);
160 object_list_insert(obj, process_queue_end);
161 process_queue_end = &(*process_queue_end)->next;
162 return 0;
165 static int loop(struct walker *walker)
167 struct object_list *elem;
168 struct progress *progress = NULL;
169 uint64_t nr = 0;
171 if (walker->get_progress)
172 progress = start_delayed_progress(_("Fetching objects"), 0);
174 while (process_queue) {
175 struct object *obj = process_queue->item;
176 elem = process_queue;
177 process_queue = elem->next;
178 free(elem);
179 if (!process_queue)
180 process_queue_end = &process_queue;
182 /* If we are not scanning this object, we placed it in
183 * the queue because we needed to fetch it first.
185 if (! (obj->flags & TO_SCAN)) {
186 if (walker->fetch(walker, obj->oid.hash)) {
187 stop_progress(&progress);
188 report_missing(obj);
189 return -1;
192 if (!obj->type)
193 parse_object(the_repository, &obj->oid);
194 if (process_object(walker, obj)) {
195 stop_progress(&progress);
196 return -1;
198 display_progress(progress, ++nr);
200 stop_progress(&progress);
201 return 0;
204 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
206 if (!get_oid_hex(target, oid))
207 return 0;
208 if (!check_refname_format(target, 0)) {
209 struct ref *ref = alloc_ref(target);
210 if (!walker->fetch_ref(walker, ref)) {
211 oidcpy(oid, &ref->old_oid);
212 free(ref);
213 return 0;
215 free(ref);
217 return -1;
220 static int mark_complete(const char *path UNUSED,
221 const struct object_id *oid,
222 int flag UNUSED,
223 void *cb_data UNUSED)
225 struct commit *commit = lookup_commit_reference_gently(the_repository,
226 oid, 1);
228 if (commit) {
229 commit->object.flags |= COMPLETE;
230 commit_list_insert(commit, &complete);
232 return 0;
235 int walker_targets_stdin(char ***target, const char ***write_ref)
237 int targets = 0, targets_alloc = 0;
238 struct strbuf buf = STRBUF_INIT;
239 *target = NULL; *write_ref = NULL;
240 while (1) {
241 char *rf_one = NULL;
242 char *tg_one;
244 if (strbuf_getline_lf(&buf, stdin) == EOF)
245 break;
246 tg_one = buf.buf;
247 rf_one = strchr(tg_one, '\t');
248 if (rf_one)
249 *rf_one++ = 0;
251 if (targets >= targets_alloc) {
252 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
253 REALLOC_ARRAY(*target, targets_alloc);
254 REALLOC_ARRAY(*write_ref, targets_alloc);
256 (*target)[targets] = xstrdup(tg_one);
257 (*write_ref)[targets] = xstrdup_or_null(rf_one);
258 targets++;
260 strbuf_release(&buf);
261 return targets;
264 void walker_targets_free(int targets, char **target, const char **write_ref)
266 while (targets--) {
267 free(target[targets]);
268 if (write_ref)
269 free((char *) write_ref[targets]);
273 int walker_fetch(struct walker *walker, int targets, char **target,
274 const char **write_ref, const char *write_ref_log_details)
276 struct strbuf refname = STRBUF_INIT;
277 struct strbuf err = STRBUF_INIT;
278 struct ref_transaction *transaction = NULL;
279 struct object_id *oids;
280 char *msg = NULL;
281 int i, ret = -1;
283 save_commit_buffer = 0;
285 ALLOC_ARRAY(oids, targets);
287 if (write_ref) {
288 transaction = ref_transaction_begin(&err);
289 if (!transaction) {
290 error("%s", err.buf);
291 goto done;
295 if (!walker->get_recover) {
296 for_each_ref(mark_complete, NULL);
297 commit_list_sort_by_date(&complete);
300 for (i = 0; i < targets; i++) {
301 if (interpret_target(walker, target[i], oids + i)) {
302 error("Could not interpret response from server '%s' as something to pull", target[i]);
303 goto done;
305 if (process(walker, lookup_unknown_object(the_repository, &oids[i])))
306 goto done;
309 if (loop(walker))
310 goto done;
311 if (!write_ref) {
312 ret = 0;
313 goto done;
315 if (write_ref_log_details) {
316 msg = xstrfmt("fetch from %s", write_ref_log_details);
317 } else {
318 msg = NULL;
320 for (i = 0; i < targets; i++) {
321 if (!write_ref[i])
322 continue;
323 strbuf_reset(&refname);
324 strbuf_addf(&refname, "refs/%s", write_ref[i]);
325 if (ref_transaction_update(transaction, refname.buf,
326 oids + i, NULL, 0,
327 msg ? msg : "fetch (unknown)",
328 &err)) {
329 error("%s", err.buf);
330 goto done;
333 if (ref_transaction_commit(transaction, &err)) {
334 error("%s", err.buf);
335 goto done;
338 ret = 0;
340 done:
341 ref_transaction_free(transaction);
342 free(msg);
343 free(oids);
344 strbuf_release(&err);
345 strbuf_release(&refname);
346 return ret;
349 void walker_free(struct walker *walker)
351 walker->cleanup(walker);
352 free(walker);