how to commit and push in libgit2sharp
Asked Answered
M

3

11

I just downloaded the nugget package for libgit2sharp. I am finding it difficult to do even basic operations.

I have an existing git repo (both remote and local). I just need to commit new changes when it occurs and push it to the remote.

I have the code below to explain what I did.

string path = @"working direcory path(local)";
Repository repo = new Repository(path);
repo.Commit("commit done for ...");

Remote remote = repo.Network.Remotes["origin"];          
var credentials = new UsernamePasswordCredentials {Username = "*******", Password = "******"};
var options = new PushOptions();
options.Credentials = credentials;
var pushRefSpec = @"refs/heads/master";                      
repo.Network.Push(remote, pushRefSpec, options, null, "push done...");

Where should I specify remote url's ? also is this the right way of doing these operations(commit & push)?

Thanks

Mcdavid answered 1/8, 2014 at 16:38 Comment(2)
You specify the url as an attribute of the remote. There is no committing going on here, what have you tried for that? What isn't working?Bharal
Well, the remote.url has only getter & no setter !?! Also I am just trying to commit the changes i made in my local working directory & later on push it to the remote repository..Mcdavid
M
14
public void StageChanges() {
    try {
        RepositoryStatus status = repo.Index.RetrieveStatus();
        List<string> filePaths = status.Modified.Select(mods => mods.FilePath).ToList();
        repo.Index.Stage(filePaths);
    }
    catch (Exception ex) {
        Console.WriteLine("Exception:RepoActions:StageChanges " + ex.Message);
    }
}

public void CommitChanges() {
    try {

        repo.Commit("updating files..", new Signature(username, email, DateTimeOffset.Now),
            new Signature(username, email, DateTimeOffset.Now));
    }
    catch (Exception e) {
        Console.WriteLine("Exception:RepoActions:CommitChanges " + e.Message);
    }
}

public void PushChanges() {
    try {
        var remote = repo.Network.Remotes["origin"];
        var options = new PushOptions();
        var credentials = new UsernamePasswordCredentials { Username = username, Password = password };
        options.Credentials = credentials;
        var pushRefSpec = @"refs/heads/master";
        repo.Network.Push(remote, pushRefSpec, options, new Signature(username, email, DateTimeOffset.Now),
            "pushed changes");
    }
    catch (Exception e) {
        Console.WriteLine("Exception:RepoActions:PushChanges " + e.Message);
    }
}
Mcdavid answered 4/8, 2014 at 16:18 Comment(2)
Yours was helpful, and thanks for that(+1). But I guess the main focus was on basic operations like commit & push. I just don't want to mislead others.Mcdavid
Breaking change in LibGit2Sharp Version 0.26.2: var status = repo.RetrieveStatus(); var filePaths = status.Modified.Select(mods => mods.FilePath).ToList(); foreach (var filePath in filePaths) { repo.Index.Add(filePath); repo.Index.Write(); }Graeco
M
4

The remote already has an url.

If you wanted to change the url associated with remote named 'origin', you would need to:

  • remove that remote:

    repo.Network.Remotes.Remove("origin");
    
    # you can check it with:
    Assert.Null(repo.Network.Remotes["origin"]);
    Assert.Empty(repo.Refs.FromGlob("refs/remotes/origin/*"));
    
  • create a new one (default refspec)

    const string name = "origin";
    const string url = "https://github.com/libgit2/libgit2sharp.git";
    repo.Network.Remotes.Add(name, url);
    
    # check it with:
    Remote remote = repo.Network.Remotes[name];
    Assert.NotNull(remote);
    

See more at LibGit2Sharp.Tests/RemoteFixture.cs


As updated in the comments by nulltoken, contributor to libgit2:

PR 803 has been merged.
This should allow some code such as

Remote updatedremote = 
   repo.Network.Remotes.Update(remote, r => r.Url = "http://yoururl"); 
Medor answered 2/8, 2014 at 4:49 Comment(2)
PR #803 has been merged. This should allow some code such as Remote updatedremote = repo.Network.Remotes.Update(remote, r => r.Url = "http://yoururl");Occasional
@Occasional that looks nicer indeed. I have included your comment in the answer for more visibility;Medor
C
0
var myGitLocalRepository = new Repository("/path/to/your/source/directory");

var signature = new Signature("CheckInUserName", "CheckInUserEMail", DateTimeOffset.UtcNow);

var commit = myGitLocalRepository.Commit("checkInComment", signature, signature);

Note that in newer versions (tested this in ver 0.29) PushOptions is created as following: (which is not covered in any other answer)

var pushOptions = new PushOptions
{
  CredentialsProvider = (url, user, cred) => CreateUsernamePasswordCredentials()
};

private Credentials CreateUsernamePasswordCredentials()
{
      return new UsernamePasswordCredentials
      {
          Username = "UserName",
          Password = "PasswordOrPersonalAccessToken",
      };
}

myGitLocalRepository.Network.Push(myGitLocalRepository.Head, pushOptions);

Also, if your repositories are hosted over LAN, or if you are getting Authentication related Exception (Too many redirects or authentication replays Result), then it means that in these cases UsernamePasswordCredentials are not accepted. In such scenarios, you need to instead set CredentialsProvider (inside PushOptions) as following:

CredentialsProvider = (url, user, cred) => new DefaultCredentials()
Cissoid answered 7/5 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.