diff options
-rw-r--r-- | Documentation/git-sh-setup.txt | 3 | ||||
-rw-r--r-- | git-sh-setup.sh | 16 | ||||
-rwxr-xr-x | t/t2301-require-clean-work-tree.sh | 29 |
3 files changed, 45 insertions, 3 deletions
diff --git a/Documentation/git-sh-setup.txt b/Documentation/git-sh-setup.txt index 4f67c4cde6..bccaa2488f 100644 --- a/Documentation/git-sh-setup.txt +++ b/Documentation/git-sh-setup.txt @@ -25,7 +25,8 @@ Before sourcing it, your script should set up a few variables; `USAGE` (and `LONG_USAGE`, if any) is used to define message given by `usage()` shell function. `SUBDIRECTORY_OK` can be set if the script can run from a subdirectory of the working tree -(some commands do not). +(some commands do not). `ORPHAN_OK` can be set if the script can +work on orphan branches. The scriptlet sets `GIT_DIR` and `GIT_OBJECT_DIRECTORY` shell variables, but does *not* export them to the environment. diff --git a/git-sh-setup.sh b/git-sh-setup.sh index 4691fbcb64..f45b69386e 100644 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -200,7 +200,19 @@ require_work_tree () { } require_clean_work_tree () { - git rev-parse --verify HEAD >/dev/null || exit 1 + if git rev-parse --verify HEAD >/dev/null 2>/dev/null + then + compare_to=HEAD + else + if [ -z "$ORPHAN_OK" ] + then + echo >&2 "Cannot $1: Your current branch does not have any commits yet." + exit 1 + else + # SHA1 of an empty tree + compare_to=4b825dc642cb6eb9a060e54bf8d69288fbee4904 + fi + fi git update-index -q --ignore-submodules --refresh err=0 @@ -210,7 +222,7 @@ require_clean_work_tree () { err=1 fi - if ! git diff-index --cached --quiet --ignore-submodules HEAD -- + if ! git diff-index --cached --quiet --ignore-submodules $compare_to -- then if [ $err = 0 ] then diff --git a/t/t2301-require-clean-work-tree.sh b/t/t2301-require-clean-work-tree.sh index 1bb41b59a5..6d0957981e 100755 --- a/t/t2301-require-clean-work-tree.sh +++ b/t/t2301-require-clean-work-tree.sh @@ -60,4 +60,33 @@ test_expect_success 'error on dirty index and work tree while on orphan branch' test_must_fail run_require_clean_work_tree ' +test_expect_success 'ORPHAN_OK - success on clean index and worktree while on orphan branch' ' + test_when_finished "git checkout master" && + git checkout --orphan orphan && + git reset --hard && + ( + ORPHAN_OK=Yes && + run_require_clean_work_tree + ) +' + +test_expect_success 'ORPHAN_OK - error on dirty index while on orphan branch' ' + test_when_finished "git checkout master" && + git checkout --orphan orphan && + ( + ORPHAN_OK=Yes && + test_must_fail run_require_clean_work_tree + ) +' + +test_expect_success 'ORPHAN_OK - error on dirty index and worktree while on orphan branch' ' + test_when_finished "git checkout master" && + git checkout --orphan orphan && + echo dirty >file && + ( + ORPHAN_OK=Yes && + test_must_fail run_require_clean_work_tree + ) +' + test_done |