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