public static void OpenRepo() { Git repository = Git.Open(@"C:\Git\NGit"); // Fetch changes without merging them NGit.Transport.FetchResult fetch = repository.Fetch().Call(); // Pull changes (will automatically merge/commit them) PullResult pull = repository.Pull().Call(); // Get the current branch status Status status = repository.Status().Call(); // The IsClean() method is helpful to check if any changes // have been detected in the working copy. I recommend using it, // as NGit will happily make a commit with no actual file changes. bool isClean = status.IsClean(); // You can also access other collections related to the status System.Collections.Generic.ICollection<string> added = status.GetAdded(); System.Collections.Generic.ICollection<string> changed = status.GetChanged(); System.Collections.Generic.ICollection<string> removed = status.GetRemoved(); // Clean our working copy System.Collections.Generic.ICollection<string> clean = repository.Clean().Call(); // Add all files to the stage (you could also be more specific) NGit.Dircache.DirCache add = repository.Add().AddFilepattern(".").Call(); // Remove files from the stage NGit.Dircache.DirCache remove = repository.Rm().AddFilepattern(".gitignore").Call(); }
} // End Sub ListCommits public virtual void Fetch(string path) { NGit.Api.Git repository = NGit.Api.Git.Open(path); // Fetch changes without merging them NGit.Transport.FetchResult fetch = repository.Fetch().Call(); CloseRepository(repository); } // End Sub Fetch
public static void WithCreds() { Git repository = Git.Open(@"C:\Git\NGit"); NGit.Transport.UsernamePasswordCredentialsProvider credentials = new NGit.Transport.UsernamePasswordCredentialsProvider("username", "password"); // On a per-command basis NGit.Transport.FetchResult fetch = repository.Fetch() .SetCredentialsProvider(credentials) .Call(); // Or globally as the default for each new command NGit.Transport.CredentialsProvider.SetDefault(credentials); }
} // End Sub SetGlobalCredentials public virtual void FetchWithCredentials(string path, string userName, string password) { NGit.Api.Git repository = NGit.Api.Git.Open(path); NGit.Transport.CredentialsProvider credentials = new NGit.Transport.UsernamePasswordCredentialsProvider(userName, password); // On a per-command basis NGit.Transport.FetchResult fetch = repository.Fetch() .SetCredentialsProvider(credentials) .Call(); CloseRepository(repository); } // End Sub FetchWithCredentials
public static void InitRepo(string[] args) { Git myrepo = Git.Init().SetDirectory(@"/tmp/myrepo.git").SetBare(true).Call(); { NGit.Transport.FetchResult fetchResult = myrepo.Fetch() .SetProgressMonitor(new TextProgressMonitor()) .SetRemote(@"/tmp/initial") .SetRefSpecs(new NGit.Transport.RefSpec("refs/heads/master:refs/heads/master")) .Call(); // // Some other work... // myrepo.GetRepository().Close(); } System.GC.Collect(); #if false System.Console.WriteLine("Killing"); BatchingProgressMonitor.ShutdownNow(); #endif System.Console.WriteLine("Done"); }