git-format-patch -3
[git/jnareb-git/bp-gitweb.git] / fetch.c
blobf69be82f10d287d71f6184c4b9203bdab3ce81fc
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 #include "strbuf.h"
11 int get_tree = 0;
12 int get_history = 0;
13 int get_all = 0;
14 int get_verbosely = 0;
15 int get_recover = 0;
16 static unsigned char current_commit_sha1[20];
18 void pull_say(const char *fmt, const char *hex)
20 if (get_verbosely)
21 fprintf(stderr, fmt, hex);
24 static void report_missing(const struct object *obj)
26 char missing_hex[41];
27 strcpy(missing_hex, sha1_to_hex(obj->sha1));;
28 fprintf(stderr, "Cannot obtain needed %s %s\n",
29 obj->type ? typename(obj->type): "object", missing_hex);
30 if (!is_null_sha1(current_commit_sha1))
31 fprintf(stderr, "while processing commit %s.\n",
32 sha1_to_hex(current_commit_sha1));
35 static int process(struct object *obj);
37 static int process_tree(struct tree *tree)
39 struct tree_desc desc;
40 struct name_entry entry;
42 if (parse_tree(tree))
43 return -1;
45 desc.buf = tree->buffer;
46 desc.size = tree->size;
47 while (tree_entry(&desc, &entry)) {
48 struct object *obj = NULL;
50 if (S_ISDIR(entry.mode)) {
51 struct tree *tree = lookup_tree(entry.sha1);
52 if (tree)
53 obj = &tree->object;
55 else {
56 struct blob *blob = lookup_blob(entry.sha1);
57 if (blob)
58 obj = &blob->object;
60 if (!obj || process(obj))
61 return -1;
63 free(tree->buffer);
64 tree->buffer = NULL;
65 tree->size = 0;
66 return 0;
69 #define COMPLETE (1U << 0)
70 #define SEEN (1U << 1)
71 #define TO_SCAN (1U << 2)
73 static struct commit_list *complete = NULL;
75 static int process_commit(struct commit *commit)
77 if (parse_commit(commit))
78 return -1;
80 while (complete && complete->item->date >= commit->date) {
81 pop_most_recent_commit(&complete, COMPLETE);
84 if (commit->object.flags & COMPLETE)
85 return 0;
87 hashcpy(current_commit_sha1, commit->object.sha1);
89 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
91 if (get_tree) {
92 if (process(&commit->tree->object))
93 return -1;
94 if (!get_all)
95 get_tree = 0;
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;
104 return 0;
107 static int process_tag(struct tag *tag)
109 if (parse_tag(tag))
110 return -1;
111 return process(tag->tagged);
114 static struct object_list *process_queue = NULL;
115 static struct object_list **process_queue_end = &process_queue;
117 static int process_object(struct object *obj)
119 if (obj->type == OBJ_COMMIT) {
120 if (process_commit((struct commit *)obj))
121 return -1;
122 return 0;
124 if (obj->type == OBJ_TREE) {
125 if (process_tree((struct tree *)obj))
126 return -1;
127 return 0;
129 if (obj->type == OBJ_BLOB) {
130 return 0;
132 if (obj->type == OBJ_TAG) {
133 if (process_tag((struct tag *)obj))
134 return -1;
135 return 0;
137 return error("Unable to determine requirements "
138 "of type %s for %s",
139 typename(obj->type), sha1_to_hex(obj->sha1));
142 static int process(struct object *obj)
144 if (obj->flags & SEEN)
145 return 0;
146 obj->flags |= SEEN;
148 if (has_sha1_file(obj->sha1)) {
149 /* We already have it, so we should scan it now. */
150 obj->flags |= TO_SCAN;
152 else {
153 if (obj->flags & COMPLETE)
154 return 0;
155 prefetch(obj->sha1);
158 object_list_insert(obj, process_queue_end);
159 process_queue_end = &(*process_queue_end)->next;
160 return 0;
163 static int loop(void)
165 struct object_list *elem;
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;
175 /* If we are not scanning this object, we placed it in
176 * the queue because we needed to fetch it first.
178 if (! (obj->flags & TO_SCAN)) {
179 if (fetch(obj->sha1)) {
180 report_missing(obj);
181 return -1;
184 if (!obj->type)
185 parse_object(obj->sha1);
186 if (process_object(obj))
187 return -1;
189 return 0;
192 static int interpret_target(char *target, unsigned char *sha1)
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;
201 return -1;
204 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
206 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
207 if (commit) {
208 commit->object.flags |= COMPLETE;
209 insert_by_date(commit, &complete);
211 return 0;
214 int pull_targets_stdin(char ***target, const char ***write_ref)
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;
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;
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));
237 (*target)[targets] = xstrdup(tg_one);
238 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
239 targets++;
241 return targets;
244 void pull_targets_free(int targets, char **target, const char **write_ref)
246 while (targets--) {
247 free(target[targets]);
248 if (write_ref && write_ref[targets])
249 free((char *) write_ref[targets]);
253 int pull(int targets, char **target, const char **write_ref,
254 const char *write_ref_log_details)
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;
262 save_commit_buffer = 0;
263 track_object_refs = 0;
265 for (i = 0; i < targets; i++) {
266 if (!write_ref || !write_ref[i])
267 continue;
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;
276 if (!get_recover)
277 for_each_ref(mark_complete, NULL);
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;
284 if (process(lookup_unknown_object(&sha1[20 * i])))
285 goto unlock_and_fail;
288 if (loop())
289 goto unlock_and_fail;
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;
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;
305 free(msg);
307 return 0;
310 unlock_and_fail:
311 for (i = 0; i < targets; i++)
312 if (lock[i])
313 unlock_ref(lock[i]);
314 return -1;