refs.c: add an err argument to delete_ref_loose
[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
208acbfb 63/* Remember to update object flag allocation in object.h */
24451c31
SV
64#define COMPLETE (1U << 0)
65#define SEEN (1U << 1)
66#define TO_SCAN (1U << 2)
85d106c2 67
d0ac30f2 68static struct commit_list *complete = NULL;
22c6e1d0 69
30ae764b 70static int process_commit(struct walker *walker, struct commit *commit)
4250a5e5 71{
1e8be59d 72 if (parse_commit(commit))
4250a5e5
DB
73 return -1;
74
22c6e1d0 75 while (complete && complete->item->date >= commit->date) {
d0ac30f2 76 pop_most_recent_commit(&complete, COMPLETE);
22c6e1d0 77 }
22c6e1d0 78
d0ac30f2 79 if (commit->object.flags & COMPLETE)
22c6e1d0
DB
80 return 0;
81
e702496e 82 hashcpy(current_commit_sha1, commit->object.sha1);
4250a5e5 83
30ae764b 84 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
85d106c2 85
30ae764b
DB
86 if (walker->get_tree) {
87 if (process(walker, &commit->tree->object))
4250a5e5 88 return -1;
30ae764b
DB
89 if (!walker->get_all)
90 walker->get_tree = 0;
4250a5e5 91 }
30ae764b 92 if (walker->get_history) {
1e8be59d 93 struct commit_list *parents = commit->parents;
4250a5e5 94 for (; parents; parents = parents->next) {
30ae764b 95 if (process(walker, &parents->item->object))
4250a5e5
DB
96 return -1;
97 }
98 }
99 return 0;
100}
101
30ae764b 102static int process_tag(struct walker *walker, struct tag *tag)
3173bd49 103{
1e8be59d 104 if (parse_tag(tag))
3173bd49 105 return -1;
30ae764b 106 return process(walker, tag->tagged);
3173bd49
DB
107}
108
1e8be59d
DB
109static struct object_list *process_queue = NULL;
110static struct object_list **process_queue_end = &process_queue;
111
30ae764b 112static int process_object(struct walker *walker, struct object *obj)
3173bd49 113{
1974632c 114 if (obj->type == OBJ_COMMIT) {
30ae764b 115 if (process_commit(walker, (struct commit *)obj))
f88fcf8b
DB
116 return -1;
117 return 0;
118 }
1974632c 119 if (obj->type == OBJ_TREE) {
30ae764b 120 if (process_tree(walker, (struct tree *)obj))
f88fcf8b
DB
121 return -1;
122 return 0;
123 }
1974632c 124 if (obj->type == OBJ_BLOB) {
f88fcf8b
DB
125 return 0;
126 }
1974632c 127 if (obj->type == OBJ_TAG) {
30ae764b 128 if (process_tag(walker, (struct tag *)obj))
f88fcf8b 129 return -1;
3173bd49 130 return 0;
f88fcf8b
DB
131 }
132 return error("Unable to determine requirements "
133 "of type %s for %s",
885a86ab 134 typename(obj->type), sha1_to_hex(obj->sha1));
f88fcf8b
DB
135}
136
30ae764b 137static int process(struct walker *walker, struct object *obj)
f88fcf8b 138{
a82d07e5
SV
139 if (obj->flags & SEEN)
140 return 0;
141 obj->flags |= SEEN;
142
80077f07 143 if (has_sha1_file(obj->sha1)) {
f88fcf8b 144 /* We already have it, so we should scan it now. */
85d106c2 145 obj->flags |= TO_SCAN;
e5f38ec3
JH
146 }
147 else {
7b64d06b
SV
148 if (obj->flags & COMPLETE)
149 return 0;
30ae764b 150 walker->prefetch(walker, obj->sha1);
f88fcf8b 151 }
a6080a0a 152
1e8be59d
DB
153 object_list_insert(obj, process_queue_end);
154 process_queue_end = &(*process_queue_end)->next;
1e8be59d
DB
155 return 0;
156}
157
30ae764b 158static int loop(struct walker *walker)
1e8be59d 159{
85d106c2
JH
160 struct object_list *elem;
161
1e8be59d
DB
162 while (process_queue) {
163 struct object *obj = process_queue->item;
85d106c2
JH
164 elem = process_queue;
165 process_queue = elem->next;
166 free(elem);
1e8be59d
DB
167 if (!process_queue)
168 process_queue_end = &process_queue;
169
85d106c2
JH
170 /* If we are not scanning this object, we placed it in
171 * the queue because we needed to fetch it first.
172 */
173 if (! (obj->flags & TO_SCAN)) {
30ae764b 174 if (walker->fetch(walker, obj->sha1)) {
0d7a6e4e 175 report_missing(obj);
85d106c2
JH
176 return -1;
177 }
178 }
1e8be59d
DB
179 if (!obj->type)
180 parse_object(obj->sha1);
30ae764b 181 if (process_object(walker, obj))
f88fcf8b 182 return -1;
1e8be59d
DB
183 }
184 return 0;
3173bd49
DB
185}
186
30ae764b 187static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
cd541a68
DB
188{
189 if (!get_sha1_hex(target, sha1))
190 return 0;
8d9c5010 191 if (!check_refname_format(target, 0)) {
59c69c0c 192 struct ref *ref = alloc_ref(target);
c13b2633
DB
193 if (!walker->fetch_ref(walker, ref)) {
194 hashcpy(sha1, ref->old_sha1);
195 free(ref);
cd541a68
DB
196 return 0;
197 }
c13b2633 198 free(ref);
cd541a68
DB
199 }
200 return -1;
201}
202
8da19775 203static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
22c6e1d0 204{
d0ac30f2
JH
205 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
206 if (commit) {
207 commit->object.flags |= COMPLETE;
3bc7a05b 208 commit_list_insert(commit, &complete);
22c6e1d0
DB
209 }
210 return 0;
211}
cd541a68 212
30ae764b 213int walker_targets_stdin(char ***target, const char ***write_ref)
8e87ca66
PB
214{
215 int targets = 0, targets_alloc = 0;
f285a2d7 216 struct strbuf buf = STRBUF_INIT;
8e87ca66 217 *target = NULL; *write_ref = NULL;
8e87ca66
PB
218 while (1) {
219 char *rf_one = NULL;
220 char *tg_one;
221
e6c019d0 222 if (strbuf_getline(&buf, stdin, '\n') == EOF)
8e87ca66
PB
223 break;
224 tg_one = buf.buf;
225 rf_one = strchr(tg_one, '\t');
226 if (rf_one)
227 *rf_one++ = 0;
228
229 if (targets >= targets_alloc) {
230 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
2756ca43
RS
231 REALLOC_ARRAY(*target, targets_alloc);
232 REALLOC_ARRAY(*write_ref, targets_alloc);
8e87ca66 233 }
9befac47
SP
234 (*target)[targets] = xstrdup(tg_one);
235 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
8e87ca66
PB
236 targets++;
237 }
e6c019d0 238 strbuf_release(&buf);
8e87ca66
PB
239 return targets;
240}
241
30ae764b 242void walker_targets_free(int targets, char **target, const char **write_ref)
8e87ca66
PB
243{
244 while (targets--) {
245 free(target[targets]);
24deea52 246 if (write_ref)
8e87ca66
PB
247 free((char *) write_ref[targets]);
248 }
249}
250
30ae764b
DB
251int walker_fetch(struct walker *walker, int targets, char **target,
252 const char **write_ref, const char *write_ref_log_details)
4250a5e5 253{
b6b10bb4
RS
254 struct strbuf refname = STRBUF_INIT;
255 struct strbuf err = STRBUF_INIT;
256 struct ref_transaction *transaction = NULL;
4211e4d1 257 unsigned char *sha1 = xmalloc(targets * 20);
b6b10bb4
RS
258 char *msg = NULL;
259 int i, ret = -1;
cd541a68 260
98533b90 261 save_commit_buffer = 0;
4211e4d1 262
b6b10bb4
RS
263 if (write_ref) {
264 transaction = ref_transaction_begin(&err);
265 if (!transaction) {
266 error("%s", err.buf);
267 goto done;
d0740d92 268 }
cd541a68
DB
269 }
270
3bc7a05b 271 if (!walker->get_recover) {
cb5d709f 272 for_each_ref(mark_complete, NULL);
3bc7a05b
RS
273 commit_list_sort_by_date(&complete);
274 }
22c6e1d0 275
4211e4d1 276 for (i = 0; i < targets; i++) {
30ae764b 277 if (interpret_target(walker, target[i], &sha1[20 * i])) {
5f48741a 278 error("Could not interpret response from server '%s' as something to pull", target[i]);
b6b10bb4 279 goto done;
4211e4d1 280 }
30ae764b 281 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
b6b10bb4 282 goto done;
4bd18c43 283 }
4211e4d1 284
30ae764b 285 if (loop(walker))
b6b10bb4
RS
286 goto done;
287 if (!write_ref) {
288 ret = 0;
289 goto done;
290 }
4211e4d1 291 if (write_ref_log_details) {
01d678a2 292 msg = xstrfmt("fetch from %s", write_ref_log_details);
4211e4d1
PB
293 } else {
294 msg = NULL;
4bd18c43 295 }
4211e4d1 296 for (i = 0; i < targets; i++) {
b6b10bb4 297 if (!write_ref[i])
4211e4d1 298 continue;
b6b10bb4
RS
299 strbuf_reset(&refname);
300 strbuf_addf(&refname, "refs/%s", write_ref[i]);
301 if (ref_transaction_update(transaction, refname.buf,
302 &sha1[20 * i], NULL, 0, 0,
303 &err)) {
304 error("%s", err.buf);
305 goto done;
306 }
307 }
308 if (ref_transaction_commit(transaction,
309 msg ? msg : "fetch (unknown)",
310 &err)) {
311 error("%s", err.buf);
312 goto done;
4bd18c43 313 }
4211e4d1 314
b6b10bb4 315 ret = 0;
30ae764b 316
b6b10bb4
RS
317done:
318 ref_transaction_free(transaction);
319 free(msg);
320 free(sha1);
321 strbuf_release(&err);
322 strbuf_release(&refname);
323 return ret;
4250a5e5 324}
30ae764b
DB
325
326void walker_free(struct walker *walker)
327{
328 walker->cleanup(walker);
329 free(walker);
330}