} // 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
} // End Function Commit public virtual string Commit(string path, string message) { string commitId = null; // No empty commit if (!HasChanges(path)) { return(commitId); } NGit.Api.Git repository = NGit.Api.Git.Open(path); NGit.PersonIdent author = new NGit.PersonIdent("FooBar2000", "*****@*****.**"); // 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; commitId = hash.Name; CloseRepository(repository); return(commitId); } // End Function Commit
// https://stackoverflow.com/questions/45793800/jgit-read-the-content-of-a-file-at-a-commit-in-a-branch private string GetFileContent(string repositoryPath, string path, NGit.Revwalk.RevCommit commit) { NGit.Api.Git repository = NGit.Api.Git.Open(repositoryPath); NGit.Treewalk.TreeWalk treeWalk = NGit.Treewalk.TreeWalk.ForPath( repository.GetRepository(), path, commit.Tree); NGit.ObjectId blobId = treeWalk.GetObjectId(0); NGit.ObjectReader objectReader = repository.GetRepository().NewObjectReader(); NGit.ObjectLoader objectLoader = objectReader.Open(blobId); byte[] bytes = objectLoader.GetBytes(); return(System.Text.Encoding.UTF8.GetString(bytes)); }
} // End Sub ListBranches // https://stackoverflow.com/questions/40590039/how-to-get-the-file-list-for-a-commit-with-jgit // https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/GetRevCommitFromObjectId.java public virtual void ListFilesIncmt(string path, NGit.Revwalk.RevCommit commit) { NGit.Api.Git repository = NGit.Api.Git.Open(path); NGit.ObjectId treeId = commit.Tree.Id; NGit.Treewalk.TreeWalk treeWalk = new NGit.Treewalk.TreeWalk(repository.GetRepository()); treeWalk.Reset(treeId); while (treeWalk.Next()) { string filePath = treeWalk.PathString; System.Console.WriteLine(filePath); } NGit.Ref @ref = repository.GetRepository().GetRef("refs/heads/master"); NGit.ObjectId head = @ref.GetObjectId(); using (NGit.Revwalk.RevWalk walk = new NGit.Revwalk.RevWalk(repository.GetRepository())) { NGit.Revwalk.RevCommit headCommit = walk.ParseCommit(head); } NGit.ObjectId commitIdFromHash = repository.GetRepository().Resolve("revstr"); }