Skip to content

Commit 9273b56

Browse files
emkgitster
authored andcommitted
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 <[email protected]>
1 parent 2819075 commit 9273b56

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

git-filter-branch.sh

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,20 @@ rm -rf "$tempdir"
442442

443443
trap - 0
444444

445+
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
446+
test -z "$ORIG_GIT_DIR" || {
447+
GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
448+
}
449+
test -z "$ORIG_GIT_WORK_TREE" || {
450+
GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
451+
export GIT_WORK_TREE
452+
}
453+
test -z "$ORIG_GIT_INDEX_FILE" || {
454+
GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
455+
export GIT_INDEX_FILE
456+
}
457+
445458
if [ "$(is_bare_repository)" = false ]; then
446-
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
447-
test -z "$ORIG_GIT_DIR" || {
448-
GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
449-
}
450-
test -z "$ORIG_GIT_WORK_TREE" || {
451-
GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
452-
export GIT_WORK_TREE
453-
}
454-
test -z "$ORIG_GIT_INDEX_FILE" || {
455-
GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
456-
export GIT_INDEX_FILE
457-
}
458459
git read-tree -u -m HEAD
459460
fi
460461

t/t7003-filter-branch.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ test_expect_success 'result is really identical' '
3939
'
4040

4141
test_expect_success 'rewrite bare repository identically' '
42-
(git config core.bare true && cd .git && git filter-branch branch)
42+
(git config core.bare true && cd .git &&
43+
git filter-branch branch > filter-output 2>&1 &&
44+
! fgrep fatal filter-output)
4345
'
4446
git config core.bare false
4547
test_expect_success 'result is really identical' '

0 commit comments

Comments
 (0)