0

I'm following the conventional commits standard and I want to make a shell function to do a squash and merge and commit changes with a parsed message, improving my coding speed and my commits consistency.

My problem is to parse the arguments and then use the result in the commit message.

  • My actual code:

    function git_merge_squash() {
      git merge --squash "$1"
      shift
      msg= echo $'\n'"$*" | tr . \\n | tr - ' '
      # echo $msg
      git commit -m $msg
    }
    
    alias gmrs=git_merge_squash
    
  • Usage example:

    $ gmrs f/my_branch Features.- Signup.-- Save hashed password
    asynchronously.-- Retrieve token.- Login.-- Retrieve token.-
    Logout.-- Destroy token
    
  • Expected result:

    git merge --squash f/my_branch
    git commit -m "
    Features
      Signup
        Save hashed password asynchronously
        Retrieve token
      Login
        Retrieve token
      Logout
        Destroy token"
    

As you can guess, in my actual code my commented echo prints exactly the message that I want. But git commit -m command takes $msg as a string. I tried multiple other options, like simulating a file input with the -F flag, but I couldn't work it out.

How can I achieve my goal? Thank you in advance.

  • I think you need to add `\n` as well in the commit message if it is mult-line. – Raza Mehdi Oct 22 '17 at 21:14
  • `git commit -m "$msg"` (with quotes surrounding the msg var)? – zigarn Oct 22 '17 at 21:25
  • Those solutions didn't work, thank you anyways. – Franco Méndez Oct 22 '17 at 22:40
  • While the resource you're touting has certain grains of good practices listed, squash-merging is not one of them: is basically destroys all the history led to the final shape of the branch merged, and makes `git bisect` useless. – kostix Oct 23 '17 at 07:54
  • An idea to use squash-merges often comes from mere incapability to use Git properly—for instance from being scared by "odd-looking history" stemming from multiple true merges. This can be easily dealt with by merely learning how Git branching works; [here](https://hackernoon.com/how-the-creators-of-git-do-branches-e6fcc57270fb) is one very good explanation. – kostix Oct 23 '17 at 08:01

2 Answers2

0

Solution

I worked it out. I hope someone can get help from this.

  • Working code

    function git_merge_squash() {
      git merge --squash "$1"
      shift
      git commit -m "$(echo $'\n'"$*" | tr . \\n | tr - ' ')"
    }
    alias gmrs=git_merge_squash
    
kostix
  • 51,517
  • 14
  • 93
  • 176
0

You don't need to invent your own encoding for newlines; just use a second argument formatted the way you want.

git_merge_squash() {
  git merge --squash "$1"
  git commit -m "$2"
}

gmrs f/my_branch "\
Features
  Signup
    Save hashed password asynchronously
    Retrieve token
  Login
    Retrieve token
  Logout
    Destroy token"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • The main reason why a don't use that is because it is easier to make mistakes and also because the " isn't fast to write (I need 2 keys, at least on my Spanish keyboard) and typing ".- " is much faster than what I replaced with it. Thank you – Franco Méndez Oct 23 '17 at 01:18
  • You have one pair of double quotes, and multiple newlines. – chepner Oct 23 '17 at 11:16