From c6f489c964bc4df29bdacb1ee4afdcdb294f3815 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 4 May 2015 17:29:12 +0200 Subject: [PATCH] submodule: add an ignore option to status This lets us specify in the status call which ignore rules we want to use (optionally falling back to whatever the submodule has in its configuration). This removes one of the reasons for having `_set_ignore()` set the value in-memory. We re-use the `IGNORE_RESET` value for this as it is no longer relevant but has a similar purpose to `IGNORE_FALLBACK`. Similarly, we remove `IGNORE_DEFAULT` which does not have use outside of initializers and move that to fall back to the configuration as well. --- include/git2/diff.h | 2 +- include/git2/submodule.h | 4 +- include/git2/types.h | 6 +-- src/checkout.c | 2 +- src/diff_file.c | 2 +- src/submodule.c | 8 ++-- tests/diff/tree.c | 2 +- tests/submodule/status.c | 65 ++++++++++++----------------- tests/submodule/submodule_helpers.c | 2 +- tests/submodule/update.c | 18 ++++---- 10 files changed, 50 insertions(+), 61 deletions(-) diff --git a/include/git2/diff.h b/include/git2/diff.h index 0ecdc1bed..90e2e1b22 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -399,7 +399,7 @@ typedef struct { * `git_diff_options_init` programmatic initialization. */ #define GIT_DIFF_OPTIONS_INIT \ - {GIT_DIFF_OPTIONS_VERSION, 0, GIT_SUBMODULE_IGNORE_DEFAULT, {NULL,0}, NULL, NULL, 3} + {GIT_DIFF_OPTIONS_VERSION, 0, GIT_SUBMODULE_IGNORE_FALLBACK, {NULL,0}, NULL, NULL, 3} /** * Initializes a `git_diff_options` with default values. Equivalent to diff --git a/include/git2/submodule.h b/include/git2/submodule.h index 737570844..31218cc94 100644 --- a/include/git2/submodule.h +++ b/include/git2/submodule.h @@ -631,12 +631,14 @@ GIT_EXTERN(int) git_submodule_reload_all(git_repository *repo, int force); * @param status Combination of `GIT_SUBMODULE_STATUS` flags * @param repo the repository in which to look * @param name name of the submodule + * @param ignore the ignore rules to follow * @return 0 on success, <0 on error */ GIT_EXTERN(int) git_submodule_status( unsigned int *status, git_repository *repo, - const char *name); + const char *name, + git_submodule_ignore_t ignore); /** * Get the locations of submodule information. diff --git a/include/git2/types.h b/include/git2/types.h index d1e7cd92c..aa0f31a9a 100644 --- a/include/git2/types.h +++ b/include/git2/types.h @@ -374,7 +374,7 @@ typedef enum { * * The values are: * - * - GIT_SUBMODULE_IGNORE_RESET: reset to the on-disk value. + * - GIT_SUBMODULE_IGNORE_FALLBACK: use the submodule's configuration * - GIT_SUBMODULE_IGNORE_NONE: don't ignore any change - i.e. even an * untracked file, will mark the submodule as dirty. Ignored files are * still ignored, of course. @@ -388,14 +388,12 @@ typedef enum { * when we don't want any particular ignore rule to be specified. */ typedef enum { - GIT_SUBMODULE_IGNORE_RESET = -1, /**< reset to on-disk value */ + GIT_SUBMODULE_IGNORE_FALLBACK = -1, /**< use the submodule's configuration */ GIT_SUBMODULE_IGNORE_NONE = 1, /**< any change or untracked == dirty */ GIT_SUBMODULE_IGNORE_UNTRACKED = 2, /**< dirty if tracked files change */ GIT_SUBMODULE_IGNORE_DIRTY = 3, /**< only dirty if HEAD moved */ GIT_SUBMODULE_IGNORE_ALL = 4, /**< never dirty */ - - GIT_SUBMODULE_IGNORE_DEFAULT = 0 } git_submodule_ignore_t; /** diff --git a/src/checkout.c b/src/checkout.c index d95244a69..351e8b0ef 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -180,7 +180,7 @@ static bool checkout_is_workdir_modified( return true; } - if (git_submodule_status(&sm_status, data->repo, wditem->path) < 0 || + if (git_submodule_status(&sm_status, data->repo, wditem->path, GIT_SUBMODULE_IGNORE_FALLBACK) < 0 || GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) rval = true; else if ((sm_oid = git_submodule_wd_id(sm)) == NULL) diff --git a/src/diff_file.c b/src/diff_file.c index 4d9ecc8d8..28edcd4c1 100644 --- a/src/diff_file.c +++ b/src/diff_file.c @@ -186,7 +186,7 @@ static int diff_file_content_commit_to_str( return error; } - if ((error = git_submodule_status(&sm_status, fc->repo, fc->file->path)) < 0) { + if ((error = git_submodule_status(&sm_status, fc->repo, fc->file->path, GIT_SUBMODULE_IGNORE_FALLBACK)) < 0) { git_submodule_free(sm); return error; } diff --git a/src/submodule.c b/src/submodule.c index 2faaa734d..c6effdef8 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -1067,7 +1067,7 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio memcpy(&clone_options.fetch_opts, &update_options.fetch_opts, sizeof(git_fetch_options)); /* Get the status of the submodule to determine if it is already initialized */ - if ((error = git_submodule_status(&submodule_status, sm->repo, sm->name)) < 0) + if ((error = git_submodule_status(&submodule_status, sm->repo, sm->name, GIT_SUBMODULE_IGNORE_FALLBACK)) < 0) goto done; /* @@ -1462,7 +1462,7 @@ int git_submodule__status( unsigned int status; git_repository *smrepo = NULL; - if (ign < GIT_SUBMODULE_IGNORE_NONE) + if (ign == GIT_SUBMODULE_IGNORE_FALLBACK) ign = sm->ignore; /* only return location info if ignore == all */ @@ -1511,7 +1511,7 @@ int git_submodule__status( return 0; } -int git_submodule_status(unsigned int *status, git_repository *repo, const char *name) +int git_submodule_status(unsigned int *status, git_repository *repo, const char *name, git_submodule_ignore_t ignore) { git_submodule *sm; int error; @@ -1521,7 +1521,7 @@ int git_submodule_status(unsigned int *status, git_repository *repo, const char if ((error = git_submodule_lookup(&sm, repo, name)) < 0) return error; - error = git_submodule__status(status, NULL, NULL, NULL, sm, 0); + error = git_submodule__status(status, NULL, NULL, NULL, sm, ignore); git_submodule_free(sm); return error; diff --git a/tests/diff/tree.c b/tests/diff/tree.c index 6dd17203d..977e21f5b 100644 --- a/tests/diff/tree.c +++ b/tests/diff/tree.c @@ -89,7 +89,7 @@ void test_diff_tree__0(void) } #define DIFF_OPTS(FLAGS, CTXT) \ - {GIT_DIFF_OPTIONS_VERSION, (FLAGS), GIT_SUBMODULE_IGNORE_DEFAULT, \ + {GIT_DIFF_OPTIONS_VERSION, (FLAGS), GIT_SUBMODULE_IGNORE_FALLBACK, \ {NULL,0}, NULL, NULL, (CTXT), 1} void test_diff_tree__options(void) diff --git a/tests/submodule/status.c b/tests/submodule/status.c index 4d51d8dc0..6721ee92a 100644 --- a/tests/submodule/status.c +++ b/tests/submodule/status.c @@ -107,56 +107,47 @@ void test_submodule_status__ignore_none(void) cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0); } -static int set_sm_ignore(git_submodule *sm, const char *name, void *payload) -{ - git_submodule_ignore_t ignore = *(git_submodule_ignore_t *)payload; - GIT_UNUSED(name); - git_submodule_set_ignore(g_repo, git_submodule_name(sm), ignore); - return 0; -} - void test_submodule_status__ignore_untracked(void) { unsigned int status; git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_UNTRACKED; rm_submodule("sm_unchanged"); - cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND); refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS); refute_submodule_exists(g_repo, "not", GIT_EEXISTS); - status = get_submodule_status(g_repo, "sm_changed_index"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_index", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0); - status = get_submodule_status(g_repo, "sm_changed_head"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_head", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - status = get_submodule_status(g_repo, "sm_changed_file"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_file", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0); - status = get_submodule_status(g_repo, "sm_changed_untracked_file"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_untracked_file", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_missing_commits"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_missing_commits", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - status = get_submodule_status(g_repo, "sm_added_and_uncommited"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_added_and_uncommited", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); /* removed sm_unchanged for deleted workdir */ - status = get_submodule_status(g_repo, "sm_unchanged"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); /* now mkdir sm_unchanged to test uninitialized */ cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0)); - status = get_submodule_status(g_repo, "sm_unchanged"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); /* update sm_changed_head in index */ add_submodule_to_index("sm_changed_head"); - status = get_submodule_status(g_repo, "sm_changed_head"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_head", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); } @@ -166,42 +157,41 @@ void test_submodule_status__ignore_dirty(void) git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_DIRTY; rm_submodule("sm_unchanged"); - cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND); refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS); refute_submodule_exists(g_repo, "not", GIT_EEXISTS); - status = get_submodule_status(g_repo, "sm_changed_index"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_index", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_changed_head"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_head", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - status = get_submodule_status(g_repo, "sm_changed_file"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_file", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_changed_untracked_file"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_untracked_file", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_missing_commits"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_missing_commits", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); - status = get_submodule_status(g_repo, "sm_added_and_uncommited"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_added_and_uncommited", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); /* removed sm_unchanged for deleted workdir */ - status = get_submodule_status(g_repo, "sm_unchanged"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); /* now mkdir sm_unchanged to test uninitialized */ cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0)); - status = get_submodule_status(g_repo, "sm_unchanged"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); /* update sm_changed_head in index */ add_submodule_to_index("sm_changed_head"); - status = get_submodule_status(g_repo, "sm_changed_head"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_head", ign)); cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); } @@ -211,42 +201,41 @@ void test_submodule_status__ignore_all(void) git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_ALL; rm_submodule("sm_unchanged"); - cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND); refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS); refute_submodule_exists(g_repo, "not", GIT_EEXISTS); - status = get_submodule_status(g_repo, "sm_changed_index"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_index", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_changed_head"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_head", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_changed_file"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_file", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_changed_untracked_file"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_untracked_file", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_missing_commits"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_missing_commits", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); - status = get_submodule_status(g_repo, "sm_added_and_uncommited"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_added_and_uncommited", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); /* removed sm_unchanged for deleted workdir */ - status = get_submodule_status(g_repo, "sm_unchanged"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); /* now mkdir sm_unchanged to test uninitialized */ cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0)); - status = get_submodule_status(g_repo, "sm_unchanged"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); /* update sm_changed_head in index */ add_submodule_to_index("sm_changed_head"); - status = get_submodule_status(g_repo, "sm_changed_head"); + cl_git_pass(git_submodule_status(&status, g_repo,"sm_changed_head", ign)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); } diff --git a/tests/submodule/submodule_helpers.c b/tests/submodule/submodule_helpers.c index 36838228f..d5e02360a 100644 --- a/tests/submodule/submodule_helpers.c +++ b/tests/submodule/submodule_helpers.c @@ -168,7 +168,7 @@ unsigned int get_submodule_status(git_repository *repo, const char *name) assert(repo && name); - cl_git_pass(git_submodule_status(&status, repo, name)); + cl_git_pass(git_submodule_status(&status, repo, name, GIT_SUBMODULE_IGNORE_FALLBACK)); return status; } diff --git a/tests/submodule/update.c b/tests/submodule/update.c index 85870434e..fed6d38b3 100644 --- a/tests/submodule/update.c +++ b/tests/submodule/update.c @@ -103,7 +103,7 @@ void test_submodule_update__update_submodule(void) cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); /* verify the initial state of the submodule */ - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | @@ -114,7 +114,7 @@ void test_submodule_update__update_submodule(void) cl_git_pass(git_submodule_update(sm, 0, &update_options)); /* verify state */ - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | @@ -142,7 +142,7 @@ void test_submodule_update__update_and_init_submodule(void) /* get the submodule */ cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | @@ -177,7 +177,7 @@ void test_submodule_update__update_already_checked_out_submodule(void) /* Initialize and update the sub repository */ cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | @@ -203,7 +203,7 @@ void test_submodule_update__update_already_checked_out_submodule(void) * HEAD commit and index should be updated, but not the workdir. */ - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); git_submodule_free(sm); cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); @@ -255,7 +255,7 @@ void test_submodule_update__update_blocks_on_dirty_wd(void) /* Initialize and update the sub repository */ cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | @@ -281,7 +281,7 @@ void test_submodule_update__update_blocks_on_dirty_wd(void) * HEAD commit and index should be updated, but not the workdir. */ - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); git_submodule_free(sm); cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); @@ -332,7 +332,7 @@ void test_submodule_update__can_force_update(void) /* Initialize and update the sub repository */ cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | @@ -357,7 +357,7 @@ void test_submodule_update__can_force_update(void) * Verify state after checkout of parent repository. The submodule ID in the * HEAD commit and index should be updated, but not the workdir. */ - cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo")); + cl_git_pass(git_submodule_status(&submodule_status, g_repo, "testrepo", GIT_SUBMODULE_IGNORE_FALLBACK)); git_submodule_free(sm); cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); -- 2.39.5