Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to commit and push in libgit2sharp

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

like image 203
user3900196 Avatar asked Aug 01 '14 16:08

user3900196


People also ask

How do I push commit to repository?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.

How to commit and push in clion?

When you're ready, click Commit or Commit and Push ( Ctrl+Alt+K ) to push the changes to the remote repository immediately after the commit. You will be able to review the current commit as well as all other commits before they are pushed to the remote.

What is LibGit2Sharp?

NET code then LibGit2Sharp is your friend. It's the open source library used by the Visual Studio Tools for Git and gives you a very idiomatic way to talk to Git from C# or VB.NET.


2 Answers

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);
    }
}
like image 55
user3900196 Avatar answered Sep 21 '22 19:09

user3900196


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"); 
like image 39
VonC Avatar answered Sep 18 '22 19:09

VonC