Whenever I do git status
, I want to reference the files that were shown easily... whether it be so I can open it in vim, git add it, delete it, git checkout (to revert), etc. So after a bit of looking around, I couldn't find any existing script for this so I made my own. It works, but the code is not pretty, and being a beginner bash scripter (if that's even a noun), I feel like I can do so much better with this.
Here is the script:
#usage gits: sets env variables $m<n> $d<n> $u<n> $a (modified, deleted, untracked, added) function gits { git status gitsall=$(git status -s) m=$(echo "$gitsall" | grep "^ M") d=$(echo "$gitsall" | grep "^ D") u=$(echo "$gitsall" | grep "^??") a=$(echo "$gitsall" | grep "^A ") s=$(echo "$gitsall" | grep "^M ") #i=0 #while [[ -n "$(echo {m,d,u}$(($i)))" ]]; do # i=$(($i+1)) # unset {m,d,u}$(($i)) # done count=1 while read -r tmpfilename; do tmpfilename=${tmpfilename:2} set m$(($count))="$(pwd)/$tmpfilename" count=$(($count+1)) done <<< "$m" count=1 while read -r tmpfilename; do tmpfilename=${tmpfilename:2} set d$(($count))="$(pwd)/$tmpfilename" count=$(($count+1)) done <<< "$d" count=1 while read -r tmpfilename; do tmpfilename=${tmpfilename:3} set u$(($count))="$(pwd)/$tmpfilename" count=$(($count+1)) done <<< "$u" count=1 while read -r tmpfilename; do tmpfilename=${tmpfilename:3} set a$(($count))="$(pwd)/$tmpfilename" count=$(($count+1)) done <<< "$a" count=1 while read -r tmpfilename; do tmpfilename=${tmpfilename:3} set s$(($count))="$(pwd)/$tmpfilename" count=$(($count+1)) done <<< "$s" unset m d u a s tmpfilename gitsall }
Basically, I call git status -s
, save the output as a variable, then grep
it to get lists of filenames of modified, deleted, untracked, and added.
I then parse each line individually and set $
to the respective file in absolute paths.
Now, much of the parsing is the same. Things that vary are env var prefix, substring location (2 or 3 so far), and file list var name. I feel like this can somehow be cleaned up nicely.
Note: I do know this is not going to cover everything. Namely, I only so far implemented it for " M" " D" "??" "A ", but I know others exist like "M ", "MM", etc. which I eventually plan to address. This script is still a WIP for me but I wanted to clean it up before I add all the change types.
I did look around google thinking someone has made something like this because I thought it'll be something people would find convenient. For those who have different workflows that work around my situation, it'll be nice if I can hear them too.
Example usage:
gits
Output of git status and set the appropriate vars
vim $m1
Opens first modified, untracked file in the list from git status