From 9273b56278e64dd47b1a96a705ddf46aeaf6afe3 Mon Sep 17 00:00:00 2001 From: Eric Kidd Date: Tue, 3 Feb 2009 13:27:03 -0500 Subject: filter-branch: Fix fatal error on bare repositories When git filter-branch is run on a bare repository, it prints out a fatal error message: $ git filter-branch branch Rewrite 476c4839280c219c2317376b661d9d95c1727fc3 (9/9) WARNING: Ref 'refs/heads/branch' is unchanged fatal: This operation must be run in a work tree Note that this fatal error message doesn't prevent git filter-branch from exiting successfully. (Why doesn't git filter-branch actually exit with an error when a shell command fails? I'm not sure why it was designed this way.) This error message is caused by the following section of code at the end of git-filter-branch.sh: if [ "$(is_bare_repository)" = false ]; then unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE test -z "$ORIG_GIT_DIR" || { GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR } ... elided ... git read-tree -u -m HEAD fi The problem is the call to $(is_bare_repository), which is made before GIT_DIR and GIT_WORK_TREE are restored. This call always returns "false", even when we're running in a bare repository. But this means that we will attempt to call 'git read-tree' even in a bare repository, which will fail and print an error. This patch modifies git-filter-branch.sh to restore the original environment variables before trying to call is_bare_repository. Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 25 +++++++++++++------------ t/t7003-filter-branch.sh | 4 +++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/git-filter-branch.sh b/git-filter-branch.sh index c106f45af7..56d1bd0f99 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -442,19 +442,20 @@ rm -rf "$tempdir" trap - 0 +unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE +test -z "$ORIG_GIT_DIR" || { + GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR +} +test -z "$ORIG_GIT_WORK_TREE" || { + GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && + export GIT_WORK_TREE +} +test -z "$ORIG_GIT_INDEX_FILE" || { + GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && + export GIT_INDEX_FILE +} + if [ "$(is_bare_repository)" = false ]; then - unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE - test -z "$ORIG_GIT_DIR" || { - GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR - } - test -z "$ORIG_GIT_WORK_TREE" || { - GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && - export GIT_WORK_TREE - } - test -z "$ORIG_GIT_INDEX_FILE" || { - GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && - export GIT_INDEX_FILE - } git read-tree -u -m HEAD fi diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh index b0a9d7d536..6a9936e5c4 100755 --- a/t/t7003-filter-branch.sh +++ b/t/t7003-filter-branch.sh @@ -39,7 +39,9 @@ test_expect_success 'result is really identical' ' ' test_expect_success 'rewrite bare repository identically' ' - (git config core.bare true && cd .git && git filter-branch branch) + (git config core.bare true && cd .git && + git filter-branch branch > filter-output 2>&1 && + ! fgrep fatal filter-output) ' git config core.bare false test_expect_success 'result is really identical' ' -- cgit v1.2.3 From 26be15f09db15d2b53a13d0f184d77fb54367f33 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 5 Feb 2009 19:19:33 +0100 Subject: filter-branch: do not consider diverging submodules a 'dirty worktree' At the end of filter-branch in a non-bare repository, the work tree is updated with "read-tree -m -u HEAD", to carry the change forward in case the current branch was rewritten. In order to avoid losing any local change during this step, filter-branch refuses to work when there are local changes in the work tree. This "read-tree -m -u HEAD" operation does not affect what commit is checked out in a submodule (iow, it does not touch .git/HEAD in a submodule checkout), and checking if there is any local change to the submodule is not useful. Staged submodules _are_ considered to be 'dirty', however, as the "read-tree -m -u HEAD" could result in loss of staged information otherwise. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-filter-branch.sh b/git-filter-branch.sh index 56d1bd0f99..0897b5971a 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -98,7 +98,7 @@ OPTIONS_SPEC= . git-sh-setup if [ "$(is_bare_repository)" = false ]; then - git diff-files --quiet && + git diff-files --ignore-submodules --quiet && git diff-index --cached --quiet HEAD -- || die "Cannot rewrite branch(es) with a dirty working directory." fi -- cgit v1.2.3