{"thread":{"id":"3066","subject":"[PATCH] checkout: merge local modifications while switching branches.","startedAt":"2006-01-13T01:55:07Z","lastAt":"2006-01-13T01:55:07Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"14584","messageId":"7vpsmwhoxw.fsf@assigned-by-dhcp.cox.net","threadId":"3066","inReplyTo":null,"subject":"[PATCH] checkout: merge local modifications while switching branches.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-01-13T01:55:07Z","receivedAt":"2006-01-13T01:55:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":" * Instead of going interactive, introduce a command line switch\n   '-m' to allow merging changes when normal two-way merge by\n   read-tree prevents branch switching.\n\n * Leave the unmerged stages intact if automerge fails, but\n   reset index entries of cleanly merged paths to that of the\n   new branch, so that \"git diff\" (not \"git diff HEAD\") would\n   show the local modifications.\n\n * Swap the order of trees in read-tree three-way merge used in\n   the fallback, so that `git diff` to show the conflicts become\n   more natural.\n\n * Describe the new option and give more examples in the documentation.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n * This comes on top of my yesterday's patch.  I am trying this\n   out today in my day-job by deliberately developing on the\n   \"test\" branch (which is a merge of all the topic branches),\n   and then doing \"checkout -m\" to switch back to the relevant\n   topic branch.  Things seem to work nicely.\n\n Documentation/git-checkout.txt |   76 ++++++++++++++++++++++++++++++++++++----\n git-checkout.sh                |   60 ++++++++++++++++++++------------\n 2 files changed, 107 insertions(+), 29 deletions(-)\n\n19394e9d4e99a1e49a5b958db66228df07dc8afb\ndiff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt\nindex 9442c66..df9a618 100644\n--- a/Documentation/git-checkout.txt\n+++ b/Documentation/git-checkout.txt\n@@ -7,7 +7,7 @@ git-checkout - Checkout and switch to a \n \n SYNOPSIS\n --------\n-'git-checkout' [-f] [-b <new_branch>] [<branch>] [<paths>...]\n+'git-checkout' [-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]\n \n DESCRIPTION\n -----------\n@@ -34,6 +34,19 @@ OPTIONS\n -b::\n \tCreate a new branch and start it at <branch>.\n \n+-m::\n+\tIf you have local modifications to a file that is\n+\tdifferent between the current branch and the branch you\n+\tare switching to, the command refuses to switch\n+\tbranches, to preserve your modifications in context.\n+\tWith this option, a three-way merge between the current\n+\tbranch, your working tree contents, and the new branch\n+\tis done, and you will be on the new branch.\n++\n+When a merge conflict happens, the index entries for conflicting\n+paths are left unmerged, and you need to resolve the conflicts\n+and mark the resolved paths with `git update-index`.\n+\n <new_branch>::\n \tName for the new branch.\n \n@@ -42,13 +55,13 @@ OPTIONS\n \tcommit. Defaults to HEAD.\n \n \n-EXAMPLE\n--------\n+EXAMPLES\n+--------\n \n-The following sequence checks out the `master` branch, reverts\n+. The following sequence checks out the `master` branch, reverts\n the `Makefile` to two revisions back, deletes hello.c by\n mistake, and gets it back from the index.\n-\n++\n ------------\n $ git checkout master <1>\n $ git checkout master~2 Makefile <2>\n@@ -59,15 +72,64 @@ $ git checkout hello.c <3>\n <2> take out a file out of other commit\n <3> or \"git checkout -- hello.c\", as in the next example.\n ------------\n-\n++\n If you have an unfortunate branch that is named `hello.c`, the\n last step above would be confused as an instruction to switch to\n that branch.  You should instead write:\n-\n++\n ------------\n $ git checkout -- hello.c\n ------------\n \n+. After working in a wrong branch, switching to the correct\n+branch you would want to is done with:\n++\n+------------\n+$ git checkout mytopic\n+------------\n++\n+However, your \"wrong\" branch and correct \"mytopic\" branch may\n+differ in files that you have locally modified, in which case,\n+the above checkout would fail like this:\n++\n+------------\n+$ git checkout mytopic\n+fatal: Entry 'frotz' not uptodate. Cannot merge.\n+------------\n++\n+You can give the `-m` flag to the command, which would try a\n+three-way merge:\n++\n+------------\n+$ git checkout -m mytopic\n+Auto-merging frotz\n+------------\n++\n+After this three-way merge, the local modifications are _not_\n+registered in your index file, so `git diff` would show you what\n+changes you made since the tip of the new branch.\n+\n+. When a merge conflict happens during switching branches with\n+the `-m` option, you would see something like this:\n++\n+------------\n+$ git checkout -m mytopic\n+Auto-merging frotz\n+merge: warning: conflicts during merge\n+ERROR: Merge conflict in frotz\n+fatal: merge program failed\n+------------\n++\n+At this point, `git diff` shows the changes cleanly merged as in\n+the previous example, as well as the changes in the conflicted\n+files.  Edit and resolve the conflict and mark it resolved with\n+`git update-index` as usual:\n++\n+------------\n+$ edit frotz\n+$ git update-index frotz\n+------------\n+\n \n Author\n ------\ndiff --git a/git-checkout.sh b/git-checkout.sh\nindex 76e6a41..bd7f007 100755\n--- a/git-checkout.sh\n+++ b/git-checkout.sh\n@@ -1,6 +1,6 @@\n #!/bin/sh\n \n-USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'\n+USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'\n SUBDIRECTORY_OK=Sometimes\n . git-sh-setup\n \n@@ -9,6 +9,7 @@ new=\n force=\n branch=\n newbranch=\n+merge=\n while [ \"$#\" != \"0\" ]; do\n     arg=\"$1\"\n     shift\n@@ -26,6 +27,9 @@ while [ \"$#\" != \"0\" ]; do\n \t\"-f\")\n \t\tforce=1\n \t\t;;\n+\t-m)\n+\t\tmerge=1\n+\t\t;;\n \t--)\n \t\tbreak\n \t\t;;\n@@ -71,7 +75,7 @@ done\n \n if test \"$#\" -ge 1\n then\n-\tif test '' != \"$newbranch$force\"\n+\tif test '' != \"$newbranch$force$merge\"\n \tthen\n \t\tdie \"updating paths and switching branches or forcing are incompatible.\"\n \tfi\n@@ -121,32 +125,44 @@ then\n \tgit-checkout-index -q -f -u -a\n else\n     git-update-index --refresh >/dev/null\n-    git-read-tree -m -u $old $new || (\n-\techo >&2 -n \"Try automerge [y/N]? \"\n-\tread yesno\n-\tcase \"$yesno\" in [yY]*) ;; *) exit 1 ;; esac\n-\n-\t# NEEDSWORK: We may want to reset the index from the $new for\n-\t# these paths after the automerge happens, but it is not done\n-\t# yet.  Probably we need to leave unmerged ones alone, and\n-\t# yank the object name & mode from $new for cleanly merged\n-\t# paths and stuff them in the index.\n-\n-\tnames=`git diff-files --name-only`\n-\tcase \"$names\" in\n-\t'')\t;;\n-\t*)\n-\t\techo \"$names\" | git update-index --remove --stdin ;;\n+    merge_error=$(git-read-tree -m -u $old $new 2>&1) || (\n+\tcase \"$merge\" in\n+\t'')\n+\t\techo >&2 \"$merge_error\"\n+\t\texit 1 ;;\n \tesac\n \n+\t# Match the index to the working tree, and do a three-way.\n+    \tgit diff-files --name-only | git update-index --remove --stdin &&\n \twork=`git write-tree` &&\n-\tgit read-tree -m -u $old $work $new || exit\n+\tgit read-tree --reset $new &&\n+\tgit checkout-index -f -u -q -a &&\n+\tgit read-tree -m -u $old $new $work || exit\n+\n \tif result=`git write-tree 2>/dev/null`\n \tthen\n-\t    echo >&2 \"Trivially automerged.\" ;# can this even happen?\n-\t    exit 0\n+\t    echo >&2 \"Trivially automerged.\"\n+\telse\n+\t    git merge-index -o git-merge-one-file -a\n \tfi\n-\tgit merge-index -o git-merge-one-file -a\n+\n+\t# Do not register the cleanly merged paths in the index yet.\n+\t# this is not a real merge before committing, but just carrying\n+\t# the working tree changes along.\n+\tunmerged=`git ls-files -u`\n+\tgit read-tree --reset $new\n+\tcase \"$unmerged\" in\n+\t'')\t;;\n+\t*)\n+\t\t(\n+\t\t\tz40=0000000000000000000000000000000000000000\n+\t\t\techo \"$unmerged\" |\n+\t\t\tsed -e 's/^[0-7]* [0-9a-f]* /'\"0 $z40 /\"\n+\t\t\techo \"$unmerged\"\n+\t\t) | git update-index --index-info\n+\t\t;;\n+\tesac\n+\texit 0\n     )\n fi\n \n-- \n1.1.1-gae96\n"}]}