Skip to content

Commit 162a13b

Browse files
committed
Merge branch 'jt/no-abuse-alternate-odb-for-submodules'
Follow through the work to use the repo interface to access submodule objects in-process, instead of abusing the alternate object database interface. * jt/no-abuse-alternate-odb-for-submodules: submodule: trace adding submodule ODB as alternate submodule: pass repo to check_has_commit() object-file: only register submodule ODB if needed merge-{ort,recursive}: remove add_submodule_odb() refs: peeling non-the_repository iterators is BUG refs: teach arbitrary repo support to iterators refs: plumb repo into ref stores
2 parents bfa646c + 71ef66d commit 162a13b

20 files changed

+148
-68
lines changed

merge-ort.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ static int err(struct merge_options *opt, const char *err, ...)
609609

610610
static void format_commit(struct strbuf *sb,
611611
int indent,
612+
struct repository *repo,
612613
struct commit *commit)
613614
{
614615
struct merge_remote_desc *desc;
@@ -622,7 +623,7 @@ static void format_commit(struct strbuf *sb,
622623
return;
623624
}
624625

625-
format_commit_message(commit, "%h %s", sb, &ctx);
626+
repo_format_commit_message(repo, commit, "%h %s", sb, &ctx);
626627
strbuf_addch(sb, '\n');
627628
}
628629

