refs: refactor resolve_gitlink_ref() to accept a repository

In `resolve_gitlink_ref()` we implicitly rely on `the_repository` to
look up the submodule ref store. Now that we can look up submodule ref
stores for arbitrary repositories we can improve this function to
instead accept a repository as parameter for which we want to resolve
the gitlink.

Do so and adjust callers accordingly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-05-17 10:18:39 +02:00
committed by Junio C Hamano
parent 965f8991e5
commit e19488a60a
11 changed files with 29 additions and 18 deletions

3
attr.c
View File

@@ -1288,7 +1288,8 @@ static const char *builtin_object_mode_attr(struct index_state *istate, const ch
if (pos >= 0) { if (pos >= 0) {
if (S_ISGITLINK(istate->cache[pos]->ce_mode)) if (S_ISGITLINK(istate->cache[pos]->ce_mode))
mode = istate->cache[pos]->ce_mode; mode = istate->cache[pos]->ce_mode;
} else if (resolve_gitlink_ref(path, "HEAD", &oid) == 0) { } else if (repo_resolve_gitlink_ref(the_repository, path,
"HEAD", &oid) == 0) {
mode = S_IFGITLINK; mode = S_IFGITLINK;
} }
} }

View File

@@ -2600,7 +2600,8 @@ static int update_submodule(struct update_data *update_data)
if (update_data->just_cloned) if (update_data->just_cloned)
oidcpy(&update_data->suboid, null_oid()); oidcpy(&update_data->suboid, null_oid());
else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid)) else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
"HEAD", &update_data->suboid))
return die_message(_("Unable to find current revision in submodule path '%s'"), return die_message(_("Unable to find current revision in submodule path '%s'"),
update_data->displaypath); update_data->displaypath);
@@ -2627,7 +2628,8 @@ static int update_submodule(struct update_data *update_data)
update_data->sm_path); update_data->sm_path);
} }
if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid)) if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
remote_ref, &update_data->oid))
return die_message(_("Unable to find %s revision in submodule path '%s'"), return die_message(_("Unable to find %s revision in submodule path '%s'"),
remote_ref, update_data->sm_path); remote_ref, update_data->sm_path);
@@ -3357,7 +3359,7 @@ static void die_on_repo_without_commits(const char *path)
strbuf_addstr(&sb, path); strbuf_addstr(&sb, path);
if (is_nonbare_repository_dir(&sb)) { if (is_nonbare_repository_dir(&sb)) {
struct object_id oid; struct object_id oid;
if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
die(_("'%s' does not have a commit checked out"), path); die(_("'%s' does not have a commit checked out"), path);
} }
strbuf_release(&sb); strbuf_release(&sb);

View File

@@ -349,7 +349,8 @@ static int process_directory(const char *path, int len, struct stat *st)
if (S_ISGITLINK(ce->ce_mode)) { if (S_ISGITLINK(ce->ce_mode)) {
/* Do nothing to the index if there is no HEAD! */ /* Do nothing to the index if there is no HEAD! */
if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) if (repo_resolve_gitlink_ref(the_repository, path,
"HEAD", &oid) < 0)
return 0; return 0;
return add_one_path(ce, path, len, st); return add_one_path(ce, path, len, st);
@@ -375,7 +376,7 @@ static int process_directory(const char *path, int len, struct stat *st)
} }
/* No match - should we add it as a gitlink? */ /* No match - should we add it as a gitlink? */
if (!resolve_gitlink_ref(path, "HEAD", &oid)) if (!repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid))
return add_one_path(NULL, path, len, st); return add_one_path(NULL, path, len, st);
/* Error out. */ /* Error out. */

View File

@@ -1066,7 +1066,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
elem->mode = canon_mode(st.st_mode); elem->mode = canon_mode(st.st_mode);
} else if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
struct object_id oid; struct object_id oid;
if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0) if (repo_resolve_gitlink_ref(the_repository, elem->path,
"HEAD", &oid) < 0)
result = grab_blob(opt->repo, &elem->oid, result = grab_blob(opt->repo, &elem->oid,
elem->mode, &result_size, elem->mode, &result_size,
NULL, NULL); NULL, NULL);

