13 int get_verbosely = 0;
15 static unsigned char current_commit_sha1[20];
17 void pull_say(const char *fmt, const char *hex)
20 fprintf(stderr, fmt, hex);
23 static void report_missing(const struct object *obj)
26 strcpy(missing_hex, sha1_to_hex(obj->sha1));;
27 fprintf(stderr, "Cannot obtain needed %s %s\n",
28 obj->type ? typename(obj->type): "object", missing_hex);
29 if (!is_null_sha1(current_commit_sha1))
30 fprintf(stderr, "while processing commit %s.\n",
31 sha1_to_hex(current_commit_sha1));
34 static int process(struct object *obj);
36 static int process_tree(struct tree *tree)
38 struct tree_desc desc;
39 struct name_entry entry;
44 init_tree_desc(&desc, tree->buffer, tree->size);
45 while (tree_entry(&desc, &entry)) {
46 struct object *obj = NULL;
48 /* submodule commits are not stored in the superproject */
49 if (S_ISGITLINK(entry.mode))
51 if (S_ISDIR(entry.mode)) {
52 struct tree *tree = lookup_tree(entry.sha1);
57 struct blob *blob = lookup_blob(entry.sha1);
61 if (!obj || process(obj))
70 #define COMPLETE (1U << 0)
71 #define SEEN (1U << 1)
72 #define TO_SCAN (1U << 2)
74 static struct commit_list *complete = NULL;
76 static int process_commit(struct commit *commit)
78 if (parse_commit(commit))
81 while (complete && complete->item->date >= commit->date) {
82 pop_most_recent_commit(&complete, COMPLETE);
85 if (commit->object.flags & COMPLETE)
88 hashcpy(current_commit_sha1, commit->object.sha1);
90 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
93 if (process(&commit->tree->object))
99 struct commit_list *parents = commit->parents;
100 for (; parents; parents = parents->next) {
101 if (process(&parents->item->object))
108 static int process_tag(struct tag *tag)
112 return process(tag->tagged);
115 static struct object_list *process_queue = NULL;
116 static struct object_list **process_queue_end = &process_queue;
118 static int process_object(struct object *obj)
120 if (obj->type == OBJ_COMMIT) {
121 if (process_commit((struct commit *)obj))
125 if (obj->type == OBJ_TREE) {
126 if (process_tree((struct tree *)obj))
130 if (obj->type == OBJ_BLOB) {
133 if (obj->type == OBJ_TAG) {
134 if (process_tag((struct tag *)obj))
138 return error("Unable to determine requirements "
140 typename(obj->type), sha1_to_hex(obj->sha1));
143 static int process(struct object *obj)
145 if (obj->flags & SEEN)
149 if (has_sha1_file(obj->sha1)) {
150 /* We already have it, so we should scan it now. */
151 obj->flags |= TO_SCAN;
154 if (obj->flags & COMPLETE)
159 object_list_insert(obj, process_queue_end);
160 process_queue_end = &(*process_queue_end)->next;
164 static int loop(void)
166 struct object_list *elem;
168 while (process_queue) {
169 struct object *obj = process_queue->item;
170 elem = process_queue;
171 process_queue = elem->next;
174 process_queue_end = &process_queue;
176 /* If we are not scanning this object, we placed it in
177 * the queue because we needed to fetch it first.
179 if (! (obj->flags & TO_SCAN)) {
180 if (fetch(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, int flag, void *cb_data)
207 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
209 commit->object.flags |= COMPLETE;
210 insert_by_date(commit, &complete);
215 int pull_targets_stdin(char ***target, const char ***write_ref)
217 int targets = 0, targets_alloc = 0;
219 *target = NULL; *write_ref = NULL;
220 strbuf_init(&buf, 0);
225 if (strbuf_getline(&buf, stdin, '\n') == EOF)
228 rf_one = strchr(tg_one, '\t');
232 if (targets >= targets_alloc) {
233 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
234 *target = xrealloc(*target, targets_alloc * sizeof(**target));
235 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
237 (*target)[targets] = xstrdup(tg_one);
238 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
241 strbuf_release(&buf);
245 void pull_targets_free(int targets, char **target, const char **write_ref)
248 free(target[targets]);
249 if (write_ref && write_ref[targets])
250 free((char *) write_ref[targets]);
254 int pull(int targets, char **target, const char **write_ref,
255 const char *write_ref_log_details)
257 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
258 unsigned char *sha1 = xmalloc(targets * 20);
263 save_commit_buffer = 0;
264 track_object_refs = 0;
266 for (i = 0; i < targets; i++) {
267 if (!write_ref || !write_ref[i])
270 lock[i] = lock_ref_sha1(write_ref[i], NULL);
272 error("Can't lock ref %s", write_ref[i]);
273 goto unlock_and_fail;
278 for_each_ref(mark_complete, NULL);
280 for (i = 0; i < targets; i++) {
281 if (interpret_target(target[i], &sha1[20 * i])) {
282 error("Could not interpret %s as something to pull", target[i]);
283 goto unlock_and_fail;
285 if (process(lookup_unknown_object(&sha1[20 * i])))
286 goto unlock_and_fail;
290 goto unlock_and_fail;
292 if (write_ref_log_details) {
293 msg = xmalloc(strlen(write_ref_log_details) + 12);
294 sprintf(msg, "fetch from %s", write_ref_log_details);
298 for (i = 0; i < targets; i++) {
299 if (!write_ref || !write_ref[i])
301 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
304 goto unlock_and_fail;
312 for (i = 0; i < targets; i++)