@@ -1578,17 +1579,6 @@ static int merge_submodule(struct merge_options *opt,
15781579
if (is_null_oid(b))
15791580
return 0;
15801581

1581-
/*
1582-
* NEEDSWORK: Remove this when all submodule object accesses are
1583-
* through explicitly specified repositores.
1584-
*/
1585-
if (add_submodule_odb(path)) {
1586-
path_msg(opt, path, 0,
1587-
_("Failed to merge submodule %s (not checked out)"),
1588-
path);
1589-
return 0;
1590-
}
1591-
15921582
if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
15931583
path_msg(opt, path, 0,
15941584
_("Failed to merge submodule %s (not checked out)"),
@@ -1653,7 +1643,7 @@ static int merge_submodule(struct merge_options *opt,
16531643
break;
16541644

16551645
case 1:
1656-
format_commit(&sb, 4,
1646+
format_commit(&sb, 4, &subrepo,
16571647
(struct commit *)merges.objects[0].item);
16581648
path_msg(opt, path, 0,
16591649
_("Failed to merge submodule %s, but a possible merge "
@@ -1670,7 +1660,7 @@ static int merge_submodule(struct merge_options *opt,
16701660
break;
16711661
default:
16721662
for (i = 0; i < merges.nr; i++)
1673-
format_commit(&sb, 4,
1663+
format_commit(&sb, 4, &subrepo,
16741664
(struct commit *)merges.objects[i].item);
16751665
path_msg(opt, path, 0,
16761666
_("Failed to merge submodule %s, but multiple "

merge-recursive.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ static void output(struct merge_options *opt, int v, const char *fmt, ...)
334334
flush_output(opt);
335335
}
336336

337-
static void output_commit_title(struct merge_options *opt, struct commit *commit)
337+
static void repo_output_commit_title(struct merge_options *opt,
338+
struct repository *repo,
339+
struct commit *commit)
338340
{
339341
struct merge_remote_desc *desc;
340342

@@ -343,23 +345,29 @@ static void output_commit_title(struct merge_options *opt, struct commit *commit
343345
if (desc)
344346
strbuf_addf(&opt->obuf, "virtual %s\n", desc->name);
345347
else {
346-
strbuf_add_unique_abbrev(&opt->obuf, &commit->object.oid,
347-
DEFAULT_ABBREV);
348+
strbuf_repo_add_unique_abbrev(&opt->obuf, repo,
349+
&commit->object.oid,
350+
DEFAULT_ABBREV);
348351
strbuf_addch(&opt->obuf, ' ');
349-
if (parse_commit(commit) != 0)
352+
if (repo_parse_commit(repo, commit) != 0)
350353
strbuf_addstr(&opt->obuf, _("(bad commit)\n"));
351354
else {
352355
const char *title;
353-
const char *msg = get_commit_buffer(commit, NULL);
356+
const char *msg = repo_get_commit_buffer(repo, commit, NULL);
354357
int len = find_commit_subject(msg, &title);
355358
if (len)
356359
strbuf_addf(&opt->obuf, "%.*s\n", len, title);
357-
unuse_commit_buffer(commit, msg);
360+
repo_unuse_commit_buffer(repo, commit, msg);
358361
}
359362
}
360363
flush_output(opt);
361364
}
362365

366+
static void output_commit_title(struct merge_options *opt, struct commit *commit)
367+
{
368+
repo_output_commit_title(opt, the_repository, commit);
369+
}
370+
363371
static int add_cacheinfo(struct merge_options *opt,
364372
const struct diff_filespec *blob,
365373
const char *path, int stage, int refresh, int options)
@@ -1152,14 +1160,14 @@ static int find_first_merges(struct repository *repo,
11521160
return result->nr;
11531161
}
11541162

1155-
static void print_commit(struct commit *commit)
1163+
static void print_commit(struct repository *repo, struct commit *commit)
11561164
{
11571165
struct strbuf sb = STRBUF_INIT;
11581166
struct pretty_print_context ctx = {0};
11591167
ctx.date_mode.type = DATE_NORMAL;
11601168
/* FIXME: Merge this with output_commit_title() */
11611169
assert(!merge_remote_util(commit));
1162-
format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1170+
repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);
11631171
fprintf(stderr, "%s\n", sb.buf);
11641172
strbuf_release(&sb);
11651173
}
@@ -1199,15 +1207,6 @@ static int merge_submodule(struct merge_options *opt,
11991207
if (is_null_oid(b))
12001208
return 0;
12011209

1202-
/*
1203-
* NEEDSWORK: Remove this when all submodule object accesses are
1204-
* through explicitly specified repositores.
1205-
*/
1206-
if (add_submodule_odb(path)) {
1207-
output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
1208-
return 0;
1209-
}
1210-
12111210
if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
12121211
output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
12131212
return 0;
@@ -1232,7 +1231,7 @@ static int merge_submodule(struct merge_options *opt,
12321231
oidcpy(result, b);
12331232
if (show(opt, 3)) {
12341233
output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
1235-
output_commit_title(opt, commit_b);
1234+
repo_output_commit_title(opt, &subrepo, commit_b);
12361235
} else if (show(opt, 2))
12371236
output(opt, 2, _("Fast-forwarding submodule %s"), path);
12381237
else
@@ -1245,7 +1244,7 @@ static int merge_submodule(struct merge_options *opt,
12451244
oidcpy(result, a);
12461245
if (show(opt, 3)) {
12471246
output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
1248-
output_commit_title(opt, commit_a);
1247+
repo_output_commit_title(opt, &subrepo, commit_a);
12491248
} else if (show(opt, 2))
12501249
output(opt, 2, _("Fast-forwarding submodule %s"), path);
12511250
else
@@ -1277,7 +1276,7 @@ static int merge_submodule(struct merge_options *opt,
12771276
case 1:
12781277
output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path);
12791278
output(opt, 2, _("Found a possible merge resolution for the submodule:\n"));
1280-
print_commit((struct commit *) merges.objects[0].item);
1279+
print_commit(&subrepo, (struct commit *) merges.objects[0].item);
12811280
output(opt, 2, _(
12821281
"If this is correct simply add it to the index "
12831282
"for example\n"
@@ -1290,7 +1289,7 @@ static int merge_submodule(struct merge_options *opt,
12901289
default:
12911290
output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path);
12921291
for (i = 0; i < merges.nr; i++)
1293-
print_commit((struct commit *) merges.objects[i].item);
1292+
print_commit(&subrepo, (struct commit *) merges.objects[i].item);
12941293
}
12951294

12961295
object_array_clear(&merges);

object-file.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,14 @@ static int do_oid_object_info_extended(struct repository *r,
15281528
break;
15291529
}
15301530

1531-
if (register_all_submodule_odb_as_alternates())
1531+
/*
1532+
* If r is the_repository, this might be an attempt at
1533+
* accessing a submodule object as if it were in the_repository
1534+
* (having called add_submodule_odb() on that submodule's ODB).
1535+
* If any such ODBs exist, register them and try again.
1536+
*/
1537+
if (r == the_repository &&
1538+
register_all_submodule_odb_as_alternates())
15321539
/* We added some alternates; retry */
15331540
continue;
15341541

refs.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,13 @@ int refname_is_safe(const char *refname)
251251
* does not exist, emit a warning and return false.
252252
*/
253253
int ref_resolves_to_object(const char *refname,
254+
struct repository *repo,
254255
const struct object_id *oid,
255256
unsigned int flags)
256257
{
257258
if (flags & REF_ISBROKEN)
258259
return 0;
259-
if (!has_object_file(oid)) {
260+
if (!repo_has_object_file(repo, oid)) {
260261
error(_("%s does not point to a valid object!"), refname);
261262
return 0;
262263
}
@@ -1870,7 +1871,8 @@ static struct ref_store *lookup_ref_store_map(struct hashmap *map,
18701871
* Create, record, and return a ref_store instance for the specified
18711872
* gitdir.
18721873
*/
1873-
static struct ref_store *ref_store_init(const char *gitdir,
1874+
static struct ref_store *ref_store_init(struct repository *repo,
1875+
const char *gitdir,
18741876
unsigned int flags)
18751877
{
18761878
const char *be_name = "files";
@@ -1880,7 +1882,7 @@ static struct ref_store *ref_store_init(const char *gitdir,
18801882
if (!be)
18811883
BUG("reference backend %s is unknown", be_name);
18821884

1883-
refs = be->init(gitdir, flags);
1885+
refs = be->init(repo, gitdir, flags);
18841886
return refs;
18851887
}
18861888

@@ -1892,7 +1894,7 @@ struct ref_store *get_main_ref_store(struct repository *r)
18921894
if (!r->gitdir)
18931895
BUG("attempting to get main_ref_store outside of repository");
18941896

1895-
r->refs_private = ref_store_init(r->gitdir, REF_STORE_ALL_CAPS);
1897+
r->refs_private = ref_store_init(r, r->gitdir, REF_STORE_ALL_CAPS);
18961898
r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
18971899
return r->refs_private;
18981900
}
@@ -1922,6 +1924,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
19221924
struct ref_store *refs;
19231925
char *to_free = NULL;
19241926
size_t len;
1927+
struct repository *subrepo;
19251928

19261929
if (!submodule)
19271930
return NULL;
@@ -1947,8 +1950,19 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
19471950
if (submodule_to_gitdir(&submodule_sb, submodule))
19481951
goto done;
19491952

1950-
/* assume that add_submodule_odb() has been called */
1951-
refs = ref_store_init(submodule_sb.buf,
1953+
subrepo = xmalloc(sizeof(*subrepo));
1954+
/*
1955+
* NEEDSWORK: Make get_submodule_ref_store() work with arbitrary
1956+
* superprojects other than the_repository. This probably should be
1957+
* done by making it take a struct repository * parameter instead of a
1958+
* submodule path.
1959+
*/
1960+
if (repo_submodule_init(subrepo, the_repository, submodule,
1961+
null_oid())) {
1962+
free(subrepo);
1963+
goto done;
1964+
}
1965+
refs = ref_store_init(subrepo, submodule_sb.buf,
19521966
REF_STORE_READ | REF_STORE_ODB);
19531967
register_ref_store_map(&submodule_ref_stores, "submodule",
19541968
refs, submodule);
@@ -1974,10 +1988,12 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
19741988
return refs;
19751989

19761990
if (wt->id)
1977-
refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
1991+
refs = ref_store_init(the_repository,
1992+
git_common_path("worktrees/%s", wt->id),
19781993
REF_STORE_ALL_CAPS);
19791994
else
1980-
refs = ref_store_init(get_git_common_dir(),
1995+
refs = ref_store_init(the_repository,
1996+
get_git_common_dir(),
19811997
REF_STORE_ALL_CAPS);
19821998

19831999
if (refs)

refs/files-backend.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,23 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
7979
* Create a new submodule ref cache and add it to the internal
8080
* set of caches.
8181
*/
82-
static struct ref_store *files_ref_store_create(const char *gitdir,
82+
static struct ref_store *files_ref_store_create(struct repository *repo,
83+
const char *gitdir,
8384
unsigned int flags)
8485
{
8586
struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
8687
struct ref_store *ref_store = (struct ref_store *)refs;
8788
struct strbuf sb = STRBUF_INIT;
8889

90+
ref_store->repo = repo;
8991
ref_store->gitdir = xstrdup(gitdir);
9092
base_ref_store_init(ref_store, &refs_be_files);
9193
refs->store_flags = flags;
9294

9395
get_common_dir_noenv(&sb, gitdir);
9496
refs->gitcommondir = strbuf_detach(&sb, NULL);
9597
strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
96-
refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
98+
refs->packed_ref_store = packed_ref_store_create(repo, sb.buf, flags);
9799
strbuf_release(&sb);
98100

99101
chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
@@ -730,6 +732,7 @@ struct files_ref_iterator {
730732
struct ref_iterator base;
731733

732734
struct ref_iterator *iter0;
735+
struct repository *repo;
733736
unsigned int flags;
734737
};
735738

@@ -751,6 +754,7 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
751754

752755
if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
753756
!ref_resolves_to_object(iter->iter0->refname,
757+
iter->repo,
754758
iter->iter0->oid,
755759
iter->iter0->flags))
756760
continue;
@@ -829,7 +833,7 @@ static struct ref_iterator *files_ref_iterator_begin(
829833
*/
830834

831835
loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
832-
prefix, 1);
836+
prefix, ref_store->repo, 1);
833837

834838
/*
835839
* The packed-refs file might contain broken references, for
@@ -853,6 +857,7 @@ static struct ref_iterator *files_ref_iterator_begin(
853857
base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
854858
overlay_iter->ordered);
855859
iter->iter0 = overlay_iter;
860+
iter->repo = ref_store->repo;
856861
iter->flags = flags;
857862

858863
return ref_iterator;
@@ -1169,7 +1174,7 @@ static int should_pack_ref(const char *refname,
11691174
return 0;
11701175

11711176
/* Do not pack broken refs: */
1172-
if (!ref_resolves_to_object(refname, oid, ref_flags))
1177+
if (!ref_resolves_to_object(refname, the_repository, oid, ref_flags))
11731178
return 0;
11741179

11751180
return 1;
@@ -1192,7 +1197,8 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
11921197

11931198
packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
11941199

1195-
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
1200+
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
1201+
the_repository, 0);
11961202
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
11971203
/*
11981204
* If the loose reference can be packed, add an entry

0 commit comments

Comments
 (0)