summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-07-11 19:41:03 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2015-07-12 12:11:22 +0200
commit247d27c2c6868e808191e6090056ecece6da30c4 (patch)
treed85076a6dab42f1c682a7d4355c0cfdd94678b87
parent0d98af0911ebf55ba64cc0c9e17a3d450c77a9be (diff)
downloadlibgit2-cmn/index-add-submodule.tar.gz
index: allow add_bypath to update submodulescmn/index-add-submodule
Similarly to how git itself does it, allow the index update operation to stage a change in a submodule's HEAD.
-rw-r--r--src/index.c24
-rw-r--r--tests/index/bypath.c12
2 files changed, 34 insertions, 2 deletions
diff --git a/src/index.c b/src/index.c
index 5ce5522f8..501498e9e 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1236,9 +1236,29 @@ int git_index_add_bypath(git_index *index, const char *path)
assert(index && path);
- if ((ret = index_entry_init(&entry, index, path)) < 0 ||
- (ret = index_insert(index, &entry, 1, false)) < 0)
+ if ((ret = index_entry_init(&entry, index, path)) == 0)
+ ret = index_insert(index, &entry, 1, false);
+
+ /* If we were given a directory, let's see if it's a submodule */
+ if (ret < 0 && ret != GIT_EDIRECTORY)
+ return ret;
+
+ if (ret == GIT_EDIRECTORY) {
+ git_submodule *sm;
+ git_error_state err;
+
+ giterr_capture(&err, ret);
+
+ ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
+ if (ret == GIT_ENOTFOUND)
+ return giterr_restore(&err);
+ else
+ git__free(err.error_msg.message);
+
+ ret = git_submodule_add_to_index(sm, false);
+ git_submodule_free(sm);
return ret;
+ }
/* Adding implies conflict was resolved, move conflict entries to REUC */
if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
diff --git a/tests/index/bypath.c b/tests/index/bypath.c
index 08c9934ea..ddb766a22 100644
--- a/tests/index/bypath.c
+++ b/tests/index/bypath.c
@@ -21,3 +21,15 @@ void test_index_bypath__add_directory(void)
{
cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
}
+
+void test_index_bypath__add_submodule(void)
+{
+ unsigned int status;
+ const char *sm_name = "sm_changed_head";
+
+ cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
+ cl_assert_equal_i(GIT_SUBMODULE_STATUS_WD_MODIFIED, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
+ cl_git_pass(git_index_add_bypath(g_idx, sm_name));
+ cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
+ cl_assert_equal_i(0, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
+}