threads / discuss / 28278

Cannot rewrite branch(es) with a dirty working directory

Subject: Cannot rewrite branch(es) with a dirty working directory

## tl;dr

5 messages between Sep 1, 2011 and Sep 2, 2011.

replies: 4people: 3as markdown or json

James Blackburn· Sep 1, 2011, 15:52 UTC · lore
Hi All,

I get a spurious: "Cannot rewrite branch(es) with a dirty working directory." trying to filter-branch in a clean git repo (having done a reset). The error disappears when I do git status.

Log of the shell commands: bash:jamesb:xl-cbga-20:33083> mkdir org.eclipse.cdt.core.linux.ia64 bash:jamesb:xl-cbga-20:33084> cp -r ../../../CDT_HEAD_GIT/org.eclipse.cdt/.git org.eclipse.cdt.core.linux.ia64/ bash:jamesb:xl-cbga-20:33085> cd org.eclipse.cdt.core.linux.ia64/ mbash:jamesb:xl-cbga-20:33086> git reset --hard Checking out files: 100% (11879/11879), done. HEAD is now at a03d454 Build against a local mirror of the 3.7 p2 repo bash:jamesb:xl-cbga-20:33087> git filter-branch --subdirectory-filter core/org.eclipse.cdt.core.linux.ia64 -- master Cannot rewrite branch(es) with a dirty working directory. bash:jamesb:xl-cbga-20:33088> git status # On branch master nothing to commit (working directory clean) bash:jamesb:xl-cbga-20:33089> git filter-branch --subdirectory-filter core/org.eclipse.cdt.core.linux.ia64 -- master Rewrite d7092b12c93925f6f7c4725a5abc72e55650621c (16/16) Ref 'refs/heads/master' was rewritten bash:jamesb:xl-cbga-20:33090> git --version git version 1.7.3.2

Is there a particular reason why filter-branch thinks the tree is dirty, and status magically fixes this?

Cheers, James

Matthieu Moy· Sep 1, 2011, 17:14 UTC · re: James Blackburn · lore

Re: Cannot rewrite branch(es) with a dirty working directory

James Blackburn <jamesblackburn@gmail.com> writes:
> Is there a particular reason why filter-branch thinks the tree is
> dirty,

No idea. It comes after a "git reset --hard", so it's supposed to be clean.

> and status magically fixes this?

The index is considered dirty if the stat information (timestamp) is different. "git status" updates the stat-cache to consider unmodified content as up-to-date. So, it's normal that a "git status" repairs the dirty index.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Jeff King· Sep 1, 2011, 21:50 UTC · re: Matthieu Moy · lore

Re: Cannot rewrite branch(es) with a dirty working directory

On Thu, Sep 01, 2011 at 07:14:30PM +0200, Matthieu Moy wrote:
Show 7 quoted lines
> James Blackburn <jamesblackburn@gmail.com> writes:
> 
> > Is there a particular reason why filter-branch thinks the tree is
> > dirty,
> 
> No idea. It comes after a "git reset --hard", so it's supposed to be
> clean.

I wonder if there are racily clean entries[1] in the index, and diff-index reports them as potential changes.

At any rate, filter-branch should probably be refreshing the index before checking for dirtiness, which would give the correct answer either way.

-Peff
[1] See Documentation/technical/racy-git.txt for more information.
Jeff King· Sep 1, 2011, 21:53 UTC · re: Jeff King · lore

Re: Cannot rewrite branch(es) with a dirty working directory

On Thu, Sep 01, 2011 at 05:50:03PM -0400, Jeff King wrote:
Show 9 quoted lines
> > No idea. It comes after a "git reset --hard", so it's supposed to be
> > clean.
> 
> I wonder if there are racily clean entries[1] in the index, and
> diff-index reports them as potential changes.
> 
> At any rate, filter-branch should probably be refreshing the index
> before checking for dirtiness, which would give the correct answer
> either way.
Actually, we've already factored this logic out, so let's use it.
-- >8 --
Subject: filter-branch: use require_clean_work_tree

Filter-branch already requires that we have a clean work tree before starting. However, it failed to refresh the index before checking, which means it could be wrong in the case of stat-dirtiness.

Instead of simply adding a call to refresh the index, let's switch to using the require_clean_work_tree function provided by git-sh-setup. It does exactly what we want, and with fewer lines of code and more specific output messages.

Signed-off-by: Jeff King <peff@peff.net>
---
 git-filter-branch.sh |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index 804a7f4..add2c02 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -108,9 +108,7 @@ OPTIONS_SPEC=
 . git-sh-setup
 
 if [ "$(is_bare_repository)" = false ]; then
-	git diff-files --ignore-submodules --quiet &&
-	git diff-index --cached --quiet HEAD -- ||
-	die "Cannot rewrite branch(es) with a dirty working directory."
+	require_clean_work_tree 'rewrite branches'
 fi
 
 tempdir=.git-rewrite
-- 
1.7.6.10.g62f04
Matthieu Moy· Sep 2, 2011, 05:52 UTC · re: Jeff King · lore

Re: Cannot rewrite branch(es) with a dirty working directory

Jeff King <peff@peff.net> writes:
Show 13 quoted lines
> --- a/git-filter-branch.sh
> +++ b/git-filter-branch.sh
> @@ -108,9 +108,7 @@ OPTIONS_SPEC=
>  . git-sh-setup
>  
>  if [ "$(is_bare_repository)" = false ]; then
> -	git diff-files --ignore-submodules --quiet &&
> -	git diff-index --cached --quiet HEAD -- ||
> -	die "Cannot rewrite branch(es) with a dirty working directory."
> +	require_clean_work_tree 'rewrite branches'
>  fi
>  
>  tempdir=.git-rewrite
Sounds good, yes.
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

← back to recent threads