list files with git status
Background
I am well aware of how git status
works, and even about git ls-files
. Usually git status
is all I need and want, it perfectly answers the question: "What is my status, and what files need my attention?"
However, I have been unable to find a quick command that answers the following question: "What files do I have, and what is their respective status?" So, I need a full listing of the directory (like ls -la
) with a column that shows the status of each file/directory.
What I have tried
git status -s --ignored
comes quite close to the output format that I want, but it just won't list the files that are unchanged between HEAD
, index, and working directory. Also, it will recurse into directories.
git ls-files
seems to be able to provide all the required info in scriptable form, but I've been unable to stop it from recursive listing the contents of all directories.
Obviously, I could hack something together that takes the output of these two commands and provides the view I would like to have. However, I would hate to reinvent the wheel if there is already some usable command out there.
Question
Is there some way of listing all files in a directory with their respective git status?
I want a full listing showing exactly the same files that ls
would show.
Notes
This other question does not answer mine, because I definitely want an ls
equivalent. Including unmodified, ignored, and untracked files, but excluding directory contents.
Solution 1:
To restrict the paths Git inspects to just the current directory, use its Unix glob pathspecs. Since git status
does a lot of checking against the index and against HEAD, use that, and to fill in the rest of the files ls
would show you, use ls
, just munge its output to have the same format as git status
's output and take only the ones git status
didn't already list.
( git status -s -- ':(glob)*'; ls -A --file-type | awk '{print " "$0}' ) \
| sort -t$'\n' -usk1.4
:(glob)
tells Git the rest of the pathspec's a Unix glob, i.e. that *
should match only one level, just like a (dotglob-enabled) shell wildcard¹.
The -t$'\n'
tells sort
that the field separator is a newline, i.e. it's all one big field, and -usk1.4
says u
niquify, only take the first of a run, s
table, preserve input order where it doesn't violate sort key order (which is a little slower so you have to ask for that specifically), k1.4
says the key starts at the first field, the fourth character in that field, with no end given so from there to the end.
¹ Why they decided to make pathspecs match neither like shell specs nor like gitignore specs by default, I might never bother learning, since I so much prefer ignorantly disapproving of their annoying choice.
Solution 2:
Because output of git status -s
is enough, let's just make a bash routine around this function! Then for unchanged files we could to echo the proper signaling manually. Following the specification we might use two symbols of space ' ' for this purpose either some another symbol. E.g. for directories, which are not tracked by Git anyway, selected symbol '_' as status code:
for FILE in *
do
if [[ -f $FILE ]]
then
if ! [[ $(git status -s $FILE) ]]
then
# first two simbols below is a two-letter status code
echo " $FILE"
else
git status -s "$FILE"
fi
fi
if [[ -d $FILE ]]
then
# first two symbols just selected as status code for directories
echo "__ $FILE"
fi
done
The script works in the same manner as ls
. It can be written in one line using ;
as well.