10 static struct object_id current_commit_oid;
12 void walker_say(struct walker *walker, const char *fmt, ...)
14 if (walker->get_verbosely) {
17 vfprintf(stderr, fmt, ap);
22 static void report_missing(const struct object *obj)
24 fprintf(stderr, "Cannot obtain needed %s %s\n",
25 obj->type ? type_name(obj->type): "object",
26 oid_to_hex(&obj->oid));
27 if (!is_null_oid(¤t_commit_oid))
28 fprintf(stderr, "while processing commit %s.\n",
29 oid_to_hex(¤t_commit_oid));
32 static int process(struct walker *walker, struct object *obj);
34 static int process_tree(struct walker *walker, struct tree *tree)
36 struct tree_desc desc;
37 struct name_entry entry;
42 init_tree_desc(&desc, tree->buffer, tree->size);
43 while (tree_entry(&desc, &entry)) {
44 struct object *obj = NULL;
46 /* submodule commits are not stored in the superproject */
47 if (S_ISGITLINK(entry.mode))
49 if (S_ISDIR(entry.mode)) {
50 struct tree *tree = lookup_tree(entry.oid);
55 struct blob *blob = lookup_blob(entry.oid);
59 if (!obj || process(walker, obj))
62 free_tree_buffer(tree);
66 /* Remember to update object flag allocation in object.h */
67 #define COMPLETE (1U << 0)
68 #define SEEN (1U << 1)
69 #define TO_SCAN (1U << 2)
71 static struct commit_list *complete = NULL;
73 static int process_commit(struct walker *walker, struct commit *commit)
75 struct commit_list *parents;
77 if (parse_commit(commit))
80 while (complete && complete->item->date >= commit->date) {
81 pop_most_recent_commit(&complete, COMPLETE);
84 if (commit->object.flags & COMPLETE)
87 oidcpy(¤t_commit_oid, &commit->object.oid);
89 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
91 if (process(walker, &get_commit_tree(commit)->object))
94 for (parents = commit->parents; parents; parents = parents->next) {
95 if (process(walker, &parents->item->object))
102 static int process_tag(struct walker *walker, struct tag *tag)
106 return process(walker, tag->tagged);
109 static struct object_list *process_queue = NULL;
110 static struct object_list **process_queue_end = &process_queue;
112 static int process_object(struct walker *walker, struct object *obj)
114 if (obj->type == OBJ_COMMIT) {
115 if (process_commit(walker, (struct commit *)obj))
119 if (obj->type == OBJ_TREE) {
120 if (process_tree(walker, (struct tree *)obj))
124 if (obj->type == OBJ_BLOB) {
127 if (obj->type == OBJ_TAG) {
128 if (process_tag(walker, (struct tag *)obj))
132 return error("Unable to determine requirements "
134 type_name(obj->type), oid_to_hex(&obj->oid));
137 static int process(struct walker *walker, struct object *obj)
139 if (obj->flags & SEEN)
143 if (has_object_file(&obj->oid)) {
144 /* We already have it, so we should scan it now. */
145 obj->flags |= TO_SCAN;
148 if (obj->flags & COMPLETE)
150 walker->prefetch(walker, obj->oid.hash);
153 object_list_insert(obj, process_queue_end);
154 process_queue_end = &(*process_queue_end)->next;
158 static int loop(struct walker *walker)
160 struct object_list *elem;
162 while (process_queue) {
163 struct object *obj = process_queue->item;
164 elem = process_queue;
165 process_queue = elem->next;
168 process_queue_end = &process_queue;
170 /* If we are not scanning this object, we placed it in
171 * the queue because we needed to fetch it first.
173 if (! (obj->flags & TO_SCAN)) {
174 if (walker->fetch(walker, obj->oid.hash)) {
180 parse_object(&obj->oid);
181 if (process_object(walker, obj))
187 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
189 if (!get_oid_hex(target, oid))
191 if (!check_refname_format(target, 0)) {
192 struct ref *ref = alloc_ref(target);
193 if (!walker->fetch_ref(walker, ref)) {
194 oidcpy(oid, &ref->old_oid);
203 static int mark_complete(const char *path, const struct object_id *oid,
204 int flag, void *cb_data)
206 struct commit *commit = lookup_commit_reference_gently(oid, 1);
209 commit->object.flags |= COMPLETE;
210 commit_list_insert(commit, &complete);
215 int walker_targets_stdin(char ***target, const char ***write_ref)
217 int targets = 0, targets_alloc = 0;
218 struct strbuf buf = STRBUF_INIT;
219 *target = NULL; *write_ref = NULL;
224 if (strbuf_getline_lf(&buf, stdin) == EOF)
227 rf_one = strchr(tg_one, '\t');
231 if (targets >= targets_alloc) {
232 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
233 REALLOC_ARRAY(*target, targets_alloc);
234 REALLOC_ARRAY(*write_ref, targets_alloc);
236 (*target)[targets] = xstrdup(tg_one);
237 (*write_ref)[targets] = xstrdup_or_null(rf_one);
240 strbuf_release(&buf);
244 void walker_targets_free(int targets, char **target, const char **write_ref)
247 free(target[targets]);
249 free((char *) write_ref[targets]);
253 int walker_fetch(struct walker *walker, int targets, char **target,
254 const char **write_ref, const char *write_ref_log_details)
256 struct strbuf refname = STRBUF_INIT;
257 struct strbuf err = STRBUF_INIT;
258 struct ref_transaction *transaction = NULL;
259 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
263 save_commit_buffer = 0;
266 transaction = ref_transaction_begin(&err);
268 error("%s", err.buf);
273 if (!walker->get_recover) {
274 for_each_ref(mark_complete, NULL);
275 commit_list_sort_by_date(&complete);
278 for (i = 0; i < targets; i++) {
279 if (interpret_target(walker, target[i], oids + i)) {
280 error("Could not interpret response from server '%s' as something to pull", target[i]);
283 if (process(walker, lookup_unknown_object(oids[i].hash)))
293 if (write_ref_log_details) {
294 msg = xstrfmt("fetch from %s", write_ref_log_details);
298 for (i = 0; i < targets; i++) {
301 strbuf_reset(&refname);
302 strbuf_addf(&refname, "refs/%s", write_ref[i]);
303 if (ref_transaction_update(transaction, refname.buf,
305 msg ? msg : "fetch (unknown)",
307 error("%s", err.buf);
311 if (ref_transaction_commit(transaction, &err)) {
312 error("%s", err.buf);
319 ref_transaction_free(transaction);
322 strbuf_release(&err);
323 strbuf_release(&refname);
327 void walker_free(struct walker *walker)
329 walker->cleanup(walker);