Skip to content

Commit 32ecc98

Browse files
committed
submodule: catch when submodule is not staged on update
When calling `git_submodule_update` on a submodule, we have to retrieve the ID of the submodule entry in the index. If the function is called on a submodule which is only partly initialized, the submodule entry may not be added to the index yet. This leads to an assert when trying to look up the blob later on. Fix the issue by checking if the index actually holds the submodule's ID and erroring out if it does not.
1 parent 53454be commit 32ecc98

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/submodule.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,8 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio
12121212
(error = git_checkout_head(sub_repo, &update_options.checkout_opts)) != 0)
12131213
goto done;
12141214
} else {
1215+
const git_oid *oid;
1216+
12151217
/**
12161218
* Work dir is initialized - look up the commit in the parent repository's index,
12171219
* update the workdir contents of the subrepository, and set the subrepository's
@@ -1220,8 +1222,14 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio
12201222
if ((error = git_submodule_open(&sub_repo, sm)) < 0)
12211223
goto done;
12221224

1225+
if ((oid = git_submodule_index_id(sm)) == NULL) {
1226+
giterr_set(GITERR_SUBMODULE, "could not get ID of submodule in index");
1227+
error = -1;
1228+
goto done;
1229+
}
1230+
12231231
/* Look up the target commit in the submodule. */
1224-
if ((error = git_object_lookup(&target_commit, sub_repo, git_submodule_index_id(sm), GIT_OBJ_COMMIT)) < 0) {
1232+
if ((error = git_object_lookup(&target_commit, sub_repo, oid, GIT_OBJ_COMMIT)) < 0) {
12251233
/* If it isn't found then fetch and try again. */
12261234
if (error != GIT_ENOTFOUND || !update_options.allow_fetch ||
12271235
(error = lookup_default_remote(&remote, sub_repo)) < 0 ||

0 commit comments

Comments
 (0)