diff --git a/bin/git-forgit b/bin/git-forgit index 41375ea..052df19 100755 --- a/bin/git-forgit +++ b/bin/git-forgit @@ -842,6 +842,7 @@ _forgit_checkout_file() { _forgit_inside_work_tree || return 1 local files opts _forgit_contains_non_flags "$@" && { _forgit_git_checkout_file "$@"; return $?; } + [[ $(_forgit_list_files --modified | wc -l) -eq 0 ]] && echo 'Nothing to checkout.' && return 1 opts=" $FORGIT_FZF_DEFAULT_OPTS -m -0 @@ -958,8 +959,9 @@ _forgit_git_checkout_tag() { # git checkout-tag selector _forgit_checkout_tag() { _forgit_inside_work_tree || return 1 - local opts + local opts tag [[ $# -ne 0 ]] && { _forgit_git_checkout_tag "$@"; return $?; } + [[ $(git tag -l | wc -l) -eq 0 ]] && echo 'Nothing to checkout: there are no tags.' && return 1 opts=" $FORGIT_FZF_DEFAULT_OPTS +s +m --tiebreak=index diff --git a/tests/empty-state.test.sh b/tests/empty-state.test.sh new file mode 100644 index 0000000..0e664ee --- /dev/null +++ b/tests/empty-state.test.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +function set_up_before_script() { + source bin/git-forgit + + # Create a temporary git repository for testing + cd "$(bashunit::temp_dir)" || return 1 + git init -q + git config user.email "test@example.com" + git config user.name "Test User" + # Create an initial commit so we have a valid repo + echo "initial" > README.md + git add README.md + git commit -q -m "Initial commit" +} + +function test_checkout_file_shows_message_when_no_modified_files() { + output=$(_forgit_checkout_file 2>&1) + assert_general_error + + assert_same "Nothing to checkout." "$output" +} + +function test_checkout_tag_shows_message_when_no_tags() { + output=$(_forgit_checkout_tag 2>&1) + assert_general_error + + assert_same "Nothing to checkout: there are no tags." "$output" +}