]> git.ipfire.org Git - thirdparty/git.git/blame - walker.c
Merge branch 'nd/mv-code-cleaning'
[thirdparty/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;
47e44ed1 208 commit_list_insert_by_date(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;
231 *target = xrealloc(*target, targets_alloc * sizeof(**target));
232 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
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{
4211e4d1
PB
254 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
255 unsigned char *sha1 = xmalloc(targets * 20);
f3320699
JK
256 const char *msg;
257 char *to_free = NULL;
d0740d92 258 int ret;
4211e4d1 259 int i;
cd541a68 260
98533b90 261 save_commit_buffer = 0;
4211e4d1
PB
262
263 for (i = 0; i < targets; i++) {
1b03dfed 264 if (!write_ref || !write_ref[i])
4211e4d1
PB
265 continue;
266
4431fcc4 267 lock[i] = lock_ref_sha1(write_ref[i], NULL);
4211e4d1
PB
268 if (!lock[i]) {
269 error("Can't lock ref %s", write_ref[i]);
270 goto unlock_and_fail;
d0740d92 271 }
cd541a68
DB
272 }
273
30ae764b 274 if (!walker->get_recover)
cb5d709f 275 for_each_ref(mark_complete, NULL);
22c6e1d0 276
4211e4d1 277 for (i = 0; i < targets; i++) {
30ae764b 278 if (interpret_target(walker, target[i], &sha1[20 * i])) {
5f48741a 279 error("Could not interpret response from server '%s' as something to pull", target[i]);
4211e4d1
PB
280 goto unlock_and_fail;
281 }
30ae764b 282 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
4211e4d1 283 goto unlock_and_fail;
4bd18c43 284 }
4211e4d1 285
30ae764b 286 if (loop(walker))
4211e4d1
PB
287 goto unlock_and_fail;
288
f3320699
JK
289 if (write_ref_log_details)
290 msg = to_free = xstrfmt("fetch from %s", write_ref_log_details);
291 else
292 msg = "fetch (unknown)";
4211e4d1 293 for (i = 0; i < targets; i++) {
1b03dfed 294 if (!write_ref || !write_ref[i])
4211e4d1 295 continue;
f3320699 296 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg);
4211e4d1
PB
297 lock[i] = NULL;
298 if (ret)
299 goto unlock_and_fail;
4bd18c43 300 }
f3320699 301 free(to_free);
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]);
f3320699 309 free(to_free);
30ae764b 310
4211e4d1 311 return -1;
4250a5e5 312}
30ae764b
DB
313
314void walker_free(struct walker *walker)
315{
316 walker->cleanup(walker);
317 free(walker);
318}