2

I'm looking for a way to write a function calling git commit with several messages ; for example :

git commit -m "This is the commit headline" -m "This is a new paragraph for the commit" -m "Adding paragraphs makes it easier to give details" 

Etc.

The thing is, I have trouble finding an efficient way to express in the script what's the option (-m) and what's the string/message.

Here's what I have so far :

function commit(){ 
  if [[ $# > 1 ]]
  then
    commit_body=`echo $2 | cut -d '=' -f 2`
    IFS='|' read -r -a paragraphs <<< "$commit_body"
    git commit -m "$1" $(for p in "${paragraphs[@]}"; do echo -n "-m $p"; done)
  else
    git commit -m "$1"
  fi
}

The function would be called by executing :

commit "Commit headline" body="1st paragraph for the commit|2nd paragraph"

The problem is that, in my function, the "-m" argument is considered as a string by the git commit command. But if I take it out of the "echo" string, then it will become an argument of the echo command, which is not what I want.

Do you have any pointers on how to bypass this?

1
  • Is something else generating the arguments for your function? I would probably just use git commit -F-, and put my commit message in a here document. Commented May 6, 2020 at 14:09

1 Answer 1

4

Perhaps something like this would work:

#!/bin/bash

function commit() {
        options=("-m" "$1")

        if [[ $# > 1 ]]
        then
                commit_body=`echo "$2" | cut -d '=' -f 2`
                IFS='|' read -r -a paragraphs <<< "$commit_body"
                for msg in "${paragraphs[@]}"
                do
                        options+=("-m" "${msg}")
                done
        fi

        git commit "${options[@]}"
}

commit "Commit headline" body="1st paragraph for the commit|2nd paragraph"
$ bash -x commit.sh
+ commit 'Commit headline' 'body=1st paragraph for the commit|2nd paragraph'
+ options=("-m" "$1")
+ [[ 2 > 1 ]]
++ echo 'body=1st paragraph for the commit|2nd paragraph'
++ cut -d = -f 2
+ commit_body='1st paragraph for the commit|2nd paragraph'
+ IFS='|'
+ read -r -a paragraphs
+ for msg in "${paragraphs[@]}"
+ options+=("-m" "${msg}")
+ for msg in "${paragraphs[@]}"
+ options+=("-m" "${msg}")
+ git commit -m 'Commit headline' -m '1st paragraph for the commit' -m '2nd paragraph'
Sign up to request clarification or add additional context in comments.

4 Comments

One suggestion: commit_body=${2#*=}
I would have done it pretty much like this – you can avoid using cut with IFS='=' read -r _ commit_body <<< "$2". Also, you should probably declare all variables as local, or they leak into the environment.
Ah, or what @chepner said for the commit body ;)
Cut is often the wrong answer, at least if used carelessly: $ echo 'body=fred=wilma barney=betty' | cut -d = -f 2 ==> fred

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.