Trying to create a custom git
function to check to see how far ahead/behind a branch is to another branch. The logic works, if I copy/paste line by line in a terminal it works.
However, putting this in my .zshrc
does not:
function gba() {
branch="$(git rev-parse --abbrev-ref HEAD)"
counts_str="$(git rev-list --left-right --count origin/dev...$branch)"
counts=(${(ps:\t:)${counts_str}})
behind=$counts[1]
ahead=$counts[2]
echo "$branch behind $behind commits, ahead $ahead commits"
}
Also, if I run in in a script, adding #!/bin/zsh
and executing with zsh gba.sh
it works.
When I have it in my .zshrc
(and make sure I open a new terminal window) it outputs all the git
remote branches.
Any ideas? I'd like to have it in my .zshrc
so I don't have a separate script file, and would like to run it with just gba
or similar like an alias.
Based on suggestions, I've run a few things to see how gba
is interpreted when defined directly in my .zshrc
:
15:33:41 ~$ which gba
gba: aliased to git branch -a
15:33:46 ~$ declare -f gba
gba () {
branch="$(git rev-parse --abbrev-ref HEAD)"
counts_str="$(git rev-list --left-right --count origin/dev...$branch)"
counts=(${(ps:\t:)${counts_str}})
behind=$counts[1]
ahead=$counts[2]
echo "$branch behind $behind commits, ahead $ahead commits"
}
15:34:04 ~$ (set -x; gba)
+-zsh:3> git branch -a
fatal: Not a git repository (or any of the parent directories): .git
Turns out I didn't realize I had the git
oh-my-zsh
plugin enabled in .zshrc
from the line:
plugins=(git)
This loads in a ton of aliases (which I was aware of, but I thought I had it disabled.) So commenting that line makes my gba
function work perfectly. The oh-my-zsh
git
aliases are nice, but I would rather learn the full commands for things as I go, then later manually add aliases if I want to.
The issue is right here:
15:33:41 ~$ which gba
gba: aliased to git branch -a
...there's an alias also named gba
, so your function isn't being invoked. Run unalias gba
to remove it.
Incidentally, one big clue was the set -x
output:
15:34:04 ~$ (set -x; gba)
+-zsh:3> git branch -a
fatal: Not a git repository (or any of the parent directories): .git
...since git branch -a
isn't present anywhere in the function, this definition had to be coming from somewhere else.