Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 678c4aa

Browse files
committed
index: allow add_bypath to update submodules
Similarly to how git itself does it, allow the index update operation to stage a change in a submodule's HEAD.
1 parent aafaa49 commit 678c4aa

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/index.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,29 @@ int git_index_add_bypath(git_index *index, const char *path)
12361236

12371237
assert(index && path);
12381238

1239-
if ((ret = index_entry_init(&entry, index, path)) < 0 ||
1240-
(ret = index_insert(index, &entry, 1, false)) < 0)
1239+
if ((ret = index_entry_init(&entry, index, path)) == 0)
1240+
ret = index_insert(index, &entry, 1, false);
1241+
1242+
/* If we were given a directory, let's see if it's a submodule */
1243+
if (ret < 0 && ret != GIT_EDIRECTORY)
1244+
return ret;
1245+
1246+
if (ret == GIT_EDIRECTORY) {
1247+
git_submodule *sm;
1248+
git_error_state err;
1249+
1250+
giterr_capture(&err, ret);
1251+
1252+
ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
1253+
if (ret == GIT_ENOTFOUND)
1254+
return giterr_restore(&err);
1255+
else
1256+
git__free(err.error_msg.message);
1257+
1258+
ret = git_submodule_add_to_index(sm, false);
1259+
git_submodule_free(sm);
12411260
return ret;
1261+
}
12421262

12431263
/* Adding implies conflict was resolved, move conflict entries to REUC */
12441264
if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)

tests/index/bypath.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ void test_index_bypath__add_directory(void)
2121
{
2222
cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
2323
}
24+
25+
void test_index_bypath__add_submodule(void)
26+
{
27+
unsigned int status;
28+
const char *sm_name = "sm_changed_head";
29+
30+
cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
31+
cl_assert_equal_i(GIT_SUBMODULE_STATUS_WD_MODIFIED, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
32+
cl_git_pass(git_index_add_bypath(g_idx, sm_name));
33+
cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
34+
cl_assert_equal_i(0, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
35+
}

0 commit comments

Comments
 (0)