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
11const char *write_ref = NULL;
12const char *write_ref_log_details = NULL;
13
14int get_tree = 0;
15int get_history = 0;
16int get_all = 0;
17int get_verbosely = 0;
18int get_recover = 0;
19static unsigned char current_commit_sha1[20];
20
21void pull_say(const char *fmt, const char *hex)
22{
23 if (get_verbosely)
24 fprintf(stderr, fmt, hex);
25}
26
27static void report_missing(const char *what, const unsigned char *missing)
28{
29 char missing_hex[41];
30
31 strcpy(missing_hex, sha1_to_hex(missing));;
32 fprintf(stderr,
33 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
34 what, missing_hex, sha1_to_hex(current_commit_sha1));
35}
36
37static int process(struct object *obj);
38
39static int process_tree(struct tree *tree)
40{
41 struct tree_desc desc;
42 struct name_entry entry;
43
44 if (parse_tree(tree))
45 return -1;
46
47 desc.buf = tree->buffer;
48 desc.size = tree->size;
49 while (tree_entry(&desc, &entry)) {
50 struct object *obj = NULL;
51
52 if (S_ISDIR(entry.mode)) {
53 struct tree *tree = lookup_tree(entry.sha1);
54 if (tree)
55 obj = &tree->object;
56 }
57 else {
58 struct blob *blob = lookup_blob(entry.sha1);
59 if (blob)
60 obj = &blob->object;
61 }
62 if (!obj || process(obj))
63 return -1;
64 }
65 free(tree->buffer);
66 tree->buffer = NULL;
67 tree->size = 0;
68 return 0;
69}
70
71#define COMPLETE (1U << 0)
72#define SEEN (1U << 1)
73#define TO_SCAN (1U << 2)
74
75static struct commit_list *complete = NULL;
76
77static int process_commit(struct commit *commit)
78{
79 if (parse_commit(commit))
80 return -1;
81
82 while (complete && complete->item->date >= commit->date) {
83 pop_most_recent_commit(&complete, COMPLETE);
84 }
85
86 if (commit->object.flags & COMPLETE)
87 return 0;
88
89 memcpy(current_commit_sha1, commit->object.sha1, 20);
90
91 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
92
93 if (get_tree) {
94 if (process(&commit->tree->object))
95 return -1;
96 if (!get_all)
97 get_tree = 0;
98 }
99 if (get_history) {
100 struct commit_list *parents = commit->parents;
101 for (; parents; parents = parents->next) {
102 if (process(&parents->item->object))
103 return -1;
104 }
105 }
106 return 0;
107}
108
109static int process_tag(struct tag *tag)
110{
111 if (parse_tag(tag))
112 return -1;
113 return process(tag->tagged);
114}
115
116static struct object_list *process_queue = NULL;
117static struct object_list **process_queue_end = &process_queue;
118
119static int process_object(struct object *obj)
120{
121 if (obj->type == OBJ_COMMIT) {
122 if (process_commit((struct commit *)obj))
123 return -1;
124 return 0;
125 }
126 if (obj->type == OBJ_TREE) {
127 if (process_tree((struct tree *)obj))
128 return -1;
129 return 0;
130 }
131 if (obj->type == OBJ_BLOB) {
132 return 0;
133 }
134 if (obj->type == OBJ_TAG) {
135 if (process_tag((struct tag *)obj))
136 return -1;
137 return 0;
138 }
139 return error("Unable to determine requirements "
140 "of type %s for %s",
141 typename(obj->type), sha1_to_hex(obj->sha1));
142}
143
144static int process(struct object *obj)
145{
146 if (obj->flags & SEEN)
147 return 0;
148 obj->flags |= SEEN;
149
150 if (has_sha1_file(obj->sha1)) {
151 /* We already have it, so we should scan it now. */
152 obj->flags |= TO_SCAN;
153 }
154 else {
155 if (obj->flags & COMPLETE)
156 return 0;
157 prefetch(obj->sha1);
158 }
159
160 object_list_insert(obj, process_queue_end);
161 process_queue_end = &(*process_queue_end)->next;
162 return 0;
163}
164
165static int loop(void)
166{
167 struct object_list *elem;
168
169 while (process_queue) {
170 struct object *obj = process_queue->item;
171 elem = process_queue;
172 process_queue = elem->next;
173 free(elem);
174 if (!process_queue)
175 process_queue_end = &process_queue;
176
177 /* If we are not scanning this object, we placed it in
178 * the queue because we needed to fetch it first.
179 */
180 if (! (obj->flags & TO_SCAN)) {
181 if (fetch(obj->sha1)) {
182 report_missing(typename(obj->type), obj->sha1);
183 return -1;
184 }
185 }
186 if (!obj->type)
187 parse_object(obj->sha1);
188 if (process_object(obj))
189 return -1;
190 }
191 return 0;
192}
193
194static int interpret_target(char *target, unsigned char *sha1)
195{
196 if (!get_sha1_hex(target, sha1))
197 return 0;
198 if (!check_ref_format(target)) {
199 if (!fetch_ref(target, sha1)) {
200 return 0;
201 }
202 }
203 return -1;
204}
205
206static int mark_complete(const char *path, const unsigned char *sha1)
207{
208 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
209 if (commit) {
210 commit->object.flags |= COMPLETE;
211 insert_by_date(commit, &complete);
212 }
213 return 0;
214}
215
216int pull(char *target)
217{
218 struct ref_lock *lock = NULL;
219 unsigned char sha1[20];
220 char *msg;
221 int ret;
222
223 save_commit_buffer = 0;
224 track_object_refs = 0;
225 if (write_ref) {
226 lock = lock_ref_sha1(write_ref, NULL, 0);
227 if (!lock) {
228 error("Can't lock ref %s", write_ref);
229 return -1;
230 }
231 }
232
233 if (!get_recover)
234 for_each_ref(mark_complete);
235
236 if (interpret_target(target, sha1)) {
237 error("Could not interpret %s as something to pull", target);
238 if (lock)
239 unlock_ref(lock);
240 return -1;
241 }
242 if (process(lookup_unknown_object(sha1))) {
243 if (lock)
244 unlock_ref(lock);
245 return -1;
246 }
247 if (loop()) {
248 if (lock)
249 unlock_ref(lock);
250 return -1;
251 }
252
253 if (write_ref) {
254 if (write_ref_log_details) {
255 msg = xmalloc(strlen(write_ref_log_details) + 12);
256 sprintf(msg, "fetch from %s", write_ref_log_details);
257 }
258 else
259 msg = NULL;
260 ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)");
261 if (msg)
262 free(msg);
263 return ret;
264 }
265 return 0;
266}