10 static unsigned char current_commit_sha1[20];
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 ? typename(obj->type): "object",
26 oid_to_hex(&obj->oid));
27 if (!is_null_sha1(current_commit_sha1))
28 fprintf(stderr, "while processing commit %s.\n",
29 sha1_to_hex(current_commit_sha1));
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 if (parse_commit(commit))
78 while (complete && complete->item->date >= commit->date) {
79 pop_most_recent_commit(&complete, COMPLETE);
82 if (commit->object.flags & COMPLETE)
85 hashcpy(current_commit_sha1, commit->object.oid.hash);
87 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
89 if (walker->get_tree) {
90 if (process(walker, &commit->tree->object))
95 if (walker->get_history) {
96 struct commit_list *parents = commit->parents;
97 for (; parents; parents = parents->next) {
98 if (process(walker, &parents->item->object))
105 static int process_tag(struct walker *walker, struct tag *tag)
109 return process(walker, tag->tagged);
112 static struct object_list *process_queue = NULL;
113 static struct object_list **process_queue_end = &process_queue;
115 static int process_object(struct walker *walker, struct object *obj)
117 if (obj->type == OBJ_COMMIT) {
118 if (process_commit(walker, (struct commit *)obj))
122 if (obj->type == OBJ_TREE) {
123 if (process_tree(walker, (struct tree *)obj))
127 if (obj->type == OBJ_BLOB) {
130 if (obj->type == OBJ_TAG) {
131 if (process_tag(walker, (struct tag *)obj))
135 return error("Unable to determine requirements "
137 typename(obj->type), oid_to_hex(&obj->oid));
140 static int process(struct walker *walker, struct object *obj)
142 if (obj->flags & SEEN)
146 if (has_object_file(&obj->oid)) {
147 /* We already have it, so we should scan it now. */
148 obj->flags |= TO_SCAN;
151 if (obj->flags & COMPLETE)
153 walker->prefetch(walker, obj->oid.hash);
156 object_list_insert(obj, process_queue_end);
157 process_queue_end = &(*process_queue_end)->next;
161 static int loop(struct walker *walker)
163 struct object_list *elem;
165 while (process_queue) {
166 struct object *obj = process_queue->item;
167 elem = process_queue;
168 process_queue = elem->next;
171 process_queue_end = &process_queue;
173 /* If we are not scanning this object, we placed it in
174 * the queue because we needed to fetch it first.
176 if (! (obj->flags & TO_SCAN)) {
177 if (walker->fetch(walker, obj->oid.hash)) {
183 parse_object(&obj->oid);
184 if (process_object(walker, obj))
190 static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
192 if (!get_sha1_hex(target, sha1))
194 if (!check_refname_format(target, 0)) {
195 struct ref *ref = alloc_ref(target);
196 if (!walker->fetch_ref(walker, ref)) {
197 hashcpy(sha1, ref->old_oid.hash);
206 static int mark_complete(const char *path, const struct object_id *oid,
207 int flag, void *cb_data)
209 struct commit *commit = lookup_commit_reference_gently(oid, 1);
212 commit->object.flags |= COMPLETE;
213 commit_list_insert(commit, &complete);
218 int walker_targets_stdin(char ***target, const char ***write_ref)
220 int targets = 0, targets_alloc = 0;
221 struct strbuf buf = STRBUF_INIT;
222 *target = NULL; *write_ref = NULL;
227 if (strbuf_getline_lf(&buf, stdin) == EOF)
230 rf_one = strchr(tg_one, '\t');
234 if (targets >= targets_alloc) {
235 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
236 REALLOC_ARRAY(*target, targets_alloc);
237 REALLOC_ARRAY(*write_ref, targets_alloc);
239 (*target)[targets] = xstrdup(tg_one);
240 (*write_ref)[targets] = xstrdup_or_null(rf_one);
243 strbuf_release(&buf);
247 void walker_targets_free(int targets, char **target, const char **write_ref)
250 free(target[targets]);
252 free((char *) write_ref[targets]);
256 int walker_fetch(struct walker *walker, int targets, char **target,
257 const char **write_ref, const char *write_ref_log_details)
259 struct strbuf refname = STRBUF_INIT;
260 struct strbuf err = STRBUF_INIT;
261 struct ref_transaction *transaction = NULL;
262 unsigned char *sha1 = xmalloc(targets * 20);
266 save_commit_buffer = 0;
269 transaction = ref_transaction_begin(&err);
271 error("%s", err.buf);
276 if (!walker->get_recover) {
277 for_each_ref(mark_complete, NULL);
278 commit_list_sort_by_date(&complete);
281 for (i = 0; i < targets; i++) {
282 if (interpret_target(walker, target[i], &sha1[20 * i])) {
283 error("Could not interpret response from server '%s' as something to pull", target[i]);
286 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
296 if (write_ref_log_details) {
297 msg = xstrfmt("fetch from %s", write_ref_log_details);
301 for (i = 0; i < targets; i++) {
304 strbuf_reset(&refname);
305 strbuf_addf(&refname, "refs/%s", write_ref[i]);
306 if (ref_transaction_update(transaction, refname.buf,
307 &sha1[20 * i], NULL, 0,
308 msg ? msg : "fetch (unknown)",
310 error("%s", err.buf);
314 if (ref_transaction_commit(transaction, &err)) {
315 error("%s", err.buf);
322 ref_transaction_free(transaction);
325 strbuf_release(&err);
326 strbuf_release(&refname);
330 void walker_free(struct walker *walker)
332 walker->cleanup(walker);