Fix some typos, punctuation, missing words, minor markup.
[git/mingw/4msysgit.git] / fetch.c
blobb1c1f07b2a91b6abebe29ceb1d5f2f7afe2e452e
1 #include "cache.h"
2 #include "fetch.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
10 int get_tree = 0;
11 int get_history = 0;
12 int get_all = 0;
13 int get_verbosely = 0;
14 int get_recover = 0;
15 static unsigned char current_commit_sha1[20];
17 void pull_say(const char *fmt, const char *hex)
19 if (get_verbosely)
20 fprintf(stderr, fmt, hex);
23 static void report_missing(const struct object *obj)
25 char missing_hex[41];
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;
41 if (parse_tree(tree))
42 return -1;
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))
50 continue;
51 if (S_ISDIR(entry.mode)) {
52 struct tree *tree = lookup_tree(entry.sha1);
53 if (tree)
54 obj = &tree->object;
56 else {
57 struct blob *blob = lookup_blob(entry.sha1);
58 if (blob)
59 obj = &blob->object;
61 if (!obj || process(obj))
62 return -1;
64 free(tree->buffer);
65 tree->buffer = NULL;
66 tree->size = 0;
67 return 0;
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))
79 return -1;
81 while (complete && complete->item->date >= commit->date) {
82 pop_most_recent_commit(&complete, COMPLETE);
85 if (commit->object.flags & COMPLETE)
86 return 0;
88 hashcpy(current_commit_sha1, commit->object.sha1);
90 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
92 if (get_tree) {
93 if (process(&commit->tree->object))
94 return -1;
95 if (!get_all)
96 get_tree = 0;
98 if (get_history) {
99 struct commit_list *parents = commit->parents;
100 for (; parents; parents = parents->next) {
101 if (process(&parents->item->object))
102 return -1;
105 return 0;
108 static int process_tag(struct tag *tag)
110 if (parse_tag(tag))
111 return -1;
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))
122 return -1;
123 return 0;
125 if (obj->type == OBJ_TREE) {
126 if (process_tree((struct tree *)obj))
127 return -1;
128 return 0;
130 if (obj->type == OBJ_BLOB) {
131 return 0;
133 if (obj->type == OBJ_TAG) {
134 if (process_tag((struct tag *)obj))
135 return -1;
136 return 0;
138 return error("Unable to determine requirements "
139 "of type %s for %s",
140 typename(obj->type), sha1_to_hex(obj->sha1));
143 static int process(struct object *obj)
145 if (obj->flags & SEEN)
146 return 0;
147 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;
153 else {
154 if (obj->flags & COMPLETE)
155 return 0;
156 prefetch(obj->sha1);
159 object_list_insert(obj, process_queue_end);
160 process_queue_end = &(*process_queue_end)->next;
161 return 0;
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;
172 free(elem);
173 if (!process_queue)
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)) {
181 report_missing(obj);
182 return -1;
185 if (!obj->type)
186 parse_object(obj->sha1);
187 if (process_object(obj))
188 return -1;
190 return 0;
193 static int interpret_target(char *target, unsigned char *sha1)
195 if (!get_sha1_hex(target, sha1))
196 return 0;
197 if (!check_ref_format(target)) {
198 if (!fetch_ref(target, sha1)) {
199 return 0;
202 return -1;
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);
208 if (commit) {
209 commit->object.flags |= COMPLETE;
210 insert_by_date(commit, &complete);
212 return 0;
215 int pull_targets_stdin(char ***target, const char ***write_ref)
217 int targets = 0, targets_alloc = 0;
218 struct strbuf buf;
219 *target = NULL; *write_ref = NULL;
220 strbuf_init(&buf, 0);
221 while (1) {
222 char *rf_one = NULL;
223 char *tg_one;
225 if (strbuf_getline(&buf, stdin, '\n') == EOF)
226 break;
227 tg_one = buf.buf;
228 rf_one = strchr(tg_one, '\t');
229 if (rf_one)
230 *rf_one++ = 0;
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;
239 targets++;
241 strbuf_release(&buf);
242 return targets;
245 void pull_targets_free(int targets, char **target, const char **write_ref)
247 while (targets--) {
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);
259 char *msg;
260 int ret;
261 int i;
263 save_commit_buffer = 0;
264 track_object_refs = 0;
266 for (i = 0; i < targets; i++) {
267 if (!write_ref || !write_ref[i])
268 continue;
270 lock[i] = lock_ref_sha1(write_ref[i], NULL);
271 if (!lock[i]) {
272 error("Can't lock ref %s", write_ref[i]);
273 goto unlock_and_fail;
277 if (!get_recover)
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;
289 if (loop())
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);
295 } else {
296 msg = NULL;
298 for (i = 0; i < targets; i++) {
299 if (!write_ref || !write_ref[i])
300 continue;
301 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
302 lock[i] = NULL;
303 if (ret)
304 goto unlock_and_fail;
306 free(msg);
308 return 0;
311 unlock_and_fail:
312 for (i = 0; i < targets; i++)
313 if (lock[i])
314 unlock_ref(lock[i]);
315 return -1;