blob: adc3e80ce14ab60b585e295d029261f5b53acb51 [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];
Alex Riesen0d7a6e42006-12-12 18:34:02 +010021 strcpy(missing_hex, sha1_to_hex(obj->sha1));;
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));
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;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070062 return 0;
63}
64
Sergey Vlasov24451c32005-09-21 20:34:24 +040065#define COMPLETE (1U << 0)
66#define SEEN (1U << 1)
67#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 01:01:07 -070068
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070069static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040070
Daniel Barkalow30ae7642007-09-10 23:02:45 -040071static int process_commit(struct walker *walker, struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070072{
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040073 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070074 return -1;
75
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040076 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070077 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040078 }
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040079
Junio C Hamanod0ac30f2005-09-16 14:30:29 -070080 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -040081 return 0;
82
Shawn Pearcee7024962006-08-23 02:49:00 -040083 hashcpy(current_commit_sha1, commit->object.sha1);
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070084
Daniel Barkalow30ae7642007-09-10 23:02:45 -040085 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
Junio C Hamano85d106c2005-09-18 01:01:07 -070086
Daniel Barkalow30ae7642007-09-10 23:02:45 -040087 if (walker->get_tree) {
88 if (process(walker, &commit->tree->object))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070089 return -1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -040090 if (!walker->get_all)
91 walker->get_tree = 0;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070092 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -040093 if (walker->get_history) {
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040094 struct commit_list *parents = commit->parents;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070095 for (; parents; parents = parents->next) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -040096 if (process(walker, &parents->item->object))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070097 return -1;
98 }
99 }
100 return 0;
101}
102
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400103static int process_tag(struct walker *walker, struct tag *tag)
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400104{
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400105 if (parse_tag(tag))
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400106 return -1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400107 return process(walker, tag->tagged);
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400108}
109
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400110static struct object_list *process_queue = NULL;
111static struct object_list **process_queue_end = &process_queue;
112
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400113static int process_object(struct walker *walker, struct object *obj)
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400114{
Linus Torvalds19746322006-07-11 20:45:31 -0700115 if (obj->type == OBJ_COMMIT) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400116 if (process_commit(walker, (struct commit *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400117 return -1;
118 return 0;
119 }
Linus Torvalds19746322006-07-11 20:45:31 -0700120 if (obj->type == OBJ_TREE) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400121 if (process_tree(walker, (struct tree *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400122 return -1;
123 return 0;
124 }
Linus Torvalds19746322006-07-11 20:45:31 -0700125 if (obj->type == OBJ_BLOB) {
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400126 return 0;
127 }
Linus Torvalds19746322006-07-11 20:45:31 -0700128 if (obj->type == OBJ_TAG) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400129 if (process_tag(walker, (struct tag *)obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400130 return -1;
131 return 0;
132 }
133 return error("Unable to determine requirements "
134 "of type %s for %s",
Linus Torvalds885a86a2006-06-14 16:45:13 -0700135 typename(obj->type), sha1_to_hex(obj->sha1));
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400136}
137
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400138static int process(struct walker *walker, struct object *obj)
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400139{
Sergey Vlasova82d07e2005-09-21 20:33:59 +0400140 if (obj->flags & SEEN)
141 return 0;
142 obj->flags |= SEEN;
143
Sergey Vlasov80077f02005-09-21 20:33:54 +0400144 if (has_sha1_file(obj->sha1)) {
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400145 /* We already have it, so we should scan it now. */
Junio C Hamano85d106c2005-09-18 01:01:07 -0700146 obj->flags |= TO_SCAN;
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -0700147 }
148 else {
Sergey Vlasov7b64d062005-09-21 20:34:14 +0400149 if (obj->flags & COMPLETE)
150 return 0;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400151 walker->prefetch(walker, obj->sha1);
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400152 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700153
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400154 object_list_insert(obj, process_queue_end);
155 process_queue_end = &(*process_queue_end)->next;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400156 return 0;
157}
158
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400159static int loop(struct walker *walker)
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400160{
Junio C Hamano85d106c2005-09-18 01:01:07 -0700161 struct object_list *elem;
162
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400163 while (process_queue) {
164 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 01:01:07 -0700165 elem = process_queue;
166 process_queue = elem->next;
167 free(elem);
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400168 if (!process_queue)
169 process_queue_end = &process_queue;
170
Junio C Hamano85d106c2005-09-18 01:01:07 -0700171 /* If we are not scanning this object, we placed it in
172 * the queue because we needed to fetch it first.
173 */
174 if (! (obj->flags & TO_SCAN)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400175 if (walker->fetch(walker, obj->sha1)) {
Alex Riesen0d7a6e42006-12-12 18:34:02 +0100176 report_missing(obj);
Junio C Hamano85d106c2005-09-18 01:01:07 -0700177 return -1;
178 }
179 }
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400180 if (!obj->type)
181 parse_object(obj->sha1);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400182 if (process_object(walker, obj))
Daniel Barkalowf88fcf82005-08-11 19:38:09 -0400183 return -1;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -0400184 }
185 return 0;
Daniel Barkalow3173bd42005-06-21 20:35:53 -0400186}
187
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400188static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400189{
190 if (!get_sha1_hex(target, sha1))
191 return 0;
192 if (!check_ref_format(target)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400193 if (!walker->fetch_ref(walker, target, sha1)) {
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400194 return 0;
195 }
196 }
197 return -1;
198}
199
Junio C Hamano8da19772006-09-20 22:02:01 -0700200static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400201{
Junio C Hamanod0ac30f2005-09-16 14:30:29 -0700202 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
203 if (commit) {
204 commit->object.flags |= COMPLETE;
205 insert_by_date(commit, &complete);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400206 }
207 return 0;
208}
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400209
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400210int walker_targets_stdin(char ***target, const char ***write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200211{
212 int targets = 0, targets_alloc = 0;
213 struct strbuf buf;
214 *target = NULL; *write_ref = NULL;
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200215 strbuf_init(&buf, 0);
Petr Baudis8e87ca62006-07-27 23:56:19 +0200216 while (1) {
217 char *rf_one = NULL;
218 char *tg_one;
219
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200220 if (strbuf_getline(&buf, stdin, '\n') == EOF)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200221 break;
222 tg_one = buf.buf;
223 rf_one = strchr(tg_one, '\t');
224 if (rf_one)
225 *rf_one++ = 0;
226
227 if (targets >= targets_alloc) {
228 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
229 *target = xrealloc(*target, targets_alloc * sizeof(**target));
230 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
231 }
Shawn Pearce9befac42006-09-02 00:16:31 -0400232 (*target)[targets] = xstrdup(tg_one);
233 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200234 targets++;
235 }
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200236 strbuf_release(&buf);
Petr Baudis8e87ca62006-07-27 23:56:19 +0200237 return targets;
238}
239
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400240void walker_targets_free(int targets, char **target, const char **write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200241{
242 while (targets--) {
243 free(target[targets]);
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200244 if (write_ref && write_ref[targets])
Petr Baudis8e87ca62006-07-27 23:56:19 +0200245 free((char *) write_ref[targets]);
246 }
247}
248
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400249int walker_fetch(struct walker *walker, int targets, char **target,
250 const char **write_ref, const char *write_ref_log_details)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700251{
Petr Baudis4211e4d2006-07-27 23:56:17 +0200252 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
253 unsigned char *sha1 = xmalloc(targets * 20);
Shawn Pearced0740d92006-05-19 03:29:26 -0400254 char *msg;
255 int ret;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200256 int i;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400257
Junio C Hamano98533b92005-09-15 15:06:39 -0700258 save_commit_buffer = 0;
Sergey Vlasova95cb6f2005-09-23 16:28:13 +0400259 track_object_refs = 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200260
261 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200262 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200263 continue;
264
Junio C Hamano4431fcc2006-09-27 01:09:18 -0700265 lock[i] = lock_ref_sha1(write_ref[i], NULL);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200266 if (!lock[i]) {
267 error("Can't lock ref %s", write_ref[i]);
268 goto unlock_and_fail;
Shawn Pearced0740d92006-05-19 03:29:26 -0400269 }
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400270 }
271
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400272 if (!walker->get_recover)
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700273 for_each_ref(mark_complete, NULL);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400274
Petr Baudis4211e4d2006-07-27 23:56:17 +0200275 for (i = 0; i < targets; i++) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400276 if (interpret_target(walker, target[i], &sha1[20 * i])) {
Sam Vilain5f487412007-12-18 01:00:43 +1300277 error("Could not interpret response from server '%s' as something to pull", target[i]);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200278 goto unlock_and_fail;
279 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400280 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
Petr Baudis4211e4d2006-07-27 23:56:17 +0200281 goto unlock_and_fail;
Shawn Pearce4bd18c42006-05-17 05:55:02 -0400282 }
283
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400284 if (loop(walker))
Petr Baudis4211e4d2006-07-27 23:56:17 +0200285 goto unlock_and_fail;
286
287 if (write_ref_log_details) {
288 msg = xmalloc(strlen(write_ref_log_details) + 12);
289 sprintf(msg, "fetch from %s", write_ref_log_details);
290 } else {
291 msg = NULL;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400292 }
Petr Baudis4211e4d2006-07-27 23:56:17 +0200293 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200294 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200295 continue;
296 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
297 lock[i] = NULL;
298 if (ret)
299 goto unlock_and_fail;
300 }
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700301 free(msg);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200302
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400303 return 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200304
Petr Baudis4211e4d2006-07-27 23:56:17 +0200305unlock_and_fail:
306 for (i = 0; i < targets; i++)
307 if (lock[i])
308 unlock_ref(lock[i]);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400309
Petr Baudis4211e4d2006-07-27 23:56:17 +0200310 return -1;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700311}
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400312
313void walker_free(struct walker *walker)
314{
315 walker->cleanup(walker);
316 free(walker);
317}