Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

get upstream branch? #600

Closed
ankurmittal opened this issue Sep 19, 2017 · 4 comments
Closed

get upstream branch? #600

ankurmittal opened this issue Sep 19, 2017 · 4 comments
Labels

Comments

@ankurmittal
Copy link

Support feature to get upstream branch

@erizocosmico
Copy link
Contributor

Same as #601, just changing the refspecs for yours

@ankurmittal
Copy link
Author

don't want to fetch, i want to get upstream tracking branch name and hash

@orirawlings
Copy link
Contributor

Hey @ankurmittal,

Right now there isn't any special support in the library for reading or writing the tracking, a.k.a. upstream, configuration of branches.

The library does have support for parsing the configuration file of the git repository, though and we can use this to retrieve the information you're looking for:

package main

import (
	"fmt"
	"os"

	"gopkg.in/src-d/go-git.v4"
	. "gopkg.in/src-d/go-git.v4/_examples"
	"gopkg.in/src-d/go-git.v4/plumbing/format/config"
)

// Print the tracking branch configuration for the repository
func main() {
	CheckArgs("<path>")
	path := os.Args[1]

	r, err := git.PlainOpen(path)
	CheckIfError(err)

	c, err := r.Config()
	CheckIfError(err)

	// There is no explicit library support for branch config sections so we will work with the raw parsed config file
	rc := c.Raw

	// Relevant config is in the "branch" section
	bs, err := section(rc, "branch")
	CheckIfError(err)

	// Each branch has its configuration in a separate subsection
	for _, b := range bs.Subsections {
		// Configuration is split into two separate options:
		//   the remote containing the tracked branch, "remote"
		//   the reference name of the tracked branch in the remote, "merge"
		// If the branch is tracking a local branch, "remote" will be "."
		fmt.Printf("%s\tremote=%q\tmerge=%q\n", b.Name, b.Option("remote"), b.Option("merge"))
	}
}

// section returns the section with name in the given config
func section(rc *config.Config, name string) (*config.Section, error) {
	for _, s := range rc.Sections {
		if s.Name == name {
			return s, nil
		}
	}
	return nil, fmt.Errorf("section %q not found", name)
}

Pull requests introducing better porcelain support for reading and writing this information is certainly welcome.

@ankurmittal
Copy link
Author

thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants