blob: be389dc9bf5161c31be29e3a72264fd6120a0bbc [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 }
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070059 free(tree->buffer);
60 tree->buffer = NULL;
Linus Torvalds1bc995a2006-05-29 12:20:48 -070061 tree->size = 0;
Jeff King541fc212008-06-04 14:38:58 -040062 tree->object.parsed = 0;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070063 return 0;
64}
65
Sergey Vlasov24451c32005-09-21 20:34:24 +040066#define COMPLETE (1U << 0)
67#define SEEN (1U << 1)
68#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 01:01:07 -070069
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070070static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040071
Daniel Barkalow30ae7642007-09-10 23:02:45 -040072static int process_commit(struct walker *walker, struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070073{
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040074 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070075 return -1;
76
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040077 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070078 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040079 }
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040080
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070081 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040082 return 0;
83
Shawn Pearcee7024962006-08-23 02:49:00 -040084 hashcpy(current_commit_sha1, commit->object.sha1);
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070085
Daniel Barkalow30ae7642007-09-10 23:02:45 -040086 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
Junio C Hamano85d106c2005-09-18 01:01:07 -070087
Daniel Barkalow30ae7642007-09-10 23:02:45 -040088 if (walker->get_tree) {
89 if (process(walker, &commit->tree->object))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070090 return -1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -040091 if (!walker->get_all)
92 walker->get_tree = 0;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070093 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -040094 if (walker->get_history) {
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040095 struct commit_list *parents = commit->parents;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070096 for (; parents; parents = parents->next) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -040097 if (process(walker, &parents->item->object))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070098 return -1;
99 }
100 }
101 return 0;
102}
103
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400104static int process_tag(struct walker *walker, struct tag *tag)
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400105{
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400106 if (parse_tag(tag))
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400107 return -1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400108 return process(walker, tag->tagged);
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400109}
110
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400111static struct object_list *process_queue = NULL;
112static struct object_list **process_queue_end = &process_queue;
113
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400114static int process_object(struct walker *walker, struct object *obj)
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400115{
Linus Torvalds19746322006-07-11 20:45:31 -0700116 if (obj->type == OBJ_COMMIT) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400117 if (process_commit(walker, (struct commit *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400118 return -1;
119 return 0;
120 }
Linus Torvalds19746322006-07-11 20:45:31 -0700121 if (obj->type == OBJ_TREE) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400122 if (process_tree(walker, (struct tree *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400123 return -1;
124 return 0;
125 }
Linus Torvalds19746322006-07-11 20:45:31 -0700126 if (obj->type == OBJ_BLOB) {
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400127 return 0;
128 }
Linus Torvalds19746322006-07-11 20:45:31 -0700129 if (obj->type == OBJ_TAG) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400130 if (process_tag(walker, (struct tag *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400131 return -1;
132 return 0;
133 }
134 return error("Unable to determine requirements "
135 "of type %s for %s",
Linus Torvalds885a86a2006-06-14 16:45:13 -0700136 typename(obj->type), sha1_to_hex(obj->sha1));
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400137}
138
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400139static int process(struct walker *walker, struct object *obj)
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400140{
Sergey Vlasova82d07e2005-09-21 20:33:59 +0400141 if (obj->flags & SEEN)
142 return 0;
143 obj->flags |= SEEN;
144
Sergey Vlasov80077f02005-09-21 20:33:54 +0400145 if (has_sha1_file(obj->sha1)) {
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400146 /* We already have it, so we should scan it now. */
Junio C Hamano85d106c2005-09-18 01:01:07 -0700147 obj->flags |= TO_SCAN;
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -0700148 }
149 else {
Sergey Vlasov7b64d062005-09-21 20:34:14 +0400150 if (obj->flags & COMPLETE)
151 return 0;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400152 walker->prefetch(walker, obj->sha1);
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400153 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700154
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400155 object_list_insert(obj, process_queue_end);
156 process_queue_end = &(*process_queue_end)->next;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400157 return 0;
158}
159
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400160static int loop(struct walker *walker)
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400161{
Junio C Hamano85d106c2005-09-18 01:01:07 -0700162 struct object_list *elem;
163
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400164 while (process_queue) {
165 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 01:01:07 -0700166 elem = process_queue;
167 process_queue = elem->next;
168 free(elem);
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400169 if (!process_queue)
170 process_queue_end = &process_queue;
171
Junio C Hamano85d106c2005-09-18 01:01:07 -0700172 /* If we are not scanning this object, we placed it in
173 * the queue because we needed to fetch it first.
174 */
175 if (! (obj->flags & TO_SCAN)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400176 if (walker->fetch(walker, obj->sha1)) {
Alex Riesen0d7a6e42006-12-12 18:34:02 +0100177 report_missing(obj);
Junio C Hamano85d106c2005-09-18 01:01:07 -0700178 return -1;
179 }
180 }
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400181 if (!obj->type)
182 parse_object(obj->sha1);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400183 if (process_object(walker, obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400184 return -1;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400185 }
186 return 0;
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400187}
188
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400189static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400190{
191 if (!get_sha1_hex(target, sha1))
192 return 0;
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200193 if (!check_refname_format(target, 0)) {
René Scharfe59c69c02008-10-18 10:44:18 +0200194 struct ref *ref = alloc_ref(target);
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400195 if (!walker->fetch_ref(walker, ref)) {
196 hashcpy(sha1, ref->old_sha1);
197 free(ref);
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400198 return 0;
199 }
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400200 free(ref);
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400201 }
202 return -1;
203}
204
Junio C Hamano8da19772006-09-20 22:02:01 -0700205static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400206{
Junio C Hamanod0ac30f2005-09-16 14:30:29 -0700207 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
208 if (commit) {
209 commit->object.flags |= COMPLETE;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200210 commit_list_insert_by_date(commit, &complete);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400211 }
212 return 0;
213}
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400214
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400215int walker_targets_stdin(char ***target, const char ***write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200216{
217 int targets = 0, targets_alloc = 0;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500218 struct strbuf buf = STRBUF_INIT;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200219 *target = NULL; *write_ref = NULL;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200220 while (1) {
221 char *rf_one = NULL;
222 char *tg_one;
223
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200224 if (strbuf_getline(&buf, stdin, '\n') == EOF)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200225 break;
226 tg_one = buf.buf;
227 rf_one = strchr(tg_one, '\t');
228 if (rf_one)
229 *rf_one++ = 0;
230
231 if (targets >= targets_alloc) {
232 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
233 *target = xrealloc(*target, targets_alloc * sizeof(**target));
234 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
235 }
Shawn Pearce9befac42006-09-02 00:16:31 -0400236 (*target)[targets] = xstrdup(tg_one);
237 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200238 targets++;
239 }
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200240 strbuf_release(&buf);
Petr Baudis8e87ca62006-07-27 23:56:19 +0200241 return targets;
242}
243
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400244void walker_targets_free(int targets, char **target, const char **write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200245{
246 while (targets--) {
247 free(target[targets]);
Pierre Habouzit24deea52009-07-22 23:51:55 +0200248 if (write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200249 free((char *) write_ref[targets]);
250 }
251}
252
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400253int walker_fetch(struct walker *walker, int targets, char **target,
254 const char **write_ref, const char *write_ref_log_details)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700255{
Petr Baudis4211e4d2006-07-27 23:56:17 +0200256 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
257 unsigned char *sha1 = xmalloc(targets * 20);
Shawn Pearced0740d92006-05-19 03:29:26 -0400258 char *msg;
259 int ret;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200260 int i;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400261
Junio C Hamano98533b92005-09-15 15:06:39 -0700262 save_commit_buffer = 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200263
264 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200265 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200266 continue;
267
Junio C Hamano4431fcc2006-09-27 01:09:18 -0700268 lock[i] = lock_ref_sha1(write_ref[i], NULL);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200269 if (!lock[i]) {
270 error("Can't lock ref %s", write_ref[i]);
271 goto unlock_and_fail;
Shawn Pearced0740d92006-05-19 03:29:26 -0400272 }
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400273 }
274
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400275 if (!walker->get_recover)
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700276 for_each_ref(mark_complete, NULL);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400277
Petr Baudis4211e4d2006-07-27 23:56:17 +0200278 for (i = 0; i < targets; i++) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400279 if (interpret_target(walker, target[i], &sha1[20 * i])) {
Sam Vilain5f487412007-12-18 01:00:43 +1300280 error("Could not interpret response from server '%s' as something to pull", target[i]);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200281 goto unlock_and_fail;
282 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400283 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
Petr Baudis4211e4d2006-07-27 23:56:17 +0200284 goto unlock_and_fail;
Shawn Pearce4bd18c42006-05-17 05:55:02 -0400285 }
286
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400287 if (loop(walker))
Petr Baudis4211e4d2006-07-27 23:56:17 +0200288 goto unlock_and_fail;
289
290 if (write_ref_log_details) {
291 msg = xmalloc(strlen(write_ref_log_details) + 12);
292 sprintf(msg, "fetch from %s", write_ref_log_details);
293 } 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++) {
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200297 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200298 continue;
299 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
300 lock[i] = NULL;
301 if (ret)
302 goto unlock_and_fail;
303 }
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700304 free(msg);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200305
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400306 return 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200307
Petr Baudis4211e4d2006-07-27 23:56:17 +0200308unlock_and_fail:
309 for (i = 0; i < targets; i++)
310 if (lock[i])
311 unlock_ref(lock[i]);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400312
Petr Baudis4211e4d2006-07-27 23:56:17 +0200313 return -1;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700314}
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400315
316void walker_free(struct walker *walker)
317{
318 walker->cleanup(walker);
319 free(walker);
320}