]>
Commit | Line | Data |
---|---|---|
215a7ad1 | 1 | #include "fetch.h" |
4250a5e5 DB |
2 | |
3 | #include "cache.h" | |
4 | #include "commit.h" | |
5 | #include "tree.h" | |
1bc995a3 | 6 | #include "tree-walk.h" |
3173bd49 DB |
7 | #include "tag.h" |
8 | #include "blob.h" | |
cd541a68 | 9 | #include "refs.h" |
8e87ca66 | 10 | #include "strbuf.h" |
cd541a68 | 11 | |
4250a5e5 DB |
12 | int get_tree = 0; |
13 | int get_history = 0; | |
14 | int get_all = 0; | |
e78d9772 | 15 | int get_verbosely = 0; |
820eca68 | 16 | int get_recover = 0; |
b2d62f16 | 17 | static unsigned char current_commit_sha1[20]; |
4250a5e5 | 18 | |
1e8be59d DB |
19 | void pull_say(const char *fmt, const char *hex) |
20 | { | |
e78d9772 JH |
21 | if (get_verbosely) |
22 | fprintf(stderr, fmt, hex); | |
23 | } | |
24 | ||
b2d62f16 JH |
25 | static void report_missing(const char *what, const unsigned char *missing) |
26 | { | |
27 | char missing_hex[41]; | |
28 | ||
29 | strcpy(missing_hex, sha1_to_hex(missing));; | |
30 | fprintf(stderr, | |
31 | "Cannot obtain needed %s %s\nwhile processing commit %s.\n", | |
32 | what, missing_hex, sha1_to_hex(current_commit_sha1)); | |
33 | } | |
34 | ||
80077f07 | 35 | static int process(struct object *obj); |
3173bd49 | 36 | |
1e8be59d | 37 | static int process_tree(struct tree *tree) |
4250a5e5 | 38 | { |
1bc995a3 | 39 | struct tree_desc desc; |
4c068a98 | 40 | struct name_entry entry; |
4250a5e5 DB |
41 | |
42 | if (parse_tree(tree)) | |
43 | return -1; | |
44 | ||
1bc995a3 LT |
45 | desc.buf = tree->buffer; |
46 | desc.size = tree->size; | |
4c068a98 | 47 | while (tree_entry(&desc, &entry)) { |
6f9012b6 JH |
48 | struct object *obj = NULL; |
49 | ||
4c068a98 LT |
50 | if (S_ISDIR(entry.mode)) { |
51 | struct tree *tree = lookup_tree(entry.sha1); | |
6f9012b6 JH |
52 | if (tree) |
53 | obj = &tree->object; | |
54 | } | |
55 | else { | |
4c068a98 | 56 | struct blob *blob = lookup_blob(entry.sha1); |
6f9012b6 JH |
57 | if (blob) |
58 | obj = &blob->object; | |
2d9c58c6 | 59 | } |
6f9012b6 | 60 | if (!obj || process(obj)) |
4250a5e5 | 61 | return -1; |
4250a5e5 | 62 | } |
2d9c58c6 LT |
63 | free(tree->buffer); |
64 | tree->buffer = NULL; | |
1bc995a3 | 65 | tree->size = 0; |
4250a5e5 DB |
66 | return 0; |
67 | } | |
68 | ||
24451c31 SV |
69 | #define COMPLETE (1U << 0) |
70 | #define SEEN (1U << 1) | |
71 | #define TO_SCAN (1U << 2) | |
85d106c2 | 72 | |
d0ac30f2 | 73 | static struct commit_list *complete = NULL; |
22c6e1d0 | 74 | |
1e8be59d | 75 | static int process_commit(struct commit *commit) |
4250a5e5 | 76 | { |
1e8be59d | 77 | if (parse_commit(commit)) |
4250a5e5 DB |
78 | return -1; |
79 | ||
22c6e1d0 | 80 | while (complete && complete->item->date >= commit->date) { |
d0ac30f2 | 81 | pop_most_recent_commit(&complete, COMPLETE); |
22c6e1d0 | 82 | } |
22c6e1d0 | 83 | |
d0ac30f2 | 84 | if (commit->object.flags & COMPLETE) |
22c6e1d0 DB |
85 | return 0; |
86 | ||
e702496e | 87 | hashcpy(current_commit_sha1, commit->object.sha1); |
4250a5e5 | 88 | |
85d106c2 JH |
89 | pull_say("walk %s\n", sha1_to_hex(commit->object.sha1)); |
90 | ||
4250a5e5 | 91 | if (get_tree) { |
80077f07 | 92 | if (process(&commit->tree->object)) |
4250a5e5 DB |
93 | return -1; |
94 | if (!get_all) | |
95 | get_tree = 0; | |
96 | } | |
97 | if (get_history) { | |
1e8be59d | 98 | struct commit_list *parents = commit->parents; |
4250a5e5 | 99 | for (; parents; parents = parents->next) { |
80077f07 | 100 | if (process(&parents->item->object)) |
4250a5e5 DB |
101 | return -1; |
102 | } | |
103 | } | |
104 | return 0; | |
105 | } | |
106 | ||
1e8be59d | 107 | static int process_tag(struct tag *tag) |
3173bd49 | 108 | { |
1e8be59d | 109 | if (parse_tag(tag)) |
3173bd49 | 110 | return -1; |
80077f07 | 111 | return process(tag->tagged); |
3173bd49 DB |
112 | } |
113 | ||
1e8be59d DB |
114 | static struct object_list *process_queue = NULL; |
115 | static struct object_list **process_queue_end = &process_queue; | |
116 | ||
f88fcf8b | 117 | static int process_object(struct object *obj) |
3173bd49 | 118 | { |
1974632c | 119 | if (obj->type == OBJ_COMMIT) { |
f88fcf8b DB |
120 | if (process_commit((struct commit *)obj)) |
121 | return -1; | |
122 | return 0; | |
123 | } | |
1974632c | 124 | if (obj->type == OBJ_TREE) { |
f88fcf8b DB |
125 | if (process_tree((struct tree *)obj)) |
126 | return -1; | |
127 | return 0; | |
128 | } | |
1974632c | 129 | if (obj->type == OBJ_BLOB) { |
f88fcf8b DB |
130 | return 0; |
131 | } | |
1974632c | 132 | if (obj->type == OBJ_TAG) { |
f88fcf8b DB |
133 | if (process_tag((struct tag *)obj)) |
134 | return -1; | |
3173bd49 | 135 | return 0; |
f88fcf8b DB |
136 | } |
137 | return error("Unable to determine requirements " | |
138 | "of type %s for %s", | |
885a86ab | 139 | typename(obj->type), sha1_to_hex(obj->sha1)); |
f88fcf8b DB |
140 | } |
141 | ||
80077f07 | 142 | static int process(struct object *obj) |
f88fcf8b | 143 | { |
a82d07e5 SV |
144 | if (obj->flags & SEEN) |
145 | return 0; | |
146 | obj->flags |= SEEN; | |
147 | ||
80077f07 | 148 | if (has_sha1_file(obj->sha1)) { |
f88fcf8b | 149 | /* We already have it, so we should scan it now. */ |
85d106c2 | 150 | obj->flags |= TO_SCAN; |
e5f38ec3 JH |
151 | } |
152 | else { | |
7b64d06b SV |
153 | if (obj->flags & COMPLETE) |
154 | return 0; | |
155 | prefetch(obj->sha1); | |
f88fcf8b | 156 | } |
7b64d06b | 157 | |
1e8be59d DB |
158 | object_list_insert(obj, process_queue_end); |
159 | process_queue_end = &(*process_queue_end)->next; | |
1e8be59d DB |
160 | return 0; |
161 | } | |
162 | ||
163 | static int loop(void) | |
164 | { | |
85d106c2 JH |
165 | struct object_list *elem; |
166 | ||
1e8be59d DB |
167 | while (process_queue) { |
168 | struct object *obj = process_queue->item; | |
85d106c2 JH |
169 | elem = process_queue; |
170 | process_queue = elem->next; | |
171 | free(elem); | |
1e8be59d DB |
172 | if (!process_queue) |
173 | process_queue_end = &process_queue; | |
174 | ||
85d106c2 JH |
175 | /* If we are not scanning this object, we placed it in |
176 | * the queue because we needed to fetch it first. | |
177 | */ | |
178 | if (! (obj->flags & TO_SCAN)) { | |
11f0dafe | 179 | if (fetch(obj->sha1)) { |
885a86ab | 180 | report_missing(typename(obj->type), obj->sha1); |
85d106c2 JH |
181 | return -1; |
182 | } | |
183 | } | |
1e8be59d DB |
184 | if (!obj->type) |
185 | parse_object(obj->sha1); | |
f88fcf8b DB |
186 | if (process_object(obj)) |
187 | return -1; | |
1e8be59d DB |
188 | } |
189 | return 0; | |
3173bd49 DB |
190 | } |
191 | ||
cd541a68 DB |
192 | static int interpret_target(char *target, unsigned char *sha1) |
193 | { | |
194 | if (!get_sha1_hex(target, sha1)) | |
195 | return 0; | |
196 | if (!check_ref_format(target)) { | |
197 | if (!fetch_ref(target, sha1)) { | |
198 | return 0; | |
199 | } | |
200 | } | |
201 | return -1; | |
202 | } | |
203 | ||
22c6e1d0 DB |
204 | static int mark_complete(const char *path, const unsigned char *sha1) |
205 | { | |
d0ac30f2 JH |
206 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); |
207 | if (commit) { | |
208 | commit->object.flags |= COMPLETE; | |
209 | insert_by_date(commit, &complete); | |
22c6e1d0 DB |
210 | } |
211 | return 0; | |
212 | } | |
cd541a68 | 213 | |
8e87ca66 PB |
214 | int pull_targets_stdin(char ***target, const char ***write_ref) |
215 | { | |
216 | int targets = 0, targets_alloc = 0; | |
217 | struct strbuf buf; | |
218 | *target = NULL; *write_ref = NULL; | |
219 | strbuf_init(&buf); | |
220 | while (1) { | |
221 | char *rf_one = NULL; | |
222 | char *tg_one; | |
223 | ||
224 | read_line(&buf, stdin, '\n'); | |
225 | if (buf.eof) | |
226 | break; | |
227 | tg_one = buf.buf; | |
228 | rf_one = strchr(tg_one, '\t'); | |
229 | if (rf_one) | |
230 | *rf_one++ = 0; | |
231 | ||
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)); | |
236 | } | |
9befac47 SP |
237 | (*target)[targets] = xstrdup(tg_one); |
238 | (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL; | |
8e87ca66 PB |
239 | targets++; |
240 | } | |
241 | return targets; | |
242 | } | |
243 | ||
244 | void pull_targets_free(int targets, char **target, const char **write_ref) | |
245 | { | |
246 | while (targets--) { | |
247 | free(target[targets]); | |
1b03dfed | 248 | if (write_ref && write_ref[targets]) |
8e87ca66 PB |
249 | free((char *) write_ref[targets]); |
250 | } | |
251 | } | |
252 | ||
4211e4d1 | 253 | int pull(int targets, char **target, const char **write_ref, |
c6b69bdb | 254 | const char *write_ref_log_details) |
4250a5e5 | 255 | { |
4211e4d1 PB |
256 | struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *)); |
257 | unsigned char *sha1 = xmalloc(targets * 20); | |
d0740d92 SP |
258 | char *msg; |
259 | int ret; | |
4211e4d1 | 260 | int i; |
cd541a68 | 261 | |
98533b90 | 262 | save_commit_buffer = 0; |
a95cb6fb | 263 | track_object_refs = 0; |
4211e4d1 PB |
264 | |
265 | for (i = 0; i < targets; i++) { | |
1b03dfed | 266 | if (!write_ref || !write_ref[i]) |
4211e4d1 PB |
267 | continue; |
268 | ||
269 | lock[i] = lock_ref_sha1(write_ref[i], NULL, 0); | |
270 | if (!lock[i]) { | |
271 | error("Can't lock ref %s", write_ref[i]); | |
272 | goto unlock_and_fail; | |
d0740d92 | 273 | } |
cd541a68 DB |
274 | } |
275 | ||
84c667ff | 276 | if (!get_recover) |
820eca68 | 277 | for_each_ref(mark_complete); |
22c6e1d0 | 278 | |
4211e4d1 PB |
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; | |
283 | } | |
284 | if (process(lookup_unknown_object(&sha1[20 * i]))) | |
285 | goto unlock_and_fail; | |
4bd18c43 | 286 | } |
4211e4d1 PB |
287 | |
288 | if (loop()) | |
289 | goto unlock_and_fail; | |
290 | ||
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); | |
294 | } else { | |
295 | msg = NULL; | |
4bd18c43 | 296 | } |
4211e4d1 | 297 | for (i = 0; i < targets; i++) { |
1b03dfed | 298 | if (!write_ref || !write_ref[i]) |
4211e4d1 PB |
299 | continue; |
300 | ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)"); | |
301 | lock[i] = NULL; | |
302 | if (ret) | |
303 | goto unlock_and_fail; | |
4bd18c43 | 304 | } |
4cac42b1 | 305 | free(msg); |
4bd18c43 | 306 | |
cd541a68 | 307 | return 0; |
4211e4d1 PB |
308 | |
309 | ||
310 | unlock_and_fail: | |
311 | for (i = 0; i < targets; i++) | |
312 | if (lock[i]) | |
313 | unlock_ref(lock[i]); | |
314 | return -1; | |
4250a5e5 | 315 | } |