diff --git a/git-checkout.sh b/git-checkout.sh index a2b8e4fa4a..66e40b90eb 100755 --- a/git-checkout.sh +++ b/git-checkout.sh @@ -135,11 +135,7 @@ fi # We are switching branches and checking out trees, so # we *NEED* to be at the toplevel. -cdup=$(git-rev-parse --show-cdup) -if test ! -z "$cdup" -then - cd "$cdup" -fi +cd_to_toplevel [ -z "$new" ] && new=$old && new_name="$old_name" diff --git a/git-commit.sh b/git-commit.sh index eddd863015..9fdf234b52 100755 --- a/git-commit.sh +++ b/git-commit.sh @@ -316,22 +316,16 @@ esac ################################################################ # Prepare index to have a tree to be committed -TOP=`git-rev-parse --show-cdup` -if test -z "$TOP" -then - TOP=./ -fi - case "$all,$also" in t,) save_index && ( - cd "$TOP" - GIT_INDEX_FILE="$NEXT_INDEX" - export GIT_INDEX_FILE + cd_to_toplevel && + GIT_INDEX_FILE="$NEXT_INDEX" && + export GIT_INDEX_FILE && git-diff-files --name-only -z | git-update-index --remove -z --stdin - ) + ) || exit ;; ,t) save_index && @@ -339,11 +333,11 @@ t,) git-diff-files --name-only -z -- "$@" | ( - cd "$TOP" - GIT_INDEX_FILE="$NEXT_INDEX" - export GIT_INDEX_FILE + cd_to_toplevel && + GIT_INDEX_FILE="$NEXT_INDEX" && + export GIT_INDEX_FILE && git-update-index --remove -z --stdin - ) + ) || exit ;; ,) case "$#" in diff --git a/git-fetch.sh b/git-fetch.sh index c58704d794..87b940b85b 100755 --- a/git-fetch.sh +++ b/git-fetch.sh @@ -5,12 +5,8 @@ USAGE=' ...' SUBDIRECTORY_OK=Yes . git-sh-setup set_reflog_action "fetch $*" +cd_to_toplevel ;# probably unnecessary... -TOP=$(git-rev-parse --show-cdup) -if test ! -z "$TOP" -then - cd "$TOP" -fi . git-parse-remote _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" diff --git a/git-merge.sh b/git-merge.sh index 3eef048efc..7de83dc76c 100755 --- a/git-merge.sh +++ b/git-merge.sh @@ -5,12 +5,14 @@ USAGE='[-n] [--no-commit] [--squash] [-s ] [-m=] +' +SUBDIRECTORY_OK=Yes . git-sh-setup set_reflog_action "merge $*" require_work_tree +cd_to_toplevel test -z "$(git ls-files -u)" || - die "You are in a middle of conflicted merge." + die "You are in the middle of a conflicted merge." LF=' ' diff --git a/git-pull.sh b/git-pull.sh index e9826fc4ce..959261757c 100755 --- a/git-pull.sh +++ b/git-pull.sh @@ -6,12 +6,14 @@ USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [] ...' LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.' +SUBDIRECTORY_OK=Yes . git-sh-setup set_reflog_action "pull $*" require_work_tree +cd_to_toplevel test -z "$(git ls-files -u)" || - die "You are in a middle of conflicted merge." + die "You are in the middle of a conflicted merge." strategy_args= no_summary= no_commit= squash= while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac diff --git a/git-rebase.sh b/git-rebase.sh index 98f9558145..c8bd0f99d1 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -27,9 +27,12 @@ Example: git-rebase master~1 topic / --> / D---E---F---G master D---E---F---G master ' + +SUBDIRECTORY_OK=Yes . git-sh-setup set_reflog_action rebase require_work_tree +cd_to_toplevel RESOLVEMSG=" When you have resolved this problem run \"git rebase --continue\". diff --git a/git-reset.sh b/git-reset.sh index b9045bc762..91c7e6e664 100755 --- a/git-reset.sh +++ b/git-reset.sh @@ -53,11 +53,7 @@ then exit fi -TOP=$(git-rev-parse --show-cdup) -if test ! -z "$TOP" -then - cd "$TOP" -fi +cd_to_toplevel if test "$reset_type" = "--hard" then diff --git a/git-revert.sh b/git-revert.sh index fcca3ebb90..224e6540ca 100755 --- a/git-revert.sh +++ b/git-revert.sh @@ -19,8 +19,11 @@ case "$0" in echo >&2 "What are you talking about?" exit 1 ;; esac + +SUBDIRECTORY_OK=Yes ;# we will cd up . git-sh-setup require_work_tree +cd_to_toplevel no_commit= while case "$#" in 0) break ;; esac diff --git a/git-sh-setup.sh b/git-sh-setup.sh index 57f7f77776..6b1c1423eb 100755 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -36,6 +36,17 @@ is_bare_repository () { esac } +cd_to_toplevel () { + cdup=$(git-rev-parse --show-cdup) + if test ! -z "$cdup" + then + cd "$cdup" || { + echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree" + exit 1 + } + fi +} + require_work_tree () { test $(is_bare_repository) = false || die "fatal: $0 cannot be used without a working tree."