{"thread":{"id":"34683","subject":"[PATCH] pull: Allow pull to preserve merges when rebasing.","startedAt":"2013-08-13T03:43:42Z","lastAt":"2013-08-13T03:43:42Z","messageCount":1,"participants":["Stephen Haberman"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"225125","messageId":"1376365422-24339-1-git-send-email-stephen@exigencecorp.com","threadId":"34683","inReplyTo":null,"subject":"[PATCH] pull: Allow pull to preserve merges when rebasing.","fromName":"Stephen Haberman","fromEmail":"stephen@exigencecorp.com","sentAt":"2013-08-13T03:43:42Z","receivedAt":"2013-08-13T03:43:42Z","isPatch":true,"sender":{"key":"stephen@exigencecorp.com","avatar":"https://gravatar.com/avatar/23b93ad70a06ce53505f17ddba65176edbcfb6588e7a4c1a2dca04aaf0a6aff1?d=mp&s=160"},"body":"If a user is working on master, and has merged in their feature branch, but now\nhas to \"git pull\" because master moved, with pull.rebase their feature branch\nwill be flattened into master.\n\nThis is because \"git pull\" currently does not know about rebase's preserve\nmerges flag, which would avoid this behavior, as it would instead replay just\nthe merge commit of the feature branch onto the new master, and not replay each\nindividual commit in the feature branch.\n\nAdd a --rebase=preserve option, which will pass along --preserve-merges to\nrebase.\n\nAlso add 'preserve' to the allowed values for the pull.rebase config setting.\n\nSigned-off-by: Stephen Haberman <stephen@exigencecorp.com>\n---\nHi,\n\nThis is v5 of my --rebase=preserve patch, the last discussion of which\nwas here:\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/232156\n\nThis update has two small changes:\n\n* Change the usage output to match Junio's suggestion, and\n\n* Respect existing true-but-not-\"true\" values of pull.rebase, using\nJunio's bool_or_string_config function (with a test for pull.rebase=1).\n\nThanks!\n\n Documentation/config.txt   |  8 +++++\n Documentation/git-pull.txt | 18 ++++++----\n git-pull.sh                | 31 +++++++++++++---\n t/t5520-pull.sh            | 89 ++++++++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 135 insertions(+), 11 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex ec57a15..4c22be2 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -766,6 +766,10 @@ branch.<name>.rebase::\n \t\"git pull\" is run. See \"pull.rebase\" for doing this in a non\n \tbranch-specific manner.\n +\n+\tWhen preserve, also pass `--preserve-merges` along to 'git rebase'\n+\tso that locally committed merge commits will not be flattened\n+\tby running 'git pull'.\n++\n *NOTE*: this is a possibly dangerous operation; do *not* use\n it unless you understand the implications (see linkgit:git-rebase[1]\n for details).\n@@ -1826,6 +1830,10 @@ pull.rebase::\n \tpull\" is run. See \"branch.<name>.rebase\" for setting this on a\n \tper-branch basis.\n +\n+\tWhen preserve, also pass `--preserve-merges` along to 'git rebase'\n+\tso that locally committed merge commits will not be flattened\n+\tby running 'git pull'.\n++\n *NOTE*: this is a possibly dangerous operation; do *not* use\n it unless you understand the implications (see linkgit:git-rebase[1]\n for details).\ndiff --git a/Documentation/git-pull.txt b/Documentation/git-pull.txt\nindex 6ef8d59..beea10b 100644\n--- a/Documentation/git-pull.txt\n+++ b/Documentation/git-pull.txt\n@@ -102,12 +102,18 @@ include::merge-options.txt[]\n :git-pull: 1\n \n -r::\n---rebase::\n-\tRebase the current branch on top of the upstream branch after\n-\tfetching.  If there is a remote-tracking branch corresponding to\n-\tthe upstream branch and the upstream branch was rebased since last\n-\tfetched, the rebase uses that information to avoid rebasing\n-\tnon-local changes.\n+--rebase[=false|true|preserve]::\n+\tWhen true, rebase the current branch on top of the upstream\n+\tbranch after fetching. If there is a remote-tracking branch\n+\tcorresponding to the upstream branch and the upstream branch\n+\twas rebased since last fetched, the rebase uses that information\n+\tto avoid rebasing non-local changes.\n++\n+When preserve, also rebase the current branch on top of the upstream\n+branch, but pass `--preserve-merges` along to `git rebase` so that\n+locally created merge commits will not be flattened.\n++\n+When false, merge the current branch into the upstream branch.\n +\n See `pull.rebase`, `branch.<name>.rebase` and `branch.autosetuprebase` in\n linkgit:git-config[1] if you want to make `git pull` always use\ndiff --git a/git-pull.sh b/git-pull.sh\nindex f0df41c..e11d9a0 100755\n--- a/git-pull.sh\n+++ b/git-pull.sh\n@@ -4,7 +4,7 @@\n #\n # Fetch one or more remote refs and merge it/them into the current HEAD.\n \n-USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'\n+USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'\n LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'\n SUBDIRECTORY_OK=Yes\n OPTIONS_SPEC=\n@@ -38,15 +38,19 @@ Please, commit your changes before you can merge.\")\"\n test -z \"$(git ls-files -u)\" || die_conflict\n test -f \"$GIT_DIR/MERGE_HEAD\" && die_merge\n \n+bool_or_string_config () {\n+\tgit config --bool \"$1\" 2>/dev/null || git config \"$1\"\n+}\n+\n strategy_args= diffstat= no_commit= squash= no_ff= ff_only=\n log_arg= verbosity= progress= recurse_submodules= verify_signatures=\n-merge_args= edit=\n+merge_args= edit= rebase_args=\n curr_branch=$(git symbolic-ref -q HEAD)\n curr_branch_short=\"${curr_branch#refs/heads/}\"\n-rebase=$(git config --bool branch.$curr_branch_short.rebase)\n+rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)\n if test -z \"$rebase\"\n then\n-\trebase=$(git config --bool pull.rebase)\n+\trebase=$(bool_or_string_config pull.rebase)\n fi\n dry_run=\n while :\n@@ -110,6 +114,9 @@ do\n \t\tesac\n \t\tmerge_args=\"$merge_args$xx \"\n \t\t;;\n+\t-r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)\n+\t\trebase=\"${1#*=}\"\n+\t\t;;\n \t-r|--r|--re|--reb|--reba|--rebas|--rebase)\n \t\trebase=true\n \t\t;;\n@@ -145,6 +152,20 @@ do\n \tshift\n done\n \n+case \"$rebase\" in\n+preserve)\n+\trebase=true\n+\trebase_args=--preserve-merges\n+\t;;\n+true|false|'')\n+\t;;\n+*)\n+\techo \"Invalid value for --rebase, should be true, false, or preserve\"\n+\tusage\n+\texit 1\n+\t;;\n+esac\n+\n error_on_no_merge_candidates () {\n \texec >&2\n \tfor opt\n@@ -292,7 +313,7 @@ fi\n merge_name=$(git fmt-merge-msg $log_arg <\"$GIT_DIR/FETCH_HEAD\") || exit\n case \"$rebase\" in\n true)\n-\teval=\"git-rebase $diffstat $strategy_args $merge_args $verbosity\"\n+\teval=\"git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity\"\n \teval=\"$eval --onto $merge_head ${oldremoteref:-$merge_head}\"\n \t;;\n *)\ndiff --git a/t/t5520-pull.sh b/t/t5520-pull.sh\nindex ed4d9c8..227d293 100755\n--- a/t/t5520-pull.sh\n+++ b/t/t5520-pull.sh\n@@ -148,6 +148,95 @@ test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '\n \ttest new = $(git show HEAD:file2)\n '\n \n+# add a feature branch, keep-merge, that is merged into master, so the\n+# test can try preserving the merge commit (or not) with various\n+# --rebase flags/pull.rebase settings.\n+test_expect_success 'preserve merge setup' '\n+\tgit reset --hard before-rebase &&\n+\tgit checkout -b keep-merge second^ &&\n+\ttest_commit file3 &&\n+\tgit checkout to-rebase &&\n+\tgit merge keep-merge &&\n+\tgit tag before-preserve-rebase\n+'\n+\n+test_expect_success 'pull.rebase=false create a new merge commit' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase false &&\n+\tgit pull . copy &&\n+\ttest $(git rev-parse HEAD^1) = $(git rev-parse before-preserve-rebase) &&\n+\ttest $(git rev-parse HEAD^2) = $(git rev-parse copy) &&\n+\ttest file3 = $(git show HEAD:file3.t)\n+'\n+\n+test_expect_success 'pull.rebase=true flattens keep-merge' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase true &&\n+\tgit pull . copy &&\n+\ttest $(git rev-parse HEAD^^) = $(git rev-parse copy) &&\n+\ttest file3 = $(git show HEAD:file3.t)\n+'\n+\n+test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase 1 &&\n+\tgit pull . copy &&\n+\ttest $(git rev-parse HEAD^^) = $(git rev-parse copy) &&\n+\ttest file3 = $(git show HEAD:file3.t)\n+'\n+\n+test_expect_success 'pull.rebase=preserve rebases and merges keep-merge' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase preserve &&\n+\tgit pull . copy &&\n+\ttest $(git rev-parse HEAD^^) = $(git rev-parse copy) &&\n+\ttest $(git rev-parse HEAD^2) = $(git rev-parse keep-merge)\n+'\n+\n+test_expect_success 'pull.rebase=invalid fails' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase invalid &&\n+\t! git pull . copy\n+'\n+\n+test_expect_success '--rebase=false create a new merge commit' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase true &&\n+\tgit pull --rebase=false . copy &&\n+\ttest $(git rev-parse HEAD^1) = $(git rev-parse before-preserve-rebase) &&\n+\ttest $(git rev-parse HEAD^2) = $(git rev-parse copy) &&\n+\ttest file3 = $(git show HEAD:file3.t)\n+'\n+\n+test_expect_success '--rebase=true rebases and flattens keep-merge' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase preserve &&\n+\tgit pull --rebase=true . copy &&\n+\ttest $(git rev-parse HEAD^^) = $(git rev-parse copy) &&\n+\ttest file3 = $(git show HEAD:file3.t)\n+'\n+\n+test_expect_success '--rebase=preserve rebases and merges keep-merge' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase true &&\n+\tgit pull --rebase=preserve . copy &&\n+\ttest $(git rev-parse HEAD^^) = $(git rev-parse copy) &&\n+\ttest $(git rev-parse HEAD^2) = $(git rev-parse keep-merge)\n+'\n+\n+test_expect_success '--rebase=invalid fails' '\n+\tgit reset --hard before-preserve-rebase &&\n+\t! git pull --rebase=invalid . copy\n+'\n+\n+test_expect_success '--rebase overrides pull.rebase=preserve and flattens keep-merge' '\n+\tgit reset --hard before-preserve-rebase &&\n+\ttest_config pull.rebase preserve &&\n+\tgit pull --rebase . copy &&\n+\ttest $(git rev-parse HEAD^^) = $(git rev-parse copy) &&\n+\ttest file3 = $(git show HEAD:file3.t)\n+'\n+\n test_expect_success '--rebase with rebased upstream' '\n \n \tgit remote add -f me . &&\n-- \n1.8.1.2\n"}]}