Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 2 | #include "walker.h" |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 3 | #include "repository.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 4 | #include "object-store.h" |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 5 | #include "commit.h" |
| 6 | #include "tree.h" |
Linus Torvalds | 1bc995a | 2006-05-29 12:20:48 -0700 | [diff] [blame] | 7 | #include "tree-walk.h" |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 8 | #include "tag.h" |
| 9 | #include "blob.h" |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 10 | #include "refs.h" |
René Scharfe | 7655b41 | 2020-03-03 21:55:34 +0100 | [diff] [blame] | 11 | #include "progress.h" |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 12 | |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 13 | static struct object_id current_commit_oid; |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 14 | |
Jeff King | fa262ca | 2016-07-08 05:25:23 -0400 | [diff] [blame] | 15 | void walker_say(struct walker *walker, const char *fmt, ...) |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 16 | { |
Jeff King | fa262ca | 2016-07-08 05:25:23 -0400 | [diff] [blame] | 17 | if (walker->get_verbosely) { |
| 18 | va_list ap; |
| 19 | va_start(ap, fmt); |
| 20 | vfprintf(stderr, fmt, ap); |
| 21 | va_end(ap); |
| 22 | } |
Junio C Hamano | e78d977 | 2005-05-06 01:37:21 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Alex Riesen | 0d7a6e4 | 2006-12-12 18:34:02 +0100 | [diff] [blame] | 25 | static void report_missing(const struct object *obj) |
Junio C Hamano | ee4f439 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 26 | { |
Alex Riesen | 0d7a6e4 | 2006-12-12 18:34:02 +0100 | [diff] [blame] | 27 | fprintf(stderr, "Cannot obtain needed %s %s\n", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 28 | obj->type ? type_name(obj->type): "object", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 29 | oid_to_hex(&obj->oid)); |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 30 | if (!is_null_oid(¤t_commit_oid)) |
Alex Riesen | 0d7a6e4 | 2006-12-12 18:34:02 +0100 | [diff] [blame] | 31 | fprintf(stderr, "while processing commit %s.\n", |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 32 | oid_to_hex(¤t_commit_oid)); |
Junio C Hamano | b2d62f1 | 2005-05-04 01:26:24 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 35 | static int process(struct walker *walker, struct object *obj); |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 36 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 37 | static int process_tree(struct walker *walker, struct tree *tree) |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 38 | { |
Linus Torvalds | 1bc995a | 2006-05-29 12:20:48 -0700 | [diff] [blame] | 39 | struct tree_desc desc; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 40 | struct name_entry entry; |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 41 | |
| 42 | if (parse_tree(tree)) |
| 43 | return -1; |
| 44 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 45 | init_tree_desc(&desc, tree->buffer, tree->size); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 46 | while (tree_entry(&desc, &entry)) { |
Junio C Hamano | 6f9012b | 2006-06-02 15:23:47 -0700 | [diff] [blame] | 47 | struct object *obj = NULL; |
| 48 | |
Sven Verdoolaege | 582c739 | 2007-06-26 23:19:41 +0200 | [diff] [blame] | 49 | /* submodule commits are not stored in the superproject */ |
Junio C Hamano | 68fb465 | 2007-06-26 18:33:24 -0700 | [diff] [blame] | 50 | if (S_ISGITLINK(entry.mode)) |
Sven Verdoolaege | 582c739 | 2007-06-26 23:19:41 +0200 | [diff] [blame] | 51 | continue; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 52 | if (S_ISDIR(entry.mode)) { |
Stefan Beller | f86bcc7 | 2018-06-28 18:21:56 -0700 | [diff] [blame] | 53 | struct tree *tree = lookup_tree(the_repository, |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 54 | &entry.oid); |
Junio C Hamano | 6f9012b | 2006-06-02 15:23:47 -0700 | [diff] [blame] | 55 | if (tree) |
| 56 | obj = &tree->object; |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 57 | } |
Junio C Hamano | 6f9012b | 2006-06-02 15:23:47 -0700 | [diff] [blame] | 58 | else { |
Stefan Beller | da14a7f | 2018-06-28 18:21:55 -0700 | [diff] [blame] | 59 | struct blob *blob = lookup_blob(the_repository, |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 60 | &entry.oid); |
Junio C Hamano | 6f9012b | 2006-06-02 15:23:47 -0700 | [diff] [blame] | 61 | if (blob) |
| 62 | obj = &blob->object; |
| 63 | } |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 64 | if (!obj || process(walker, obj)) |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 65 | return -1; |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 66 | } |
Jeff King | 6e454b9 | 2013-06-05 18:37:39 -0400 | [diff] [blame] | 67 | free_tree_buffer(tree); |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 71 | /* Remember to update object flag allocation in object.h */ |
Sergey Vlasov | 24451c3 | 2005-09-21 20:34:24 +0400 | [diff] [blame] | 72 | #define COMPLETE (1U << 0) |
| 73 | #define SEEN (1U << 1) |
| 74 | #define TO_SCAN (1U << 2) |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 75 | |
Junio C Hamano | d0ac30f | 2005-09-16 14:30:29 -0700 | [diff] [blame] | 76 | static struct commit_list *complete = NULL; |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 77 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 78 | static int process_commit(struct walker *walker, struct commit *commit) |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 79 | { |
Martin Ågren | 0b6b342 | 2018-04-22 20:12:50 +0200 | [diff] [blame] | 80 | struct commit_list *parents; |
| 81 | |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 82 | if (parse_commit(commit)) |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 83 | return -1; |
| 84 | |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 85 | while (complete && complete->item->date >= commit->date) { |
Junio C Hamano | d0ac30f | 2005-09-16 14:30:29 -0700 | [diff] [blame] | 86 | pop_most_recent_commit(&complete, COMPLETE); |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 87 | } |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 88 | |
Junio C Hamano | d0ac30f | 2005-09-16 14:30:29 -0700 | [diff] [blame] | 89 | if (commit->object.flags & COMPLETE) |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 90 | return 0; |
| 91 | |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 92 | oidcpy(¤t_commit_oid, &commit->object.oid); |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 93 | |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 94 | walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid)); |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 95 | |
Junio C Hamano | c89b6e1 | 2018-05-23 14:38:13 +0900 | [diff] [blame] | 96 | if (process(walker, &get_commit_tree(commit)->object)) |
Martin Ågren | 0b6b342 | 2018-04-22 20:12:50 +0200 | [diff] [blame] | 97 | return -1; |
| 98 | |
| 99 | for (parents = commit->parents; parents; parents = parents->next) { |
| 100 | if (process(walker, &parents->item->object)) |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 101 | return -1; |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 102 | } |
Martin Ågren | 0b6b342 | 2018-04-22 20:12:50 +0200 | [diff] [blame] | 103 | |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 107 | static int process_tag(struct walker *walker, struct tag *tag) |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 108 | { |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 109 | if (parse_tag(tag)) |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 110 | return -1; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 111 | return process(walker, tag->tagged); |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 112 | } |
| 113 | |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 114 | static struct object_list *process_queue = NULL; |
| 115 | static struct object_list **process_queue_end = &process_queue; |
| 116 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 117 | static int process_object(struct walker *walker, struct object *obj) |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 118 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 119 | if (obj->type == OBJ_COMMIT) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 120 | if (process_commit(walker, (struct commit *)obj)) |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 121 | return -1; |
| 122 | return 0; |
| 123 | } |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 124 | if (obj->type == OBJ_TREE) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 125 | if (process_tree(walker, (struct tree *)obj)) |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 126 | return -1; |
| 127 | return 0; |
| 128 | } |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 129 | if (obj->type == OBJ_BLOB) { |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 130 | return 0; |
| 131 | } |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 132 | if (obj->type == OBJ_TAG) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 133 | if (process_tag(walker, (struct tag *)obj)) |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 134 | return -1; |
| 135 | return 0; |
| 136 | } |
| 137 | return error("Unable to determine requirements " |
| 138 | "of type %s for %s", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 139 | type_name(obj->type), oid_to_hex(&obj->oid)); |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 142 | static int process(struct walker *walker, struct object *obj) |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 143 | { |
Sergey Vlasov | a82d07e | 2005-09-21 20:33:59 +0400 | [diff] [blame] | 144 | if (obj->flags & SEEN) |
| 145 | return 0; |
| 146 | obj->flags |= SEEN; |
| 147 | |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 148 | if (has_object_file(&obj->oid)) { |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 149 | /* We already have it, so we should scan it now. */ |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 150 | obj->flags |= TO_SCAN; |
Junio C Hamano | e5f38ec | 2006-06-06 14:04:17 -0700 | [diff] [blame] | 151 | } |
| 152 | else { |
Sergey Vlasov | 7b64d06 | 2005-09-21 20:34:14 +0400 | [diff] [blame] | 153 | if (obj->flags & COMPLETE) |
| 154 | return 0; |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 155 | walker->prefetch(walker, obj->oid.hash); |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 156 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 157 | |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 158 | object_list_insert(obj, process_queue_end); |
| 159 | process_queue_end = &(*process_queue_end)->next; |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 163 | static int loop(struct walker *walker) |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 164 | { |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 165 | struct object_list *elem; |
René Scharfe | 7655b41 | 2020-03-03 21:55:34 +0100 | [diff] [blame] | 166 | struct progress *progress = NULL; |
| 167 | uint64_t nr = 0; |
| 168 | |
| 169 | if (walker->get_progress) |
| 170 | progress = start_delayed_progress(_("Fetching objects"), 0); |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 171 | |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 172 | while (process_queue) { |
| 173 | struct object *obj = process_queue->item; |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 174 | elem = process_queue; |
| 175 | process_queue = elem->next; |
| 176 | free(elem); |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 177 | if (!process_queue) |
| 178 | process_queue_end = &process_queue; |
| 179 | |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 180 | /* If we are not scanning this object, we placed it in |
| 181 | * the queue because we needed to fetch it first. |
| 182 | */ |
| 183 | if (! (obj->flags & TO_SCAN)) { |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 184 | if (walker->fetch(walker, obj->oid.hash)) { |
René Scharfe | 7655b41 | 2020-03-03 21:55:34 +0100 | [diff] [blame] | 185 | stop_progress(&progress); |
Alex Riesen | 0d7a6e4 | 2006-12-12 18:34:02 +0100 | [diff] [blame] | 186 | report_missing(obj); |
Junio C Hamano | 85d106c | 2005-09-18 01:01:07 -0700 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | } |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 190 | if (!obj->type) |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 191 | parse_object(the_repository, &obj->oid); |
René Scharfe | 7655b41 | 2020-03-03 21:55:34 +0100 | [diff] [blame] | 192 | if (process_object(walker, obj)) { |
| 193 | stop_progress(&progress); |
Daniel Barkalow | f88fcf8 | 2005-08-11 19:38:09 -0400 | [diff] [blame] | 194 | return -1; |
René Scharfe | 7655b41 | 2020-03-03 21:55:34 +0100 | [diff] [blame] | 195 | } |
| 196 | display_progress(progress, ++nr); |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 197 | } |
René Scharfe | 7655b41 | 2020-03-03 21:55:34 +0100 | [diff] [blame] | 198 | stop_progress(&progress); |
barkalow@iabervon.org | 1e8be59 | 2005-08-02 19:46:10 -0400 | [diff] [blame] | 199 | return 0; |
Daniel Barkalow | 3173bd4 | 2005-06-21 20:35:53 -0400 | [diff] [blame] | 200 | } |
| 201 | |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 202 | static int interpret_target(struct walker *walker, char *target, struct object_id *oid) |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 203 | { |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 204 | if (!get_oid_hex(target, oid)) |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 205 | return 0; |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 206 | if (!check_refname_format(target, 0)) { |
René Scharfe | 59c69c0 | 2008-10-18 10:44:18 +0200 | [diff] [blame] | 207 | struct ref *ref = alloc_ref(target); |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 208 | if (!walker->fetch_ref(walker, ref)) { |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 209 | oidcpy(oid, &ref->old_oid); |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 210 | free(ref); |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 211 | return 0; |
| 212 | } |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 213 | free(ref); |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 214 | } |
| 215 | return -1; |
| 216 | } |
| 217 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 218 | static int mark_complete(const char *path UNUSED, |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 219 | const struct object_id *oid, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 220 | int flag UNUSED, |
| 221 | void *cb_data UNUSED) |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 222 | { |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 223 | struct commit *commit = lookup_commit_reference_gently(the_repository, |
| 224 | oid, 1); |
Michael Haggerty | b4ebaf9 | 2015-05-25 18:39:14 +0000 | [diff] [blame] | 225 | |
Junio C Hamano | d0ac30f | 2005-09-16 14:30:29 -0700 | [diff] [blame] | 226 | if (commit) { |
| 227 | commit->object.flags |= COMPLETE; |
René Scharfe | 3bc7a05 | 2014-08-21 20:30:24 +0200 | [diff] [blame] | 228 | commit_list_insert(commit, &complete); |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 229 | } |
| 230 | return 0; |
| 231 | } |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 232 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 233 | int walker_targets_stdin(char ***target, const char ***write_ref) |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 234 | { |
| 235 | int targets = 0, targets_alloc = 0; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 236 | struct strbuf buf = STRBUF_INIT; |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 237 | *target = NULL; *write_ref = NULL; |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 238 | while (1) { |
| 239 | char *rf_one = NULL; |
| 240 | char *tg_one; |
| 241 | |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 242 | if (strbuf_getline_lf(&buf, stdin) == EOF) |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 243 | break; |
| 244 | tg_one = buf.buf; |
| 245 | rf_one = strchr(tg_one, '\t'); |
| 246 | if (rf_one) |
| 247 | *rf_one++ = 0; |
| 248 | |
| 249 | if (targets >= targets_alloc) { |
| 250 | targets_alloc = targets_alloc ? targets_alloc * 2 : 64; |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 251 | REALLOC_ARRAY(*target, targets_alloc); |
| 252 | REALLOC_ARRAY(*write_ref, targets_alloc); |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 253 | } |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 254 | (*target)[targets] = xstrdup(tg_one); |
Jeff King | 8c53f07 | 2015-01-12 20:59:09 -0500 | [diff] [blame] | 255 | (*write_ref)[targets] = xstrdup_or_null(rf_one); |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 256 | targets++; |
| 257 | } |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 258 | strbuf_release(&buf); |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 259 | return targets; |
| 260 | } |
| 261 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 262 | void walker_targets_free(int targets, char **target, const char **write_ref) |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 263 | { |
| 264 | while (targets--) { |
| 265 | free(target[targets]); |
Pierre Habouzit | 24deea5 | 2009-07-22 23:51:55 +0200 | [diff] [blame] | 266 | if (write_ref) |
Petr Baudis | 8e87ca6 | 2006-07-27 23:56:19 +0200 | [diff] [blame] | 267 | free((char *) write_ref[targets]); |
| 268 | } |
| 269 | } |
| 270 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 271 | int walker_fetch(struct walker *walker, int targets, char **target, |
| 272 | const char **write_ref, const char *write_ref_log_details) |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 273 | { |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 274 | struct strbuf refname = STRBUF_INIT; |
| 275 | struct strbuf err = STRBUF_INIT; |
| 276 | struct ref_transaction *transaction = NULL; |
Jeff King | 667b76e | 2020-01-30 04:52:32 -0500 | [diff] [blame] | 277 | struct object_id *oids; |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 278 | char *msg = NULL; |
| 279 | int i, ret = -1; |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 280 | |
Junio C Hamano | 98533b9 | 2005-09-15 15:06:39 -0700 | [diff] [blame] | 281 | save_commit_buffer = 0; |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 282 | |
Jeff King | 667b76e | 2020-01-30 04:52:32 -0500 | [diff] [blame] | 283 | ALLOC_ARRAY(oids, targets); |
| 284 | |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 285 | if (write_ref) { |
| 286 | transaction = ref_transaction_begin(&err); |
| 287 | if (!transaction) { |
| 288 | error("%s", err.buf); |
| 289 | goto done; |
Shawn Pearce | d0740d9 | 2006-05-19 03:29:26 -0400 | [diff] [blame] | 290 | } |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 291 | } |
| 292 | |
René Scharfe | 3bc7a05 | 2014-08-21 20:30:24 +0200 | [diff] [blame] | 293 | if (!walker->get_recover) { |
Michael Haggerty | b4ebaf9 | 2015-05-25 18:39:14 +0000 | [diff] [blame] | 294 | for_each_ref(mark_complete, NULL); |
René Scharfe | 3bc7a05 | 2014-08-21 20:30:24 +0200 | [diff] [blame] | 295 | commit_list_sort_by_date(&complete); |
| 296 | } |
Daniel Barkalow | 22c6e1d | 2005-09-14 21:31:42 -0400 | [diff] [blame] | 297 | |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 298 | for (i = 0; i < targets; i++) { |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 299 | if (interpret_target(walker, target[i], oids + i)) { |
Sam Vilain | 5f48741 | 2007-12-18 01:00:43 +1300 | [diff] [blame] | 300 | error("Could not interpret response from server '%s' as something to pull", target[i]); |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 301 | goto done; |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 302 | } |
Jeff King | 45a187c | 2021-04-13 03:16:36 -0400 | [diff] [blame] | 303 | if (process(walker, lookup_unknown_object(the_repository, &oids[i]))) |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 304 | goto done; |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 307 | if (loop(walker)) |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 308 | goto done; |
| 309 | if (!write_ref) { |
| 310 | ret = 0; |
| 311 | goto done; |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 312 | } |
Shawn Pearce | d0740d9 | 2006-05-19 03:29:26 -0400 | [diff] [blame] | 313 | if (write_ref_log_details) { |
Junio C Hamano | 01d678a | 2014-09-11 10:33:30 -0700 | [diff] [blame] | 314 | msg = xstrfmt("fetch from %s", write_ref_log_details); |
Shawn Pearce | d0740d9 | 2006-05-19 03:29:26 -0400 | [diff] [blame] | 315 | } else { |
| 316 | msg = NULL; |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 317 | } |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 318 | for (i = 0; i < targets; i++) { |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 319 | if (!write_ref[i]) |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 320 | continue; |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 321 | strbuf_reset(&refname); |
| 322 | strbuf_addf(&refname, "refs/%s", write_ref[i]); |
| 323 | if (ref_transaction_update(transaction, refname.buf, |
brian m. carlson | 89f3bbd | 2017-10-15 22:06:53 +0000 | [diff] [blame] | 324 | oids + i, NULL, 0, |
Ronnie Sahlberg | db7516a | 2014-04-30 12:22:42 -0700 | [diff] [blame] | 325 | msg ? msg : "fetch (unknown)", |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 326 | &err)) { |
| 327 | error("%s", err.buf); |
| 328 | goto done; |
| 329 | } |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 330 | } |
Ronnie Sahlberg | db7516a | 2014-04-30 12:22:42 -0700 | [diff] [blame] | 331 | if (ref_transaction_commit(transaction, &err)) { |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 332 | error("%s", err.buf); |
| 333 | goto done; |
| 334 | } |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 335 | |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 336 | ret = 0; |
Petr Baudis | 4211e4d | 2006-07-27 23:56:17 +0200 | [diff] [blame] | 337 | |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 338 | done: |
| 339 | ref_transaction_free(transaction); |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 340 | free(msg); |
brian m. carlson | 94f5a12 | 2017-10-15 22:06:48 +0000 | [diff] [blame] | 341 | free(oids); |
Ronnie Sahlberg | b6b10bb | 2014-04-17 11:31:06 -0700 | [diff] [blame] | 342 | strbuf_release(&err); |
| 343 | strbuf_release(&refname); |
| 344 | return ret; |
Daniel Barkalow | 4250a5e | 2005-04-30 16:53:56 -0700 | [diff] [blame] | 345 | } |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 346 | |
| 347 | void walker_free(struct walker *walker) |
| 348 | { |
| 349 | walker->cleanup(walker); |
| 350 | free(walker); |
| 351 | } |