Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 1 | #include "cache.h" |
Jonathan Nieder | 3f5787f | 2018-05-15 16:42:18 -0700 | [diff] [blame] | 2 | #include "repository.h" |
Michael Haggerty | 6e122b4 | 2015-08-10 11:47:46 +0200 | [diff] [blame] | 3 | #include "tempfile.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 4 | #include "lockfile.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 5 | #include "object-store.h" |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 6 | #include "commit.h" |
Alexandre Julliard | abef3a1 | 2006-11-11 14:57:23 +0100 | [diff] [blame] | 7 | #include "tag.h" |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 8 | #include "pkt-line.h" |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 9 | #include "remote.h" |
| 10 | #include "refs.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 11 | #include "oid-array.h" |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 12 | #include "diff.h" |
| 13 | #include "revision.h" |
| 14 | #include "commit-slab.h" |
Nguyễn Thái Ngọc Duy | 3d9ff4d | 2016-06-12 17:53:57 +0700 | [diff] [blame] | 15 | #include "list-objects.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 16 | #include "commit-reach.h" |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 17 | #include "shallow.h" |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 18 | |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 19 | void set_alternate_shallow_file(struct repository *r, const char *path, int override) |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 20 | { |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 21 | if (r->parsed_objects->is_shallow != -1) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 22 | BUG("is_repository_shallow must not be called before set_alternate_shallow_file"); |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 23 | if (r->parsed_objects->alternate_shallow_file && !override) |
Nguyễn Thái Ngọc Duy | 069c053 | 2013-12-05 20:02:45 +0700 | [diff] [blame] | 24 | return; |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 25 | free(r->parsed_objects->alternate_shallow_file); |
| 26 | r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path); |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 27 | } |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 28 | |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 29 | int register_shallow(struct repository *r, const struct object_id *oid) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 30 | { |
| 31 | struct commit_graft *graft = |
| 32 | xmalloc(sizeof(struct commit_graft)); |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 33 | struct commit *commit = lookup_commit(the_repository, oid); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 34 | |
brian m. carlson | e92b848 | 2017-05-06 22:10:06 +0000 | [diff] [blame] | 35 | oidcpy(&graft->oid, oid); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 36 | graft->nr_parent = -1; |
| 37 | if (commit && commit->object.parsed) |
| 38 | commit->parents = NULL; |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 39 | return register_commit_graft(r, graft, 0); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 42 | int unregister_shallow(const struct object_id *oid) |
| 43 | { |
Jeff King | 98c431b | 2021-01-28 01:12:35 -0500 | [diff] [blame] | 44 | int pos = commit_graft_pos(the_repository, oid); |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 45 | if (pos < 0) |
| 46 | return -1; |
| 47 | if (pos + 1 < the_repository->parsed_objects->grafts_nr) |
| 48 | MOVE_ARRAY(the_repository->parsed_objects->grafts + pos, |
| 49 | the_repository->parsed_objects->grafts + pos + 1, |
| 50 | the_repository->parsed_objects->grafts_nr - pos - 1); |
| 51 | the_repository->parsed_objects->grafts_nr--; |
| 52 | return 0; |
| 53 | } |
| 54 | |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 55 | int is_repository_shallow(struct repository *r) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 56 | { |
| 57 | FILE *fp; |
| 58 | char buf[1024]; |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 59 | const char *path = r->parsed_objects->alternate_shallow_file; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 60 | |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 61 | if (r->parsed_objects->is_shallow >= 0) |
| 62 | return r->parsed_objects->is_shallow; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 63 | |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 64 | if (!path) |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 65 | path = git_path_shallow(r); |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 66 | /* |
| 67 | * fetch-pack sets '--shallow-file ""' as an indicator that no |
| 68 | * shallow file should be used. We could just open it and it |
| 69 | * will likely fail. But let's do an explicit check instead. |
| 70 | */ |
Jeff King | 0cc77c3 | 2014-02-27 05:56:31 -0500 | [diff] [blame] | 71 | if (!*path || (fp = fopen(path, "r")) == NULL) { |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 72 | stat_validity_clear(r->parsed_objects->shallow_stat); |
| 73 | r->parsed_objects->is_shallow = 0; |
| 74 | return r->parsed_objects->is_shallow; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 75 | } |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 76 | stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp)); |
| 77 | r->parsed_objects->is_shallow = 1; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 78 | |
| 79 | while (fgets(buf, sizeof(buf), fp)) { |
brian m. carlson | e92b848 | 2017-05-06 22:10:06 +0000 | [diff] [blame] | 80 | struct object_id oid; |
| 81 | if (get_oid_hex(buf, &oid)) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 82 | die("bad shallow line: %s", buf); |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 83 | register_shallow(r, &oid); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 84 | } |
| 85 | fclose(fp); |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 86 | return r->parsed_objects->is_shallow; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 89 | static void reset_repository_shallow(struct repository *r) |
| 90 | { |
| 91 | r->parsed_objects->is_shallow = -1; |
| 92 | stat_validity_clear(r->parsed_objects->shallow_stat); |
| 93 | } |
| 94 | |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 95 | int commit_shallow_file(struct repository *r, struct shallow_lock *lk) |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 96 | { |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 97 | int res = commit_lock_file(&lk->lock); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 98 | reset_repository_shallow(r); |
| 99 | return res; |
| 100 | } |
| 101 | |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 102 | void rollback_shallow_file(struct repository *r, struct shallow_lock *lk) |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 103 | { |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 104 | rollback_lock_file(&lk->lock); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 105 | reset_repository_shallow(r); |
| 106 | } |
| 107 | |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 108 | /* |
| 109 | * TODO: use "int" elemtype instead of "int *" when/if commit-slab |
| 110 | * supports a "valid" flag. |
| 111 | */ |
| 112 | define_commit_slab(commit_depth, int *); |
SZEDER Gábor | 1df15f8 | 2020-06-05 13:00:26 +0000 | [diff] [blame] | 113 | static void free_depth_in_slab(int **ptr) |
| 114 | { |
| 115 | FREE_AND_NULL(*ptr); |
| 116 | } |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 117 | struct commit_list *get_shallow_commits(struct object_array *heads, int depth, |
| 118 | int shallow_flag, int not_shallow_flag) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 119 | { |
| 120 | int i = 0, cur_depth = 0; |
| 121 | struct commit_list *result = NULL; |
Thiago Farina | 3cd4745 | 2010-08-28 23:04:17 -0300 | [diff] [blame] | 122 | struct object_array stack = OBJECT_ARRAY_INIT; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 123 | struct commit *commit = NULL; |
Nguyễn Thái Ngọc Duy | 79d3a23 | 2013-12-05 20:02:41 +0700 | [diff] [blame] | 124 | struct commit_graft *graft; |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 125 | struct commit_depth depths; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 126 | |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 127 | init_commit_depth(&depths); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 128 | while (commit || i < heads->nr || stack.nr) { |
| 129 | struct commit_list *p; |
| 130 | if (!commit) { |
| 131 | if (i < heads->nr) { |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 132 | int **depth_slot; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 133 | commit = (struct commit *) |
Stefan Beller | a74093d | 2018-06-28 18:22:05 -0700 | [diff] [blame] | 134 | deref_tag(the_repository, |
| 135 | heads->objects[i++].item, |
| 136 | NULL, 0); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 137 | if (!commit || commit->object.type != OBJ_COMMIT) { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 138 | commit = NULL; |
| 139 | continue; |
| 140 | } |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 141 | depth_slot = commit_depth_at(&depths, commit); |
| 142 | if (!*depth_slot) |
| 143 | *depth_slot = xmalloc(sizeof(int)); |
| 144 | **depth_slot = 0; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 145 | cur_depth = 0; |
| 146 | } else { |
| 147 | commit = (struct commit *) |
Martin Ågren | 7199203 | 2017-09-23 01:34:53 +0200 | [diff] [blame] | 148 | object_array_pop(&stack); |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 149 | cur_depth = **commit_depth_at(&depths, commit); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
Jeff King | 367068e | 2013-10-24 04:54:01 -0400 | [diff] [blame] | 152 | parse_commit_or_die(commit); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 153 | cur_depth++; |
Nguyễn Thái Ngọc Duy | 79d3a23 | 2013-12-05 20:02:41 +0700 | [diff] [blame] | 154 | if ((depth != INFINITE_DEPTH && cur_depth >= depth) || |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 155 | (is_repository_shallow(the_repository) && !commit->parents && |
Jonathan Nieder | 1f93ecd | 2018-05-17 15:51:42 -0700 | [diff] [blame] | 156 | (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL && |
Nguyễn Thái Ngọc Duy | 79d3a23 | 2013-12-05 20:02:41 +0700 | [diff] [blame] | 157 | graft->nr_parent < 0)) { |
Nguyễn Thái Ngọc Duy | 682c7d2 | 2013-01-11 16:05:47 +0700 | [diff] [blame] | 158 | commit_list_insert(commit, &result); |
| 159 | commit->object.flags |= shallow_flag; |
| 160 | commit = NULL; |
| 161 | continue; |
| 162 | } |
| 163 | commit->object.flags |= not_shallow_flag; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 164 | for (p = commit->parents, commit = NULL; p; p = p->next) { |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 165 | int **depth_slot = commit_depth_at(&depths, p->item); |
| 166 | if (!*depth_slot) { |
| 167 | *depth_slot = xmalloc(sizeof(int)); |
| 168 | **depth_slot = cur_depth; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 169 | } else { |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 170 | if (cur_depth >= **depth_slot) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 171 | continue; |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 172 | **depth_slot = cur_depth; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 173 | } |
Matthijs Kooijman | 4b79695 | 2013-07-11 13:25:52 +0200 | [diff] [blame] | 174 | if (p->next) |
| 175 | add_object_array(&p->item->object, |
| 176 | NULL, &stack); |
| 177 | else { |
| 178 | commit = p->item; |
Nguyễn Thái Ngọc Duy | 58dbe58 | 2018-05-19 07:28:21 +0200 | [diff] [blame] | 179 | cur_depth = **commit_depth_at(&depths, commit); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 180 | } |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 181 | } |
| 182 | } |
SZEDER Gábor | 1df15f8 | 2020-06-05 13:00:26 +0000 | [diff] [blame] | 183 | deep_clear_commit_depth(&depths, free_depth_in_slab); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 184 | |
| 185 | return result; |
| 186 | } |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 187 | |
Nguyễn Thái Ngọc Duy | 3d9ff4d | 2016-06-12 17:53:57 +0700 | [diff] [blame] | 188 | static void show_commit(struct commit *commit, void *data) |
| 189 | { |
| 190 | commit_list_insert(commit, data); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Given rev-list arguments, run rev-list. All reachable commits |
| 195 | * except border ones are marked with not_shallow_flag. Border commits |
| 196 | * are marked with shallow_flag. The list of border/shallow commits |
| 197 | * are also returned. |
| 198 | */ |
| 199 | struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av, |
| 200 | int shallow_flag, |
| 201 | int not_shallow_flag) |
| 202 | { |
| 203 | struct commit_list *result = NULL, *p; |
| 204 | struct commit_list *not_shallow_list = NULL; |
| 205 | struct rev_info revs; |
| 206 | int both_flags = shallow_flag | not_shallow_flag; |
| 207 | |
| 208 | /* |
| 209 | * SHALLOW (excluded) and NOT_SHALLOW (included) should not be |
| 210 | * set at this point. But better be safe than sorry. |
| 211 | */ |
| 212 | clear_object_flags(both_flags); |
| 213 | |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 214 | is_repository_shallow(the_repository); /* make sure shallows are read */ |
Nguyễn Thái Ngọc Duy | 3d9ff4d | 2016-06-12 17:53:57 +0700 | [diff] [blame] | 215 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 216 | repo_init_revisions(the_repository, &revs, NULL); |
Nguyễn Thái Ngọc Duy | 3d9ff4d | 2016-06-12 17:53:57 +0700 | [diff] [blame] | 217 | save_commit_buffer = 0; |
| 218 | setup_revisions(ac, av, &revs, NULL); |
| 219 | |
| 220 | if (prepare_revision_walk(&revs)) |
| 221 | die("revision walk setup failed"); |
| 222 | traverse_commit_list(&revs, show_commit, NULL, ¬_shallow_list); |
| 223 | |
Nguyễn Thái Ngọc Duy | e34de73 | 2018-05-26 13:35:18 +0200 | [diff] [blame] | 224 | if (!not_shallow_list) |
| 225 | die("no commits selected for shallow requests"); |
| 226 | |
Nguyễn Thái Ngọc Duy | 3d9ff4d | 2016-06-12 17:53:57 +0700 | [diff] [blame] | 227 | /* Mark all reachable commits as NOT_SHALLOW */ |
| 228 | for (p = not_shallow_list; p; p = p->next) |
| 229 | p->item->object.flags |= not_shallow_flag; |
| 230 | |
| 231 | /* |
| 232 | * mark border commits SHALLOW + NOT_SHALLOW. |
| 233 | * We cannot clear NOT_SHALLOW right now. Imagine border |
| 234 | * commit A is processed first, then commit B, whose parent is |
| 235 | * A, later. If NOT_SHALLOW on A is cleared at step 1, B |
| 236 | * itself is considered border at step 2, which is incorrect. |
| 237 | */ |
| 238 | for (p = not_shallow_list; p; p = p->next) { |
| 239 | struct commit *c = p->item; |
| 240 | struct commit_list *parent; |
| 241 | |
| 242 | if (parse_commit(c)) |
| 243 | die("unable to parse commit %s", |
| 244 | oid_to_hex(&c->object.oid)); |
| 245 | |
| 246 | for (parent = c->parents; parent; parent = parent->next) |
| 247 | if (!(parent->item->object.flags & not_shallow_flag)) { |
| 248 | c->object.flags |= shallow_flag; |
| 249 | commit_list_insert(c, &result); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | free_commit_list(not_shallow_list); |
| 254 | |
| 255 | /* |
| 256 | * Now we can clean up NOT_SHALLOW on border commits. Having |
| 257 | * both flags set can confuse the caller. |
| 258 | */ |
| 259 | for (p = result; p; p = p->next) { |
| 260 | struct object *o = &p->item->object; |
| 261 | if ((o->flags & both_flags) == both_flags) |
| 262 | o->flags &= ~not_shallow_flag; |
| 263 | } |
| 264 | return result; |
| 265 | } |
| 266 | |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 267 | static void check_shallow_file_for_update(struct repository *r) |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 268 | { |
Stefan Beller | eee4502 | 2018-05-17 15:51:52 -0700 | [diff] [blame] | 269 | if (r->parsed_objects->is_shallow == -1) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 270 | BUG("shallow must be initialized by now"); |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 271 | |
Nguyễn Thái Ngọc Duy | 34e7771 | 2019-06-27 16:28:52 +0700 | [diff] [blame] | 272 | if (!stat_validity_check(r->parsed_objects->shallow_stat, |
| 273 | git_path_shallow(r))) |
Jeff King | 0cc77c3 | 2014-02-27 05:56:31 -0500 | [diff] [blame] | 274 | die("shallow file has changed since we read it"); |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 275 | } |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 276 | |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 277 | #define SEEN_ONLY 1 |
| 278 | #define VERBOSE 2 |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 279 | #define QUICK 4 |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 280 | |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 281 | struct write_shallow_data { |
| 282 | struct strbuf *out; |
| 283 | int use_pack_protocol; |
| 284 | int count; |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 285 | unsigned flags; |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
| 289 | { |
| 290 | struct write_shallow_data *data = cb_data; |
brian m. carlson | 7683e2e | 2015-03-13 23:39:34 +0000 | [diff] [blame] | 291 | const char *hex = oid_to_hex(&graft->oid); |
Nguyễn Thái Ngọc Duy | 6a3bbb4 | 2013-08-16 16:52:03 +0700 | [diff] [blame] | 292 | if (graft->nr_parent != -1) |
| 293 | return 0; |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 294 | if (data->flags & QUICK) { |
| 295 | if (!has_object_file(&graft->oid)) |
| 296 | return 0; |
| 297 | } else if (data->flags & SEEN_ONLY) { |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 298 | struct commit *c = lookup_commit(the_repository, &graft->oid); |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 299 | if (!c || !(c->object.flags & SEEN)) { |
| 300 | if (data->flags & VERBOSE) |
| 301 | printf("Removing %s from .git/shallow\n", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 302 | oid_to_hex(&c->object.oid)); |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | } |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 306 | data->count++; |
| 307 | if (data->use_pack_protocol) |
| 308 | packet_buf_write(data->out, "shallow %s", hex); |
| 309 | else { |
| 310 | strbuf_addstr(data->out, hex); |
| 311 | strbuf_addch(data->out, '\n'); |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 316 | static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 317 | const struct oid_array *extra, |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 318 | unsigned flags) |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 319 | { |
| 320 | struct write_shallow_data data; |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 321 | int i; |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 322 | data.out = out; |
| 323 | data.use_pack_protocol = use_pack_protocol; |
| 324 | data.count = 0; |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 325 | data.flags = flags; |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 326 | for_each_commit_graft(write_one_shallow, &data); |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 327 | if (!extra) |
| 328 | return data.count; |
| 329 | for (i = 0; i < extra->nr; i++) { |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 330 | strbuf_addstr(out, oid_to_hex(extra->oid + i)); |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 331 | strbuf_addch(out, '\n'); |
| 332 | data.count++; |
| 333 | } |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 334 | return data.count; |
| 335 | } |
| 336 | |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 337 | int write_shallow_commits(struct strbuf *out, int use_pack_protocol, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 338 | const struct oid_array *extra) |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 339 | { |
| 340 | return write_shallow_commits_1(out, use_pack_protocol, extra, 0); |
| 341 | } |
| 342 | |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 343 | const char *setup_temporary_shallow(const struct oid_array *extra) |
Jeff King | 0179c94 | 2014-02-27 06:25:20 -0500 | [diff] [blame] | 344 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 345 | struct tempfile *temp; |
Nguyễn Thái Ngọc Duy | 08ea65a | 2013-08-16 16:52:04 +0700 | [diff] [blame] | 346 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 08ea65a | 2013-08-16 16:52:04 +0700 | [diff] [blame] | 347 | |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 348 | if (write_shallow_commits(&sb, 0, extra)) { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 349 | temp = xmks_tempfile(git_path("shallow_XXXXXX")); |
Jeff King | 0179c94 | 2014-02-27 06:25:20 -0500 | [diff] [blame] | 350 | |
Junio C Hamano | c50424a | 2017-09-25 15:24:06 +0900 | [diff] [blame] | 351 | if (write_in_full(temp->fd, sb.buf, sb.len) < 0 || |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 352 | close_tempfile_gently(temp) < 0) |
Nguyễn Thái Ngọc Duy | 08ea65a | 2013-08-16 16:52:04 +0700 | [diff] [blame] | 353 | die_errno("failed to write to %s", |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 354 | get_tempfile_path(temp)); |
Nguyễn Thái Ngọc Duy | 08ea65a | 2013-08-16 16:52:04 +0700 | [diff] [blame] | 355 | strbuf_release(&sb); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 356 | return get_tempfile_path(temp); |
Nguyễn Thái Ngọc Duy | 08ea65a | 2013-08-16 16:52:04 +0700 | [diff] [blame] | 357 | } |
| 358 | /* |
| 359 | * is_repository_shallow() sees empty string as "no shallow |
| 360 | * file". |
| 361 | */ |
Jeff King | 0899013 | 2017-09-05 08:14:16 -0400 | [diff] [blame] | 362 | return ""; |
Nguyễn Thái Ngọc Duy | 08ea65a | 2013-08-16 16:52:04 +0700 | [diff] [blame] | 363 | } |
| 364 | |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 365 | void setup_alternate_shallow(struct shallow_lock *shallow_lock, |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 366 | const char **alternate_shallow_file, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 367 | const struct oid_array *extra) |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 368 | { |
| 369 | struct strbuf sb = STRBUF_INIT; |
| 370 | int fd; |
| 371 | |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 372 | fd = hold_lock_file_for_update(&shallow_lock->lock, |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 373 | git_path_shallow(the_repository), |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 374 | LOCK_DIE_ON_ERROR); |
Stefan Beller | 22bdc7c | 2018-05-17 15:51:45 -0700 | [diff] [blame] | 375 | check_shallow_file_for_update(the_repository); |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 376 | if (write_shallow_commits(&sb, 0, extra)) { |
Jeff King | 06f46f2 | 2017-09-13 13:16:03 -0400 | [diff] [blame] | 377 | if (write_in_full(fd, sb.buf, sb.len) < 0) |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 378 | die_errno("failed to write to %s", |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 379 | get_lock_file_path(&shallow_lock->lock)); |
| 380 | *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock); |
Nguyễn Thái Ngọc Duy | 3125fe5 | 2013-08-16 16:52:02 +0700 | [diff] [blame] | 381 | } else |
| 382 | /* |
| 383 | * is_repository_shallow() sees empty string as "no |
| 384 | * shallow file". |
| 385 | */ |
| 386 | *alternate_shallow_file = ""; |
| 387 | strbuf_release(&sb); |
| 388 | } |
Nguyễn Thái Ngọc Duy | ad49136 | 2013-12-05 20:02:32 +0700 | [diff] [blame] | 389 | |
| 390 | static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb) |
| 391 | { |
| 392 | int fd = *(int *)cb; |
| 393 | if (graft->nr_parent == -1) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 394 | packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid)); |
Nguyễn Thái Ngọc Duy | ad49136 | 2013-12-05 20:02:32 +0700 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | void advertise_shallow_grafts(int fd) |
| 399 | { |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 400 | if (!is_repository_shallow(the_repository)) |
Nguyễn Thái Ngọc Duy | ad49136 | 2013-12-05 20:02:32 +0700 | [diff] [blame] | 401 | return; |
| 402 | for_each_commit_graft(advertise_shallow_grafts_cb, &fd); |
| 403 | } |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 404 | |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 405 | /* |
| 406 | * mark_reachable_objects() should have been run prior to this and all |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 407 | * reachable commits marked as "SEEN", except when quick_prune is non-zero, |
| 408 | * in which case lines are excised from the shallow file if they refer to |
| 409 | * commits that do not exist (any longer). |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 410 | */ |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 411 | void prune_shallow(unsigned options) |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 412 | { |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 413 | struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT; |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 414 | struct strbuf sb = STRBUF_INIT; |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 415 | unsigned flags = SEEN_ONLY; |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 416 | int fd; |
| 417 | |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 418 | if (options & PRUNE_QUICK) |
| 419 | flags |= QUICK; |
| 420 | |
| 421 | if (options & PRUNE_SHOW_ONLY) { |
| 422 | flags |= VERBOSE; |
| 423 | write_shallow_commits_1(&sb, 0, NULL, flags); |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 424 | strbuf_release(&sb); |
| 425 | return; |
| 426 | } |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 427 | fd = hold_lock_file_for_update(&shallow_lock.lock, |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 428 | git_path_shallow(the_repository), |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 429 | LOCK_DIE_ON_ERROR); |
Stefan Beller | 22bdc7c | 2018-05-17 15:51:45 -0700 | [diff] [blame] | 430 | check_shallow_file_for_update(the_repository); |
Johannes Schindelin | 2588f6e | 2018-10-24 08:56:12 -0700 | [diff] [blame] | 431 | if (write_shallow_commits_1(&sb, 0, NULL, flags)) { |
Jeff King | 06f46f2 | 2017-09-13 13:16:03 -0400 | [diff] [blame] | 432 | if (write_in_full(fd, sb.buf, sb.len) < 0) |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 433 | die_errno("failed to write to %s", |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 434 | get_lock_file_path(&shallow_lock.lock)); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 435 | commit_shallow_file(the_repository, &shallow_lock); |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 436 | } else { |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 437 | unlink(git_path_shallow(the_repository)); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 438 | rollback_shallow_file(the_repository, &shallow_lock); |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 439 | } |
| 440 | strbuf_release(&sb); |
| 441 | } |
| 442 | |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 443 | struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW); |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * Step 1, split sender shallow commits into "ours" and "theirs" |
| 447 | * Step 2, clean "ours" based on .git/shallow |
| 448 | */ |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 449 | void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa) |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 450 | { |
| 451 | int i; |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 452 | trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n"); |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 453 | memset(info, 0, sizeof(*info)); |
| 454 | info->shallow = sa; |
| 455 | if (!sa) |
| 456 | return; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 457 | ALLOC_ARRAY(info->ours, sa->nr); |
| 458 | ALLOC_ARRAY(info->theirs, sa->nr); |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 459 | for (i = 0; i < sa->nr; i++) { |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 460 | if (has_object_file(sa->oid + i)) { |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 461 | struct commit_graft *graft; |
Jonathan Nieder | 1f93ecd | 2018-05-17 15:51:42 -0700 | [diff] [blame] | 462 | graft = lookup_commit_graft(the_repository, |
| 463 | &sa->oid[i]); |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 464 | if (graft && graft->nr_parent < 0) |
| 465 | continue; |
| 466 | info->ours[info->nr_ours++] = i; |
| 467 | } else |
| 468 | info->theirs[info->nr_theirs++] = i; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void clear_shallow_info(struct shallow_info *info) |
| 473 | { |
| 474 | free(info->ours); |
| 475 | free(info->theirs); |
| 476 | } |
| 477 | |
| 478 | /* Step 4, remove non-existent ones in "theirs" after getting the pack */ |
| 479 | |
| 480 | void remove_nonexistent_theirs_shallow(struct shallow_info *info) |
| 481 | { |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 482 | struct object_id *oid = info->shallow->oid; |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 483 | int i, dst; |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 484 | trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n"); |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 485 | for (i = dst = 0; i < info->nr_theirs; i++) { |
| 486 | if (i != dst) |
| 487 | info->theirs[dst] = info->theirs[i]; |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 488 | if (has_object_file(oid + info->theirs[i])) |
Nguyễn Thái Ngọc Duy | 58babff | 2013-12-05 20:02:35 +0700 | [diff] [blame] | 489 | dst++; |
| 490 | } |
| 491 | info->nr_theirs = dst; |
| 492 | } |
| 493 | |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 494 | define_commit_slab(ref_bitmap, uint32_t *); |
| 495 | |
Nguyễn Thái Ngọc Duy | 6bc3d8c | 2016-12-06 19:53:35 +0700 | [diff] [blame] | 496 | #define POOL_SIZE (512 * 1024) |
| 497 | |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 498 | struct paint_info { |
| 499 | struct ref_bitmap ref_bitmap; |
| 500 | unsigned nr_bits; |
Nguyễn Thái Ngọc Duy | 0afd307 | 2016-12-06 19:53:34 +0700 | [diff] [blame] | 501 | char **pools; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 502 | char *free, *end; |
Nguyễn Thái Ngọc Duy | 0afd307 | 2016-12-06 19:53:34 +0700 | [diff] [blame] | 503 | unsigned pool_count; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | static uint32_t *paint_alloc(struct paint_info *info) |
| 507 | { |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 508 | unsigned nr = DIV_ROUND_UP(info->nr_bits, 32); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 509 | unsigned size = nr * sizeof(uint32_t); |
| 510 | void *p; |
Rasmus Villemoes | 381aa8e | 2016-12-06 19:53:37 +0700 | [diff] [blame] | 511 | if (!info->pool_count || size > info->end - info->free) { |
Nguyễn Thái Ngọc Duy | f2386c6 | 2016-12-06 19:53:36 +0700 | [diff] [blame] | 512 | if (size > POOL_SIZE) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 513 | BUG("pool size too small for %d in paint_alloc()", |
Nguyễn Thái Ngọc Duy | f2386c6 | 2016-12-06 19:53:36 +0700 | [diff] [blame] | 514 | size); |
Nguyễn Thái Ngọc Duy | 0afd307 | 2016-12-06 19:53:34 +0700 | [diff] [blame] | 515 | info->pool_count++; |
| 516 | REALLOC_ARRAY(info->pools, info->pool_count); |
Nguyễn Thái Ngọc Duy | 6bc3d8c | 2016-12-06 19:53:35 +0700 | [diff] [blame] | 517 | info->free = xmalloc(POOL_SIZE); |
Nguyễn Thái Ngọc Duy | 0afd307 | 2016-12-06 19:53:34 +0700 | [diff] [blame] | 518 | info->pools[info->pool_count - 1] = info->free; |
Nguyễn Thái Ngọc Duy | 6bc3d8c | 2016-12-06 19:53:35 +0700 | [diff] [blame] | 519 | info->end = info->free + POOL_SIZE; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 520 | } |
| 521 | p = info->free; |
| 522 | info->free += size; |
| 523 | return p; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Given a commit SHA-1, walk down to parents until either SEEN, |
| 528 | * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for |
| 529 | * all walked commits. |
| 530 | */ |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 531 | static void paint_down(struct paint_info *info, const struct object_id *oid, |
Rasmus Villemoes | 1127b3c | 2016-12-06 19:53:38 +0700 | [diff] [blame] | 532 | unsigned int id) |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 533 | { |
| 534 | unsigned int i, nr; |
| 535 | struct commit_list *head = NULL; |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 536 | int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32); |
René Scharfe | 50492f7 | 2016-07-30 20:18:31 +0200 | [diff] [blame] | 537 | size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr); |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 538 | struct commit *c = lookup_commit_reference_gently(the_repository, oid, |
| 539 | 1); |
Johannes Schindelin | 7c565a6 | 2017-05-04 15:58:35 +0200 | [diff] [blame] | 540 | uint32_t *tmp; /* to be freed before return */ |
| 541 | uint32_t *bitmap; |
| 542 | |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 543 | if (!c) |
| 544 | return; |
Johannes Schindelin | 7c565a6 | 2017-05-04 15:58:35 +0200 | [diff] [blame] | 545 | |
| 546 | tmp = xmalloc(bitmap_size); |
| 547 | bitmap = paint_alloc(info); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 548 | memset(bitmap, 0, bitmap_size); |
Rasmus Villemoes | 1127b3c | 2016-12-06 19:53:38 +0700 | [diff] [blame] | 549 | bitmap[id / 32] |= (1U << (id % 32)); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 550 | commit_list_insert(c, &head); |
| 551 | while (head) { |
| 552 | struct commit_list *p; |
René Scharfe | e510ab8 | 2015-10-24 18:21:31 +0200 | [diff] [blame] | 553 | struct commit *c = pop_commit(&head); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 554 | uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c); |
| 555 | |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 556 | /* XXX check "UNINTERESTING" from pack bitmaps if available */ |
| 557 | if (c->object.flags & (SEEN | UNINTERESTING)) |
| 558 | continue; |
| 559 | else |
| 560 | c->object.flags |= SEEN; |
| 561 | |
| 562 | if (*refs == NULL) |
| 563 | *refs = bitmap; |
| 564 | else { |
| 565 | memcpy(tmp, *refs, bitmap_size); |
| 566 | for (i = 0; i < bitmap_nr; i++) |
| 567 | tmp[i] |= bitmap[i]; |
| 568 | if (memcmp(tmp, *refs, bitmap_size)) { |
| 569 | *refs = paint_alloc(info); |
| 570 | memcpy(*refs, tmp, bitmap_size); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if (c->object.flags & BOTTOM) |
| 575 | continue; |
| 576 | |
| 577 | if (parse_commit(c)) |
| 578 | die("unable to parse commit %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 579 | oid_to_hex(&c->object.oid)); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 580 | |
| 581 | for (p = c->parents; p; p = p->next) { |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 582 | if (p->item->object.flags & SEEN) |
| 583 | continue; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 584 | commit_list_insert(p->item, &head); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | nr = get_max_object_index(); |
| 589 | for (i = 0; i < nr; i++) { |
| 590 | struct object *o = get_indexed_object(i); |
| 591 | if (o && o->type == OBJ_COMMIT) |
| 592 | o->flags &= ~SEEN; |
| 593 | } |
| 594 | |
| 595 | free(tmp); |
| 596 | } |
| 597 | |
Michael Haggerty | 580b04e | 2015-05-25 18:39:06 +0000 | [diff] [blame] | 598 | static int mark_uninteresting(const char *refname, const struct object_id *oid, |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 599 | int flags, void *cb_data) |
| 600 | { |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 601 | struct commit *commit = lookup_commit_reference_gently(the_repository, |
| 602 | oid, 1); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 603 | if (!commit) |
| 604 | return 0; |
| 605 | commit->object.flags |= UNINTERESTING; |
| 606 | mark_parents_uninteresting(commit); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static void post_assign_shallow(struct shallow_info *info, |
| 611 | struct ref_bitmap *ref_bitmap, |
| 612 | int *ref_status); |
| 613 | /* |
| 614 | * Step 6(+7), associate shallow commits with new refs |
| 615 | * |
| 616 | * info->ref must be initialized before calling this function. |
| 617 | * |
| 618 | * If used is not NULL, it's an array of info->shallow->nr |
| 619 | * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the |
| 620 | * m-th shallow commit from info->shallow. |
| 621 | * |
| 622 | * If used is NULL, "ours" and "theirs" are updated. And if ref_status |
| 623 | * is not NULL it's an array of ref->nr ints. ref_status[i] is true if |
| 624 | * the ref needs some shallow commits from either info->ours or |
| 625 | * info->theirs. |
| 626 | */ |
| 627 | void assign_shallow_commits_to_refs(struct shallow_info *info, |
| 628 | uint32_t **used, int *ref_status) |
| 629 | { |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 630 | struct object_id *oid = info->shallow->oid; |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 631 | struct oid_array *ref = info->ref; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 632 | unsigned int i, nr; |
| 633 | int *shallow, nr_shallow = 0; |
| 634 | struct paint_info pi; |
| 635 | |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 636 | trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n"); |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 637 | ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 638 | for (i = 0; i < info->nr_ours; i++) |
| 639 | shallow[nr_shallow++] = info->ours[i]; |
| 640 | for (i = 0; i < info->nr_theirs; i++) |
| 641 | shallow[nr_shallow++] = info->theirs[i]; |
| 642 | |
| 643 | /* |
| 644 | * Prepare the commit graph to track what refs can reach what |
| 645 | * (new) shallow commits. |
| 646 | */ |
| 647 | nr = get_max_object_index(); |
| 648 | for (i = 0; i < nr; i++) { |
| 649 | struct object *o = get_indexed_object(i); |
| 650 | if (!o || o->type != OBJ_COMMIT) |
| 651 | continue; |
| 652 | |
| 653 | o->flags &= ~(UNINTERESTING | BOTTOM | SEEN); |
| 654 | } |
| 655 | |
| 656 | memset(&pi, 0, sizeof(pi)); |
| 657 | init_ref_bitmap(&pi.ref_bitmap); |
| 658 | pi.nr_bits = ref->nr; |
| 659 | |
| 660 | /* |
| 661 | * "--not --all" to cut short the traversal if new refs |
| 662 | * connect to old refs. If not (e.g. force ref updates) it'll |
| 663 | * have to go down to the current shallow commits. |
| 664 | */ |
Michael Haggerty | 580b04e | 2015-05-25 18:39:06 +0000 | [diff] [blame] | 665 | head_ref(mark_uninteresting, NULL); |
| 666 | for_each_ref(mark_uninteresting, NULL); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 667 | |
| 668 | /* Mark potential bottoms so we won't go out of bound */ |
| 669 | for (i = 0; i < nr_shallow; i++) { |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 670 | struct commit *c = lookup_commit(the_repository, |
| 671 | &oid[shallow[i]]); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 672 | c->object.flags |= BOTTOM; |
| 673 | } |
| 674 | |
| 675 | for (i = 0; i < ref->nr; i++) |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 676 | paint_down(&pi, ref->oid + i, i); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 677 | |
| 678 | if (used) { |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 679 | int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 680 | memset(used, 0, sizeof(*used) * info->shallow->nr); |
| 681 | for (i = 0; i < nr_shallow; i++) { |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 682 | const struct commit *c = lookup_commit(the_repository, |
| 683 | &oid[shallow[i]]); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 684 | uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c); |
| 685 | if (*map) |
| 686 | used[shallow[i]] = xmemdupz(*map, bitmap_size); |
| 687 | } |
| 688 | /* |
| 689 | * unreachable shallow commits are not removed from |
| 690 | * "ours" and "theirs". The user is supposed to run |
| 691 | * step 7 on every ref separately and not trust "ours" |
| 692 | * and "theirs" any more. |
| 693 | */ |
| 694 | } else |
| 695 | post_assign_shallow(info, &pi.ref_bitmap, ref_status); |
| 696 | |
| 697 | clear_ref_bitmap(&pi.ref_bitmap); |
Nguyễn Thái Ngọc Duy | 0afd307 | 2016-12-06 19:53:34 +0700 | [diff] [blame] | 698 | for (i = 0; i < pi.pool_count; i++) |
| 699 | free(pi.pools[i]); |
| 700 | free(pi.pools); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 701 | free(shallow); |
| 702 | } |
| 703 | |
| 704 | struct commit_array { |
| 705 | struct commit **commits; |
| 706 | int nr, alloc; |
| 707 | }; |
| 708 | |
Michael Haggerty | 580b04e | 2015-05-25 18:39:06 +0000 | [diff] [blame] | 709 | static int add_ref(const char *refname, const struct object_id *oid, |
| 710 | int flags, void *cb_data) |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 711 | { |
| 712 | struct commit_array *ca = cb_data; |
| 713 | ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc); |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 714 | ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository, |
| 715 | oid, 1); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 716 | if (ca->commits[ca->nr]) |
| 717 | ca->nr++; |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap) |
| 722 | { |
Rasmus Villemoes | 1127b3c | 2016-12-06 19:53:38 +0700 | [diff] [blame] | 723 | unsigned int i; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 724 | if (!ref_status) |
| 725 | return; |
| 726 | for (i = 0; i < nr; i++) |
Rasmus Villemoes | 1127b3c | 2016-12-06 19:53:38 +0700 | [diff] [blame] | 727 | if (bitmap[i / 32] & (1U << (i % 32))) |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 728 | ref_status[i]++; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Step 7, reachability test on "ours" at commit level |
| 733 | */ |
| 734 | static void post_assign_shallow(struct shallow_info *info, |
| 735 | struct ref_bitmap *ref_bitmap, |
| 736 | int *ref_status) |
| 737 | { |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 738 | struct object_id *oid = info->shallow->oid; |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 739 | struct commit *c; |
| 740 | uint32_t **bitmap; |
| 741 | int dst, i, j; |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 742 | int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 743 | struct commit_array ca; |
| 744 | |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 745 | trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n"); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 746 | if (ref_status) |
| 747 | memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr); |
| 748 | |
| 749 | /* Remove unreachable shallow commits from "theirs" */ |
| 750 | for (i = dst = 0; i < info->nr_theirs; i++) { |
| 751 | if (i != dst) |
| 752 | info->theirs[dst] = info->theirs[i]; |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 753 | c = lookup_commit(the_repository, &oid[info->theirs[i]]); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 754 | bitmap = ref_bitmap_at(ref_bitmap, c); |
| 755 | if (!*bitmap) |
| 756 | continue; |
| 757 | for (j = 0; j < bitmap_nr; j++) |
| 758 | if (bitmap[0][j]) { |
| 759 | update_refstatus(ref_status, info->ref->nr, *bitmap); |
| 760 | dst++; |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | info->nr_theirs = dst; |
| 765 | |
| 766 | memset(&ca, 0, sizeof(ca)); |
Michael Haggerty | 580b04e | 2015-05-25 18:39:06 +0000 | [diff] [blame] | 767 | head_ref(add_ref, &ca); |
| 768 | for_each_ref(add_ref, &ca); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 769 | |
| 770 | /* Remove unreachable shallow commits from "ours" */ |
| 771 | for (i = dst = 0; i < info->nr_ours; i++) { |
| 772 | if (i != dst) |
| 773 | info->ours[dst] = info->ours[i]; |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 774 | c = lookup_commit(the_repository, &oid[info->ours[i]]); |
Nguyễn Thái Ngọc Duy | 8e27738 | 2013-12-05 20:02:36 +0700 | [diff] [blame] | 775 | bitmap = ref_bitmap_at(ref_bitmap, c); |
| 776 | if (!*bitmap) |
| 777 | continue; |
| 778 | for (j = 0; j < bitmap_nr; j++) |
| 779 | if (bitmap[0][j] && |
| 780 | /* Step 7, reachability test at commit level */ |
| 781 | !in_merge_bases_many(c, ca.nr, ca.commits)) { |
| 782 | update_refstatus(ref_status, info->ref->nr, *bitmap); |
| 783 | dst++; |
| 784 | break; |
| 785 | } |
| 786 | } |
| 787 | info->nr_ours = dst; |
| 788 | |
| 789 | free(ca.commits); |
| 790 | } |
Nguyễn Thái Ngọc Duy | 0a1bc12 | 2013-12-05 20:02:47 +0700 | [diff] [blame] | 791 | |
| 792 | /* (Delayed) step 7, reachability test at commit level */ |
| 793 | int delayed_reachability_test(struct shallow_info *si, int c) |
| 794 | { |
| 795 | if (si->need_reachability_test[c]) { |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 796 | struct commit *commit = lookup_commit(the_repository, |
| 797 | &si->shallow->oid[c]); |
Nguyễn Thái Ngọc Duy | 0a1bc12 | 2013-12-05 20:02:47 +0700 | [diff] [blame] | 798 | |
| 799 | if (!si->commits) { |
| 800 | struct commit_array ca; |
Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 +0000 | [diff] [blame] | 801 | |
Nguyễn Thái Ngọc Duy | 0a1bc12 | 2013-12-05 20:02:47 +0700 | [diff] [blame] | 802 | memset(&ca, 0, sizeof(ca)); |
Michael Haggerty | 580b04e | 2015-05-25 18:39:06 +0000 | [diff] [blame] | 803 | head_ref(add_ref, &ca); |
| 804 | for_each_ref(add_ref, &ca); |
Nguyễn Thái Ngọc Duy | 0a1bc12 | 2013-12-05 20:02:47 +0700 | [diff] [blame] | 805 | si->commits = ca.commits; |
| 806 | si->nr_commits = ca.nr; |
| 807 | } |
| 808 | |
| 809 | si->reachable[c] = in_merge_bases_many(commit, |
| 810 | si->nr_commits, |
| 811 | si->commits); |
| 812 | si->need_reachability_test[c] = 0; |
| 813 | } |
| 814 | return si->reachable[c]; |
| 815 | } |