Re-implement the behavior introduced by f9189cf (pull --rebase: exit early when the working directory is dirty, 2008-05-21). Signed-off-by: Paul Tan <pyokagan@xxxxxxxxx> --- builtin/pull.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- t/t5520-pull.sh | 6 ++--- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/builtin/pull.c b/builtin/pull.c index a0958a7..c8d673d 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -14,6 +14,8 @@ #include "remote.h" #include "dir.h" #include "refs.h" +#include "revision.h" +#include "lockfile.h" /** * Given an option opt, where opt->value points to a char* and opt->defval is a @@ -327,6 +329,73 @@ static int config_get_rebase(void) return value; } +/** + * 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) +{ + struct rev_info rev_info; + int result; + + if (is_cache_unborn()) + return 0; + + init_revisions(&rev_info, prefix); + DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES); + DIFF_OPT_SET(&rev_info.diffopt, QUICK); + add_head_to_pending(&rev_info); + diff_setup_done(&rev_info.diffopt); + result = run_diff_index(&rev_info, 1); + return diff_result_code(&rev_info.diffopt, result); +} + +/** + * If the work tree has unstaged or uncommitted changes, dies with the + * appropriate message. + */ +static void die_on_unclean_work_tree(const char *prefix) +{ + struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file)); + int do_die = 0; + + hold_locked_index(lock_file, 0); + refresh_cache(REFRESH_QUIET); + update_index_if_able(&the_index, lock_file); + rollback_lock_file(lock_file); + + if (has_unstaged_changes(prefix)) { + error(_("Cannot pull with rebase: You have unstaged changes.")); + do_die = 1; + } + + if (has_uncommitted_changes(prefix)) { + if (do_die) + error(_("Additionally, your index contains uncommitted changes.")); + else + error(_("Cannot pull with rebase: Your index contains uncommitted changes.")); + do_die = 1; + } + + if (do_die) + exit(1); +} + struct known_remote { struct known_remote *next; struct remote *remote; @@ -782,9 +851,15 @@ int cmd_pull(int argc, const char **argv, const char *prefix) if (get_sha1("HEAD", orig_head)) hashclr(orig_head); - if (opt_rebase) + if (opt_rebase) { + if (is_null_sha1(orig_head) && !is_cache_unborn()) + die(_("Updating an unborn branch with changes added to the index.")); + + die_on_unclean_work_tree(prefix); + if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs)) hashclr(rebase_fork_point); + } if (run_fetch(repo, refspecs)) return 1; diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh index 17254ee..62dbfb5 100755 --- a/t/t5520-pull.sh +++ b/t/t5520-pull.sh @@ -390,7 +390,7 @@ test_expect_success 'rebased upstream + fetch + pull --rebase' ' ' -test_expect_failure 'pull --rebase dies early with dirty working directory' ' +test_expect_success 'pull --rebase dies early with dirty working directory' ' git checkout to-rebase && git update-ref refs/remotes/me/copy copy^ && @@ -420,7 +420,7 @@ test_expect_success 'pull --rebase works on branch yet to be born' ' test_cmp expect actual ' -test_expect_failure 'pull --rebase fails on unborn branch with staged changes' ' +test_expect_success 'pull --rebase fails on unborn branch with staged changes' ' test_when_finished "rm -rf empty_repo2" && git init empty_repo2 && ( @@ -492,7 +492,7 @@ test_expect_success 'git pull --rebase does not reapply old patches' ' ) ' -test_expect_failure 'git pull --rebase against local branch' ' +test_expect_success 'git pull --rebase against local branch' ' git checkout -b copy2 to-rebase-orig && git pull --rebase . to-rebase && test "conflicting modification" = "$(cat file)" && -- 2.1.4 -- 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