Drop strbuf's 'eof' marker, and make read_line a first class citizen.
[git] / fetch.c
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"
9
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];
16
17 void pull_say(const char *fmt, const char *hex)
18 {
19         if (get_verbosely)
20                 fprintf(stderr, fmt, hex);
21 }
22
23 static void report_missing(const struct object *obj)
24 {
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));
32 }
33
34 static int process(struct object *obj);
35
36 static int process_tree(struct tree *tree)
37 {
38         struct tree_desc desc;
39         struct name_entry entry;
40
41         if (parse_tree(tree))
42                 return -1;
43
44         init_tree_desc(&desc, tree->buffer, tree->size);
45         while (tree_entry(&desc, &entry)) {
46                 struct object *obj = NULL;
47
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;
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
74 static struct commit_list *complete = NULL;
75
76 static 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
108 static int process_tag(struct tag *tag)
109 {
110         if (parse_tag(tag))
111                 return -1;
112         return process(tag->tagged);
113 }
114
115 static struct object_list *process_queue = NULL;
116 static struct object_list **process_queue_end = &process_queue;
117
118 static 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
143 static 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
164 static 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
193 static 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
205 static 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
215 int 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, 0);
221         while (1) {
222                 char *rf_one = NULL;
223                 char *tg_one;
224
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;
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                 }
237                 (*target)[targets] = xstrdup(tg_one);
238                 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
239                 targets++;
240         }
241         strbuf_release(&buf);
242         return targets;
243 }
244
245 void 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
254 int 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
311 unlock_and_fail:
312         for (i = 0; i < targets; i++)
313                 if (lock[i])
314                         unlock_ref(lock[i]);
315         return -1;
316 }