diff --git a/bin/git-forgit b/bin/git-forgit index 3ae4e51..20b4883 100755 --- a/bin/git-forgit +++ b/bin/git-forgit @@ -608,13 +608,8 @@ _forgit_clean_preview() { fi } -# git clean selector -_forgit_clean() { - _forgit_inside_work_tree || return 1 - _forgit_contains_non_flags "$@" && { git clean -q "$@"; return $?; } - local files opts - _forgit_clean_git_opts=() - _forgit_parse_array _forgit_clean_git_opts "$FORGIT_CLEAN_GIT_OPTS" +_forgit_clean_select_files() { + local opts opts=" $FORGIT_FZF_DEFAULT_OPTS --preview=\"$FORGIT clean_preview {}\" @@ -622,7 +617,17 @@ _forgit_clean() { $FORGIT_CLEAN_FZF_OPTS " # Note: Postfix '/' in directory path should be removed. Otherwise the directory itself will not be removed. - files=$(git -c core.quotePath=false clean -xdffn "$@"| sed 's/^Would remove //' | FZF_DEFAULT_OPTS="$opts" fzf |sed 's#/$##') + _forgit_list_files --others "$@" | FZF_DEFAULT_OPTS="$opts" fzf | sed 's#/$##' +} + +# git clean selector +_forgit_clean() { + _forgit_inside_work_tree || return 1 + _forgit_contains_non_flags "$@" && { git clean -q "$@"; return $?; } + local files + _forgit_clean_git_opts=() + _forgit_parse_array _forgit_clean_git_opts "$FORGIT_CLEAN_GIT_OPTS" + files=$(_forgit_clean_select_files "$@") [[ -n "$files" ]] && echo "$files" | tr '\n' '\0' | xargs -0 -I% git clean "${_forgit_clean_git_opts[@]}" -xdff '%' && git status --short && return echo 'Nothing to clean.' } diff --git a/tests/clean-select-files.test.sh b/tests/clean-select-files.test.sh new file mode 100644 index 0000000..174bedd --- /dev/null +++ b/tests/clean-select-files.test.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +function set_up_before_script() { + source bin/git-forgit + + # create a new git repository in a temp directory + cd "$(temp_dir)" || return 1 + git init --quiet + + # create files to test against + touch file.txt + touch file_with\\backslashes\\.txt + touch "file with spaces.txt" + touch "file_with\ttab.txt" +} + +# @data_provider provider_clean_select_files +function test_forgit_clean_select_files_preview() { + mock "fzf" "sed -n ${1}p" + + local file + file=$(_forgit_clean_select_files) + + assert_file_exists "$file" +} + + function provider_clean_select_files() { + data_set 1 + data_set 2 + data_set 3 + data_set 4 + }