-2

I want to use the below on a windows 10 machine to know the nearest parent of a git branch.

But I am not able to run it, please help.

#!/usr/bin/env zsh

git show-branch -a \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| sed 's/.*\[\(.*\)\].*/\1/' \
| sed 's/[\^~].*//'

# How it works:
# 1| Display a textual history of all commits.
# 2| Ancestors of the current commit are indicated
#    by a star. Filter out everything else.
# 3| Ignore all the commits in the current branch.
# 4| The first result will be the nearest ancestor branch.
#    Ignore the other results.
# 5| Branch names are displayed [in brackets]. Ignore 
#    everything outside the brackets, and the brackets.
# 6| Sometimes the branch name will include a ~2 or ^1 to
#    indicate how many commits are between the referenced
#    commit and the branch tip. We don't care. Ignore them.

I have taken the script form https://gist.github.com/joechrysler/6073741#file-who_is_my_mummy-sh

FYI, I have very limited Linux knowledge.

I don't want to use WSL or Cygwin

I have got the grep.exe and sed.exe and other exes from Linux utils.

Even If I run the below command in the windows command prompt, I get the below error

git show-branch -a | grep '\*'

Error:

warning: ignoring origin/branchname; cannot handle more than 26 refs

Edit:

I saved the above command in a file parent.sh and When I run it in Git Bash, It gives the below error. Am I missing anything here?

enter image description here

torek
  • 448,244
  • 59
  • 642
  • 775
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • @CherryDT Could you please guide me on how to run the above command? As I am not from Linux background. How to run in Git Bash? I have added more info to the question, please refer – Vivek Nuna Oct 02 '22 at 09:54
  • @Philippe what does that mean? Can I change it to bigger number? – Vivek Nuna Oct 02 '22 at 13:48

1 Answers1

3

This has nothing to do with Linux or Windows, and not much to do with shells: you are running into a built in limit in the git show-branch command. The git show-branch command has an internal maximum of 26 "refs" (branch names plus remote-tracking names).1 You have a large number of remote-tracking names—the names that start with origin/, such as origin/63747—and it's more than git show-branch can handle.

You could increase this limit by downloading the source for Git and building a new version of git show-branch that has a bigger limit in it, but that's a pretty big job even for an experienced C programmer (because it involves changing the way the bits work in show-branch, which gets its limit based on the number of flags used in internal commit objects; see the TODO in builtin/showbranch.c).

You might just need a different (more capable) script. Consider some of the other answers to this question: How to find the nearest parent of a Git branch


1Note that this limit used to be slightly larger (29, at the time some of the scripts in the above StackOverflow question were written).

torek
  • 448,244
  • 59
  • 642
  • 775
  • I have already tried all the solutions mentioned in this answer and your answer is also telling me the limitations not the solution – Vivek Nuna Oct 02 '22 at 15:40
  • 1
    @viveknuna Disliking the truth doesn't make the answer wrong. – matt Oct 02 '22 at 15:44
  • 1
    There's one answer that, while it uses `git show-ref`, handles more than the limit. See in particular the [batch solution here](https://stackoverflow.com/a/68673744/1256452). – torek Oct 02 '22 at 15:45
  • @torek it’s not answering my question hence downvoted, nothing personal with you. – Vivek Nuna Oct 02 '22 at 16:22
  • @matt above comment is for you. And please take it in the right spirit. If the answer works I would accept it. – Vivek Nuna Oct 02 '22 at 16:31
  • @torek how to run the solution on a windows machine which is mentioned here, based on your link https://gist.github.com/rsitze/6a9d0ac280bca850e2471634a3f83204 – Vivek Nuna Oct 03 '22 at 04:40
  • You need to do some of your own homework and thinking here. It's clear that this particular adaptation has been modified to look specifically for branches whose name starts with `release/`. It also depends on the older version of Git where show-branch handled 29 branches at a time (note the MAX setting). – torek Oct 03 '22 at 18:29