git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/2] Optionally check for uncommitted changes before switching branches.
       [not found] <6ed9774cb95e873e76a4ac406dd740caf954bd3b.1165485618.git.spearce@spearce.org>
@ 2006-12-07 10:02 ` Shawn O. Pearce
  2006-12-07 19:38   ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Shawn O. Pearce @ 2006-12-07 10:02 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Lately I have noticed a number of users are forgetting that they
have uncommitted changes in their working directory when they switch
to another branch.  This causes the user to accidentally carry those
changes into the new branch, which is usually not where they wanted
to commit them.  A correlation does appear to exist between the user
being interrupted in the middle of their task and the branch switch,
indicating they simply got distracted and forgot what was going on.

Git shouldn't cause the user to make mistakes when it can help to
prevent them.  So now users may set checkout.requireCleanDirectory
to true in their config file to have git-checkout verify the working
directory is clean before switching branches.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 Documentation/config.txt |    8 ++++++++
 git-checkout.sh          |   10 ++++++++++
 t/t3200-branch.sh        |   11 +++++++++++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 9d754c8..f10e8ac 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -136,6 +136,14 @@ checkout.requireSourceBranch::
 	be the source version if only one argument is supplied.
 	Default is false, to stay compatible with prior behavior.
 
+checkout.requireCleanDirectory::
+	If true tells git-checkout to verify there are no uncommitted
+	changes still in the index or working directory before
+	switching branches.  If uncommitted changes exist the -m
+	flag can be used to skip the check if the user really wanted
+	to carry those onto the new branch.  Default is false,
+	to stay compatible with prior behavior.
+
 pager.color::
 	A boolean to enable/disable colored output when the pager is in
 	use (default is true).
diff --git a/git-checkout.sh b/git-checkout.sh
index 5f9fb6e..c04b8c1 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -171,6 +171,16 @@ then
     git-read-tree --reset -u $new
 else
     git-update-index --refresh >/dev/null
+	if [ -n "$old" ] &&
+	   [ -z "$merge" ] &&
+	   [ Xtrue = "X`git-repo-config --bool checkout.requireCleanDirectory`" ]
+	then
+		if [ `git-diff-index --cached $old | wc -l` -gt 0 ] ||
+		   [ `git-diff-files | wc -l` -gt 0 ]
+		then
+			die "Working directory has uncommitted changes; commit, reset or use -m"
+		fi
+	fi
     merge_error=$(git-read-tree -m -u $old $new 2>&1) || (
 	case "$merge" in
 	'')
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 7e0c48b..9429827 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -84,4 +84,15 @@ test_expect_success \
 	'git-repo-config checkout.requireSourceBranch false
 	 git-checkout -b N'
 
+test_expect_failure \
+	'git checkout -b O works only if tree is clean' \
+	'git-repo-config checkout.requireCleanDirectory true
+	 echo atest >atest
+	 git add atest
+	 git-checkout -b O'
+
+test_expect_success \
+	'git checkout -m -b O works' \
+	'git-checkout -m -b O'
+
 test_done
-- 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] Optionally check for uncommitted changes before switching branches.
  2006-12-07 10:02 ` [PATCH 2/2] Optionally check for uncommitted changes before switching branches Shawn O. Pearce
@ 2006-12-07 19:38   ` Junio C Hamano
  2006-12-07 19:43     ` Shawn Pearce
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2006-12-07 19:38 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git

"Shawn O. Pearce" <spearce@spearce.org> writes:

> Git shouldn't cause the user to make mistakes when it can help to
> prevent them.  So now users may set checkout.requireCleanDirectory
> to true in their config file to have git-checkout verify the working
> directory is clean before switching branches.

A lot of times the reason to switch branches is because the user
starts to make a trivial change in the worktree and realizes
that the change belongs to another branch.  Other times it is
done by mistake and making it easier to notice that mistake is a
laudable goal.

Your patch allows -m to override this, but that destroys one
very useful feature of -m.  In the above "ah, this trivial thing
belongs to the other branch, so let's switch to the branch to
commit only that trivial piece and come back to the current
branch to continue what I'm doing" workflow, I usually first say
"git checkout" without -m to switch, and if it does not allow me
to switch, it is an indication that "the trivial thing" I
thought was trivial was not trivial.  I take it as a cue that I
should instead do it "the right way" (i.e. finish or stash away
what I am doing, switch to the branch in a clean state and fix
it properly).

Other times, when all (or most of) the changes in the work tree
logically should have started on a different branch, I do force
it with -m (and take the conflict markers in my worktree), but
being able to forbid the worktree merge by not giving -m is
important.

People with this new configuration set and has to override it
with a command line switch will lose this ability if you
overload that on '-m'.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] Optionally check for uncommitted changes before switching branches.
  2006-12-07 19:38   ` Junio C Hamano
@ 2006-12-07 19:43     ` Shawn Pearce
  2006-12-07 23:01       ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Shawn Pearce @ 2006-12-07 19:43 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Junio C Hamano <junkio@cox.net> wrote:
> "Shawn O. Pearce" <spearce@spearce.org> writes:
> 
> > Git shouldn't cause the user to make mistakes when it can help to
> > prevent them.  So now users may set checkout.requireCleanDirectory
> > to true in their config file to have git-checkout verify the working
> > directory is clean before switching branches.
[...snip...]
> People with this new configuration set and has to override it
> with a command line switch will lose this ability if you
> overload that on '-m'.

Yes.  That's a problem with this patch.

What about when this option is enabled then -m means do what we
did before, and -m -m (or -mm, or -m2) does what -m does when the
option is false?

-- 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] Optionally check for uncommitted changes before switching branches.
  2006-12-07 19:43     ` Shawn Pearce
@ 2006-12-07 23:01       ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2006-12-07 23:01 UTC (permalink / raw)
  To: Shawn Pearce; +Cc: git

Shawn Pearce <spearce@spearce.org> writes:

> What about when this option is enabled then -m means do what we
> did before, and -m -m (or -mm, or -m2) does what -m does when the
> option is false?

Wouldn't --force be more appropriate?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-12-07 23:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <6ed9774cb95e873e76a4ac406dd740caf954bd3b.1165485618.git.spearce@spearce.org>
2006-12-07 10:02 ` [PATCH 2/2] Optionally check for uncommitted changes before switching branches Shawn O. Pearce
2006-12-07 19:38   ` Junio C Hamano
2006-12-07 19:43     ` Shawn Pearce
2006-12-07 23:01       ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).