15 int get_verbosely
= 0;
17 static unsigned char current_commit_sha1
[20];
19 void pull_say(const char *fmt
, const char *hex
)
22 fprintf(stderr
, fmt
, hex
);
25 static void report_missing(const char *what
, const unsigned char *missing
)
29 strcpy(missing_hex
, sha1_to_hex(missing
));;
31 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
32 what
, missing_hex
, sha1_to_hex(current_commit_sha1
));
35 static int process(struct object
*obj
);
37 static int process_tree(struct tree
*tree
)
39 struct tree_desc desc
;
40 struct name_entry entry
;
45 desc
.buf
= tree
->buffer
;
46 desc
.size
= tree
->size
;
47 while (tree_entry(&desc
, &entry
)) {
48 struct object
*obj
= NULL
;
50 if (S_ISDIR(entry
.mode
)) {
51 struct tree
*tree
= lookup_tree(entry
.sha1
);
56 struct blob
*blob
= lookup_blob(entry
.sha1
);
60 if (!obj
|| process(obj
))
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
== OBJ_COMMIT
) {
120 if (process_commit((struct commit
*)obj
))
124 if (obj
->type
== OBJ_TREE
) {
125 if (process_tree((struct tree
*)obj
))
129 if (obj
->type
== OBJ_BLOB
) {
132 if (obj
->type
== OBJ_TAG
) {
133 if (process_tag((struct tag
*)obj
))
137 return error("Unable to determine requirements "
139 typename(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
;
153 if (obj
->flags
& COMPLETE
)
158 object_list_insert(obj
, process_queue_end
);
159 process_queue_end
= &(*process_queue_end
)->next
;
163 static int loop(void)
165 struct object_list
*elem
;
167 while (process_queue
) {
168 struct object
*obj
= process_queue
->item
;
169 elem
= process_queue
;
170 process_queue
= elem
->next
;
173 process_queue_end
= &process_queue
;
175 /* If we are not scanning this object, we placed it in
176 * the queue because we needed to fetch it first.
178 if (! (obj
->flags
& TO_SCAN
)) {
179 if (fetch(obj
->sha1
)) {
180 report_missing(typename(obj
->type
), obj
->sha1
);
185 parse_object(obj
->sha1
);
186 if (process_object(obj
))
192 static int interpret_target(char *target
, unsigned char *sha1
)
194 if (!get_sha1_hex(target
, sha1
))
196 if (!check_ref_format(target
)) {
197 if (!fetch_ref(target
, sha1
)) {
204 static int mark_complete(const char *path
, const unsigned char *sha1
)
206 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
208 commit
->object
.flags
|= COMPLETE
;
209 insert_by_date(commit
, &complete
);
214 int pull_targets_stdin(char ***target
, const char ***write_ref
)
216 int targets
= 0, targets_alloc
= 0;
218 *target
= NULL
; *write_ref
= NULL
;
224 read_line(&buf
, stdin
, '\n');
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
] = strdup(tg_one
);
238 (*write_ref
)[targets
] = rf_one
? strdup(rf_one
) : NULL
;
244 void pull_targets_free(int targets
, char **target
, const char **write_ref
)
247 free(target
[targets
]);
248 if (write_ref
&& write_ref
[targets
])
249 free((char *) write_ref
[targets
]);
253 int pull(int targets
, char **target
, const char **write_ref
,
254 const char *write_ref_log_details
)
256 struct ref_lock
**lock
= xcalloc(targets
, sizeof(struct ref_lock
*));
257 unsigned char *sha1
= xmalloc(targets
* 20);
262 save_commit_buffer
= 0;
263 track_object_refs
= 0;
265 for (i
= 0; i
< targets
; i
++) {
266 if (!write_ref
|| !write_ref
[i
])
269 lock
[i
] = lock_ref_sha1(write_ref
[i
], NULL
, 0);
271 error("Can't lock ref %s", write_ref
[i
]);
272 goto unlock_and_fail
;
277 for_each_ref(mark_complete
);
279 for (i
= 0; i
< targets
; i
++) {
280 if (interpret_target(target
[i
], &sha1
[20 * i
])) {
281 error("Could not interpret %s as something to pull", target
[i
]);
282 goto unlock_and_fail
;
284 if (process(lookup_unknown_object(&sha1
[20 * i
])))
285 goto unlock_and_fail
;
289 goto unlock_and_fail
;
291 if (write_ref_log_details
) {
292 msg
= xmalloc(strlen(write_ref_log_details
) + 12);
293 sprintf(msg
, "fetch from %s", write_ref_log_details
);
297 for (i
= 0; i
< targets
; i
++) {
298 if (!write_ref
|| !write_ref
[i
])
300 ret
= write_ref_sha1(lock
[i
], &sha1
[20 * i
], msg
? msg
: "fetch (unknown)");
303 goto unlock_and_fail
;
312 for (i
= 0; i
< targets
; i
++)