ironcodestudio / ironcode-git-enhancements

Enhancements for Command Line Git
14 stars 5 forks source link

Improve git current-branch #92

Open salcode opened 5 years ago

salcode commented 5 years ago

See

# Outputs the name of the current branch
# Usage example: git pull origin $(git_current_branch)
# Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
# it's not a symbolic ref, but in a Git repo.
function git_current_branch() {
  local ref
  ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
  local ret=$?
  if [[ $ret != 0 ]]; then
    [[ $ret == 128 ]] && return  # no git repo.
    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
  fi
  echo ${ref#refs/heads/}
}

source

salcode commented 5 years ago

I think we should deprecate

git FeCurrentBranchName

and change everything to use

git current-branch

salcode commented 4 years ago

As of Git 2.22 we can use

git branch --show-current

Do we want to refactor to use the new way but fallback to a pre-Git 2.22 way? e.g.

current-branch = "!f() {               \
    git                                \
        "branch --show-current"        \
        "2>/dev/null"                  \
    ||                                 \
    git "rev-parse --abbrev-ref HEAD"; \
}; f"                                  \