In the upcoming git-rebase-to-C rewrite, it is a common operation to check if the worktree has unstaged changes, so that it can complain that the worktree is dirty. builtin/pull.c already implements this function. Move it to rebase-common.c so that it can be shared between all rebase backends and git-pull. Signed-off-by: Paul Tan <pyokagan@xxxxxxxxx> --- builtin/pull.c | 19 ++----------------- rebase-common.c | 14 ++++++++++++++ rebase-common.h | 5 +++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/builtin/pull.c b/builtin/pull.c index 10eff03..9e65dc9 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -17,6 +17,7 @@ #include "revision.h" #include "tempfile.h" #include "lockfile.h" +#include "rebase-common.h" enum rebase_type { REBASE_INVALID = -1, @@ -306,22 +307,6 @@ static enum rebase_type config_get_rebase(void) } /** - * Returns 1 if there are unstaged changes, 0 otherwise. - */ -static int has_unstaged_changes(const char *prefix) -{ - struct rev_info rev_info; - int result; - - init_revisions(&rev_info, prefix); - DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES); - DIFF_OPT_SET(&rev_info.diffopt, QUICK); - diff_setup_done(&rev_info.diffopt); - result = run_diff_files(&rev_info, 0); - return diff_result_code(&rev_info.diffopt, result); -} - -/** * Returns 1 if there are uncommitted changes, 0 otherwise. */ static int has_uncommitted_changes(const char *prefix) @@ -355,7 +340,7 @@ static void die_on_unclean_work_tree(const char *prefix) update_index_if_able(&the_index, lock_file); rollback_lock_file(lock_file); - if (has_unstaged_changes(prefix)) { + if (cache_has_unstaged_changes()) { error(_("Cannot pull with rebase: You have unstaged changes.")); do_die = 1; } diff --git a/rebase-common.c b/rebase-common.c index 97b0687..61be8f1 100644 --- a/rebase-common.c +++ b/rebase-common.c @@ -4,6 +4,7 @@ #include "run-command.h" #include "refs.h" #include "lockfile.h" +#include "revision.h" void refresh_and_write_cache(unsigned int flags) { @@ -15,6 +16,19 @@ void refresh_and_write_cache(unsigned int flags) die(_("unable to write index file")); } +int cache_has_unstaged_changes(void) +{ + struct rev_info rev_info; + int result; + + init_revisions(&rev_info, NULL); + DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES); + DIFF_OPT_SET(&rev_info.diffopt, QUICK); + diff_setup_done(&rev_info.diffopt); + result = run_diff_files(&rev_info, 0); + return diff_result_code(&rev_info.diffopt, result); +} + void rebase_options_init(struct rebase_options *opts) { oidclr(&opts->onto); diff --git a/rebase-common.h b/rebase-common.h index 4586f03..9d14e25 100644 --- a/rebase-common.h +++ b/rebase-common.h @@ -6,6 +6,11 @@ */ void refresh_and_write_cache(unsigned int); +/** + * Returns 1 if there are unstaged changes, 0 otherwise. + */ +int cache_has_unstaged_changes(void); + /* common rebase backend options */ struct rebase_options { struct object_id onto; -- 2.7.0 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html