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>

Patrick Steinhardt committed May 17, 2024 at 10:18 UTC e19488a60aab022ca4c8d32d6629c04ce94f2779
11 files changed +29 -18
attr.c
+2 -1
@@ -1288,7 +1288,8 @@ static const char *builtin_object_mode_attr(struct index_state *istate, const ch
1288 if (pos >= 0) {
1289 if (S_ISGITLINK(istate->cache[pos]->ce_mode))
1290 mode = istate->cache[pos]->ce_mode;
1291 - } else if (resolve_gitlink_ref(path, "HEAD", &oid) == 0) {
1291 + } else if (repo_resolve_gitlink_ref(the_repository, path,
1292 + "HEAD", &oid) == 0) {
1293 mode = S_IFGITLINK;
1294 }
1295 }
builtin/submodule--helper.c
+5 -3
@@ -2600,7 +2600,8 @@ static int update_submodule(struct update_data *update_data)
2600
2601 if (update_data->just_cloned)
2602 oidcpy(&update_data->suboid, null_oid());
2603 - else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2603 + else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2604 + "HEAD", &update_data->suboid))
2605 return die_message(_("Unable to find current revision in submodule path '%s'"),
2606 update_data->displaypath);
2607
@@ -2627,7 +2628,8 @@ static int update_submodule(struct update_data *update_data)
2628 update_data->sm_path);
2629 }
2630
2630 - if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2631 + if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2632 + remote_ref, &update_data->oid))
2633 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2634 remote_ref, update_data->sm_path);
2635
@@ -3357,7 +3359,7 @@ static void die_on_repo_without_commits(const char *path)
3359 strbuf_addstr(&sb, path);
3360 if (is_nonbare_repository_dir(&sb)) {
3361 struct object_id oid;
3360 - if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3362 + if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
3363 die(_("'%s' does not have a commit checked out"), path);
3364 }
3365 strbuf_release(&sb);
builtin/update-index.c
+3 -2
@@ -349,7 +349,8 @@ static int process_directory(const char *path, int len, struct stat *st)
349 if (S_ISGITLINK(ce->ce_mode)) {
350
351 /* Do nothing to the index if there is no HEAD! */
352 - if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
352 + if (repo_resolve_gitlink_ref(the_repository, path,
353 + "HEAD", &oid) < 0)
354 return 0;
355
356 return add_one_path(ce, path, len, st);
@@ -375,7 +376,7 @@ static int process_directory(const char *path, int len, struct stat *st)
376 }
377
378 /* No match - should we add it as a gitlink? */
378 - if (!resolve_gitlink_ref(path, "HEAD", &oid))
379 + if (!repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid))
380 return add_one_path(NULL, path, len, st);
381
382 /* Error out. */
combine-diff.c
+2 -1
@@ -1066,7 +1066,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1066 elem->mode = canon_mode(st.st_mode);
1067 } else if (S_ISDIR(st.st_mode)) {
1068 struct object_id oid;
1069 - if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1069 + if (repo_resolve_gitlink_ref(the_repository, elem->path,
1070 + "HEAD", &oid) < 0)
1071 result = grab_blob(opt->repo, &elem->oid,
1072 elem->mode, &result_size,
1073 NULL, NULL);
diff-lib.c
+2 -1
@@ -66,7 +66,8 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
66 * a directory --- the blob was removed!
67 */
68 if (!S_ISGITLINK(ce->ce_mode) &&
69 - resolve_gitlink_ref(ce->name, "HEAD", &sub))
69 + repo_resolve_gitlink_ref(the_repository, ce->name,
70 + "HEAD", &sub))
71 return 1;
72 }
73 return 0;
dir.c
+2 -1
@@ -3318,7 +3318,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
3318 struct object_id submodule_head;
3319
3320 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
3321 - !resolve_gitlink_ref(path->buf, "HEAD", &submodule_head)) {
3321 + !repo_resolve_gitlink_ref(the_repository, path->buf,
3322 + "HEAD", &submodule_head)) {
3323 /* Do not descend and nuke a nested git work tree. */
3324 if (kept_up)
3325 *kept_up = 1;
object-file.c
+1 -1
@@ -2669,7 +2669,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
2669 strbuf_release(&sb);
2670 break;
2671 case S_IFDIR:
2672 - return resolve_gitlink_ref(path, "HEAD", oid);
2672 + return repo_resolve_gitlink_ref(the_repository, path, "HEAD", oid);
2673 default:
2674 return error(_("%s: unsupported file type"), path);
2675 }
read-cache.c
+3 -2
@@ -271,7 +271,8 @@ static int ce_compare_gitlink(const struct cache_entry *ce)
271 *
272 * If so, we consider it always to match.
273 */
274 - if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0)
274 + if (repo_resolve_gitlink_ref(the_repository, ce->name,
275 + "HEAD", &oid) < 0)
276 return 0;
277 return !oideq(&oid, &ce->oid);
278 }
@@ -711,7 +712,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
712
713 namelen = strlen(path);
714 if (S_ISDIR(st_mode)) {
714 - if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
715 + if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
716 return error(_("'%s' does not have a commit checked out"), path);
717 while (namelen && path[namelen-1] == '/')
718 namelen--;
refs.c
+4 -3
@@ -1943,13 +1943,14 @@ int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *e
1943 return refs->be->create_on_disk(refs, flags, err);
1944 }
1945
1946 -int resolve_gitlink_ref(const char *submodule, const char *refname,
1947 - struct object_id *oid)
1946 +int repo_resolve_gitlink_ref(struct repository *r,
1947 + const char *submodule, const char *refname,
1948 + struct object_id *oid)
1949 {
1950 struct ref_store *refs;
1951 int flags;
1952
1952 - refs = repo_get_submodule_ref_store(the_repository, submodule);
1953 + refs = repo_get_submodule_ref_store(r, submodule);
1954 if (!refs)
1955 return -1;
1956
refs.h
+3 -2
@@ -141,8 +141,9 @@ int peel_iterated_oid(const struct object_id *base, struct object_id *peeled);
141 * successful, return 0 and set oid to the name of the object;
142 * otherwise, return a non-zero value.
143 */
144 -int resolve_gitlink_ref(const char *submodule, const char *refname,
145 - struct object_id *oid);
144 +int repo_resolve_gitlink_ref(struct repository *r,
145 + const char *submodule, const char *refname,
146 + struct object_id *oid);
147
148 /*
149 * Return true iff abbrev_name is a possible abbreviation for
unpack-trees.c
+2 -1
@@ -2318,7 +2318,8 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
2318
2319 if (S_ISGITLINK(ce->ce_mode)) {
2320 struct object_id oid;
2321 - int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid);
2321 + int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name,
2322 + "HEAD", &oid);
2323 /*
2324 * If we are not going to update the submodule, then
2325 * we don't care.