Sync with 'maint'
[alt-git.git] / walker.c
blob0fafdc97cf17e8ffa9d90d82b6929641b56d66ae
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "walker.h"
7 #include "repository.h"
8 #include "object-store-ll.h"
9 #include "commit.h"
10 #include "strbuf.h"
11 #include "tree.h"
12 #include "tree-walk.h"
13 #include "tag.h"
14 #include "blob.h"
15 #include "refs.h"
16 #include "progress.h"
18 static struct object_id current_commit_oid;
20 void walker_say(struct walker *walker, const char *fmt, ...)
22 if (walker->get_verbosely) {
23 va_list ap;
24 va_start(ap, fmt);
25 vfprintf(stderr, fmt, ap);
26 va_end(ap);
30 static void report_missing(const struct object *obj)
32 fprintf(stderr, "Cannot obtain needed %s %s\n",
33 obj->type ? type_name(obj->type): "object",
34 oid_to_hex(&obj->oid));
35 if (!is_null_oid(&current_commit_oid))
36 fprintf(stderr, "while processing commit %s.\n",
37 oid_to_hex(&current_commit_oid));
40 static int process(struct walker *walker, struct object *obj);
42 static int process_tree(struct walker *walker, struct tree *tree)
44 struct tree_desc desc;
45 struct name_entry entry;
47 if (parse_tree(tree))
48 return -1;
50 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
51 while (tree_entry(&desc, &entry)) {
52 struct object *obj = NULL;
54 /* submodule commits are not stored in the superproject */
55 if (S_ISGITLINK(entry.mode))
56 continue;
57 if (S_ISDIR(entry.mode)) {
58 struct tree *tree = lookup_tree(the_repository,
59 &entry.oid);
60 if (tree)
61 obj = &tree->object;
63 else {
64 struct blob *blob = lookup_blob(the_repository,
65 &entry.oid);
66 if (blob)
67 obj = &blob->object;
69 if (!obj || process(walker, obj))
70 return -1;
72 free_tree_buffer(tree);
73 return 0;
76 /* Remember to update object flag allocation in object.h */
77 #define COMPLETE (1U << 0)
78 #define SEEN (1U << 1)
79 #define TO_SCAN (1U << 2)
81 static struct commit_list *complete = NULL;
83 static int process_commit(struct walker *walker, struct commit *commit)
85 struct commit_list *parents;
87 if (repo_parse_commit(the_repository, commit))
88 return -1;
90 while (complete && complete->item->date >= commit->date) {
91 pop_most_recent_commit(&complete, COMPLETE);
94 if (commit->object.flags & COMPLETE)
95 return 0;
97 oidcpy(&current_commit_oid, &commit->object.oid);
99 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
101 if (process(walker, &repo_get_commit_tree(the_repository, commit)->object))
102 return -1;
104 for (parents = commit->parents; parents; parents = parents->next) {
105 if (process(walker, &parents->item->object))
106 return -1;
109 return 0;
112 static int process_tag(struct walker *walker, struct tag *tag)
114 if (parse_tag(tag))
115 return -1;
116 return process(walker, tag->tagged);
119 static struct object_list *process_queue = NULL;
120 static struct object_list **process_queue_end = &process_queue;
122 static int process_object(struct walker *walker, struct object *obj)
124 if (obj->type == OBJ_COMMIT) {
125 if (process_commit(walker, (struct commit *)obj))
126 return -1;
127 return 0;
129 if (obj->type == OBJ_TREE) {
130 if (process_tree(walker, (struct tree *)obj))
131 return -1;
132 return 0;
134 if (obj->type == OBJ_BLOB) {
135 return 0;
137 if (obj->type == OBJ_TAG) {
138 if (process_tag(walker, (struct tag *)obj))
139 return -1;
140 return 0;
142 return error("Unable to determine requirements "
143 "of type %s for %s",
144 type_name(obj->type), oid_to_hex(&obj->oid));
147 static int process(struct walker *walker, struct object *obj)
149 if (obj->flags & SEEN)
150 return 0;
151 obj->flags |= SEEN;
153 if (repo_has_object_file(the_repository, &obj->oid)) {
154 /* We already have it, so we should scan it now. */
155 obj->flags |= TO_SCAN;
157 else {
158 if (obj->flags & COMPLETE)
159 return 0;
160 walker->prefetch(walker, obj->oid.hash);
163 object_list_insert(obj, process_queue_end);
164 process_queue_end = &(*process_queue_end)->next;
165 return 0;
168 static int loop(struct walker *walker)
170 struct object_list *elem;
171 struct progress *progress = NULL;
172 uint64_t nr = 0;
174 if (walker->get_progress)
175 progress = start_delayed_progress(_("Fetching objects"), 0);
177 while (process_queue) {
178 struct object *obj = process_queue->item;
179 elem = process_queue;
180 process_queue = elem->next;
181 free(elem);
182 if (!process_queue)
183 process_queue_end = &process_queue;
185 /* If we are not scanning this object, we placed it in
186 * the queue because we needed to fetch it first.
188 if (! (obj->flags & TO_SCAN)) {
189 if (walker->fetch(walker, obj->oid.hash)) {
190 stop_progress(&progress);
191 report_missing(obj);
192 return -1;
195 if (!obj->type)
196 parse_object(the_repository, &obj->oid);
197 if (process_object(walker, obj)) {
198 stop_progress(&progress);
199 return -1;
201 display_progress(progress, ++nr);
203 stop_progress(&progress);
204 return 0;
207 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
209 if (!get_oid_hex(target, oid))
210 return 0;
211 if (!check_refname_format(target, 0)) {
212 struct ref *ref = alloc_ref(target);
213 if (!walker->fetch_ref(walker, ref)) {
214 oidcpy(oid, &ref->old_oid);
215 free(ref);
216 return 0;
218 free(ref);
220 return -1;
223 static int mark_complete(const char *path UNUSED,
224 const struct object_id *oid,
225 int flag UNUSED,
226 void *cb_data UNUSED)
228 struct commit *commit = lookup_commit_reference_gently(the_repository,
229 oid, 1);
231 if (commit) {
232 commit->object.flags |= COMPLETE;
233 commit_list_insert(commit, &complete);
235 return 0;
238 int walker_targets_stdin(char ***target, const char ***write_ref)
240 int targets = 0, targets_alloc = 0;
241 struct strbuf buf = STRBUF_INIT;
242 *target = NULL; *write_ref = NULL;
243 while (1) {
244 char *rf_one = NULL;
245 char *tg_one;
247 if (strbuf_getline_lf(&buf, stdin) == EOF)
248 break;
249 tg_one = buf.buf;
250 rf_one = strchr(tg_one, '\t');
251 if (rf_one)
252 *rf_one++ = 0;
254 if (targets >= targets_alloc) {
255 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
256 REALLOC_ARRAY(*target, targets_alloc);
257 REALLOC_ARRAY(*write_ref, targets_alloc);
259 (*target)[targets] = xstrdup(tg_one);
260 (*write_ref)[targets] = xstrdup_or_null(rf_one);
261 targets++;
263 strbuf_release(&buf);
264 return targets;
267 void walker_targets_free(int targets, char **target, const char **write_ref)
269 while (targets--) {
270 free(target[targets]);
271 if (write_ref)
272 free((char *) write_ref[targets]);
276 int walker_fetch(struct walker *walker, int targets, char **target,
277 const char **write_ref, const char *write_ref_log_details)
279 struct strbuf refname = STRBUF_INIT;
280 struct strbuf err = STRBUF_INIT;
281 struct ref_transaction *transaction = NULL;
282 struct object_id *oids;
283 char *msg = NULL;
284 int i, ret = -1;
286 save_commit_buffer = 0;
288 ALLOC_ARRAY(oids, targets);
290 if (write_ref) {
291 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
292 &err);
293 if (!transaction) {
294 error("%s", err.buf);
295 goto done;
299 if (!walker->get_recover) {
300 refs_for_each_ref(get_main_ref_store(the_repository),
301 mark_complete, NULL);
302 commit_list_sort_by_date(&complete);
305 for (i = 0; i < targets; i++) {
306 if (interpret_target(walker, target[i], oids + i)) {
307 error("Could not interpret response from server '%s' as something to pull", target[i]);
308 goto done;
310 if (process(walker, lookup_unknown_object(the_repository, &oids[i])))
311 goto done;
314 if (loop(walker))
315 goto done;
316 if (!write_ref) {
317 ret = 0;
318 goto done;
320 if (write_ref_log_details) {
321 msg = xstrfmt("fetch from %s", write_ref_log_details);
322 } else {
323 msg = NULL;
325 for (i = 0; i < targets; i++) {
326 if (!write_ref[i])
327 continue;
328 strbuf_reset(&refname);
329 strbuf_addf(&refname, "refs/%s", write_ref[i]);
330 if (ref_transaction_update(transaction, refname.buf,
331 oids + i, NULL, NULL, NULL, 0,
332 msg ? msg : "fetch (unknown)",
333 &err)) {
334 error("%s", err.buf);
335 goto done;
338 if (ref_transaction_commit(transaction, &err)) {
339 error("%s", err.buf);
340 goto done;
343 ret = 0;
345 done:
346 ref_transaction_free(transaction);
347 free(msg);
348 free(oids);
349 strbuf_release(&err);
350 strbuf_release(&refname);
351 return ret;
354 void walker_free(struct walker *walker)
356 walker->cleanup(walker);
357 free(walker);