0

I'm using ruby-git to operate my Git repo. I can get the local branch that checkout from remote branch, how can I get it upstream remote branch? This's the code:

require 'Git'
repo = Git.open("xxxpath.git")
localbranch = repo.branches["localbranchnamexxx"]
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
李晨光
  • 3
  • 1

2 Answers2

1

The same way you would do it in normal git

remote_branch = repo.branches["origin/localbranchnamexxx"]
Siim Liiser
  • 3,860
  • 11
  • 13
0

Counter-intuitively (to me, at least), the branch tracking information is stored in the git config, not in any branch or ref structures.

require 'git'
repo = Git.open("xxxpath.git")
localbranch = repo.current_branch
upstream_remote = repo.config["branch.#{localbranch}.remote"]
upstream_ref = repo.config["branch.#{localbranch}.merge"]
upstream_branch = upstream_ref.split('/').last
upstream = "#{upstream_remote}/#{upstream_branch}"
mblythe
  • 101