Merge branch 'ky/branch-m-worktree'
[git] / walker.c
1 #include "cache.h"
2 #include "walker.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
9
10 static unsigned char current_commit_sha1[20];
11
12 void walker_say(struct walker *walker, const char *fmt, const char *hex)
13 {
14         if (walker->get_verbosely)
15                 fprintf(stderr, fmt, hex);
16 }
17
18 static void report_missing(const struct object *obj)
19 {
20         fprintf(stderr, "Cannot obtain needed %s %s\n",
21                 obj->type ? typename(obj->type): "object",
22                 oid_to_hex(&obj->oid));
23         if (!is_null_sha1(current_commit_sha1))
24                 fprintf(stderr, "while processing commit %s.\n",
25                         sha1_to_hex(current_commit_sha1));
26 }
27
28 static int process(struct walker *walker, struct object *obj);
29
30 static int process_tree(struct walker *walker, struct tree *tree)
31 {
32         struct tree_desc desc;
33         struct name_entry entry;
34
35         if (parse_tree(tree))
36                 return -1;
37
38         init_tree_desc(&desc, tree->buffer, tree->size);
39         while (tree_entry(&desc, &entry)) {
40                 struct object *obj = NULL;
41
42                 /* submodule commits are not stored in the superproject */
43                 if (S_ISGITLINK(entry.mode))
44                         continue;
45                 if (S_ISDIR(entry.mode)) {
46                         struct tree *tree = lookup_tree(entry.sha1);
47                         if (tree)
48                                 obj = &tree->object;
49                 }
50                 else {
51                         struct blob *blob = lookup_blob(entry.sha1);
52                         if (blob)
53                                 obj = &blob->object;
54                 }
55                 if (!obj || process(walker, obj))
56                         return -1;
57         }
58         free_tree_buffer(tree);
59         return 0;
60 }
61
62 /* Remember to update object flag allocation in object.h */
63 #define COMPLETE        (1U << 0)
64 #define SEEN            (1U << 1)
65 #define TO_SCAN         (1U << 2)
66
67 static struct commit_list *complete = NULL;
68
69 static int process_commit(struct walker *walker, struct commit *commit)
70 {
71         if (parse_commit(commit))
72                 return -1;
73
74         while (complete && complete->item->date >= commit->date) {
75                 pop_most_recent_commit(&complete, COMPLETE);
76         }
77
78         if (commit->object.flags & COMPLETE)
79                 return 0;
80
81         hashcpy(current_commit_sha1, commit->object.oid.hash);
82
83         walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
84
85         if (walker->get_tree) {
86                 if (process(walker, &commit->tree->object))
87                         return -1;
88                 if (!walker->get_all)
89                         walker->get_tree = 0;
90         }
91         if (walker->get_history) {
92                 struct commit_list *parents = commit->parents;
93                 for (; parents; parents = parents->next) {
94                         if (process(walker, &parents->item->object))
95                                 return -1;
96                 }
97         }
98         return 0;
99 }
100
101 static int process_tag(struct walker *walker, struct tag *tag)
102 {
103         if (parse_tag(tag))
104                 return -1;
105         return process(walker, tag->tagged);
106 }
107
108 static struct object_list *process_queue = NULL;
109 static struct object_list **process_queue_end = &process_queue;
110
111 static int process_object(struct walker *walker, struct object *obj)
112 {
113         if (obj->type == OBJ_COMMIT) {
114                 if (process_commit(walker, (struct commit *)obj))
115                         return -1;
116                 return 0;
117         }
118         if (obj->type == OBJ_TREE) {
119                 if (process_tree(walker, (struct tree *)obj))
120                         return -1;
121                 return 0;
122         }
123         if (obj->type == OBJ_BLOB) {
124                 return 0;
125         }
126         if (obj->type == OBJ_TAG) {
127                 if (process_tag(walker, (struct tag *)obj))
128                         return -1;
129                 return 0;
130         }
131         return error("Unable to determine requirements "
132                      "of type %s for %s",
133                      typename(obj->type), oid_to_hex(&obj->oid));
134 }
135
136 static int process(struct walker *walker, struct object *obj)
137 {
138         if (obj->flags & SEEN)
139                 return 0;
140         obj->flags |= SEEN;
141
142         if (has_object_file(&obj->oid)) {
143                 /* We already have it, so we should scan it now. */
144                 obj->flags |= TO_SCAN;
145         }
146         else {
147                 if (obj->flags & COMPLETE)
148                         return 0;
149                 walker->prefetch(walker, obj->oid.hash);
150         }
151
152         object_list_insert(obj, process_queue_end);
153         process_queue_end = &(*process_queue_end)->next;
154         return 0;
155 }
156
157 static int loop(struct walker *walker)
158 {
159         struct object_list *elem;
160
161         while (process_queue) {
162                 struct object *obj = process_queue->item;
163                 elem = process_queue;
164                 process_queue = elem->next;
165                 free(elem);
166                 if (!process_queue)
167                         process_queue_end = &process_queue;
168
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)) {
173                         if (walker->fetch(walker, obj->oid.hash)) {
174                                 report_missing(obj);
175                                 return -1;
176                         }
177                 }
178                 if (!obj->type)
179                         parse_object(obj->oid.hash);
180                 if (process_object(walker, obj))
181                         return -1;
182         }
183         return 0;
184 }
185
186 static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
187 {
188         if (!get_sha1_hex(target, sha1))
189                 return 0;
190         if (!check_refname_format(target, 0)) {
191                 struct ref *ref = alloc_ref(target);
192                 if (!walker->fetch_ref(walker, ref)) {
193                         hashcpy(sha1, ref->old_oid.hash);
194                         free(ref);
195                         return 0;
196                 }
197                 free(ref);
198         }
199         return -1;
200 }
201
202 static int mark_complete(const char *path, const struct object_id *oid,
203                          int flag, void *cb_data)
204 {
205         struct commit *commit = lookup_commit_reference_gently(oid->hash, 1);
206
207         if (commit) {
208                 commit->object.flags |= COMPLETE;
209                 commit_list_insert(commit, &complete);
210         }
211         return 0;
212 }
213
214 int walker_targets_stdin(char ***target, const char ***write_ref)
215 {
216         int targets = 0, targets_alloc = 0;
217         struct strbuf buf = STRBUF_INIT;
218         *target = NULL; *write_ref = NULL;
219         while (1) {
220                 char *rf_one = NULL;
221                 char *tg_one;
222
223                 if (strbuf_getline_lf(&buf, stdin) == EOF)
224                         break;
225                 tg_one = buf.buf;
226                 rf_one = strchr(tg_one, '\t');
227                 if (rf_one)
228                         *rf_one++ = 0;
229
230                 if (targets >= targets_alloc) {
231                         targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
232                         REALLOC_ARRAY(*target, targets_alloc);
233                         REALLOC_ARRAY(*write_ref, targets_alloc);
234                 }
235                 (*target)[targets] = xstrdup(tg_one);
236                 (*write_ref)[targets] = xstrdup_or_null(rf_one);
237                 targets++;
238         }
239         strbuf_release(&buf);
240         return targets;
241 }
242
243 void walker_targets_free(int targets, char **target, const char **write_ref)
244 {
245         while (targets--) {
246                 free(target[targets]);
247                 if (write_ref)
248                         free((char *) write_ref[targets]);
249         }
250 }
251
252 int walker_fetch(struct walker *walker, int targets, char **target,
253                  const char **write_ref, const char *write_ref_log_details)
254 {
255         struct strbuf refname = STRBUF_INIT;
256         struct strbuf err = STRBUF_INIT;
257         struct ref_transaction *transaction = NULL;
258         unsigned char *sha1 = xmalloc(targets * 20);
259         char *msg = NULL;
260         int i, ret = -1;
261
262         save_commit_buffer = 0;
263
264         if (write_ref) {
265                 transaction = ref_transaction_begin(&err);
266                 if (!transaction) {
267                         error("%s", err.buf);
268                         goto done;
269                 }
270         }
271
272         if (!walker->get_recover) {
273                 for_each_ref(mark_complete, NULL);
274                 commit_list_sort_by_date(&complete);
275         }
276
277         for (i = 0; i < targets; i++) {
278                 if (interpret_target(walker, target[i], &sha1[20 * i])) {
279                         error("Could not interpret response from server '%s' as something to pull", target[i]);
280                         goto done;
281                 }
282                 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
283                         goto done;
284         }
285
286         if (loop(walker))
287                 goto done;
288         if (!write_ref) {
289                 ret = 0;
290                 goto done;
291         }
292         if (write_ref_log_details) {
293                 msg = xstrfmt("fetch from %s", write_ref_log_details);
294         } else {
295                 msg = NULL;
296         }
297         for (i = 0; i < targets; i++) {
298                 if (!write_ref[i])
299                         continue;
300                 strbuf_reset(&refname);
301                 strbuf_addf(&refname, "refs/%s", write_ref[i]);
302                 if (ref_transaction_update(transaction, refname.buf,
303                                            &sha1[20 * i], NULL, 0,
304                                            msg ? msg : "fetch (unknown)",
305                                            &err)) {
306                         error("%s", err.buf);
307                         goto done;
308                 }
309         }
310         if (ref_transaction_commit(transaction, &err)) {
311                 error("%s", err.buf);
312                 goto done;
313         }
314
315         ret = 0;
316
317 done:
318         ref_transaction_free(transaction);
319         free(msg);
320         free(sha1);
321         strbuf_release(&err);
322         strbuf_release(&refname);
323         return ret;
324 }
325
326 void walker_free(struct walker *walker)
327 {
328         walker->cleanup(walker);
329         free(walker);
330 }