View File

@@ -66,7 +66,8 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
* a directory --- the blob was removed! * a directory --- the blob was removed!
*/ */
if (!S_ISGITLINK(ce->ce_mode) && if (!S_ISGITLINK(ce->ce_mode) &&
resolve_gitlink_ref(ce->name, "HEAD", &sub)) repo_resolve_gitlink_ref(the_repository, ce->name,
"HEAD", &sub))
return 1; return 1;
} }
return 0; return 0;

3
dir.c
View File

@@ -3318,7 +3318,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
struct object_id submodule_head; struct object_id submodule_head;
if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
!resolve_gitlink_ref(path->buf, "HEAD", &submodule_head)) { !repo_resolve_gitlink_ref(the_repository, path->buf,
"HEAD", &submodule_head)) {
/* Do not descend and nuke a nested git work tree. */ /* Do not descend and nuke a nested git work tree. */
if (kept_up) if (kept_up)
*kept_up = 1; *kept_up = 1;

View File

@@ -2669,7 +2669,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
strbuf_release(&sb); strbuf_release(&sb);
break; break;
case S_IFDIR: case S_IFDIR:
return resolve_gitlink_ref(path, "HEAD", oid); return repo_resolve_gitlink_ref(the_repository, path, "HEAD", oid);
default: default:
return error(_("%s: unsupported file type"), path); return error(_("%s: unsupported file type"), path);
} }

View File

@@ -271,7 +271,8 @@ static int ce_compare_gitlink(const struct cache_entry *ce)
* *
* If so, we consider it always to match. * If so, we consider it always to match.
*/ */
if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0) if (repo_resolve_gitlink_ref(the_repository, ce->name,
"HEAD", &oid) < 0)
return 0; return 0;
return !oideq(&oid, &ce->oid); return !oideq(&oid, &ce->oid);
} }
@@ -711,7 +712,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
namelen = strlen(path); namelen = strlen(path);
if (S_ISDIR(st_mode)) { if (S_ISDIR(st_mode)) {
if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
return error(_("'%s' does not have a commit checked out"), path); return error(_("'%s' does not have a commit checked out"), path);
while (namelen && path[namelen-1] == '/') while (namelen && path[namelen-1] == '/')
namelen--; namelen--;

7
refs.c
View File

@@ -1943,13 +1943,14 @@ int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *e
return refs->be->create_on_disk(refs, flags, err); return refs->be->create_on_disk(refs, flags, err);
} }
int resolve_gitlink_ref(const char *submodule, const char *refname, int repo_resolve_gitlink_ref(struct repository *r,
struct object_id *oid) const char *submodule, const char *refname,
struct object_id *oid)
{ {
struct ref_store *refs; struct ref_store *refs;
int flags; int flags;
refs = repo_get_submodule_ref_store(the_repository, submodule); refs = repo_get_submodule_ref_store(r, submodule);
if (!refs) if (!refs)
return -1; return -1;

5
refs.h
View File

@@ -141,8 +141,9 @@ int peel_iterated_oid(const struct object_id *base, struct object_id *peeled);
* successful, return 0 and set oid to the name of the object; * successful, return 0 and set oid to the name of the object;
* otherwise, return a non-zero value. * otherwise, return a non-zero value.
*/ */
int resolve_gitlink_ref(const char *submodule, const char *refname, int repo_resolve_gitlink_ref(struct repository *r,
struct object_id *oid); const char *submodule, const char *refname,
struct object_id *oid);
/* /*
* Return true iff abbrev_name is a possible abbreviation for * Return true iff abbrev_name is a possible abbreviation for

View File

@@ -2318,7 +2318,8 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
if (S_ISGITLINK(ce->ce_mode)) { if (S_ISGITLINK(ce->ce_mode)) {
struct object_id oid; struct object_id oid;
int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid); int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name,
"HEAD", &oid);
/* /*
* If we are not going to update the submodule, then * If we are not going to update the submodule, then
* we don't care. * we don't care.