git_config_free(cfg);
/* confirm no submodule data in config */
- cl_git_pass(git_repository_config(&cfg, g_repo));
- cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
- cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
- cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
+ cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
git_config_free(cfg);
/* call init and see that settings are copied */
cl_git_pass(git_submodule_foreach(g_repo, init_one_submodule, NULL));
- git_submodule_reload_all(g_repo, 1);
-
/* confirm submodule data in config */
cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
git_submodule_free(sm3);
}
-void test_submodule_modify__edit_and_save(void)
+static void assert_ignore_change(git_submodule_ignore_t ignore)
{
- git_submodule *sm1, *sm2;
- char *old_url, *old_branch;
- git_submodule_ignore_t old_ignore;
- git_submodule_update_t old_update;
- git_repository *r2;
- git_submodule_recurse_t old_fetchrecurse;
-
- cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));
-
- old_url = git__strdup(git_submodule_url(sm1));
- old_branch = NULL;
-
- /* modify properties of submodule */
- cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
- cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
- old_ignore = git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
- old_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
- old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(
- sm1, GIT_SUBMODULE_RECURSE_YES);
-
- cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
- cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
- cl_assert_equal_i(
- GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm1));
- cl_assert_equal_i(
- GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm1));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
-
- /* revert without saving (and confirm setters return old value) */
- cl_git_pass(git_submodule_set_url(sm1, old_url));
- cl_git_pass(git_submodule_set_branch(sm1, old_branch));
- cl_assert_equal_i(
- GIT_SUBMODULE_IGNORE_UNTRACKED,
- git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET));
- cl_assert_equal_i(
- GIT_SUBMODULE_UPDATE_REBASE,
- git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_YES, git_submodule_set_fetch_recurse_submodules(
- sm1, GIT_SUBMODULE_RECURSE_RESET));
-
- /* check that revert was successful */
- cl_assert_equal_s(old_url, git_submodule_url(sm1));
- cl_assert_equal_s(old_branch, git_submodule_branch(sm1));
- cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1));
- cl_assert_equal_i((int)old_update, (int)git_submodule_update_strategy(sm1));
- cl_assert_equal_i(
- old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));
-
- /* modify properties of submodule (again) */
- cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
- cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
- git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
- git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
- git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_YES);
-
- /* call save */
- cl_git_pass(git_submodule_save(sm1));
-
- /* attempt to "revert" values */
- git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET);
- git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET);
-
- /* but ignore and update should NOT revert because the RESET
- * should now be the newly saved value...
- */
- cl_assert_equal_i(
- (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
- cl_assert_equal_i(
- (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
- cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
-
- /* call reload and check that the new values are loaded */
- cl_git_pass(git_submodule_reload(sm1, 0));
-
- cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
- cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
- cl_assert_equal_i(
- (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
- cl_assert_equal_i(
- (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
- cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
-
- /* unset branch again and verify that the property is deleted in config */
- cl_git_pass(git_submodule_set_branch(sm1, NULL));
- cl_git_pass(git_submodule_save(sm1));
- cl_git_pass(git_submodule_reload(sm1, 0));
- cl_assert_equal_s(NULL, git_submodule_branch(sm1));
-
- /* open a second copy of the repo and compare submodule */
- cl_git_pass(git_repository_open(&r2, "submod2"));
- cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));
-
- cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
- cl_assert_equal_i(
- GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm2));
- cl_assert_equal_i(
- GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm2));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_NO, git_submodule_fetch_recurse_submodules(sm2));
-
- /* set fetchRecurseSubmodules on-demand */
- cl_git_pass(git_submodule_reload(sm1, 0));
- git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_ONDEMAND);
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
- /* call save */
- cl_git_pass(git_submodule_save(sm1));
- cl_git_pass(git_submodule_reload(sm1, 0));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
+ git_submodule *sm;
- git_submodule_free(sm1);
- git_submodule_free(sm2);
- git_repository_free(r2);
- git__free(old_url);
+ cl_git_pass(git_submodule_set_ignore(g_repo, "sm_changed_head", ignore));
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_i(ignore, git_submodule_ignore(sm));
+ git_submodule_free(sm);
+}
+
+void test_submodule_modify__set_ignore(void)
+{
+ assert_ignore_change(GIT_SUBMODULE_IGNORE_UNTRACKED);
+ assert_ignore_change(GIT_SUBMODULE_IGNORE_NONE);
+ assert_ignore_change(GIT_SUBMODULE_IGNORE_ALL);
+}
+
+static void assert_update_change(git_submodule_update_t update)
+{
+ git_submodule *sm;
+
+ cl_git_pass(git_submodule_set_update(g_repo, "sm_changed_head", update));
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_i(update, git_submodule_update_strategy(sm));
+ git_submodule_free(sm);
}
-void test_submodule_modify__save_last(void)
+void test_submodule_modify__set_update(void)
+{
+ assert_update_change(GIT_SUBMODULE_UPDATE_REBASE);
+ assert_update_change(GIT_SUBMODULE_UPDATE_NONE);
+ assert_update_change(GIT_SUBMODULE_UPDATE_CHECKOUT);
+}
+
+static void assert_recurse_change(git_submodule_recurse_t recurse)
{
git_submodule *sm;
- cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_gitmodules_only"));
- cl_git_pass(git_submodule_save(sm));
+ cl_git_pass(git_submodule_set_fetch_recurse_submodules(g_repo, "sm_changed_head", recurse));
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_i(recurse, git_submodule_fetch_recurse_submodules(sm));
+ git_submodule_free(sm);
+}
+
+void test_submodule_modify__set_fetch_recurse_submodules(void)
+{
+ assert_recurse_change(GIT_SUBMODULE_RECURSE_YES);
+ assert_recurse_change(GIT_SUBMODULE_RECURSE_NO);
+ assert_recurse_change(GIT_SUBMODULE_RECURSE_ONDEMAND);
+}
+
+void test_submodule_modify__set_branch(void)
+{
+ git_submodule *sm;
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert(git_submodule_branch(sm) == NULL);
+ git_submodule_free(sm);
+
+ cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", SM_LIBGIT2_BRANCH));
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm));
+ git_submodule_free(sm);
+
+ cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", NULL));
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert(git_submodule_branch(sm) == NULL);
+ git_submodule_free(sm);
+}
+
+void test_submodule_modify__set_url(void)
+{
+ git_submodule *sm;
+
+ cl_git_pass(git_submodule_set_url(g_repo, "sm_changed_head", SM_LIBGIT2_URL));
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm));
+ git_submodule_free(sm);
+}
+
+void test_submodule_modify__set_relative_url(void)
+{
+ git_str path = GIT_STR_INIT;
+ git_repository *repo;
+ git_submodule *sm;
+
+ cl_git_pass(git_submodule_set_url(g_repo, SM1, "../relative-url"));
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, SM1));
+ cl_git_pass(git_submodule_sync(sm));
+ cl_git_pass(git_submodule_open(&repo, sm));
+
+ cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "relative-url"));
+
+ assert_config_entry_value(g_repo, "submodule."SM1".url", path.ptr);
+ assert_config_entry_value(repo, "remote.origin.url", path.ptr);
+
+ git_repository_free(repo);
git_submodule_free(sm);
+ git_str_dispose(&path);
}