From: Junio C Hamano Date: Thu, 12 Jan 2006 07:33:20 GMT Subject: [PATCH] checkout: automerge local changes while switching branches. Message-ID: <7v4q49uchr.fsf_-_@assigned-by-dhcp.cox.net> In-Reply-To: <81b0412b0601100731p46ec276btfe04382a9e53bd5c@mail.gmail.com> When switching branches from A to B, if the working tree has a local modification at paths that are different between A and B, we refused the operation saying "cannot merge." This attempts to do an automerge for such paths. Signed-off-by: Junio C Hamano --- Alex Riesen writes: > On 1/9/06, Junio C Hamano wrote: >> 2. Fix "git checkout " so that it does a reasonable thing >> even when a dirty path is different in current HEAD and >> destination branch. Then I could: >> >> $ git checkout symref ;# this would not work in the current git >> # it would die like this: >> # $ git checkout symref >> # fatal: Entry 'gitweb.cgi' not uptodate. Cannot merge. > > That is actually very interesting. I already wished sometimes to be > able to switch branches with a dirty working directory (and usually > ended up with git diff+checkout+apply). > Even if it results in a merge and conflict markers in files it looks > like a very practical idea! This is still experimental and probably has rough edges, but I actually tested it once and it worked fine ;-). git-checkout.sh | 24 +++++++++++++++++++++++- 1 files changed, 23 insertions(+), 1 deletions(-) 7929db987a9aac1d0370b64a8a00ffa13e6bab82 diff --git a/git-checkout.sh b/git-checkout.sh index 3bbd111..1b2db91 100755 --- a/git-checkout.sh +++ b/git-checkout.sh @@ -121,7 +121,29 @@ then git-checkout-index -q -f -u -a else git-update-index --refresh >/dev/null - git-read-tree -m -u $old $new + git-read-tree -m -u $old $new || ( + echo >&2 -n "Try automerge [y/N]? " + read yesno + case "$yesno" in [yY]*) ;; *) exit 1 ;; esac + + # NEEDSWORK: We may want to reset the index from the $new for + # these paths after the automerge happens, but it is not done + # yet. Probably we need to leave unmerged ones alone, and + # yank the object name & mode from $new for cleanly merged + # paths and stuff them in the index. + + names=`git diff-files --name-only` + echo "$names" | git update-index --remove --stdin + + work=`git write-tree` && + git read-tree -m -u $old $work $new || exit + if result=`git write-tree 2>/dev/null` + then + echo >&2 "Trivially automerged." ;# can this even happen? + exit 0 + fi + git merge-index -o git-merge-one-file -a + ) fi # -- 1.1.1-g8ecb