Skip to content

Instantly share code, notes, and snippets.

@hyliang96
Last active March 16, 2020 09:22
Show Gist options
  • Save hyliang96/4a98c4b729b7026fc87e5965a9117913 to your computer and use it in GitHub Desktop.
Save hyliang96/4a98c4b729b7026fc87e5965a9117913 to your computer and use it in GitHub Desktop.
list files or dirs traced or ignored by git, just like `ls` does, auto-colored
# list files or dirs traced or ignored by git
# gitls [<path> [<path> ...]] [--ignore] [<other-args-of-ls>]
# --ignore : ls files ignored by git, and dirs contain such files
# without --ignore : ls files traced by git, and dirs contain such files
# without <path> : ls files or dirs under current path
# auto colored
gitls()
{
local git_args=''
local ls_args=''
local dir_paths
declare -a dir_paths
while test $# -gt 0
do
case "$1" in
--ignore) git_args='-o -i --exclude-standard' ;;
-*) ls_args="$ls_args $1" ;;
--*) ls_args="$ls_args $1" ;;
*) dir_paths=("${dir_paths[@]}" "$1");;
esac
shift
done
local gls_command="git ls-files $git_args | awk -F / '{print \$1}' | uniq | sed 's/\ /\\\ /g' | xargs ls -dh --color=auto $ls_args"
if [ ${#dir_paths} -eq 0 ]; then
eval "$gls_command"
else
local here_path="$(pwd | sed 's/\ /\\\ /g')"
for dir_path in "${dir_paths[@]}"; do
echo
echo -E "$dir_path:" | sed 's/\ /\\\ /g'
cd $dir_path
eval "$gls_command"
cd $here_path
done
fi
}
alias gl='gitls' # list files or dirs traced by git, only show name
alias gli='gitls --ignored' # list files or dirs ingnored by git, only show name
alias gll='gitls -l' # list files or dirs traced by git, only full information, one file/dir per line
alias glli='gitls -l --ignored' # list files or dirs ignored by git, only full information, one file/dir per line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment