Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git alias with optional parameter

I want to ensure that my latest commit has the current date before I push. Since I always rebase to master before a merge + push, I made this alias:

[alias]
    sync = !git commit --amend --date=today && git rebase master

Problem is, it keeps launching my text editor asking for a new commit message. Is there a way to have an optional parameter, so that I can choose to use either:

git sync 'my commit message'

or

git sync

where the latter will simply use the existing commit message, whatever it happens to be?

like image 638
ceramica Avatar asked Jul 08 '11 08:07

ceramica


2 Answers

To apply the extra parameters to anything except the end of your alias’ “command line”, you will need to put your shell commands in a script. You can do it with an external script (like jdelStrother’s answer), or you can do it with an “inline” shell script.

You can use -m to feed git commit your new message or use the -C HEAD/--reuse-message=HEAD option to have it use the existing message and author (it would also reuse the author timestamp, but you are resetting that with --date=…). Using any of these options will prevent Git from opening an editor for your commit message.

Here it is as an “inline” shell script:

git config --global alias.sync '!sh -c '\''git commit --amend --date=today ${1+-m} "${1---reuse-message=HEAD}" && git rebase master'\'' -'

The core of this small script is the pair of conditional parameter expansions:

${1+-m} "${1---reuse-message=HEAD}"

When you call it with an extra parameter (i.e. your replacement log message), these expand to two shell words: -m "<your new log message>". When you do not supply the extra parameter, they expand to just a single word: "--reuse-message=HEAD".

The trailing dash is also important; it could be any shell word, the point is that something must be there because the shell will use it to initialize its $0 parameter (which usually has a default value, so it is useless for the conditional expansion itself).


If I misunderstood and you actually want to see the editor when you do not supply the extra parameter, then use the single expansion ${1+-m "$1"} instead of the pair of expansions.

like image 176
Chris Johnsen Avatar answered Oct 31 '22 03:10

Chris Johnsen


When your aliases start getting more complex, it's probably easiest just to create a separate script for them. If you add a file 'git-sync' to your path, it will be automatically called when you do 'git sync'.

So, if you created that file with something along the lines of -

#!/bin/sh

if [ -z "$1" ]; then
  git commit --amend --date=today
else
  git commit --amend --date=today -m "$1"
fi
git rebase master

- that would probably work. It's typed off the top of my head though, so caveat lector.

like image 44
Jonathan del Strother Avatar answered Oct 31 '22 03:10

Jonathan del Strother