1#include "fetch.h"
2
3#include "cache.h"
4#include "commit.h"
5#include "tree.h"
6#include "tree-walk.h"
7#include "tag.h"
8#include "blob.h"
9#include "refs.h"
10#include "strbuf.h"
11
12int get_tree = 0;
13int get_history = 0;
14int get_all = 0;
15int get_verbosely = 0;
16int get_recover = 0;
17static unsigned char current_commit_sha1[20];
18
19void pull_say(const char *fmt, const char *hex)
20{
21 if (get_verbosely)
22 fprintf(stderr, fmt, hex);
23}
24
25static void report_missing(const struct object *obj)
26{
27 char missing_hex[41];
28 strcpy(missing_hex, sha1_to_hex(obj->sha1));;
29 fprintf(stderr, "Cannot obtain needed %s %s\n",
30 obj->type ? typename(obj->type): "object", missing_hex);
31 if (!is_null_sha1(current_commit_sha1))
32 fprintf(stderr, "while processing commit %s.\n",
33 sha1_to_hex(current_commit_sha1));
34}
35
36static int process(struct object *obj);
37
38static int process_tree(struct tree *tree)
39{
40 struct tree_desc desc;
41 struct name_entry entry;
42
43 if (parse_tree(tree))
44 return -1;
45
46 desc.buf = tree->buffer;
47 desc.size = tree->size;
48 while (tree_entry(&desc, &entry)) {
49 struct object *obj = NULL;
50
51 if (S_ISDIR(entry.mode)) {
52 struct tree *tree = lookup_tree(entry.sha1);
53 if (tree)
54 obj = &tree->object;
55 }
56 else {
57 struct blob *blob = lookup_blob(entry.sha1);
58 if (blob)
59 obj = &blob->object;
60 }
61 if (!obj || process(obj))
62 return -1;
63 }
64 free(tree->buffer);
65 tree->buffer = NULL;
66 tree->size = 0;
67 return 0;
68}
69
70#define COMPLETE (1U << 0)
71#define SEEN (1U << 1)
72#define TO_SCAN (1U << 2)
73
74static struct commit_list *complete = NULL;
75
76static int process_commit(struct commit *commit)
77{
78 if (parse_commit(commit))
79 return -1;
80
81 while (complete && complete->item->date >= commit->date) {
82 pop_most_recent_commit(&complete, COMPLETE);
83 }
84
85 if (commit->object.flags & COMPLETE)
86 return 0;
87
88 hashcpy(current_commit_sha1, commit->object.sha1);
89
90 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
91
92 if (get_tree) {
93 if (process(&commit->tree->object))
94 return -1;
95 if (!get_all)
96 get_tree = 0;
97 }
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;
103 }
104 }
105 return 0;
106}
107
108static int process_tag(struct tag *tag)
109{
110 if (parse_tag(tag))
111 return -1;
112 return process(tag->tagged);
113}
114
115static struct object_list *process_queue = NULL;
116static struct object_list **process_queue_end = &process_queue;
117
118static int process_object(struct object *obj)
119{
120 if (obj->type == OBJ_COMMIT) {
121 if (process_commit((struct commit *)obj))
122 return -1;
123 return 0;
124 }
125 if (obj->type == OBJ_TREE) {
126 if (process_tree((struct tree *)obj))
127 return -1;
128 return 0;
129 }
130 if (obj->type == OBJ_BLOB) {
131 return 0;
132 }
133 if (obj->type == OBJ_TAG) {
134 if (process_tag((struct tag *)obj))
135 return -1;
136 return 0;
137 }
138 return error("Unable to determine requirements "
139 "of type %s for %s",
140 typename(obj->type), sha1_to_hex(obj->sha1));
141}
142
143static int process(struct object *obj)
144{
145 if (obj->flags & SEEN)
146 return 0;
147 obj->flags |= SEEN;
148
149 if (has_sha1_file(obj->sha1)) {
150 /* We already have it, so we should scan it now. */
151 obj->flags |= TO_SCAN;
152 }
153 else {
154 if (obj->flags & COMPLETE)
155 return 0;
156 prefetch(obj->sha1);
157 }
158
159 object_list_insert(obj, process_queue_end);
160 process_queue_end = &(*process_queue_end)->next;
161 return 0;
162}
163
164static int loop(void)
165{
166 struct object_list *elem;
167
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;
175
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)) {
180 if (fetch(obj->sha1)) {
181 report_missing(obj);
182 return -1;
183 }
184 }
185 if (!obj->type)
186 parse_object(obj->sha1);
187 if (process_object(obj))
188 return -1;
189 }
190 return 0;
191}
192
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
205static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
206{
207 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
208 if (commit) {
209 commit->object.flags |= COMPLETE;
210 insert_by_date(commit, &complete);
211 }
212 return 0;
213}
214
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;
220 strbuf_init(&buf);
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 }
238 (*target)[targets] = xstrdup(tg_one);
239 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
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]);
249 if (write_ref && write_ref[targets])
250 free((char *) write_ref[targets]);
251 }
252}
253
254int pull(int targets, char **target, const char **write_ref,
255 const char *write_ref_log_details)
256{
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;
262
263 save_commit_buffer = 0;
264 track_object_refs = 0;
265
266 for (i = 0; i < targets; i++) {
267 if (!write_ref || !write_ref[i])
268 continue;
269
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;
274 }
275 }
276
277 if (!get_recover)
278 for_each_ref(mark_complete, NULL);
279
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;
287 }
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;
297 }
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;
305 }
306 free(msg);
307
308 return 0;
309
310
311unlock_and_fail:
312 for (i = 0; i < targets; i++)
313 if (lock[i])
314 unlock_ref(lock[i]);
315 return -1;
316}