11 const char *write_ref = NULL;
16 int get_verbosely = 0;
18 static unsigned char current_commit_sha1[20];
20 void pull_say(const char *fmt, const char *hex)
23 fprintf(stderr, fmt, hex);
26 static void report_missing(const char *what, const unsigned char *missing)
30 strcpy(missing_hex, sha1_to_hex(missing));;
32 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
33 what, missing_hex, sha1_to_hex(current_commit_sha1));
36 static int process(struct object *obj);
38 static int process_tree(struct tree *tree)
40 struct tree_desc desc;
45 desc.buf = tree->buffer;
46 desc.size = tree->size;
50 const unsigned char *sha1;
52 sha1 = tree_entry_extract(&desc, &name, &mode);
53 update_tree_entry(&desc);
56 struct tree *tree = lookup_tree(sha1);
59 struct blob *blob = lookup_blob(sha1);
60 process(&blob->object);
69 #define COMPLETE (1U << 0)
70 #define SEEN (1U << 1)
71 #define TO_SCAN (1U << 2)
73 static struct commit_list *complete = NULL;
75 static int process_commit(struct commit *commit)
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 memcpy(current_commit_sha1, commit->object.sha1, 20);
89 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
92 if (process(&commit->tree->object))
98 struct commit_list *parents = commit->parents;
99 for (; parents; parents = parents->next) {
100 if (process(&parents->item->object))
107 static int process_tag(struct tag *tag)
111 return process(tag->tagged);
114 static struct object_list *process_queue = NULL;
115 static struct object_list **process_queue_end = &process_queue;
117 static int process_object(struct object *obj)
119 if (obj->type == commit_type) {
120 if (process_commit((struct commit *)obj))
124 if (obj->type == tree_type) {
125 if (process_tree((struct tree *)obj))
129 if (obj->type == blob_type) {
132 if (obj->type == tag_type) {
133 if (process_tag((struct tag *)obj))
137 return error("Unable to determine requirements "
139 obj->type, sha1_to_hex(obj->sha1));
142 static int process(struct object *obj)
144 if (obj->flags & SEEN)
148 if (has_sha1_file(obj->sha1)) {
149 /* We already have it, so we should scan it now. */
150 obj->flags |= TO_SCAN;
152 if (obj->flags & COMPLETE)
157 object_list_insert(obj, process_queue_end);
158 process_queue_end = &(*process_queue_end)->next;
162 static int loop(void)
164 struct object_list *elem;
166 while (process_queue) {
167 struct object *obj = process_queue->item;
168 elem = process_queue;
169 process_queue = elem->next;
172 process_queue_end = &process_queue;
174 /* If we are not scanning this object, we placed it in
175 * the queue because we needed to fetch it first.
177 if (! (obj->flags & TO_SCAN)) {
178 if (fetch(obj->sha1)) {
179 report_missing(obj->type
181 : "object", obj->sha1);
186 parse_object(obj->sha1);
187 if (process_object(obj))
193 static int interpret_target(char *target, unsigned char *sha1)
195 if (!get_sha1_hex(target, sha1))
197 if (!check_ref_format(target)) {
198 if (!fetch_ref(target, sha1)) {
205 static int mark_complete(const char *path, const unsigned char *sha1)
207 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
209 commit->object.flags |= COMPLETE;
210 insert_by_date(commit, &complete);
215 int pull(char *target)
217 unsigned char sha1[20];
219 save_commit_buffer = 0;
220 track_object_refs = 0;
223 for_each_ref(mark_complete);
225 if (interpret_target(target, sha1))
226 return error("Could not interpret %s as something to pull",
228 if (process(lookup_unknown_object(sha1)))
234 write_ref_sha1_unlocked(write_ref, sha1);