How to commit and click on libgit2sharp

I just downloaded the nugget package for libgit2sharp. It’s hard for me to perform even basic operations.

I have an existing git repository (both remote and local). I just need to make new changes when this happens, and click on it.

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 provide the remote URL? Is this also the right way to perform these operations (hold and push)?

thanks

+6
source share
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); } } 
+9
source

The remote device has a URL.

If you want to change the URL associated with the remote name ' origin ', you need to:

  • remove this 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 new (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); 

More details at LibGit2Sharp.Tests/RemoteFixture.cs


As updated in the nulltoken comments, libgit2 contributor :

PR 803 merged.
This should allow some code, for example

 Remote updatedremote = repo.Network.Remotes.Update(remote, r => r.Url = "http://yoururl"); 
+3
source

Source: https://habr.com/ru/post/973234/


All Articles