Typefix builtin-prune.c::prune_object()
[git] / fetch.c
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
12 int get_tree = 0;
13 int get_history = 0;
14 int get_all = 0;
15 int get_verbosely = 0;
16 int get_recover = 0;
17 static unsigned char current_commit_sha1[20];
18
19 void pull_say(const char *fmt, const char *hex) 
20 {
21         if (get_verbosely)
22                 fprintf(stderr, fmt, hex);
23 }
24
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
35 static int process(struct object *obj);
36
37 static int process_tree(struct tree *tree)
38 {
39         struct tree_desc desc;
40         struct name_entry entry;
41
42         if (parse_tree(tree))
43                 return -1;
44
45         desc.buf = tree->buffer;
46         desc.size = tree->size;
47         while (tree_entry(&desc, &entry)) {
48                 struct object *obj = NULL;
49
50                 if (S_ISDIR(entry.mode)) {
51                         struct tree *tree = lookup_tree(entry.sha1);
52                         if (tree)
53                                 obj = &tree->object;
54                 }
55                 else {
56                         struct blob *blob = lookup_blob(entry.sha1);
57                         if (blob)
58                                 obj = &blob->object;
59                 }
60                 if (!obj || process(obj))
61                         return -1;
62         }
63         free(tree->buffer);
64         tree->buffer = NULL;
65         tree->size = 0;
66         return 0;
67 }
68
69 #define COMPLETE        (1U << 0)
70 #define SEEN            (1U << 1)
71 #define TO_SCAN         (1U << 2)
72
73 static struct commit_list *complete = NULL;
74
75 static int process_commit(struct commit *commit)
76 {
77         if (parse_commit(commit))
78                 return -1;
79
80         while (complete && complete->item->date >= commit->date) {
81                 pop_most_recent_commit(&complete, COMPLETE);
82         }
83
84         if (commit->object.flags & COMPLETE)
85                 return 0;
86
87         hashcpy(current_commit_sha1, commit->object.sha1);
88
89         pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
90
91         if (get_tree) {
92                 if (process(&commit->tree->object))
93                         return -1;
94                 if (!get_all)
95                         get_tree = 0;
96         }
97         if (get_history) {
98                 struct commit_list *parents = commit->parents;
99                 for (; parents; parents = parents->next) {
100                         if (process(&parents->item->object))
101                                 return -1;
102                 }
103         }
104         return 0;
105 }
106
107 static int process_tag(struct tag *tag)
108 {
109         if (parse_tag(tag))
110                 return -1;
111         return process(tag->tagged);
112 }
113
114 static struct object_list *process_queue = NULL;
115 static struct object_list **process_queue_end = &process_queue;
116
117 static int process_object(struct object *obj)
118 {
119         if (obj->type == OBJ_COMMIT) {
120                 if (process_commit((struct commit *)obj))
121                         return -1;
122                 return 0;
123         }
124         if (obj->type == OBJ_TREE) {
125                 if (process_tree((struct tree *)obj))
126                         return -1;
127                 return 0;
128         }
129         if (obj->type == OBJ_BLOB) {
130                 return 0;
131         }
132         if (obj->type == OBJ_TAG) {
133                 if (process_tag((struct tag *)obj))
134                         return -1;
135                 return 0;
136         }
137         return error("Unable to determine requirements "
138                      "of type %s for %s",
139                      typename(obj->type), sha1_to_hex(obj->sha1));
140 }
141
142 static int process(struct object *obj)
143 {
144         if (obj->flags & SEEN)
145                 return 0;
146         obj->flags |= SEEN;
147
148         if (has_sha1_file(obj->sha1)) {
149                 /* We already have it, so we should scan it now. */
150                 obj->flags |= TO_SCAN;
151         }
152         else {
153                 if (obj->flags & COMPLETE)
154                         return 0;
155                 prefetch(obj->sha1);
156         }
157                 
158         object_list_insert(obj, process_queue_end);
159         process_queue_end = &(*process_queue_end)->next;
160         return 0;
161 }
162
163 static int loop(void)
164 {
165         struct object_list *elem;
166
167         while (process_queue) {
168                 struct object *obj = process_queue->item;
169                 elem = process_queue;
170                 process_queue = elem->next;
171                 free(elem);
172                 if (!process_queue)
173                         process_queue_end = &process_queue;
174
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)) {
179                         if (fetch(obj->sha1)) {
180                                 report_missing(typename(obj->type), obj->sha1);
181                                 return -1;
182                         }
183                 }
184                 if (!obj->type)
185                         parse_object(obj->sha1);
186                 if (process_object(obj))
187                         return -1;
188         }
189         return 0;
190 }
191
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
204 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
205 {
206         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
207         if (commit) {
208                 commit->object.flags |= COMPLETE;
209                 insert_by_date(commit, &complete);
210         }
211         return 0;
212 }
213
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                 }
237                 (*target)[targets] = xstrdup(tg_one);
238                 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
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]);
248                 if (write_ref && write_ref[targets])
249                         free((char *) write_ref[targets]);
250         }
251 }
252
253 int pull(int targets, char **target, const char **write_ref,
254          const char *write_ref_log_details)
255 {
256         struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
257         unsigned char *sha1 = xmalloc(targets * 20);
258         char *msg;
259         int ret;
260         int i;
261
262         save_commit_buffer = 0;
263         track_object_refs = 0;
264
265         for (i = 0; i < targets; i++) {
266                 if (!write_ref || !write_ref[i])
267                         continue;
268
269                 lock[i] = lock_ref_sha1(write_ref[i], NULL);
270                 if (!lock[i]) {
271                         error("Can't lock ref %s", write_ref[i]);
272                         goto unlock_and_fail;
273                 }
274         }
275
276         if (!get_recover)
277                 for_each_ref(mark_complete, NULL);
278
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;
286         }
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;
296         }
297         for (i = 0; i < targets; i++) {
298                 if (!write_ref || !write_ref[i])
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;
304         }
305         free(msg);
306
307         return 0;
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;
315 }