0

I am creating a git hook to check new local branch name against all remote branches for matching names and then check case differences. I have not got compare to work using basename to trim the branch.

Just running echo "$(basename $branch)" in the loop:

branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/remotes/origin/)"
for branch in "${branches[@]}"; do   
    echo "$(basename $branch)"
done

works fine and returns the correct branch names stripped down from refs/remotes/origin/master to just master etc.

integration_2021R1
master
net-standard-upgrade
net5-stable
netstandard-actions
platform
stable
stable_15.2_hfr7
stable_2020R1
stable_2021R1

but in the comparison:

if ["$local_branch_name" == "$(basename $branch)"]; then
    echo "branch match"
   fi  

all I get is command not found.

I even tried to change the echo to a variable assign but that also causes command not found.

var = "$(basename $branch)"

output:

GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found
GitHooks/pre-commit: line 19: var: command not found

for all branches.

torek
  • 448,244
  • 59
  • 642
  • 775
TobyJEP
  • 1
  • 1
  • In bash, use `echo "$(basename $branch)"` (without the `=` sign), and `var x="$(basename $branch)"` with a variable name (here `x`) and without the additional spaces. Also, it is preferrable to put quotes around your variables `"$branch"` wherever you can. – pmf Feb 15 '22 at 11:53
  • is this bourne shell (`sh`) and not `bash`? I'm not sure if this matters, but using `==` for string compare may give unpredictable results across different platforms if using `[...]` and not the bash `[[...]]` (ps: and the missing space after `[` is an error, but I'm sure that's a typo) – michael Feb 15 '22 at 11:59
  • @pmf that = after echo was a typo when I copied it to stackoverflow. thanks for your suggestions. – TobyJEP Feb 15 '22 at 12:15
  • 1
    @michael haha I'm new to this and adding space after my square brackets worked. Thank you – TobyJEP Feb 15 '22 at 12:16
  • https://stackoverflow.com/search?q=%5Bbash%5D+test+command+not+found – phd Feb 15 '22 at 13:36
  • @michael, note that `sh` is usually POSIX sh these days, not Bourne. (Bourne is from the 1970s, POSIX sh is a specification from the early 1990s). – Charles Duffy Feb 15 '22 at 14:50
  • @michael, ...the easy way to tell the difference between them is to check the output of `echo hello ^ cat` -- a POSIX-compliant shell will emit `hello ^ cat` as output; a traditional Bourne shell will emit only `hello`, because it treats `^` as a pipe character. – Charles Duffy Feb 15 '22 at 14:51
  • @CharlesDuffy: Huh. The 4BSD /bin/sh Bourne shell (written in Bournegol, so definitely an original-Bourne-shell-descendant) didn't use `^` for pipe... – torek Feb 15 '22 at 22:45
  • @CharlesDuffy good point! Like Bourne, I'm also from the 70s :-) so my terminology is probably anachronistic (though I didn't know about `^`, interesting, TIL). For a long while (back 10-15 yrs ago), I was writing lots of cross-platform scripts (aix, solaris, z/OS, linux, hp-ux, macOS, cygwin) and really had to be careful; a lowest-common-denominator `bash` was the safest option (at the time). Lots of gotchas, to be sure. – michael Feb 16 '22 at 10:53
  • @torek, interesting. That's a trick I learned inspecting how late-90s/early-2000s autoconf checked for 1970-era Bourne (my first "real tech company" job was porting and packaging for MontaVista Linux, an embedded-systems distro); grepping today's autoconf codebase I don't see it still in use. – Charles Duffy Feb 16 '22 at 11:53
  • ...quoting https://www.in-ulm.de/~mascheck/bourne/: *How to identify a traditional Bourne shell? A simple check for an often undocumented but characteristic feature: You can use the circumflex ^ (caret) as replacement for | (pipe). This was inherited from the predecessor, the Thompson shell, probably for reasons of convenience on early upper-case-only terminals.* – Charles Duffy Feb 16 '22 at 11:58
  • @CharlesDuffy: re 4.3-tahoe "bugfix: "set -e" is disabled while executing an if/while/until condition": I was the one who fixed that bug. :-) Apparently others did as well, in other variants. – torek Feb 16 '22 at 23:54
  • @TobyJEP, btw, http://shellcheck.net/ will catch a lot of the family of issues seen in this question; it's well worth using (and is also a piece of software you can locally install, not just a website). – Charles Duffy Feb 17 '22 at 16:28

0 Answers0