Skip to content

Commit

Permalink
Add recursive ls option
Browse files Browse the repository at this point in the history
  • Loading branch information
GrandLarseny committed Aug 3, 2017
1 parent 69a7bdc commit 8bf759c
Showing 1 changed file with 93 additions and 75 deletions.
168 changes: 93 additions & 75 deletions git-ls
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ usage(){
dotfiles=0
colors=1
formatting=1
recursive=0

while :
do
Expand All @@ -45,6 +46,10 @@ do
formatting=0
colors=0
;;
-r)
recursive=1
shift
;;

--)
shift
Expand Down Expand Up @@ -97,79 +102,92 @@ then
exit 0
fi

comitted="$(git ls-tree --name-only HEAD)"

for file in *
do
if [ "$file" = ".git" ]
then
continue
fi

if [ "$formatting" -eq 0 ]
then
echo -ne "$file\t"
else
printf "%-${file_length}s\t" "$file"
fi

git diff-index --quiet --cached HEAD "$file"
staged=$?
git diff-files --quiet "$file"
modified=$?

if [ -d "$file" ]
then
untracked="$file/"
else
untracked="$file"
fi

! git ls-files --other --error-unmatch --exclude-standard --directory "$untracked" 2>/dev/null | grep -q .
untracked=$?
ls-dir () {
cd "$1"

local comitted="$(git ls-tree --name-only HEAD)"

for file in *
do
if [ "$file" = ".git" ]
then
continue
fi

echo -ne "$2/"
if [ "$formatting" -eq 0 ]
then
echo -ne "$file\t"
else
printf "%-${file_length}s\t" "$file"
fi

git diff-index --quiet --cached HEAD "$file"
staged=$?
git diff-files --quiet "$file"
modified=$?

if [ -d "$file" ]
then
untracked="$file/"
else
untracked="$file"
fi

! git ls-files --error-unmatch --exclude-standard --directory "$untracked" 2>/dev/null | grep -q .
untracked=$?

if echo "$comitted" | grep -Fx "$file"
then
if [ $staged -eq 1 ]
then
status="$cStaged"
elif [ $modified -eq 1 -o $untracked -eq 1 ]
then
status="$cUntracked"
else
status=""
fi

log="$(git log -1 --pretty="format:%cr"$'\t'"%H"$'\t'"%s" -- "$file")"

if [ "$formatting" -eq 0 ]
then
echo "$log"
else
awk -v gitlog="$log" -v status="$status" 'BEGIN {
tab = index(gitlog, "\t");
time = substr(gitlog, 0, tab - 1);
hash = substr(gitlog, tab + 1, 40);
message = substr(gitlog, tab + 42);
printf "%s%-8.15s'"$cReset"'\t'"$cHash"'%.7s'"$cReset"'\t%s\n", status, time, hash, message
}'
fi
else
if [ $staged -eq 1 ]
then
echo -e "${cStaged}Staged$cReset"
elif git check-ignore "$file" > /dev/null
then
echo -e "${cIgnored}Gitignored$cReset"
elif [ $untracked -eq 1 ]
then
echo -e "${cUntracked}Untracked$cReset"
elif [ -z "$(find "$file" -maxdepth 0 -type d -empty 2>/dev/null)" ]
then
echo -e "${cIgnored}Gitignored$cReset"
else
echo "Empty"
fi
fi

if [ "$recursive" -eq 1 ] && [ -d "$file" ]
then
ls-dir "$file" "$2/$file"
cd ../
fi
done
}

if echo "$comitted" | grep -Fxq "$file"
then
if [ $staged -eq 1 ]
then
status="$cStaged"
elif [ $modified -eq 1 -o $untracked -eq 1 ]
then
status="$cUntracked"
else
status=""
fi

log="$(git log -1 --pretty="format:%cr"$'\t'"%H"$'\t'"%s" -- "$file")"

if [ "$formatting" -eq 0 ]
then
echo "$log"
else
awk -v gitlog="$log" -v status="$status" 'BEGIN {
tab = index(gitlog, "\t");
time = substr(gitlog, 0, tab - 1);
hash = substr(gitlog, tab + 1, 40);
message = substr(gitlog, tab + 42);
printf "%s%-8.15s'"$cReset"'\t'"$cHash"'%.7s'"$cReset"'\t%s\n", status, time, hash, message
}'
fi
else
if [ $staged -eq 1 ]
then
echo -e "${cStaged}Staged$cReset"
elif git check-ignore "$file" > /dev/null
then
echo -e "${cIgnored}Gitignored$cReset"
elif [ $untracked -eq 1 ]
then
echo -e "${cUntracked}Untracked$cReset"
elif [ -z "$(find "$file" -maxdepth 0 -type d -empty 2>/dev/null)" ]
then
echo -e "${cIgnored}Gitignored$cReset"
else
echo "Empty"
fi
fi
done
ls-dir . "$1"

0 comments on commit 8bf759c

Please sign in to comment.