]>
Commit | Line | Data |
---|---|---|
4250a5e5 DB |
1 | #include "pull.h" |
2 | ||
3 | #include "cache.h" | |
4 | #include "commit.h" | |
5 | #include "tree.h" | |
3173bd49 DB |
6 | #include "tag.h" |
7 | #include "blob.h" | |
cd541a68 DB |
8 | #include "refs.h" |
9 | ||
10 | const char *write_ref = NULL; | |
11 | ||
12 | const unsigned char *current_ref = NULL; | |
4250a5e5 DB |
13 | |
14 | int get_tree = 0; | |
15 | int get_history = 0; | |
16 | int get_all = 0; | |
e78d9772 | 17 | int get_verbosely = 0; |
b2d62f16 | 18 | static unsigned char current_commit_sha1[20]; |
4250a5e5 | 19 | |
b2d62f16 JH |
20 | static const char commitS[] = "commit"; |
21 | static const char treeS[] = "tree"; | |
22 | static const char blobS[] = "blob"; | |
23 | ||
e78d9772 JH |
24 | void pull_say(const char *fmt, const char *hex) { |
25 | if (get_verbosely) | |
26 | fprintf(stderr, fmt, hex); | |
27 | } | |
28 | ||
b2d62f16 JH |
29 | static void report_missing(const char *what, const unsigned char *missing) |
30 | { | |
31 | char missing_hex[41]; | |
32 | ||
33 | strcpy(missing_hex, sha1_to_hex(missing));; | |
34 | fprintf(stderr, | |
35 | "Cannot obtain needed %s %s\nwhile processing commit %s.\n", | |
36 | what, missing_hex, sha1_to_hex(current_commit_sha1)); | |
37 | } | |
38 | ||
39 | static int make_sure_we_have_it(const char *what, unsigned char *sha1) | |
ee4f439f | 40 | { |
a48e1d67 JH |
41 | int status = 0; |
42 | ||
43 | if (!has_sha1_file(sha1)) { | |
44 | status = fetch(sha1); | |
45 | if (status && what) | |
46 | report_missing(what, sha1); | |
47 | } | |
b2d62f16 | 48 | return status; |
ee4f439f JH |
49 | } |
50 | ||
3173bd49 DB |
51 | static int process_unknown(unsigned char *sha1); |
52 | ||
4250a5e5 DB |
53 | static int process_tree(unsigned char *sha1) |
54 | { | |
55 | struct tree *tree = lookup_tree(sha1); | |
56 | struct tree_entry_list *entries; | |
57 | ||
58 | if (parse_tree(tree)) | |
59 | return -1; | |
60 | ||
61 | for (entries = tree->entries; entries; entries = entries->next) { | |
b2d62f16 JH |
62 | const char *what = entries->directory ? treeS : blobS; |
63 | if (make_sure_we_have_it(what, entries->item.tree->object.sha1)) | |
4250a5e5 DB |
64 | return -1; |
65 | if (entries->directory) { | |
66 | if (process_tree(entries->item.tree->object.sha1)) | |
67 | return -1; | |
68 | } | |
69 | } | |
70 | return 0; | |
71 | } | |
72 | ||
73 | static int process_commit(unsigned char *sha1) | |
74 | { | |
75 | struct commit *obj = lookup_commit(sha1); | |
76 | ||
b2d62f16 | 77 | if (make_sure_we_have_it(commitS, sha1)) |
4250a5e5 DB |
78 | return -1; |
79 | ||
80 | if (parse_commit(obj)) | |
81 | return -1; | |
82 | ||
83 | if (get_tree) { | |
b2d62f16 | 84 | if (make_sure_we_have_it(treeS, obj->tree->object.sha1)) |
4250a5e5 DB |
85 | return -1; |
86 | if (process_tree(obj->tree->object.sha1)) | |
87 | return -1; | |
88 | if (!get_all) | |
89 | get_tree = 0; | |
90 | } | |
91 | if (get_history) { | |
92 | struct commit_list *parents = obj->parents; | |
93 | for (; parents; parents = parents->next) { | |
94 | if (has_sha1_file(parents->item->object.sha1)) | |
95 | continue; | |
b2d62f16 JH |
96 | if (make_sure_we_have_it(NULL, |
97 | parents->item->object.sha1)) { | |
4250a5e5 DB |
98 | /* The server might not have it, and |
99 | * we don't mind. | |
100 | */ | |
101 | continue; | |
102 | } | |
103 | if (process_commit(parents->item->object.sha1)) | |
104 | return -1; | |
b2d62f16 | 105 | memcpy(current_commit_sha1, sha1, 20); |
4250a5e5 DB |
106 | } |
107 | } | |
108 | return 0; | |
109 | } | |
110 | ||
3173bd49 DB |
111 | static int process_tag(unsigned char *sha1) |
112 | { | |
113 | struct tag *obj = lookup_tag(sha1); | |
114 | ||
115 | if (parse_tag(obj)) | |
116 | return -1; | |
117 | return process_unknown(obj->tagged->sha1); | |
118 | } | |
119 | ||
120 | static int process_unknown(unsigned char *sha1) | |
121 | { | |
122 | struct object *obj; | |
123 | if (make_sure_we_have_it("object", sha1)) | |
124 | return -1; | |
125 | obj = parse_object(sha1); | |
126 | if (!obj) | |
127 | return error("Unable to parse object %s", sha1_to_hex(sha1)); | |
128 | if (obj->type == commit_type) | |
129 | return process_commit(sha1); | |
130 | if (obj->type == tree_type) | |
131 | return process_tree(sha1); | |
132 | if (obj->type == blob_type) | |
133 | return 0; | |
134 | if (obj->type == tag_type) | |
135 | return process_tag(sha1); | |
136 | return error("Unable to determine requirement of type %s for %s", | |
137 | obj->type, sha1_to_hex(sha1)); | |
138 | } | |
139 | ||
cd541a68 DB |
140 | static int interpret_target(char *target, unsigned char *sha1) |
141 | { | |
142 | if (!get_sha1_hex(target, sha1)) | |
143 | return 0; | |
144 | if (!check_ref_format(target)) { | |
145 | if (!fetch_ref(target, sha1)) { | |
146 | return 0; | |
147 | } | |
148 | } | |
149 | return -1; | |
150 | } | |
151 | ||
152 | ||
4250a5e5 DB |
153 | int pull(char *target) |
154 | { | |
4250a5e5 | 155 | unsigned char sha1[20]; |
cd541a68 DB |
156 | int fd = -1; |
157 | ||
158 | if (write_ref && current_ref) { | |
159 | fd = lock_ref_sha1(write_ref, current_ref); | |
160 | if (fd < 0) | |
161 | return -1; | |
162 | } | |
163 | ||
164 | if (interpret_target(target, sha1)) | |
165 | return error("Could not interpret %s as something to pull", | |
166 | target); | |
3173bd49 | 167 | if (process_unknown(sha1)) |
cd541a68 DB |
168 | return -1; |
169 | ||
170 | if (write_ref) { | |
171 | if (current_ref) { | |
172 | write_ref_sha1(write_ref, fd, sha1); | |
173 | } else { | |
174 | write_ref_sha1_unlocked(write_ref, sha1); | |
175 | } | |
176 | } | |
177 | return 0; | |
4250a5e5 | 178 | } |