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