blob: 7fd04afed19af7de9dc1438b328567d0b7f2b2b4 [file] [log] [blame]
Johannes Schindelined09aef2006-10-30 20:09:06 +01001#include "cache.h"
Jonathan Nieder3f5787f2018-05-15 16:42:18 -07002#include "repository.h"
Michael Haggerty6e122b42015-08-10 11:47:46 +02003#include "tempfile.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02004#include "lockfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07005#include "object-store.h"
Johannes Schindelined09aef2006-10-30 20:09:06 +01006#include "commit.h"
Alexandre Julliardabef3a12006-11-11 14:57:23 +01007#include "tag.h"
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +07008#include "pkt-line.h"
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +07009#include "remote.h"
10#include "refs.h"
11#include "sha1-array.h"
12#include "diff.h"
13#include "revision.h"
14#include "commit-slab.h"
Nguyễn Thái Ngọc Duy3d9ff4d2016-06-12 17:53:57 +070015#include "list-objects.h"
Derrick Stolee64043552018-07-20 16:33:04 +000016#include "commit-reach.h"
Johannes Schindelined09aef2006-10-30 20:09:06 +010017
Stefan Bellereee45022018-05-17 15:51:52 -070018void set_alternate_shallow_file(struct repository *r, const char *path, int override)
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070019{
Stefan Bellereee45022018-05-17 15:51:52 -070020 if (r->parsed_objects->is_shallow != -1)
Johannes Schindelin033abf92018-05-02 11:38:39 +020021 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
Stefan Bellereee45022018-05-17 15:51:52 -070022 if (r->parsed_objects->alternate_shallow_file && !override)
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +070023 return;
Stefan Bellereee45022018-05-17 15:51:52 -070024 free(r->parsed_objects->alternate_shallow_file);
25 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070026}
Johannes Schindelined09aef2006-10-30 20:09:06 +010027
Stefan Bellereee45022018-05-17 15:51:52 -070028int register_shallow(struct repository *r, const struct object_id *oid)
Johannes Schindelined09aef2006-10-30 20:09:06 +010029{
30 struct commit_graft *graft =
31 xmalloc(sizeof(struct commit_graft));
Stefan Bellerc1f5eb42018-06-28 18:21:59 -070032 struct commit *commit = lookup_commit(the_repository, oid);
Johannes Schindelined09aef2006-10-30 20:09:06 +010033
brian m. carlsone92b8482017-05-06 22:10:06 +000034 oidcpy(&graft->oid, oid);
Johannes Schindelined09aef2006-10-30 20:09:06 +010035 graft->nr_parent = -1;
36 if (commit && commit->object.parsed)
37 commit->parents = NULL;
Stefan Bellereee45022018-05-17 15:51:52 -070038 return register_commit_graft(r, graft, 0);
Johannes Schindelined09aef2006-10-30 20:09:06 +010039}
40
Stefan Bellereee45022018-05-17 15:51:52 -070041int is_repository_shallow(struct repository *r)
Johannes Schindelined09aef2006-10-30 20:09:06 +010042{
Jonathan Tanbd0b42a2019-01-10 11:36:45 -080043 /*
44 * NEEDSWORK: This function updates
45 * r->parsed_objects->{is_shallow,shallow_stat} as a side effect but
46 * there is no corresponding function to clear them when the shallow
47 * file is updated.
48 */
49
Johannes Schindelined09aef2006-10-30 20:09:06 +010050 FILE *fp;
51 char buf[1024];
Stefan Bellereee45022018-05-17 15:51:52 -070052 const char *path = r->parsed_objects->alternate_shallow_file;
Johannes Schindelined09aef2006-10-30 20:09:06 +010053
Stefan Bellereee45022018-05-17 15:51:52 -070054 if (r->parsed_objects->is_shallow >= 0)
55 return r->parsed_objects->is_shallow;
Johannes Schindelined09aef2006-10-30 20:09:06 +010056
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070057 if (!path)
Stefan Bellereee45022018-05-17 15:51:52 -070058 path = git_path_shallow(r);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070059 /*
60 * fetch-pack sets '--shallow-file ""' as an indicator that no
61 * shallow file should be used. We could just open it and it
62 * will likely fail. But let's do an explicit check instead.
63 */
Jeff King0cc77c32014-02-27 05:56:31 -050064 if (!*path || (fp = fopen(path, "r")) == NULL) {
Stefan Bellereee45022018-05-17 15:51:52 -070065 stat_validity_clear(r->parsed_objects->shallow_stat);
66 r->parsed_objects->is_shallow = 0;
67 return r->parsed_objects->is_shallow;
Johannes Schindelined09aef2006-10-30 20:09:06 +010068 }
Stefan Bellereee45022018-05-17 15:51:52 -070069 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
70 r->parsed_objects->is_shallow = 1;
Johannes Schindelined09aef2006-10-30 20:09:06 +010071
72 while (fgets(buf, sizeof(buf), fp)) {
brian m. carlsone92b8482017-05-06 22:10:06 +000073 struct object_id oid;
74 if (get_oid_hex(buf, &oid))
Johannes Schindelined09aef2006-10-30 20:09:06 +010075 die("bad shallow line: %s", buf);
Stefan Bellereee45022018-05-17 15:51:52 -070076 register_shallow(r, &oid);
Johannes Schindelined09aef2006-10-30 20:09:06 +010077 }
78 fclose(fp);
Stefan Bellereee45022018-05-17 15:51:52 -070079 return r->parsed_objects->is_shallow;
Johannes Schindelined09aef2006-10-30 20:09:06 +010080}
81
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +020082/*
83 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
84 * supports a "valid" flag.
85 */
86define_commit_slab(commit_depth, int *);
Johannes Schindelinf53514b2006-10-30 20:09:53 +010087struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
88 int shallow_flag, int not_shallow_flag)
Johannes Schindelined09aef2006-10-30 20:09:06 +010089{
90 int i = 0, cur_depth = 0;
91 struct commit_list *result = NULL;
Thiago Farina3cd47452010-08-28 23:04:17 -030092 struct object_array stack = OBJECT_ARRAY_INIT;
Johannes Schindelined09aef2006-10-30 20:09:06 +010093 struct commit *commit = NULL;
Nguyễn Thái Ngọc Duy79d3a232013-12-05 20:02:41 +070094 struct commit_graft *graft;
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +020095 struct commit_depth depths;
Johannes Schindelined09aef2006-10-30 20:09:06 +010096
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +020097 init_commit_depth(&depths);
Johannes Schindelined09aef2006-10-30 20:09:06 +010098 while (commit || i < heads->nr || stack.nr) {
99 struct commit_list *p;
100 if (!commit) {
101 if (i < heads->nr) {
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200102 int **depth_slot;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100103 commit = (struct commit *)
Stefan Bellera74093d2018-06-28 18:22:05 -0700104 deref_tag(the_repository,
105 heads->objects[i++].item,
106 NULL, 0);
Martin Koegleraffeef12008-02-18 08:31:54 +0100107 if (!commit || commit->object.type != OBJ_COMMIT) {
Johannes Schindelined09aef2006-10-30 20:09:06 +0100108 commit = NULL;
109 continue;
110 }
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200111 depth_slot = commit_depth_at(&depths, commit);
112 if (!*depth_slot)
113 *depth_slot = xmalloc(sizeof(int));
114 **depth_slot = 0;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100115 cur_depth = 0;
116 } else {
117 commit = (struct commit *)
Martin Ågren71992032017-09-23 01:34:53 +0200118 object_array_pop(&stack);
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200119 cur_depth = **commit_depth_at(&depths, commit);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100120 }
121 }
Jeff King367068e2013-10-24 04:54:01 -0400122 parse_commit_or_die(commit);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100123 cur_depth++;
Nguyễn Thái Ngọc Duy79d3a232013-12-05 20:02:41 +0700124 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
Stefan Bellerc8813482018-05-17 15:51:46 -0700125 (is_repository_shallow(the_repository) && !commit->parents &&
Jonathan Nieder1f93ecd2018-05-17 15:51:42 -0700126 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
Nguyễn Thái Ngọc Duy79d3a232013-12-05 20:02:41 +0700127 graft->nr_parent < 0)) {
Nguyễn Thái Ngọc Duy682c7d22013-01-11 16:05:47 +0700128 commit_list_insert(commit, &result);
129 commit->object.flags |= shallow_flag;
130 commit = NULL;
131 continue;
132 }
133 commit->object.flags |= not_shallow_flag;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100134 for (p = commit->parents, commit = NULL; p; p = p->next) {
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200135 int **depth_slot = commit_depth_at(&depths, p->item);
136 if (!*depth_slot) {
137 *depth_slot = xmalloc(sizeof(int));
138 **depth_slot = cur_depth;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100139 } else {
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200140 if (cur_depth >= **depth_slot)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100141 continue;
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200142 **depth_slot = cur_depth;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100143 }
Matthijs Kooijman4b796952013-07-11 13:25:52 +0200144 if (p->next)
145 add_object_array(&p->item->object,
146 NULL, &stack);
147 else {
148 commit = p->item;
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200149 cur_depth = **commit_depth_at(&depths, commit);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100150 }
Johannes Schindelined09aef2006-10-30 20:09:06 +0100151 }
152 }
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200153 for (i = 0; i < depths.slab_count; i++) {
154 int j;
155
Ali Utku Selenddb3c852019-10-01 01:33:10 +0200156 if (!depths.slab[i])
157 continue;
Nguyễn Thái Ngọc Duy58dbe582018-05-19 07:28:21 +0200158 for (j = 0; j < depths.slab_size; j++)
159 free(depths.slab[i][j]);
160 }
161 clear_commit_depth(&depths);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100162
163 return result;
164}
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700165
Nguyễn Thái Ngọc Duy3d9ff4d2016-06-12 17:53:57 +0700166static void show_commit(struct commit *commit, void *data)
167{
168 commit_list_insert(commit, data);
169}
170
171/*
172 * Given rev-list arguments, run rev-list. All reachable commits
173 * except border ones are marked with not_shallow_flag. Border commits
174 * are marked with shallow_flag. The list of border/shallow commits
175 * are also returned.
176 */
177struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
178 int shallow_flag,
179 int not_shallow_flag)
180{
181 struct commit_list *result = NULL, *p;
182 struct commit_list *not_shallow_list = NULL;
183 struct rev_info revs;
184 int both_flags = shallow_flag | not_shallow_flag;
185
186 /*
187 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
188 * set at this point. But better be safe than sorry.
189 */
190 clear_object_flags(both_flags);
191
Stefan Bellerc8813482018-05-17 15:51:46 -0700192 is_repository_shallow(the_repository); /* make sure shallows are read */
Nguyễn Thái Ngọc Duy3d9ff4d2016-06-12 17:53:57 +0700193
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200194 repo_init_revisions(the_repository, &revs, NULL);
Nguyễn Thái Ngọc Duy3d9ff4d2016-06-12 17:53:57 +0700195 save_commit_buffer = 0;
196 setup_revisions(ac, av, &revs, NULL);
197
198 if (prepare_revision_walk(&revs))
199 die("revision walk setup failed");
200 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
201
Nguyễn Thái Ngọc Duye34de732018-05-26 13:35:18 +0200202 if (!not_shallow_list)
203 die("no commits selected for shallow requests");
204
Nguyễn Thái Ngọc Duy3d9ff4d2016-06-12 17:53:57 +0700205 /* Mark all reachable commits as NOT_SHALLOW */
206 for (p = not_shallow_list; p; p = p->next)
207 p->item->object.flags |= not_shallow_flag;
208
209 /*
210 * mark border commits SHALLOW + NOT_SHALLOW.
211 * We cannot clear NOT_SHALLOW right now. Imagine border
212 * commit A is processed first, then commit B, whose parent is
213 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
214 * itself is considered border at step 2, which is incorrect.
215 */
216 for (p = not_shallow_list; p; p = p->next) {
217 struct commit *c = p->item;
218 struct commit_list *parent;
219
220 if (parse_commit(c))
221 die("unable to parse commit %s",
222 oid_to_hex(&c->object.oid));
223
224 for (parent = c->parents; parent; parent = parent->next)
225 if (!(parent->item->object.flags & not_shallow_flag)) {
226 c->object.flags |= shallow_flag;
227 commit_list_insert(c, &result);
228 break;
229 }
230 }
231 free_commit_list(not_shallow_list);
232
233 /*
234 * Now we can clean up NOT_SHALLOW on border commits. Having
235 * both flags set can confuse the caller.
236 */
237 for (p = result; p; p = p->next) {
238 struct object *o = &p->item->object;
239 if ((o->flags & both_flags) == both_flags)
240 o->flags &= ~not_shallow_flag;
241 }
242 return result;
243}
244
Stefan Bellereee45022018-05-17 15:51:52 -0700245static void check_shallow_file_for_update(struct repository *r)
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700246{
Stefan Bellereee45022018-05-17 15:51:52 -0700247 if (r->parsed_objects->is_shallow == -1)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200248 BUG("shallow must be initialized by now");
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700249
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700250 if (!stat_validity_check(r->parsed_objects->shallow_stat,
251 git_path_shallow(r)))
Jeff King0cc77c32014-02-27 05:56:31 -0500252 die("shallow file has changed since we read it");
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700253}
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700254
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700255#define SEEN_ONLY 1
256#define VERBOSE 2
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700257#define QUICK 4
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700258
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700259struct write_shallow_data {
260 struct strbuf *out;
261 int use_pack_protocol;
262 int count;
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700263 unsigned flags;
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700264};
265
266static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
267{
268 struct write_shallow_data *data = cb_data;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000269 const char *hex = oid_to_hex(&graft->oid);
Nguyễn Thái Ngọc Duy6a3bbb42013-08-16 16:52:03 +0700270 if (graft->nr_parent != -1)
271 return 0;
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700272 if (data->flags & QUICK) {
273 if (!has_object_file(&graft->oid))
274 return 0;
275 } else if (data->flags & SEEN_ONLY) {
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700276 struct commit *c = lookup_commit(the_repository, &graft->oid);
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700277 if (!c || !(c->object.flags & SEEN)) {
278 if (data->flags & VERBOSE)
279 printf("Removing %s from .git/shallow\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000280 oid_to_hex(&c->object.oid));
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700281 return 0;
282 }
283 }
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700284 data->count++;
285 if (data->use_pack_protocol)
286 packet_buf_write(data->out, "shallow %s", hex);
287 else {
288 strbuf_addstr(data->out, hex);
289 strbuf_addch(data->out, '\n');
290 }
291 return 0;
292}
293
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700294static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
brian m. carlson910650d2017-03-31 01:40:00 +0000295 const struct oid_array *extra,
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700296 unsigned flags)
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700297{
298 struct write_shallow_data data;
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700299 int i;
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700300 data.out = out;
301 data.use_pack_protocol = use_pack_protocol;
302 data.count = 0;
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700303 data.flags = flags;
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700304 for_each_commit_graft(write_one_shallow, &data);
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700305 if (!extra)
306 return data.count;
307 for (i = 0; i < extra->nr; i++) {
brian m. carlsonee3051b2017-03-26 16:01:37 +0000308 strbuf_addstr(out, oid_to_hex(extra->oid + i));
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700309 strbuf_addch(out, '\n');
310 data.count++;
311 }
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700312 return data.count;
313}
314
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700315int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
brian m. carlson910650d2017-03-31 01:40:00 +0000316 const struct oid_array *extra)
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700317{
318 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
319}
320
brian m. carlson910650d2017-03-31 01:40:00 +0000321const char *setup_temporary_shallow(const struct oid_array *extra)
Jeff King0179c942014-02-27 06:25:20 -0500322{
Jeff King076aa2c2017-09-05 08:15:08 -0400323 struct tempfile *temp;
Nguyễn Thái Ngọc Duy08ea65a2013-08-16 16:52:04 +0700324 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy08ea65a2013-08-16 16:52:04 +0700325
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700326 if (write_shallow_commits(&sb, 0, extra)) {
Jeff King076aa2c2017-09-05 08:15:08 -0400327 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
Jeff King0179c942014-02-27 06:25:20 -0500328
Junio C Hamanoc50424a2017-09-25 15:24:06 +0900329 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -0400330 close_tempfile_gently(temp) < 0)
Nguyễn Thái Ngọc Duy08ea65a2013-08-16 16:52:04 +0700331 die_errno("failed to write to %s",
Jeff King076aa2c2017-09-05 08:15:08 -0400332 get_tempfile_path(temp));
Nguyễn Thái Ngọc Duy08ea65a2013-08-16 16:52:04 +0700333 strbuf_release(&sb);
Jeff King076aa2c2017-09-05 08:15:08 -0400334 return get_tempfile_path(temp);
Nguyễn Thái Ngọc Duy08ea65a2013-08-16 16:52:04 +0700335 }
336 /*
337 * is_repository_shallow() sees empty string as "no shallow
338 * file".
339 */
Jeff King08990132017-09-05 08:14:16 -0400340 return "";
Nguyễn Thái Ngọc Duy08ea65a2013-08-16 16:52:04 +0700341}
342
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700343void setup_alternate_shallow(struct lock_file *shallow_lock,
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700344 const char **alternate_shallow_file,
brian m. carlson910650d2017-03-31 01:40:00 +0000345 const struct oid_array *extra)
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700346{
347 struct strbuf sb = STRBUF_INIT;
348 int fd;
349
Stefan Beller102de882018-05-17 15:51:51 -0700350 fd = hold_lock_file_for_update(shallow_lock,
351 git_path_shallow(the_repository),
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700352 LOCK_DIE_ON_ERROR);
Stefan Beller22bdc7c2018-05-17 15:51:45 -0700353 check_shallow_file_for_update(the_repository);
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700354 if (write_shallow_commits(&sb, 0, extra)) {
Jeff King06f46f22017-09-13 13:16:03 -0400355 if (write_in_full(fd, sb.buf, sb.len) < 0)
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700356 die_errno("failed to write to %s",
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200357 get_lock_file_path(shallow_lock));
358 *alternate_shallow_file = get_lock_file_path(shallow_lock);
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700359 } else
360 /*
361 * is_repository_shallow() sees empty string as "no
362 * shallow file".
363 */
364 *alternate_shallow_file = "";
365 strbuf_release(&sb);
366}
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +0700367
368static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
369{
370 int fd = *(int *)cb;
371 if (graft->nr_parent == -1)
Lars Schneider81c634e2016-10-16 16:20:29 -0700372 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +0700373 return 0;
374}
375
376void advertise_shallow_grafts(int fd)
377{
Stefan Bellerc8813482018-05-17 15:51:46 -0700378 if (!is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +0700379 return;
380 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
381}
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700382
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700383/*
384 * mark_reachable_objects() should have been run prior to this and all
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700385 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
386 * in which case lines are excised from the shallow file if they refer to
387 * commits that do not exist (any longer).
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700388 */
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700389void prune_shallow(unsigned options)
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700390{
Martin Ågrenb2275862018-05-09 22:55:38 +0200391 struct lock_file shallow_lock = LOCK_INIT;
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700392 struct strbuf sb = STRBUF_INIT;
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700393 unsigned flags = SEEN_ONLY;
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700394 int fd;
395
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700396 if (options & PRUNE_QUICK)
397 flags |= QUICK;
398
399 if (options & PRUNE_SHOW_ONLY) {
400 flags |= VERBOSE;
401 write_shallow_commits_1(&sb, 0, NULL, flags);
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700402 strbuf_release(&sb);
403 return;
404 }
Stefan Beller102de882018-05-17 15:51:51 -0700405 fd = hold_lock_file_for_update(&shallow_lock,
406 git_path_shallow(the_repository),
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700407 LOCK_DIE_ON_ERROR);
Stefan Beller22bdc7c2018-05-17 15:51:45 -0700408 check_shallow_file_for_update(the_repository);
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700409 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
Jeff King06f46f22017-09-13 13:16:03 -0400410 if (write_in_full(fd, sb.buf, sb.len) < 0)
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700411 die_errno("failed to write to %s",
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200412 get_lock_file_path(&shallow_lock));
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700413 commit_lock_file(&shallow_lock);
414 } else {
Stefan Beller102de882018-05-17 15:51:51 -0700415 unlink(git_path_shallow(the_repository));
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700416 rollback_lock_file(&shallow_lock);
417 }
418 strbuf_release(&sb);
419}
420
Karsten Blees6aa30852014-07-12 02:00:06 +0200421struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700422
423/*
424 * Step 1, split sender shallow commits into "ours" and "theirs"
425 * Step 2, clean "ours" based on .git/shallow
426 */
brian m. carlson910650d2017-03-31 01:40:00 +0000427void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700428{
429 int i;
Karsten Blees6aa30852014-07-12 02:00:06 +0200430 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700431 memset(info, 0, sizeof(*info));
432 info->shallow = sa;
433 if (!sa)
434 return;
Jeff Kingb32fa952016-02-22 17:44:25 -0500435 ALLOC_ARRAY(info->ours, sa->nr);
436 ALLOC_ARRAY(info->theirs, sa->nr);
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700437 for (i = 0; i < sa->nr; i++) {
brian m. carlsonee3051b2017-03-26 16:01:37 +0000438 if (has_object_file(sa->oid + i)) {
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700439 struct commit_graft *graft;
Jonathan Nieder1f93ecd2018-05-17 15:51:42 -0700440 graft = lookup_commit_graft(the_repository,
441 &sa->oid[i]);
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700442 if (graft && graft->nr_parent < 0)
443 continue;
444 info->ours[info->nr_ours++] = i;
445 } else
446 info->theirs[info->nr_theirs++] = i;
447 }
448}
449
450void clear_shallow_info(struct shallow_info *info)
451{
452 free(info->ours);
453 free(info->theirs);
454}
455
456/* Step 4, remove non-existent ones in "theirs" after getting the pack */
457
458void remove_nonexistent_theirs_shallow(struct shallow_info *info)
459{
brian m. carlsonee3051b2017-03-26 16:01:37 +0000460 struct object_id *oid = info->shallow->oid;
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700461 int i, dst;
Karsten Blees6aa30852014-07-12 02:00:06 +0200462 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700463 for (i = dst = 0; i < info->nr_theirs; i++) {
464 if (i != dst)
465 info->theirs[dst] = info->theirs[i];
brian m. carlsonee3051b2017-03-26 16:01:37 +0000466 if (has_object_file(oid + info->theirs[i]))
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700467 dst++;
468 }
469 info->nr_theirs = dst;
470}
471
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700472define_commit_slab(ref_bitmap, uint32_t *);
473
Nguyễn Thái Ngọc Duy6bc3d8c2016-12-06 19:53:35 +0700474#define POOL_SIZE (512 * 1024)
475
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700476struct paint_info {
477 struct ref_bitmap ref_bitmap;
478 unsigned nr_bits;
Nguyễn Thái Ngọc Duy0afd3072016-12-06 19:53:34 +0700479 char **pools;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700480 char *free, *end;
Nguyễn Thái Ngọc Duy0afd3072016-12-06 19:53:34 +0700481 unsigned pool_count;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700482};
483
484static uint32_t *paint_alloc(struct paint_info *info)
485{
René Scharfe42c78a22017-07-08 12:35:35 +0200486 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700487 unsigned size = nr * sizeof(uint32_t);
488 void *p;
Rasmus Villemoes381aa8e2016-12-06 19:53:37 +0700489 if (!info->pool_count || size > info->end - info->free) {
Nguyễn Thái Ngọc Duyf2386c62016-12-06 19:53:36 +0700490 if (size > POOL_SIZE)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200491 BUG("pool size too small for %d in paint_alloc()",
Nguyễn Thái Ngọc Duyf2386c62016-12-06 19:53:36 +0700492 size);
Nguyễn Thái Ngọc Duy0afd3072016-12-06 19:53:34 +0700493 info->pool_count++;
494 REALLOC_ARRAY(info->pools, info->pool_count);
Nguyễn Thái Ngọc Duy6bc3d8c2016-12-06 19:53:35 +0700495 info->free = xmalloc(POOL_SIZE);
Nguyễn Thái Ngọc Duy0afd3072016-12-06 19:53:34 +0700496 info->pools[info->pool_count - 1] = info->free;
Nguyễn Thái Ngọc Duy6bc3d8c2016-12-06 19:53:35 +0700497 info->end = info->free + POOL_SIZE;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700498 }
499 p = info->free;
500 info->free += size;
501 return p;
502}
503
504/*
505 * Given a commit SHA-1, walk down to parents until either SEEN,
506 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
507 * all walked commits.
508 */
brian m. carlson1e43ed92017-05-06 22:10:09 +0000509static void paint_down(struct paint_info *info, const struct object_id *oid,
Rasmus Villemoes1127b3c2016-12-06 19:53:38 +0700510 unsigned int id)
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700511{
512 unsigned int i, nr;
513 struct commit_list *head = NULL;
René Scharfe42c78a22017-07-08 12:35:35 +0200514 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
René Scharfe50492f72016-07-30 20:18:31 +0200515 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
Stefan Beller21e1ee82018-06-28 18:21:57 -0700516 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
517 1);
Johannes Schindelin7c565a62017-05-04 15:58:35 +0200518 uint32_t *tmp; /* to be freed before return */
519 uint32_t *bitmap;
520
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700521 if (!c)
522 return;
Johannes Schindelin7c565a62017-05-04 15:58:35 +0200523
524 tmp = xmalloc(bitmap_size);
525 bitmap = paint_alloc(info);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700526 memset(bitmap, 0, bitmap_size);
Rasmus Villemoes1127b3c2016-12-06 19:53:38 +0700527 bitmap[id / 32] |= (1U << (id % 32));
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700528 commit_list_insert(c, &head);
529 while (head) {
530 struct commit_list *p;
René Scharfee510ab82015-10-24 18:21:31 +0200531 struct commit *c = pop_commit(&head);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700532 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
533
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700534 /* XXX check "UNINTERESTING" from pack bitmaps if available */
535 if (c->object.flags & (SEEN | UNINTERESTING))
536 continue;
537 else
538 c->object.flags |= SEEN;
539
540 if (*refs == NULL)
541 *refs = bitmap;
542 else {
543 memcpy(tmp, *refs, bitmap_size);
544 for (i = 0; i < bitmap_nr; i++)
545 tmp[i] |= bitmap[i];
546 if (memcmp(tmp, *refs, bitmap_size)) {
547 *refs = paint_alloc(info);
548 memcpy(*refs, tmp, bitmap_size);
549 }
550 }
551
552 if (c->object.flags & BOTTOM)
553 continue;
554
555 if (parse_commit(c))
556 die("unable to parse commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000557 oid_to_hex(&c->object.oid));
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700558
559 for (p = c->parents; p; p = p->next) {
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700560 if (p->item->object.flags & SEEN)
561 continue;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700562 commit_list_insert(p->item, &head);
563 }
564 }
565
566 nr = get_max_object_index();
567 for (i = 0; i < nr; i++) {
568 struct object *o = get_indexed_object(i);
569 if (o && o->type == OBJ_COMMIT)
570 o->flags &= ~SEEN;
571 }
572
573 free(tmp);
574}
575
Michael Haggerty580b04e2015-05-25 18:39:06 +0000576static int mark_uninteresting(const char *refname, const struct object_id *oid,
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700577 int flags, void *cb_data)
578{
Stefan Beller21e1ee82018-06-28 18:21:57 -0700579 struct commit *commit = lookup_commit_reference_gently(the_repository,
580 oid, 1);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700581 if (!commit)
582 return 0;
583 commit->object.flags |= UNINTERESTING;
584 mark_parents_uninteresting(commit);
585 return 0;
586}
587
588static void post_assign_shallow(struct shallow_info *info,
589 struct ref_bitmap *ref_bitmap,
590 int *ref_status);
591/*
592 * Step 6(+7), associate shallow commits with new refs
593 *
594 * info->ref must be initialized before calling this function.
595 *
596 * If used is not NULL, it's an array of info->shallow->nr
597 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
598 * m-th shallow commit from info->shallow.
599 *
600 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
601 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
602 * the ref needs some shallow commits from either info->ours or
603 * info->theirs.
604 */
605void assign_shallow_commits_to_refs(struct shallow_info *info,
606 uint32_t **used, int *ref_status)
607{
brian m. carlsonee3051b2017-03-26 16:01:37 +0000608 struct object_id *oid = info->shallow->oid;
brian m. carlson910650d2017-03-31 01:40:00 +0000609 struct oid_array *ref = info->ref;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700610 unsigned int i, nr;
611 int *shallow, nr_shallow = 0;
612 struct paint_info pi;
613
Karsten Blees6aa30852014-07-12 02:00:06 +0200614 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
Jeff Kingb32fa952016-02-22 17:44:25 -0500615 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700616 for (i = 0; i < info->nr_ours; i++)
617 shallow[nr_shallow++] = info->ours[i];
618 for (i = 0; i < info->nr_theirs; i++)
619 shallow[nr_shallow++] = info->theirs[i];
620
621 /*
622 * Prepare the commit graph to track what refs can reach what
623 * (new) shallow commits.
624 */
625 nr = get_max_object_index();
626 for (i = 0; i < nr; i++) {
627 struct object *o = get_indexed_object(i);
628 if (!o || o->type != OBJ_COMMIT)
629 continue;
630
631 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
632 }
633
634 memset(&pi, 0, sizeof(pi));
635 init_ref_bitmap(&pi.ref_bitmap);
636 pi.nr_bits = ref->nr;
637
638 /*
639 * "--not --all" to cut short the traversal if new refs
640 * connect to old refs. If not (e.g. force ref updates) it'll
641 * have to go down to the current shallow commits.
642 */
Michael Haggerty580b04e2015-05-25 18:39:06 +0000643 head_ref(mark_uninteresting, NULL);
644 for_each_ref(mark_uninteresting, NULL);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700645
646 /* Mark potential bottoms so we won't go out of bound */
647 for (i = 0; i < nr_shallow; i++) {
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700648 struct commit *c = lookup_commit(the_repository,
649 &oid[shallow[i]]);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700650 c->object.flags |= BOTTOM;
651 }
652
653 for (i = 0; i < ref->nr; i++)
brian m. carlson1e43ed92017-05-06 22:10:09 +0000654 paint_down(&pi, ref->oid + i, i);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700655
656 if (used) {
René Scharfe42c78a22017-07-08 12:35:35 +0200657 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700658 memset(used, 0, sizeof(*used) * info->shallow->nr);
659 for (i = 0; i < nr_shallow; i++) {
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700660 const struct commit *c = lookup_commit(the_repository,
661 &oid[shallow[i]]);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700662 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
663 if (*map)
664 used[shallow[i]] = xmemdupz(*map, bitmap_size);
665 }
666 /*
667 * unreachable shallow commits are not removed from
668 * "ours" and "theirs". The user is supposed to run
669 * step 7 on every ref separately and not trust "ours"
670 * and "theirs" any more.
671 */
672 } else
673 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
674
675 clear_ref_bitmap(&pi.ref_bitmap);
Nguyễn Thái Ngọc Duy0afd3072016-12-06 19:53:34 +0700676 for (i = 0; i < pi.pool_count; i++)
677 free(pi.pools[i]);
678 free(pi.pools);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700679 free(shallow);
680}
681
682struct commit_array {
683 struct commit **commits;
684 int nr, alloc;
685};
686
Michael Haggerty580b04e2015-05-25 18:39:06 +0000687static int add_ref(const char *refname, const struct object_id *oid,
688 int flags, void *cb_data)
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700689{
690 struct commit_array *ca = cb_data;
691 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
Stefan Beller21e1ee82018-06-28 18:21:57 -0700692 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
693 oid, 1);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700694 if (ca->commits[ca->nr])
695 ca->nr++;
696 return 0;
697}
698
699static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
700{
Rasmus Villemoes1127b3c2016-12-06 19:53:38 +0700701 unsigned int i;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700702 if (!ref_status)
703 return;
704 for (i = 0; i < nr; i++)
Rasmus Villemoes1127b3c2016-12-06 19:53:38 +0700705 if (bitmap[i / 32] & (1U << (i % 32)))
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700706 ref_status[i]++;
707}
708
709/*
710 * Step 7, reachability test on "ours" at commit level
711 */
712static void post_assign_shallow(struct shallow_info *info,
713 struct ref_bitmap *ref_bitmap,
714 int *ref_status)
715{
brian m. carlsonee3051b2017-03-26 16:01:37 +0000716 struct object_id *oid = info->shallow->oid;
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700717 struct commit *c;
718 uint32_t **bitmap;
719 int dst, i, j;
René Scharfe42c78a22017-07-08 12:35:35 +0200720 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700721 struct commit_array ca;
722
Karsten Blees6aa30852014-07-12 02:00:06 +0200723 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700724 if (ref_status)
725 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
726
727 /* Remove unreachable shallow commits from "theirs" */
728 for (i = dst = 0; i < info->nr_theirs; i++) {
729 if (i != dst)
730 info->theirs[dst] = info->theirs[i];
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700731 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700732 bitmap = ref_bitmap_at(ref_bitmap, c);
733 if (!*bitmap)
734 continue;
735 for (j = 0; j < bitmap_nr; j++)
736 if (bitmap[0][j]) {
737 update_refstatus(ref_status, info->ref->nr, *bitmap);
738 dst++;
739 break;
740 }
741 }
742 info->nr_theirs = dst;
743
744 memset(&ca, 0, sizeof(ca));
Michael Haggerty580b04e2015-05-25 18:39:06 +0000745 head_ref(add_ref, &ca);
746 for_each_ref(add_ref, &ca);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700747
748 /* Remove unreachable shallow commits from "ours" */
749 for (i = dst = 0; i < info->nr_ours; i++) {
750 if (i != dst)
751 info->ours[dst] = info->ours[i];
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700752 c = lookup_commit(the_repository, &oid[info->ours[i]]);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700753 bitmap = ref_bitmap_at(ref_bitmap, c);
754 if (!*bitmap)
755 continue;
756 for (j = 0; j < bitmap_nr; j++)
757 if (bitmap[0][j] &&
758 /* Step 7, reachability test at commit level */
759 !in_merge_bases_many(c, ca.nr, ca.commits)) {
760 update_refstatus(ref_status, info->ref->nr, *bitmap);
761 dst++;
762 break;
763 }
764 }
765 info->nr_ours = dst;
766
767 free(ca.commits);
768}
Nguyễn Thái Ngọc Duy0a1bc122013-12-05 20:02:47 +0700769
770/* (Delayed) step 7, reachability test at commit level */
771int delayed_reachability_test(struct shallow_info *si, int c)
772{
773 if (si->need_reachability_test[c]) {
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700774 struct commit *commit = lookup_commit(the_repository,
775 &si->shallow->oid[c]);
Nguyễn Thái Ngọc Duy0a1bc122013-12-05 20:02:47 +0700776
777 if (!si->commits) {
778 struct commit_array ca;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000779
Nguyễn Thái Ngọc Duy0a1bc122013-12-05 20:02:47 +0700780 memset(&ca, 0, sizeof(ca));
Michael Haggerty580b04e2015-05-25 18:39:06 +0000781 head_ref(add_ref, &ca);
782 for_each_ref(add_ref, &ca);
Nguyễn Thái Ngọc Duy0a1bc122013-12-05 20:02:47 +0700783 si->commits = ca.commits;
784 si->nr_commits = ca.nr;
785 }
786
787 si->reachable[c] = in_merge_bases_many(commit,
788 si->nr_commits,
789 si->commits);
790 si->need_reachability_test[c] = 0;
791 }
792 return si->reachable[c];
793}