private static IEnumerable <DiffEntry> GetDiffEntries(this RevCommit commit, Repository repository) { var tw = new TreeWalk(repository) { Recursive = true }; if (commit.ParentCount > 0) { if (commit.Parents.First().Tree != null) { tw.AddTree(commit.Parents.First().Tree); } else { tw.AddTree(new EmptyTreeIterator()); } } else { tw.AddTree(new EmptyTreeIterator()); } tw.Filter = TreeFilter.ANY_DIFF; tw.AddTree(commit.Tree); return(DiffEntry.Scan(tw)); }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> private void UpdateFollowFilter(ObjectId[] trees) { TreeWalk tw = pathFilter; FollowFilter oldFilter = (FollowFilter)tw.Filter; tw.Filter = TreeFilter.ANY_DIFF; tw.Reset(trees); IList <DiffEntry> files = DiffEntry.Scan(tw); RenameDetector rd = new RenameDetector(repository); rd.AddAll(files); files = rd.Compute(); TreeFilter newFilter = oldFilter; foreach (DiffEntry ent in files) { if (IsRename(ent) && ent.GetNewPath().Equals(oldFilter.GetPath())) { newFilter = FollowFilter.Create(ent.GetOldPath()); RenameCallback callback = oldFilter.GetRenameCallback(); if (callback != null) { callback.Renamed(ent); // forward the callback to the new follow filter ((FollowFilter)newFilter).SetRenameCallback(callback); } break; } } tw.Filter = newFilter; }
public virtual void CommitNewSubmodule() { Git git = new Git(db); WriteTrashFile("file.txt", "content"); git.Add().AddFilepattern("file.txt").Call(); RevCommit commit = git.Commit().SetMessage("create file").Call(); SubmoduleAddCommand command = new SubmoduleAddCommand(db); string path = "sub"; command.SetPath(path); string uri = db.Directory.ToURI().ToString(); command.SetURI(uri); Repository repo = command.Call(); NUnit.Framework.Assert.IsNotNull(repo); AddRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.ForIndex(db); NUnit.Framework.Assert.IsTrue(generator.Next()); NUnit.Framework.Assert.AreEqual(path, generator.GetPath()); NUnit.Framework.Assert.AreEqual(commit, generator.GetObjectId()); NUnit.Framework.Assert.AreEqual(uri, generator.GetModulesUrl()); NUnit.Framework.Assert.AreEqual(path, generator.GetModulesPath()); NUnit.Framework.Assert.AreEqual(uri, generator.GetConfigUrl()); Repository subModRepo = generator.GetRepository(); AddRepoToClose(subModRepo); NUnit.Framework.Assert.IsNotNull(subModRepo); NUnit.Framework.Assert.AreEqual(commit, repo.Resolve(Constants.HEAD)); RevCommit submoduleCommit = git.Commit().SetMessage("submodule add").SetOnly(path ).Call(); NUnit.Framework.Assert.IsNotNull(submoduleCommit); TreeWalk walk = new TreeWalk(db); walk.AddTree(commit.Tree); walk.AddTree(submoduleCommit.Tree); walk.Filter = TreeFilter.ANY_DIFF; IList <DiffEntry> diffs = DiffEntry.Scan(walk); NUnit.Framework.Assert.AreEqual(1, diffs.Count); DiffEntry subDiff = diffs[0]; NUnit.Framework.Assert.AreEqual(FileMode.MISSING, subDiff.GetOldMode()); NUnit.Framework.Assert.AreEqual(FileMode.GITLINK, subDiff.GetNewMode()); NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, subDiff.GetOldId().ToObjectId()); NUnit.Framework.Assert.AreEqual(commit, subDiff.GetNewId().ToObjectId()); NUnit.Framework.Assert.AreEqual(path, subDiff.GetNewPath()); }
/// <exception cref="System.IO.IOException"></exception> private IList <DiffEntry> DiffIndexAgainstHead(RevCommit commit) { TreeWalk walk = CreateTreeWalk(); try { walk.AddTree(commit.GetParent(0).Tree); walk.AddTree(commit.GetParent(1).Tree); return(DiffEntry.Scan(walk)); } finally { walk.Release(); } }
/// <exception cref="System.IO.IOException"></exception> private DiffEntry FindRename(RevCommit parent, RevCommit commit, PathFilter path) { if (renameDetector == null) { return(null); } treeWalk.Filter = TreeFilter.ANY_DIFF; treeWalk.Reset(parent.Tree, commit.Tree); renameDetector.AddAll(DiffEntry.Scan(treeWalk)); foreach (DiffEntry ent in renameDetector.Compute()) { if (IsRename(ent) && ent.GetNewPath().Equals(path.GetPath())) { return(ent); } } return(null); }
public IEnumerable <FileDiff> GetFileDiffs(string newHash, string oldHash) { NGit.Repository repo = this.git.GetRepository(); ObjectId newCommit = repo.Resolve(newHash + "^{tree}"); ObjectId oldCommit = repo.Resolve(oldHash + "^{tree}"); var walk = new TreeWalk(repo) { Recursive = true }; walk.AddTree(oldCommit); walk.AddTree(newCommit); IEnumerable <DiffEntry> entries = DiffEntry.Scan(walk); var diffs = entries.Where(diff => diff.GetNewId().Name != diff.GetOldId().Name); return(from diffEntry in diffs let diffType = ToDiffType(diffEntry.GetChangeType()) let path = diffType == DiffType.Delete ? diffEntry.GetOldPath() : diffEntry.GetNewPath() select new FileDiff(path, diffType)); }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> private void UpdateFollowFilter(ObjectId[] trees) { TreeWalk tw = pathFilter; FollowFilter oldFilter = (FollowFilter)tw.Filter; tw.Filter = TreeFilter.ANY_DIFF; tw.Reset(trees); IList <DiffEntry> files = DiffEntry.Scan(tw); RenameDetector rd = new RenameDetector(repository); rd.AddAll(files); files = rd.Compute(); TreeFilter newFilter = oldFilter; foreach (DiffEntry ent in files) { if (IsRename(ent) && ent.GetNewPath().Equals(oldFilter.GetPath())) { newFilter = FollowFilter.Create(ent.GetOldPath()); break; } } tw.Filter = newFilter; }