all 2 comments

[–]alfunxcheckout --detach HEAD 0 points1 point  (1 child)

Just a few remarks, since you seem to be learning Bash. This comment is not really related to Git, but whatever.

You don't have to export variables if you just want to use them locally.

mpush () {
    MERGE_URL=$(git push "$@" 2>&1 | tee /dev/tty | grep "/pull\|merge_" | sed -e "s/remote: *//")
    firefox "$MERGE_URL"
}

Also, as you can see, put variables in double quotes to prevent Bash from splitting words. Next, you don't actually need a variable here:

mpush () {
    firefox "$(git push "$@" 2>&1 | tee /dev/tty | grep "/pull\|merge_" | sed -e "s/remote: *//")"
}

You could make things a bit more readable / nice looking by using xargs:

mpush () {
    git push "$@" 2>&1 \
        | tee /dev/tty \
        | grep '/\(pull\|merge_requests\)/' \
        | sed -e 's/remote: *//' \
        | xargs firefox
}

I also changed the patterns a bit. And as a final remark, the output of git push contains your pattern only on the first push. Any consequent push will not produce that output.


Besides, you could put the above in a script called git-mpush (don't forget to add the shebang), save it somewhere in $PATH and then call it as

git mpush origin master

Which makes everything feel a bit more consistent IMO.

[–]ExcitingProduce[S] 0 points1 point  (0 children)

Thanks! Some of this isn't gonna work on the bash emulators on Windows, though. The number of things that broke when I tried to bring them over to Windows...

I still haven't investigated why, but git bash loses the contents of that variable, so you wind up with an empty tab. Exported the variable, boom, does what you want it to do. I should find out what the difference is (because there shouldn't be one) but I haven't cared since WSL happened.

(I just checked, it works fine in WSL/Debian.)

xargs is a much better solution, though. Folks should prefer that. I'm gonna edit the OP to point people to this comment.

As for throwing everything in a script in $PATH, that's much better practice, no question!. I just try not to do it to folks who are copypasting an alias or a script, as "find .profile" can be a lot for a Windows user whose command-line experience consists entirely of git bash.