blob: 58ffeca264b765326a234b21356a02a3152e1c38 [file] [log] [blame]
Daniel Barkalow4250a5e2005-04-30 16:53:56 -07001#include "cache.h"
Daniel Barkalow30ae7642007-09-10 23:02:45 -04002#include "walker.h"
Daniel Barkalow4250a5e2005-04-30 16:53:56 -07003#include "commit.h"
4#include "tree.h"
Linus Torvalds1bc995a2006-05-29 12:20:48 -07005#include "tree-walk.h"
Daniel Barkalow3173bd42005-06-21 20:35:53 -04006#include "tag.h"
7#include "blob.h"
Daniel Barkalowcd541a62005-06-06 16:38:26 -04008#include "refs.h"
9
Junio C Hamanob2d62f12005-05-04 01:26:24 -070010static unsigned char current_commit_sha1[20];
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070011
Daniel Barkalow30ae7642007-09-10 23:02:45 -040012void walker_say(struct walker *walker, const char *fmt, const char *hex)
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040013{
Daniel Barkalow30ae7642007-09-10 23:02:45 -040014 if (walker->get_verbosely)
Junio C Hamanoe78d9772005-05-06 01:37:21 -070015 fprintf(stderr, fmt, hex);
16}
17
Alex Riesen0d7a6e42006-12-12 18:34:02 +010018static void report_missing(const struct object *obj)
Junio C Hamanoee4f4392005-05-01 21:07:40 -070019{
Junio C Hamanob2d62f12005-05-04 01:26:24 -070020 char missing_hex[41];
Junio C Hamanoba19a802009-02-10 17:42:04 -080021 strcpy(missing_hex, sha1_to_hex(obj->sha1));
Alex Riesen0d7a6e42006-12-12 18:34:02 +010022 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));
Junio C Hamanob2d62f12005-05-04 01:26:24 -070027}
28
Daniel Barkalow30ae7642007-09-10 23:02:45 -040029static int process(struct walker *walker, struct object *obj);
Daniel Barkalow3173bd42005-06-21 20:35:53 -040030
Daniel Barkalow30ae7642007-09-10 23:02:45 -040031static int process_tree(struct walker *walker, struct tree *tree)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070032{
Linus Torvalds1bc995a2006-05-29 12:20:48 -070033 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 09:45:45 -070034 struct name_entry entry;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070035
36 if (parse_tree(tree))
37 return -1;
38
Linus Torvalds6fda5e52007-03-21 10:08:25 -070039 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds4c068a92006-05-30 09:45:45 -070040 while (tree_entry(&desc, &entry)) {
Junio C Hamano6f9012b2006-06-02 15:23:47 -070041 struct object *obj = NULL;
42
Sven Verdoolaege582c7392007-06-26 23:19:41 +020043 /* submodule commits are not stored in the superproject */
Junio C Hamano68fb4652007-06-26 18:33:24 -070044 if (S_ISGITLINK(entry.mode))
Sven Verdoolaege582c7392007-06-26 23:19:41 +020045 continue;
Linus Torvalds4c068a92006-05-30 09:45:45 -070046 if (S_ISDIR(entry.mode)) {
47 struct tree *tree = lookup_tree(entry.sha1);
Junio C Hamano6f9012b2006-06-02 15:23:47 -070048 if (tree)
49 obj = &tree->object;
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070050 }
Junio C Hamano6f9012b2006-06-02 15:23:47 -070051 else {
52 struct blob *blob = lookup_blob(entry.sha1);
53 if (blob)
54 obj = &blob->object;
55 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -040056 if (!obj || process(walker, obj))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070057 return -1;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070058 }
Jeff King6e454b92013-06-05 18:37:39 -040059 free_tree_buffer(tree);
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070060 return 0;
61}
62
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070063/* Remember to update object flag allocation in object.h */
Sergey Vlasov24451c32005-09-21 20:34:24 +040064#define COMPLETE (1U << 0)
65#define SEEN (1U << 1)
66#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 01:01:07 -070067
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070068static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040069
Daniel Barkalow30ae7642007-09-10 23:02:45 -040070static int process_commit(struct walker *walker, struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070071{
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040072 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070073 return -1;
74
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040075 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070076 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040077 }
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040078
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070079 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040080 return 0;
81
Shawn Pearcee7024962006-08-23 02:49:00 -040082 hashcpy(current_commit_sha1, commit->object.sha1);
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070083
Daniel Barkalow30ae7642007-09-10 23:02:45 -040084 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
Junio C Hamano85d106c2005-09-18 01:01:07 -070085
Daniel Barkalow30ae7642007-09-10 23:02:45 -040086 if (walker->get_tree) {
87 if (process(walker, &commit->tree->object))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070088 return -1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -040089 if (!walker->get_all)
90 walker->get_tree = 0;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070091 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -040092 if (walker->get_history) {
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040093 struct commit_list *parents = commit->parents;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070094 for (; parents; parents = parents->next) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -040095 if (process(walker, &parents->item->object))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070096 return -1;
97 }
98 }
99 return 0;
100}
101
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400102static int process_tag(struct walker *walker, struct tag *tag)
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400103{
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400104 if (parse_tag(tag))
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400105 return -1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400106 return process(walker, tag->tagged);
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400107}
108
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400109static struct object_list *process_queue = NULL;
110static struct object_list **process_queue_end = &process_queue;
111
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400112static int process_object(struct walker *walker, struct object *obj)
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400113{
Linus Torvalds19746322006-07-11 20:45:31 -0700114 if (obj->type == OBJ_COMMIT) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400115 if (process_commit(walker, (struct commit *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400116 return -1;
117 return 0;
118 }
Linus Torvalds19746322006-07-11 20:45:31 -0700119 if (obj->type == OBJ_TREE) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400120 if (process_tree(walker, (struct tree *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400121 return -1;
122 return 0;
123 }
Linus Torvalds19746322006-07-11 20:45:31 -0700124 if (obj->type == OBJ_BLOB) {
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400125 return 0;
126 }
Linus Torvalds19746322006-07-11 20:45:31 -0700127 if (obj->type == OBJ_TAG) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400128 if (process_tag(walker, (struct tag *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400129 return -1;
130 return 0;
131 }
132 return error("Unable to determine requirements "
133 "of type %s for %s",
Linus Torvalds885a86a2006-06-14 16:45:13 -0700134 typename(obj->type), sha1_to_hex(obj->sha1));
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400135}
136
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400137static int process(struct walker *walker, struct object *obj)
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400138{
Sergey Vlasova82d07e2005-09-21 20:33:59 +0400139 if (obj->flags & SEEN)
140 return 0;
141 obj->flags |= SEEN;
142
Sergey Vlasov80077f02005-09-21 20:33:54 +0400143 if (has_sha1_file(obj->sha1)) {
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400144 /* We already have it, so we should scan it now. */
Junio C Hamano85d106c2005-09-18 01:01:07 -0700145 obj->flags |= TO_SCAN;
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -0700146 }
147 else {
Sergey Vlasov7b64d062005-09-21 20:34:14 +0400148 if (obj->flags & COMPLETE)
149 return 0;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400150 walker->prefetch(walker, obj->sha1);
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400151 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700152
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400153 object_list_insert(obj, process_queue_end);
154 process_queue_end = &(*process_queue_end)->next;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400155 return 0;
156}
157
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400158static int loop(struct walker *walker)
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400159{
Junio C Hamano85d106c2005-09-18 01:01:07 -0700160 struct object_list *elem;
161
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400162 while (process_queue) {
163 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 01:01:07 -0700164 elem = process_queue;
165 process_queue = elem->next;
166 free(elem);
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400167 if (!process_queue)
168 process_queue_end = &process_queue;
169
Junio C Hamano85d106c2005-09-18 01:01:07 -0700170 /* 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)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400174 if (walker->fetch(walker, obj->sha1)) {
Alex Riesen0d7a6e42006-12-12 18:34:02 +0100175 report_missing(obj);
Junio C Hamano85d106c2005-09-18 01:01:07 -0700176 return -1;
177 }
178 }
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400179 if (!obj->type)
180 parse_object(obj->sha1);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400181 if (process_object(walker, obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400182 return -1;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400183 }
184 return 0;
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400185}
186
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400187static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400188{
189 if (!get_sha1_hex(target, sha1))
190 return 0;
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200191 if (!check_refname_format(target, 0)) {
René Scharfe59c69c02008-10-18 10:44:18 +0200192 struct ref *ref = alloc_ref(target);
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400193 if (!walker->fetch_ref(walker, ref)) {
194 hashcpy(sha1, ref->old_sha1);
195 free(ref);
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400196 return 0;
197 }
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400198 free(ref);
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400199 }
200 return -1;
201}
202
Junio C Hamano8da19772006-09-20 22:02:01 -0700203static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400204{
Junio C Hamanod0ac30f2005-09-16 14:30:29 -0700205 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
206 if (commit) {
207 commit->object.flags |= COMPLETE;
René Scharfe3bc7a052014-08-21 20:30:24 +0200208 commit_list_insert(commit, &complete);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400209 }
210 return 0;
211}
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400212
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400213int walker_targets_stdin(char ***target, const char ***write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200214{
215 int targets = 0, targets_alloc = 0;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500216 struct strbuf buf = STRBUF_INIT;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200217 *target = NULL; *write_ref = NULL;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200218 while (1) {
219 char *rf_one = NULL;
220 char *tg_one;
221
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200222 if (strbuf_getline(&buf, stdin, '\n') == EOF)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200223 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;
René Scharfe2756ca42014-09-16 20:56:57 +0200231 REALLOC_ARRAY(*target, targets_alloc);
232 REALLOC_ARRAY(*write_ref, targets_alloc);
Petr Baudis8e87ca62006-07-27 23:56:19 +0200233 }
Shawn Pearce9befac42006-09-02 00:16:31 -0400234 (*target)[targets] = xstrdup(tg_one);
Jeff King8c53f072015-01-12 20:59:09 -0500235 (*write_ref)[targets] = xstrdup_or_null(rf_one);
Petr Baudis8e87ca62006-07-27 23:56:19 +0200236 targets++;
237 }
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200238 strbuf_release(&buf);
Petr Baudis8e87ca62006-07-27 23:56:19 +0200239 return targets;
240}
241
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400242void walker_targets_free(int targets, char **target, const char **write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200243{
244 while (targets--) {
245 free(target[targets]);
Pierre Habouzit24deea52009-07-22 23:51:55 +0200246 if (write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200247 free((char *) write_ref[targets]);
248 }
249}
250
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400251int walker_fetch(struct walker *walker, int targets, char **target,
252 const char **write_ref, const char *write_ref_log_details)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700253{
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700254 struct strbuf refname = STRBUF_INIT;
255 struct strbuf err = STRBUF_INIT;
256 struct ref_transaction *transaction = NULL;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200257 unsigned char *sha1 = xmalloc(targets * 20);
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700258 char *msg = NULL;
259 int i, ret = -1;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400260
Junio C Hamano98533b92005-09-15 15:06:39 -0700261 save_commit_buffer = 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200262
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700263 if (write_ref) {
264 transaction = ref_transaction_begin(&err);
265 if (!transaction) {
266 error("%s", err.buf);
267 goto done;
Shawn Pearced0740d92006-05-19 03:29:26 -0400268 }
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400269 }
270
René Scharfe3bc7a052014-08-21 20:30:24 +0200271 if (!walker->get_recover) {
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700272 for_each_ref(mark_complete, NULL);
René Scharfe3bc7a052014-08-21 20:30:24 +0200273 commit_list_sort_by_date(&complete);
274 }
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400275
Petr Baudis4211e4d2006-07-27 23:56:17 +0200276 for (i = 0; i < targets; i++) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400277 if (interpret_target(walker, target[i], &sha1[20 * i])) {
Sam Vilain5f487412007-12-18 01:00:43 +1300278 error("Could not interpret response from server '%s' as something to pull", target[i]);
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700279 goto done;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200280 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400281 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700282 goto done;
Shawn Pearce4bd18c42006-05-17 05:55:02 -0400283 }
284
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400285 if (loop(walker))
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700286 goto done;
287 if (!write_ref) {
288 ret = 0;
289 goto done;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200290 }
Shawn Pearced0740d92006-05-19 03:29:26 -0400291 if (write_ref_log_details) {
Junio C Hamano01d678a2014-09-11 10:33:30 -0700292 msg = xstrfmt("fetch from %s", write_ref_log_details);
Shawn Pearced0740d92006-05-19 03:29:26 -0400293 } else {
294 msg = NULL;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400295 }
Petr Baudis4211e4d2006-07-27 23:56:17 +0200296 for (i = 0; i < targets; i++) {
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700297 if (!write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200298 continue;
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700299 strbuf_reset(&refname);
300 strbuf_addf(&refname, "refs/%s", write_ref[i]);
301 if (ref_transaction_update(transaction, refname.buf,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100302 &sha1[20 * i], NULL, 0,
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700303 msg ? msg : "fetch (unknown)",
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700304 &err)) {
305 error("%s", err.buf);
306 goto done;
307 }
Petr Baudis4211e4d2006-07-27 23:56:17 +0200308 }
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700309 if (ref_transaction_commit(transaction, &err)) {
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700310 error("%s", err.buf);
311 goto done;
312 }
Petr Baudis4211e4d2006-07-27 23:56:17 +0200313
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700314 ret = 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200315
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700316done:
317 ref_transaction_free(transaction);
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700318 free(msg);
Ronnie Sahlbergb6b10bb2014-04-17 11:31:06 -0700319 free(sha1);
320 strbuf_release(&err);
321 strbuf_release(&refname);
322 return ret;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700323}
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400324
325void walker_free(struct walker *walker)
326{
327 walker->cleanup(walker);
328 free(walker);
329}