git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] checkout: automerge local changes while switching branches.

From
Junio C Hamano <junkio@cox.net>
Date
Jan 12, 2006, 07:33 UTC
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 <junkio@cox.net>
---
 Alex Riesen <raa.lkml-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
 > On 1/9/06, Junio C Hamano <junkio-j9pdmedNgrk@public.gmane.org> wrote:
 >> 2. Fix "git checkout <branch>" 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
Previous: Alex Riesen
Message 5 of 5 in “RE: git pull on Linux/ACPI release tree”
  1. Brown, LenJan 9, 2006
  2. Linus TorvaldsJan 9, 2006
  3. Junio C HamanoJan 9, 2006
  4. Alex RiesenJan 10, 2006
  5. checkout: automerge local changes while switching branches.Junio C Hamano, Jan 12, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.