1

Maybe asked before, but I cannot find the clear answer. When I need to list the branches of a repo with gitlib2sharp, do I really first need to clone to a local repo? What is the sense in that? I just want to clone a specific branch, like you do with git clone https://bla/repo.git -b branch1

Now I first need to do a local checkout, then get the branches and from there do a second round.

Am I missing something here (hope I do).

TIA for your answer!

Grtz, Ronald

Update (to long for comment): OK, so here's the use case. A company delivers ssrs reports, which we need to deploy through TAP. So my thoughts were to do this via Git. For each change let them create a branche, upload/alter/etc in this branche. And iterate changes in this branch till all is fine. In the iterations they should be able to (re)deploy themselves on at least T. At the final stage we merge the brnanch to master and (re)deploy master in P. In Github you can completely 'click' this flow, but of course I want to automate this and have preferably someelse push the buttons, so they don't need me for this. So what's the best programmatic choice here? When they make a branch and start deploying in T, should I create (clone) the repo, point my local repo to the specific branch, get the files (.sql and .rdl files) and execute/upload these? I was not aware that when you clone a repo, you clone all the branches with it. Thanks so far already!

Ronald
  • 1,083
  • 3
  • 13
  • 20
  • FYI: `git clone https://bla/repo.git -b branch1` fetches all branches anyway, if you truly wanted to only fetch one branch without the `comm-ish` that are not within that branches `tree-ish`, you would need to initialize an empty repo, add a remote tracking branch with an origin of the original repo and then do a checkout on that branch... – SushiHangover May 30 '16 at 14:30
  • @SushiHangover Can't you just do: `git clone https://bla/repo.git -b branch1 --single-branch`. I haven't really had reason to use that, but looks in documentation like `--single-branch` should make it only fetch commits related to the desired branch. – Alderath May 30 '16 at 14:57
  • @Alderath you are correct, using `--single--branch` works (depending upon git version, along with shallow cloning (`--depth 1`) and even more recently cloning a single commit if enabled on the server (`upload-pack` set to `true`)... just depends upon git versions (client and server) ;-) – SushiHangover May 30 '16 at 15:04
  • FYI: libgit2 (and thus libgit2sharp) does not currently support shallow cloning. https://github.com/libgit2/libgit2/issues/3058 and https://github.com/libgit2/libgit2sharp/issues/229 – SushiHangover May 30 '16 at 15:16

2 Answers2

2

If you want to do something like git clone https://bla/repo.git -b branch1 with libgtk2sharp try this one:

var exampleRepositoryUrl = "https://github.com/npgsql/npgsql.git";
var exampleDestinationFolder = "branch-hotfix-3.0.8";
var exampleBranchName = "hotfix/3.0.8";

var repositoryClonedPath = Repository.Clone(exampleRepositoryUrl, 
                                            exampleDestinationFolder,
                                            new CloneOptions()
{
    BranchName = exampleBranchName 
});

using (var clonedRepo = new Repository(repositoryClonedPath))
{
    // ...
}

To list names of remote branches without cloning a repo you can use something like this:

var branches = Repository.ListRemoteReferences(exampleRepositoryUrl)
                         .Where(elem => elem.IsLocalBranch)
                         .Select(elem => elem.CanonicalName
                                             .Replace("refs/heads/", ""));
PiKos
  • 1,344
  • 1
  • 16
  • 15
  • Thanks! I also got here, but I need to get the FriendlyName of the branches. You can access the branch collection via the repo object, but then you need the repo object first, which is a checked out local disk repo. And that was what i wanted to avoid for retrieving the branch names. But maybe I am using Git in the wrong way? – Ronald May 30 '16 at 21:07
  • I've updated my answer with an example of listing names of remote branches without cloning a repo. Maybe it will be helpful. – PiKos May 30 '16 at 21:50
  • Thanks! that was what I was looking for! – Ronald Jun 02 '16 at 11:46
0

PiKos answer worked for me, but I had to set a credentials handler, e.g.:

var branches = Repository.ListRemoteReferences(exampleRepositoryUrl, MyCredentialsHandler)
                         .Where(elem => elem.IsLocalBranch)
                         .Select(elem => elem.CanonicalName
                                             .Replace("refs/heads/", ""));

For more details on how to set a custom credentials handler, see this answer: https://stackoverflow.com/a/55371988/5507590

Hugo Leote
  • 41
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33306408) – tarleb Dec 04 '22 at 11:19
  • I can't add further details to this comment. I'm just complementing pikos answer by saying that on git servers that require *authentication* you need to set a credentials helper. – Hugo Leote Dec 05 '22 at 13:50