I am trying to create a function in bash script to automate the messages when doing git commit -S -am 'Message'
, depending on the file created, deleted or modified.
But I don't know how to do it and I just created a function, where it saves me a bit of work but I still have to manually add the messages of each commit.
This is my code:
function gitpush () {
if [ -d .git ]; then
git add .
if [ -z "$2" ]; then
echo "No commit message"
else
git commit -S -am "$2"
fi
if [ -z "$1" ]; then
echo "No repository selected"
else
git push $1 master
fi
else
echo false
fi
}
gitpush origin "Message"
I know that with the command git status --porcelain
you can get the list of affected files. And doing some research I created this little command.
git status --porcelain | \
sed -e 's/\sD/Remove/g;s/\sM/Update/g;s/\s??/Create/g' | \
awk -F: '{
print "git commit -S -am " $1 $2
}'
But it didn't work for me :(
Apparently you were confused:
\s
it is used to represent white space, while$
it is used to represent the beginning of a new line (at least in POSIX regular expressions, which is what itsed
uses). Correcting that, your code would look like this:What I did was replace
\s
in your code with$\s*
, which means "Start of line and any number of whitespaces".And, for example, when updating file
README.txt
, creating file,LICENSE
and deleting file.log.bak
, the output would be: