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

How to identify branches and get their files #199

Closed
skyflyer opened this issue Jan 3, 2017 · 4 comments
Closed

How to identify branches and get their files #199

skyflyer opened this issue Jan 3, 2017 · 4 comments
Labels

Comments

@skyflyer
Copy link

skyflyer commented Jan 3, 2017

I'm having troubles identifying branches. Is there a specific way I can identify remote branches and "check them out" locally?

I.e., I would like to know which remote refs are branches and how to check them out locally, so I can get their files.

While iterating through Repository.References, I see the following refs:

2017/01/03 16:00:54 Ref:8a7363b0912adafb89cb947a9ea5a95780bb4ec7 refs/heads/master
2017/01/03 16:00:54 Ref:e5cd2c000f1ab71d999af0eb993f0f9d191a53d8 refs/remotes/origin/develop
2017/01/03 16:00:54 Ref:8a7363b0912adafb89cb947a9ea5a95780bb4ec7 refs/remotes/origin/master
2017/01/03 16:00:54 Ref:e9a1a207b2d7cc0de898750df2df92f703f38aef refs/tags/testtag
2017/01/03 16:00:54 Ref:ref: refs/heads/master HEAD

Only refs/heads/master has the IsBranch() "flag" set to true. Others are false. I doubt that the only way to identify branches is to search for string prefix refs/remotes/origin (where origin should not be special at all, and is just the default remote name, right)? (options.go, line 13)

Perhaps we could write an example? (I can write a few examples once this is clarified)

@skyflyer skyflyer changed the title How to branches How to identify branches and get their files Jan 3, 2017
@skyflyer
Copy link
Author

skyflyer commented Jan 3, 2017

So far, I've figured to:

  • Get a reference to a branch: ref, err := r.Reference("refs/remotes/origin/BRANCH_NAME_HERE", true)
  • Then, to get a commit: commit, err := r.Commit(ref)
    *retrieve and do something with files: commit.Files() and files.ForEach

Is this a "correct" way of doing things? It seems pretty awkward to hardcode "refs/remotes/origin/" in there to find and define branches?

The git ls-remote command returns refs/heads/master and refs/heads/develop for branches, for instance.

I'm not sure whether this is something arbitrary that each client chooses or if it is something that is universal across git? (I see the same difference when comparing git vs go-git on custom gitolite or Bitbucket, for instance)

As far as I see right now, the way to identify branches would be to list references and then find all of the ones that have the prefix refs/remotes/origin (or even replace origin with the name of the (first?) remote)?

@smola
Copy link
Collaborator

smola commented Jan 9, 2017

@skyflyer Thanks for taking the time to open the issue.

The recommended way to do it is the one you're using. go-git behaves the standard way in this case.

refs/remotes/origin (or refs/remotes/<remote name>) is the default way to store remote references locally. So if you want to list the references that were fetched from a given remote, that's the way to go (both with git and go-git).

git ls-remote is quite different. It does not operate on the local repository, but actually asks the server to get the avaiable remote branches. So it requires network operations. If you get a reference name from git ls-remote, such as refs/heads/develop and you want to operate with it in your local repository, you'll need to translate the remote name (refs/heads/develop) to the local name. Assuming you were using default configuration, the local name will be refs/remotes/origin/develop, but it can be changed with the fetch refspec (per-remote configuration).

There is no high-level API for git ls-remote in go-git, but you can achieve the same behavior with the low-level API for the git protocol: https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/transport/client

ep, err := transport.NewEndpoint(url)
c, err := client.NewClient(ep)
s, err := c.NewUploadPackSession(ep)
ar, err := r.AdvertisedReferences()
refs := r.References()
err := s.Close()

@smola smola added the question label Jan 9, 2017
@smola
Copy link
Collaborator

smola commented Jan 9, 2017

Closing this issue, since it's standard git behavior.

@smola smola closed this as completed Jan 9, 2017
@skyflyer
Copy link
Author

skyflyer commented Jan 9, 2017

@smola, thank you for your explanation and a client sample code to list remote refs. It is good to know I'm on the right track.

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

2 participants