sha1_file: make the new sha1 object writing be coda-friendly.
[git/jrn.git] / pull.c
blobbbef245fc8321a64f5bc936ac2c221c71983f68e
1 #include "pull.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "tree.h"
7 int get_tree = 0;
8 int get_history = 0;
9 int get_all = 0;
11 static int make_sure_we_have_it(unsigned char *sha1)
13 if (has_sha1_file(sha1))
14 return 0;
15 return fetch(sha1);
18 static int process_tree(unsigned char *sha1)
20 struct tree *tree = lookup_tree(sha1);
21 struct tree_entry_list *entries;
23 if (parse_tree(tree))
24 return -1;
26 for (entries = tree->entries; entries; entries = entries->next) {
27 if (make_sure_we_have_it(entries->item.tree->object.sha1))
28 return -1;
29 if (entries->directory) {
30 if (process_tree(entries->item.tree->object.sha1))
31 return -1;
34 return 0;
37 static int process_commit(unsigned char *sha1)
39 struct commit *obj = lookup_commit(sha1);
41 if (make_sure_we_have_it(sha1))
42 return -1;
44 if (parse_commit(obj))
45 return -1;
47 if (get_tree) {
48 if (make_sure_we_have_it(obj->tree->object.sha1))
49 return -1;
50 if (process_tree(obj->tree->object.sha1))
51 return -1;
52 if (!get_all)
53 get_tree = 0;
55 if (get_history) {
56 struct commit_list *parents = obj->parents;
57 for (; parents; parents = parents->next) {
58 if (has_sha1_file(parents->item->object.sha1))
59 continue;
60 if (make_sure_we_have_it(parents->item->object.sha1)) {
61 /* The server might not have it, and
62 * we don't mind.
64 continue;
66 if (process_commit(parents->item->object.sha1))
67 return -1;
70 return 0;
73 int pull(char *target)
75 int retval;
76 unsigned char sha1[20];
77 retval = get_sha1_hex(target, sha1);
78 if (retval)
79 return retval;
80 retval = make_sure_we_have_it(sha1);
81 if (retval)
82 return retval;
83 return process_commit(sha1);