Merge branch 'jc/lt-tree-n-cache-tree' into lt/tree-2
[git.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
11 const char *write_ref = NULL;
12
13 int get_tree = 0;
14 int get_history = 0;
15 int get_all = 0;
16 int get_verbosely = 0;
17 int get_recover = 0;
18 static unsigned char current_commit_sha1[20];
19
20 void pull_say(const char *fmt, const char *hex) 
21 {
22         if (get_verbosely)
23                 fprintf(stderr, fmt, hex);
24 }
25
26 static void report_missing(const char *what, const unsigned char *missing)
27 {
28         char missing_hex[41];
29
30         strcpy(missing_hex, sha1_to_hex(missing));;
31         fprintf(stderr,
32                 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
33                 what, missing_hex, sha1_to_hex(current_commit_sha1));
34 }
35
36 static int process(struct object *obj);
37
38 static int process_tree(struct tree *tree)
39 {
40         struct tree_desc desc;
41
42         if (parse_tree(tree))
43                 return -1;
44
45         desc.buf = tree->buffer;
46         desc.size = tree->size;
47         while (desc.size) {
48                 unsigned mode;
49                 const char *name;
50                 const unsigned char *sha1;
51
52                 sha1 = tree_entry_extract(&desc, &name, &mode);
53                 update_tree_entry(&desc);
54
55                 if (S_ISDIR(mode)) {
56                         struct tree *tree = lookup_tree(sha1);
57                         process_tree(tree);
58                 } else {
59                         struct blob *blob = lookup_blob(sha1);
60                         process(&blob->object);
61                 }
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         memcpy(current_commit_sha1, commit->object.sha1, 20);
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 == commit_type) {
120                 if (process_commit((struct commit *)obj))
121                         return -1;
122                 return 0;
123         }
124         if (obj->type == tree_type) {
125                 if (process_tree((struct tree *)obj))
126                         return -1;
127                 return 0;
128         }
129         if (obj->type == blob_type) {
130                 return 0;
131         }
132         if (obj->type == tag_type) {
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                      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         } else {
152                 if (obj->flags & COMPLETE)
153                         return 0;
154                 prefetch(obj->sha1);
155         }
156                 
157         object_list_insert(obj, process_queue_end);
158         process_queue_end = &(*process_queue_end)->next;
159         return 0;
160 }
161
162 static int loop(void)
163 {
164         struct object_list *elem;
165
166         while (process_queue) {
167                 struct object *obj = process_queue->item;
168                 elem = process_queue;
169                 process_queue = elem->next;
170                 free(elem);
171                 if (!process_queue)
172                         process_queue_end = &process_queue;
173
174                 /* If we are not scanning this object, we placed it in
175                  * the queue because we needed to fetch it first.
176                  */
177                 if (! (obj->flags & TO_SCAN)) {
178                         if (fetch(obj->sha1)) {
179                                 report_missing(obj->type
180                                                ? obj->type
181                                                : "object", obj->sha1);
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)
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(char *target)
216 {
217         unsigned char sha1[20];
218
219         save_commit_buffer = 0;
220         track_object_refs = 0;
221
222         if (!get_recover)
223                 for_each_ref(mark_complete);
224
225         if (interpret_target(target, sha1))
226                 return error("Could not interpret %s as something to pull",
227                              target);
228         if (process(lookup_unknown_object(sha1)))
229                 return -1;
230         if (loop())
231                 return -1;
232         
233         if (write_ref)
234                 write_ref_sha1_unlocked(write_ref, sha1);
235         return 0;
236 }