Git 1.9-rc0
[git/git.git] / walker.c
CommitLineData
4250a5e5 1#include "cache.h"
30ae764b 2#include "walker.h"
4250a5e5
DB
3#include "commit.h"
4#include "tree.h"
1bc995a3 5#include "tree-walk.h"
3173bd49
DB
6#include "tag.h"
7#include "blob.h"
cd541a68
DB
8#include "refs.h"
9
b2d62f16 10static unsigned char current_commit_sha1[20];
4250a5e5 11
30ae764b 12void walker_say(struct walker *walker, const char *fmt, const char *hex)
1e8be59d 13{
30ae764b 14 if (walker->get_verbosely)
e78d9772
JH
15 fprintf(stderr, fmt, hex);
16}
17
0d7a6e4e 18static void report_missing(const struct object *obj)
b2d62f16
JH
19{
20 char missing_hex[41];
ba19a808 21 strcpy(missing_hex, sha1_to_hex(obj->sha1));
0d7a6e4e
AR
22 fprintf(stderr, "Cannot obtain needed %s %s\n",
23 obj->type ? typename(obj->type): "object", missing_hex);
24 if (!is_null_sha1(current_commit_sha1))
25 fprintf(stderr, "while processing commit %s.\n",
26 sha1_to_hex(current_commit_sha1));
b2d62f16
JH
27}
28
30ae764b 29static int process(struct walker *walker, struct object *obj);
3173bd49 30
30ae764b 31static int process_tree(struct walker *walker, struct tree *tree)
4250a5e5 32{
1bc995a3 33 struct tree_desc desc;
4c068a98 34 struct name_entry entry;
4250a5e5
DB
35
36 if (parse_tree(tree))
37 return -1;
38
6fda5e51 39 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 40 while (tree_entry(&desc, &entry)) {
6f9012b6
JH
41 struct object *obj = NULL;
42
582c7393 43 /* submodule commits are not stored in the superproject */
68fb4650 44 if (S_ISGITLINK(entry.mode))
582c7393 45 continue;
4c068a98
LT
46 if (S_ISDIR(entry.mode)) {
47 struct tree *tree = lookup_tree(entry.sha1);
6f9012b6
JH
48 if (tree)
49 obj = &tree->object;
50 }
51 else {
4c068a98 52 struct blob *blob = lookup_blob(entry.sha1);
6f9012b6
JH
53 if (blob)
54 obj = &blob->object;
2d9c58c6 55 }
30ae764b 56 if (!obj || process(walker, obj))
4250a5e5 57 return -1;
4250a5e5 58 }
6e454b9a 59 free_tree_buffer(tree);
4250a5e5
DB
60 return 0;
61}
62
24451c31
SV
63#define COMPLETE (1U << 0)
64#define SEEN (1U << 1)
65#define TO_SCAN (1U << 2)
85d106c2 66
d0ac30f2 67static struct commit_list *complete = NULL;
22c6e1d0 68
30ae764b 69static int process_commit(struct walker *walker, struct commit *commit)
4250a5e5 70{
1e8be59d 71 if (parse_commit(commit))
4250a5e5
DB
72 return -1;
73
22c6e1d0 74 while (complete && complete->item->date >= commit->date) {
d0ac30f2 75 pop_most_recent_commit(&complete, COMPLETE);
22c6e1d0 76 }
22c6e1d0 77
d0ac30f2 78 if (commit->object.flags & COMPLETE)
22c6e1d0
DB
79 return 0;
80
e702496e 81 hashcpy(current_commit_sha1, commit->object.sha1);
4250a5e5 82
30ae764b 83 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
85d106c2 84
30ae764b
DB
85 if (walker->get_tree) {
86 if (process(walker, &commit->tree->object))
4250a5e5 87 return -1;
30ae764b
DB
88 if (!walker->get_all)
89 walker->get_tree = 0;
4250a5e5 90 }
30ae764b 91 if (walker->get_history) {
1e8be59d 92 struct commit_list *parents = commit->parents;
4250a5e5 93 for (; parents; parents = parents->next) {
30ae764b 94 if (process(walker, &parents->item->object))
4250a5e5
DB
95 return -1;
96 }
97 }
98 return 0;
99}
100
30ae764b 101static int process_tag(struct walker *walker, struct tag *tag)
3173bd49 102{
1e8be59d 103 if (parse_tag(tag))
3173bd49 104 return -1;
30ae764b 105 return process(walker, tag->tagged);
3173bd49
DB
106}
107
1e8be59d
DB
108static struct object_list *process_queue = NULL;
109static struct object_list **process_queue_end = &process_queue;
110
30ae764b 111static int process_object(struct walker *walker, struct object *obj)
3173bd49 112{
1974632c 113 if (obj->type == OBJ_COMMIT) {
30ae764b 114 if (process_commit(walker, (struct commit *)obj))
f88fcf8b
DB
115 return -1;
116 return 0;
117 }
1974632c 118 if (obj->type == OBJ_TREE) {
30ae764b 119 if (process_tree(walker, (struct tree *)obj))
f88fcf8b
DB
120 return -1;
121 return 0;
122 }
1974632c 123 if (obj->type == OBJ_BLOB) {
f88fcf8b
DB
124 return 0;
125 }
1974632c 126 if (obj->type == OBJ_TAG) {
30ae764b 127 if (process_tag(walker, (struct tag *)obj))
f88fcf8b 128 return -1;
3173bd49 129 return 0;
f88fcf8b
DB
130 }
131 return error("Unable to determine requirements "
132 "of type %s for %s",
885a86ab 133 typename(obj->type), sha1_to_hex(obj->sha1));
f88fcf8b
DB
134}
135
30ae764b 136static int process(struct walker *walker, struct object *obj)
f88fcf8b 137{
a82d07e5
SV
138 if (obj->flags & SEEN)
139 return 0;
140 obj->flags |= SEEN;
141
80077f07 142 if (has_sha1_file(obj->sha1)) {
f88fcf8b 143 /* We already have it, so we should scan it now. */
85d106c2 144 obj->flags |= TO_SCAN;
e5f38ec3
JH
145 }
146 else {
7b64d06b
SV
147 if (obj->flags & COMPLETE)
148 return 0;
30ae764b 149 walker->prefetch(walker, obj->sha1);
f88fcf8b 150 }
a6080a0a 151
1e8be59d
DB
152 object_list_insert(obj, process_queue_end);
153 process_queue_end = &(*process_queue_end)->next;
1e8be59d
DB
154 return 0;
155}
156
30ae764b 157static int loop(struct walker *walker)
1e8be59d 158{
85d106c2
JH
159 struct object_list *elem;
160
1e8be59d
DB
161 while (process_queue) {
162 struct object *obj = process_queue->item;
85d106c2
JH
163 elem = process_queue;
164 process_queue = elem->next;
165 free(elem);
1e8be59d
DB
166 if (!process_queue)
167 process_queue_end = &process_queue;
168
85d106c2
JH
169 /* If we are not scanning this object, we placed it in
170 * the queue because we needed to fetch it first.
171 */
172 if (! (obj->flags & TO_SCAN)) {
30ae764b 173 if (walker->fetch(walker, obj->sha1)) {
0d7a6e4e 174 report_missing(obj);
85d106c2
JH
175 return -1;
176 }
177 }
1e8be59d
DB
178 if (!obj->type)
179 parse_object(obj->sha1);
30ae764b 180 if (process_object(walker, obj))
f88fcf8b 181 return -1;
1e8be59d
DB
182 }
183 return 0;
3173bd49
DB
184}
185
30ae764b 186static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
cd541a68
DB
187{
188 if (!get_sha1_hex(target, sha1))
189 return 0;
8d9c5010 190 if (!check_refname_format(target, 0)) {
59c69c0c 191 struct ref *ref = alloc_ref(target);
c13b2633
DB
192 if (!walker->fetch_ref(walker, ref)) {
193 hashcpy(sha1, ref->old_sha1);
194 free(ref);
cd541a68
DB
195 return 0;
196 }
c13b2633 197 free(ref);
cd541a68
DB
198 }
199 return -1;
200}
201
8da19775 202static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
22c6e1d0 203{
d0ac30f2
JH
204 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
205 if (commit) {
206 commit->object.flags |= COMPLETE;
47e44ed1 207 commit_list_insert_by_date(commit, &complete);
22c6e1d0
DB
208 }
209 return 0;
210}
cd541a68 211
30ae764b 212int walker_targets_stdin(char ***target, const char ***write_ref)
8e87ca66
PB
213{
214 int targets = 0, targets_alloc = 0;
f285a2d7 215 struct strbuf buf = STRBUF_INIT;
8e87ca66 216 *target = NULL; *write_ref = NULL;
8e87ca66
PB
217 while (1) {
218 char *rf_one = NULL;
219 char *tg_one;
220
e6c019d0 221 if (strbuf_getline(&buf, stdin, '\n') == EOF)
8e87ca66
PB
222 break;
223 tg_one = buf.buf;
224 rf_one = strchr(tg_one, '\t');
225 if (rf_one)
226 *rf_one++ = 0;
227
228 if (targets >= targets_alloc) {
229 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
230 *target = xrealloc(*target, targets_alloc * sizeof(**target));
231 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
232 }
9befac47
SP
233 (*target)[targets] = xstrdup(tg_one);
234 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
8e87ca66
PB
235 targets++;
236 }
e6c019d0 237 strbuf_release(&buf);
8e87ca66
PB
238 return targets;
239}
240
30ae764b 241void walker_targets_free(int targets, char **target, const char **write_ref)
8e87ca66
PB
242{
243 while (targets--) {
244 free(target[targets]);
24deea52 245 if (write_ref)
8e87ca66
PB
246 free((char *) write_ref[targets]);
247 }
248}
249
30ae764b
DB
250int walker_fetch(struct walker *walker, int targets, char **target,
251 const char **write_ref, const char *write_ref_log_details)
4250a5e5 252{
4211e4d1
PB
253 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
254 unsigned char *sha1 = xmalloc(targets * 20);
d0740d92
SP
255 char *msg;
256 int ret;
4211e4d1 257 int i;
cd541a68 258
98533b90 259 save_commit_buffer = 0;
4211e4d1
PB
260
261 for (i = 0; i < targets; i++) {
1b03dfed 262 if (!write_ref || !write_ref[i])
4211e4d1
PB
263 continue;
264
4431fcc4 265 lock[i] = lock_ref_sha1(write_ref[i], NULL);
4211e4d1
PB
266 if (!lock[i]) {
267 error("Can't lock ref %s", write_ref[i]);
268 goto unlock_and_fail;
d0740d92 269 }
cd541a68
DB
270 }
271
30ae764b 272 if (!walker->get_recover)
cb5d709f 273 for_each_ref(mark_complete, NULL);
22c6e1d0 274
4211e4d1 275 for (i = 0; i < targets; i++) {
30ae764b 276 if (interpret_target(walker, target[i], &sha1[20 * i])) {
5f48741a 277 error("Could not interpret response from server '%s' as something to pull", target[i]);
4211e4d1
PB
278 goto unlock_and_fail;
279 }
30ae764b 280 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
4211e4d1 281 goto unlock_and_fail;
4bd18c43 282 }
4211e4d1 283
30ae764b 284 if (loop(walker))
4211e4d1
PB
285 goto unlock_and_fail;
286
287 if (write_ref_log_details) {
288 msg = xmalloc(strlen(write_ref_log_details) + 12);
289 sprintf(msg, "fetch from %s", write_ref_log_details);
290 } else {
291 msg = NULL;
4bd18c43 292 }
4211e4d1 293 for (i = 0; i < targets; i++) {
1b03dfed 294 if (!write_ref || !write_ref[i])
4211e4d1
PB
295 continue;
296 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
297 lock[i] = NULL;
298 if (ret)
299 goto unlock_and_fail;
4bd18c43 300 }
4cac42b1 301 free(msg);
4bd18c43 302
cd541a68 303 return 0;
4211e4d1 304
4211e4d1
PB
305unlock_and_fail:
306 for (i = 0; i < targets; i++)
307 if (lock[i])
308 unlock_ref(lock[i]);
30ae764b 309
4211e4d1 310 return -1;
4250a5e5 311}
30ae764b
DB
312
313void walker_free(struct walker *walker)
314{
315 walker->cleanup(walker);
316 free(walker);
317}