Testing for existence of a remote branch from a script
- From
Chris Packham <judge.packham@gmail.com>
- Date
- Jan 6, 2025, 04:40 UTC
- Message-ID
- <CAFOYHZDQs-mftqLQn5HiFgBWcFN6Z-WDscJt=zVLRyGTo36=HQ@mail.gmail.com>
Hi,
I look after some scripts we use at $dayjob for pushing changes though our review system.
For some of our repositories we operate a triangular workflow where changes are fetched from one branch (e.g. 'foo') but are pushed to a different one ('foo_incoming'). Our CI system runs to test the changes and when they pass 'foo_incoming' is merged (fast-forward most of the time) into 'foo'.
The problem I have is not all our projects use this workflow so I've tried to automate the detection of this. My script does something like
br=$(git rev-parse --symbolic-full-name refs/remotes/origin/foo_incoming -- 2>/dev/null || echo refs/remotes/origin/foo)
The '--' is necessary because if foo_incoming doesn't exist then there is extra output on stdout that puts off users. But when foo_incoming does exist then br gets set to `refs/remotes/origin/foo_incoming\n--`.
Is there a better way of checking for the existence of a remote branch?
Thanks, Chris