public static void Commit() { Git repository = Git.Open(@"C:\Git\NGit"); PersonIdent author = new PersonIdent("Lance Mcnearney", "*****@*****.**"); string message = "My commit message"; // Commit our changes after adding files to the stage RevCommit commit = repository.Commit() .SetMessage(message) .SetAuthor(author) .SetAll(true) // This automatically stages modified and deleted files .Call(); // Our new commit's hash ObjectId hash = commit.Id; // Push our changes back to the origin Sharpen.Iterable<NGit.Transport.PushResult> push = repository.Push().Call(); // Handle disposing of NGit's locks repository.GetRepository().Close(); repository.GetRepository().ObjectDatabase.Close(); repository = null; }
} // End Function GetRepoPath // https://github.com/mono/ngit/commits/master public static void GetCommitsByBranch(string branchName) { // D:\Stefan.Steiger\Documents\Visual Studio 2013\Projects string dir = GetRepoPath(); System.Console.WriteLine(dir); // dir = "https://github.com/mono/ngit.git"; // https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ListRemoteRepository.java // https://stackoverflow.com/questions/13667988/how-to-use-ls-remote-in-ngit // git.LsRemote(); Git git = Git.Open(dir); Repository repo = git.GetRepository(); ObjectId branchOid = repo.Resolve(branchName); System.Console.WriteLine("Commits of branch: '{0}' ({1})", branchName, branchOid); System.Console.WriteLine("-------------------------------------"); Sharpen.Iterable<RevCommit> commits = git.Log().Add(branchOid).Call(); int count = 0; RevCommit laterCommit = null; // Note: Apparently sorted DESCENDING by COMMIT DATE foreach (RevCommit earlierCommit in commits) { System.Console.WriteLine(earlierCommit.Name); System.Console.WriteLine(earlierCommit.GetAuthorIdent().GetName()); System.DateTime dt = UnixTimeStampToDateTime(earlierCommit.CommitTime); System.Console.WriteLine(dt); System.Console.WriteLine(earlierCommit.GetFullMessage()); if (laterCommit != null) { GetChanges(git, repo, earlierCommit, laterCommit); } // End if (laterCommit != null) // https://github.com/gitblit/gitblit/blob/master/src/main/java/com/gitblit/utils/JGitUtils.java#L718 laterCommit = earlierCommit; count++; } // Next earlierCommit System.Console.WriteLine(count); // Handle disposing of NGit's locks repo.Close(); repo.ObjectDatabase.Close(); repo = null; git = null; // https://github.com/mono/ngit/blob/master/NGit/NGit.Revwalk/RevWalkUtils.cs } // End Sub GetCommitsByBranch
} // End Function Commit public virtual void CommitAndPush(string path) { // No empty commit if (!HasChanges(path)) { return; } NGit.Api.Git repository = NGit.Api.Git.Open(path); NGit.PersonIdent author = new NGit.PersonIdent("Lance Mcnearney", "*****@*****.**"); string message = "My commit message"; // Commit our changes after adding files to the stage NGit.Revwalk.RevCommit commit = repository.Commit() .SetMessage(message) .SetAuthor(author) .SetAll(true) // This automatically stages modified and deleted files .Call(); // Our new commit's hash NGit.ObjectId hash = commit.Id; // Push our changes back to the origin Sharpen.Iterable <NGit.Transport.PushResult> push = repository.Push().Call(); CloseRepository(repository); } // End Sub CommitAndPush
public virtual void ListTags(string path) { NGit.Api.Git repository = NGit.Api.Git.Open(path); // repository.GetRepository().GetTags() System.Collections.Generic.IList <NGit.Ref> call = repository.TagList().Call(); foreach (NGit.Ref refa in call) { System.Console.WriteLine("Tag: " + refa + " " + refa.GetName() + " " + refa.GetObjectId().Name); NGit.Api.LogCommand log = repository.Log(); if (refa.GetPeeledObjectId() != null) { log.Add(refa.GetPeeledObjectId()); } else { log.Add(refa.GetObjectId()); } Sharpen.Iterable <NGit.Revwalk.RevCommit> logs = log.Call(); foreach (NGit.Revwalk.RevCommit rev in logs) { System.Console.WriteLine("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); } // Next rev } // Next refa CloseRepository(repository); } // End Sub ListTags
public virtual void ListCommits(string path) { NGit.Api.Git repository = NGit.Api.Git.Open(path); Sharpen.Iterable <NGit.Revwalk.RevCommit> la = repository.Log().All().Call(); int count = 0; foreach (NGit.Revwalk.RevCommit commit in la) { System.Console.WriteLine("LogCommit: " + commit); count++; } // Next commit CloseRepository(repository); } // End Sub ListCommits
} // End Sub ListAllBranches // https://stackoverflow.com/questions/15822544/jgit-how-to-get-all-commits-of-a-branch-without-changes-to-the-working-direct public static void WalkCommits() { string dir = GetRepoPath(); Git git = Git.Open(dir); Repository repo = git.GetRepository(); RevWalk walk = new RevWalk(repo); System.Collections.Generic.IList<Ref> branches = git.BranchList().Call(); // https://stackoverflow.com/questions/15822544/jgit-how-to-get-all-commits-of-a-branch-without-changes-to-the-working-direct foreach (Ref branch in branches) { string branchName = branch.GetName(); System.Console.WriteLine("Commits of branch: " + branchName); System.Console.WriteLine("-------------------------------------"); Sharpen.Iterable<RevCommit> commits = git.Log().All().Call(); foreach (RevCommit commit in commits) { bool foundInThisBranch = false; RevCommit targetCommit = walk.ParseCommit(repo.Resolve(commit.Name)); foreach (System.Collections.Generic.KeyValuePair<string, Ref> e in repo.GetAllRefs()) { if (e.Key.StartsWith(Constants.R_HEADS)) { if (walk.IsMergedInto(targetCommit, walk.ParseCommit(e.Value.GetObjectId()))) { string foundInBranch = e.Value.GetName(); if (branchName.Equals(foundInBranch)) { foundInThisBranch = true; break; } // End if (branchName.Equals(foundInBranch)) } // End if (walk.IsMergedInto(targetCommit, walk.ParseCommit(e.Value.GetObjectId()))) } // End if (e.Key.StartsWith(Constants.R_HEADS)) } // Next e if (foundInThisBranch) { System.Console.WriteLine(commit.Name); System.Console.WriteLine(commit.GetAuthorIdent().GetName()); // System.DateTime dt = new System.DateTime(commit.CommitTime); System.DateTime dt = UnixTimeStampToDateTime(commit.CommitTime); System.Console.WriteLine(dt); System.Console.WriteLine(commit.GetFullMessage()); } // End if (foundInThisBranch) } // Next commit } // Next branch // Handle disposing of NGit's locks repo.Close(); repo.ObjectDatabase.Close(); repo = null; git = null; } // End Sub