So I am trying to clean up a script I have that gets the list of currently staged files using git status
to return the relative file path of each staged file. I have looked at the documentation for git status
to see if there was a way to get it to return the absolute path of each staged file, but there doesn't seem to be an option for that and git ls files
nor git diff
will work for this since the specific use case is during a merge.
During the merge, using git diff
returns nothing, while git status
does show all of the staged files, so I am stuck using git status
.
From this list I have, I want to then grep
through the list of files to extract any line(s) that contain the string "Path: "
and output it. Basically, I have a list of staged .yml files and I want to extract all changes to the Path
property in those ymls. Heres what I have so far:
IFS=$'\n'
for file in `git status -s -uno | sed s/^..//`
do
relativePath=$(echo $file | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '"')
startPath=`pwd`
grep "Path: " "$startPath/$relativePath"
done
unset IFS
Explanation:
git status -s -uno | sed s/^..//
- I am piping the result of
git status
intosed
to remove any extra whitespace
relativePath=$(echo $file | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '"')
- I echo the file path and pipe it into
sed
to remove any extra spaces that weren't removed from the initialsed
call in the start of thefor
loop. I then pipe that intotr
to remove the first and last double quotes from the string since I need to combine that relative path with my starting path in order to get the complete path.
startPath=`pwd`
grep "Path: " "$startPath/$relativePath"
- Store the current working directory in a variable, then combine it with our relative path and pass that into
grep
This script works and extracts the data that I need, but I feel like there is a much cleaner way I could be doing this. Is there a way I can get git status
to return the full path of each staged file so I don't have to have my second $startPath
variable that I combine with my $relativePath
thats passed into grep
?