blob: 5c65ea494de149d006d726484032f1b44f45a511 [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"
Petr Baudis8e87ca62006-07-27 23:56:19 +02009#include "strbuf.h"
Daniel Barkalowcd541a62005-06-06 16:38:26 -040010
Junio C Hamanob2d62f12005-05-04 01:26:24 -070011static unsigned char current_commit_sha1[20];
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070012
Daniel Barkalow30ae7642007-09-10 23:02:45 -040013void walker_say(struct walker *walker, const char *fmt, const char *hex)
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040014{
Daniel Barkalow30ae7642007-09-10 23:02:45 -040015 if (walker->get_verbosely)
Junio C Hamanoe78d9772005-05-06 01:37:21 -070016 fprintf(stderr, fmt, hex);
17}
18
Alex Riesen0d7a6e42006-12-12 18:34:02 +010019static void report_missing(const struct object *obj)
Junio C Hamanoee4f4392005-05-01 21:07:40 -070020{
Junio C Hamanob2d62f12005-05-04 01:26:24 -070021 char missing_hex[41];
Alex Riesen0d7a6e42006-12-12 18:34:02 +010022 strcpy(missing_hex, sha1_to_hex(obj->sha1));;
23 fprintf(stderr, "Cannot obtain needed %s %s\n",
24 obj->type ? typename(obj->type): "object", missing_hex);
25 if (!is_null_sha1(current_commit_sha1))
26 fprintf(stderr, "while processing commit %s.\n",
27 sha1_to_hex(current_commit_sha1));
Junio C Hamanob2d62f12005-05-04 01:26:24 -070028}
29
Daniel Barkalow30ae7642007-09-10 23:02:45 -040030static int process(struct walker *walker, struct object *obj);
Daniel Barkalow3173bd42005-06-21 20:35:53 -040031
Daniel Barkalow30ae7642007-09-10 23:02:45 -040032static int process_tree(struct walker *walker, struct tree *tree)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070033{
Linus Torvalds1bc995a2006-05-29 12:20:48 -070034 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 09:45:45 -070035 struct name_entry entry;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070036
37 if (parse_tree(tree))
38 return -1;
39
Linus Torvalds6fda5e52007-03-21 10:08:25 -070040 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds4c068a92006-05-30 09:45:45 -070041 while (tree_entry(&desc, &entry)) {
Junio C Hamano6f9012b2006-06-02 15:23:47 -070042 struct object *obj = NULL;
43
Sven Verdoolaege582c7392007-06-26 23:19:41 +020044 /* submodule commits are not stored in the superproject */
Junio C Hamano68fb4652007-06-26 18:33:24 -070045 if (S_ISGITLINK(entry.mode))
Sven Verdoolaege582c7392007-06-26 23:19:41 +020046 continue;
Linus Torvalds4c068a92006-05-30 09:45:45 -070047 if (S_ISDIR(entry.mode)) {
48 struct tree *tree = lookup_tree(entry.sha1);
Junio C Hamano6f9012b2006-06-02 15:23:47 -070049 if (tree)
50 obj = &tree->object;
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070051 }
Junio C Hamano6f9012b2006-06-02 15:23:47 -070052 else {
53 struct blob *blob = lookup_blob(entry.sha1);
54 if (blob)
55 obj = &blob->object;
56 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -040057 if (!obj || process(walker, obj))
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070058 return -1;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070059 }
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070060 free(tree->buffer);
61 tree->buffer = NULL;
Linus Torvalds1bc995a2006-05-29 12:20:48 -070062 tree->size = 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;
193 if (!check_ref_format(target)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400194 if (!walker->fetch_ref(walker, target, sha1)) {
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400195 return 0;
196 }
197 }
198 return -1;
199}
200
Junio C Hamano8da19772006-09-20 22:02:01 -0700201static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400202{
Junio C Hamanod0ac30f2005-09-16 14:30:29 -0700203 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
204 if (commit) {
205 commit->object.flags |= COMPLETE;
206 insert_by_date(commit, &complete);
Daniel Barkalow22c6e1d2005-09-14 21:31:42 -0400207 }
208 return 0;
209}
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400210
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400211int walker_targets_stdin(char ***target, const char ***write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200212{
213 int targets = 0, targets_alloc = 0;
214 struct strbuf buf;
215 *target = NULL; *write_ref = NULL;
216 strbuf_init(&buf);
217 while (1) {
218 char *rf_one = NULL;
219 char *tg_one;
220
221 read_line(&buf, stdin, '\n');
222 if (buf.eof)
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 }
Shawn Pearce9befac42006-09-02 00:16:31 -0400234 (*target)[targets] = xstrdup(tg_one);
235 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
Petr Baudis8e87ca62006-07-27 23:56:19 +0200236 targets++;
237 }
238 return targets;
239}
240
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400241void walker_targets_free(int targets, char **target, const char **write_ref)
Petr Baudis8e87ca62006-07-27 23:56:19 +0200242{
243 while (targets--) {
244 free(target[targets]);
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200245 if (write_ref && write_ref[targets])
Petr Baudis8e87ca62006-07-27 23:56:19 +0200246 free((char *) write_ref[targets]);
247 }
248}
249
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400250int walker_fetch(struct walker *walker, int targets, char **target,
251 const char **write_ref, const char *write_ref_log_details)
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700252{
Petr Baudis4211e4d2006-07-27 23:56:17 +0200253 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
254 unsigned char *sha1 = xmalloc(targets * 20);
Shawn Pearced0740d92006-05-19 03:29:26 -0400255 char *msg;
256 int ret;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200257 int i;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400258
Junio C Hamano98533b92005-09-15 15:06:39 -0700259 save_commit_buffer = 0;
Sergey Vlasova95cb6f2005-09-23 16:28:13 +0400260 track_object_refs = 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200261
262 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200263 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200264 continue;
265
Junio C Hamano4431fcc2006-09-27 01:09:18 -0700266 lock[i] = lock_ref_sha1(write_ref[i], NULL);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200267 if (!lock[i]) {
268 error("Can't lock ref %s", write_ref[i]);
269 goto unlock_and_fail;
Shawn Pearced0740d92006-05-19 03:29:26 -0400270 }
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400271 }
272
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400273 if (!walker->get_recover)
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700274 for_each_ref(mark_complete, NULL);
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])) {
Petr Baudis4211e4d2006-07-27 23:56:17 +0200278 error("Could not interpret %s as something to pull", target[i]);
279 goto unlock_and_fail;
280 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400281 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
Petr Baudis4211e4d2006-07-27 23:56:17 +0200282 goto unlock_and_fail;
Shawn Pearce4bd18c42006-05-17 05:55:02 -0400283 }
284
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400285 if (loop(walker))
Petr Baudis4211e4d2006-07-27 23:56:17 +0200286 goto unlock_and_fail;
287
288 if (write_ref_log_details) {
289 msg = xmalloc(strlen(write_ref_log_details) + 12);
290 sprintf(msg, "fetch from %s", write_ref_log_details);
291 } else {
292 msg = NULL;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400293 }
Petr Baudis4211e4d2006-07-27 23:56:17 +0200294 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 02:10:07 +0200295 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 23:56:17 +0200296 continue;
297 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
298 lock[i] = NULL;
299 if (ret)
300 goto unlock_and_fail;
301 }
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700302 free(msg);
Petr Baudis4211e4d2006-07-27 23:56:17 +0200303
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400304 return 0;
Petr Baudis4211e4d2006-07-27 23:56:17 +0200305
Petr Baudis4211e4d2006-07-27 23:56:17 +0200306unlock_and_fail:
307 for (i = 0; i < targets; i++)
308 if (lock[i])
309 unlock_ref(lock[i]);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400310
Petr Baudis4211e4d2006-07-27 23:56:17 +0200311 return -1;
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700312}
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400313
314void walker_free(struct walker *walker)
315{
316 walker->cleanup(walker);
317 free(walker);
318}