99e2c31484bba519925a65b442d0516fc6e01c94

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
Carlo Sala <carlosalag@protonmail.com>
Date

Message

feat(git): add `git_previous_branch` function

Closes #12538

Diff

 1diff --git a/lib/git.zsh b/lib/git.zsh
 2index 2ad5afe04a180f155572912dfb4736fdd2b6b2df..8237af7519daeb5c32c1587a773eaef6681e8d05 100644
 3--- a/lib/git.zsh
 4+++ b/lib/git.zsh
 5@@ -162,6 +162,18 @@ function git_current_branch() {
 6   echo ${ref#refs/heads/}
 7 }
 8 
 9+# Outputs the name of the previously checked out branch
10+# Usage example: git pull origin $(git_current_branch)
11+# rev-parse --symbolic-full-name @{-1} only prints if it is a branch
12+function git_previous_branch() {
13+  local ref
14+  ref=$(__git_prompt_git rev-parse --quiet --symbolic-full-name @{-1} 2> /dev/null)
15+  local ret=$?
16+  if [[ $ret != 0 ]] || [[ -z $ref ]]; then
17+    return  # no git repo or non-branch previous ref
18+  fi
19+  echo ${ref#refs/heads/}
20+}
21 
22 # Gets the number of commits ahead from remote
23 function git_commits_ahead() {