public virtual void TestUnchangedSimple() { GitIndex index = new GitIndex(db); index.Add(trash, WriteTrashFile("a.b", "a.b")); index.Add(trash, WriteTrashFile("a.c", "a.c")); index.Add(trash, WriteTrashFile("a=c", "a=c")); index.Add(trash, WriteTrashFile("a=d", "a=d")); index.Write(); Tree tree = new Tree(db); // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin tree.AddFile("a.b").SetId(ObjectId.FromString("f6f28df96c2b40c951164286e08be7c38ec74851" )); tree.AddFile("a.c").SetId(ObjectId.FromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca" )); tree.AddFile("a=c").SetId(ObjectId.FromString("06022365ddbd7fb126761319633bf73517770714" )); tree.AddFile("a=d").SetId(ObjectId.FromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2" )); tree.SetId(InsertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count); }
public virtual void TestRemoved() { WriteTrashFile("file2", "file2"); WriteTrashFile("dir/file3", "dir/file3"); Tree tree = new Tree(db); tree.AddFile("file2"); tree.AddFile("dir/file3"); NUnit.Framework.Assert.AreEqual(2, tree.MemberCount()); tree.FindBlobMember("file2").SetId(ObjectId.FromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad" )); Tree tree2 = (Tree)tree.FindTreeMember("dir"); tree2.FindBlobMember("file3").SetId(ObjectId.FromString("873fb8d667d05436d728c52b1d7a09528e6eb59b" )); tree2.SetId(InsertTree(tree2)); tree.SetId(InsertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(2, diff.GetRemoved().Count); NUnit.Framework.Assert.IsTrue(diff.GetRemoved().Contains("file2")); NUnit.Framework.Assert.IsTrue(diff.GetRemoved().Contains("dir/file3")); NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetAdded().Count); }
public virtual void TestModified() { GitIndex index = new GitIndex(db); index.Add(trash, WriteTrashFile("file2", "file2")); index.Add(trash, WriteTrashFile("dir/file3", "dir/file3")); index.Write(); WriteTrashFile("dir/file3", "changed"); Tree tree = new Tree(db); tree.AddFile("file2").SetId(ObjectId.FromString("0123456789012345678901234567890123456789" )); tree.AddFile("dir/file3").SetId(ObjectId.FromString("0123456789012345678901234567890123456789" )); NUnit.Framework.Assert.AreEqual(2, tree.MemberCount()); Tree tree2 = (Tree)tree.FindTreeMember("dir"); tree2.SetId(InsertTree(tree2)); tree.SetId(InsertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(2, diff.GetChanged().Count); NUnit.Framework.Assert.IsTrue(diff.GetChanged().Contains("file2")); NUnit.Framework.Assert.IsTrue(diff.GetChanged().Contains("dir/file3")); NUnit.Framework.Assert.AreEqual(1, diff.GetModified().Count); NUnit.Framework.Assert.IsTrue(diff.GetModified().Contains("dir/file3")); NUnit.Framework.Assert.AreEqual(0, diff.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetMissing().Count); }
public virtual void TestConflictingFromMultipleCreations() { Git git = new Git(db); WriteTrashFile("a", "1\na\n3\n"); git.Add().AddFilepattern("a").Call(); RevCommit initialCommit = git.Commit().SetMessage("initial").Call(); CreateBranch(initialCommit, "refs/heads/side"); CheckoutBranch("refs/heads/side"); WriteTrashFile("b", "1\nb(side)\n3\n"); git.Add().AddFilepattern("b").Call(); RevCommit secondCommit = git.Commit().SetMessage("side").Call(); CheckoutBranch("refs/heads/master"); WriteTrashFile("b", "1\nb(main)\n3\n"); git.Add().AddFilepattern("b").Call(); git.Commit().SetMessage("main").Call(); MergeCommandResult result = git.Merge().Include(secondCommit.Id).SetStrategy(MergeStrategy .RESOLVE).Call(); NUnit.Framework.Assert.AreEqual(MergeStatus.CONFLICTING, result.GetMergeStatus()); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual("[]", new TreeSet <string>(diff.GetChanged()).ToString ()); NUnit.Framework.Assert.AreEqual("[]", diff.GetAdded().ToString()); NUnit.Framework.Assert.AreEqual("[]", diff.GetRemoved().ToString()); NUnit.Framework.Assert.AreEqual("[]", diff.GetMissing().ToString()); NUnit.Framework.Assert.AreEqual("[]", diff.GetModified().ToString()); NUnit.Framework.Assert.AreEqual("[b]", diff.GetConflicting().ToString()); }
public virtual void TestAdded() { WriteTrashFile("file1", "file1"); WriteTrashFile("dir/subfile", "dir/subfile"); Tree tree = new Tree(db); tree.SetId(InsertTree(tree)); DirCache index = db.LockDirCache(); DirCacheEditor editor = index.Editor(); editor.Add(Add(db, trash, "file1")); editor.Add(Add(db, trash, "dir/subfile")); editor.Commit(); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(2, diff.GetAdded().Count); NUnit.Framework.Assert.IsTrue(diff.GetAdded().Contains("file1")); NUnit.Framework.Assert.IsTrue(diff.GetAdded().Contains("dir/subfile")); NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count); NUnit.Framework.CollectionAssert.AreEquivalent(Collections <string> .EMPTY_SET, diff.GetUntrackedFolders() ); }
public virtual void TestDiff() { Write(new FilePath(db.Directory.GetParent(), "test.txt"), "test"); FilePath folder = new FilePath(db.Directory.GetParent(), "folder"); folder.Mkdir(); Write(new FilePath(folder, "folder.txt"), "folder"); Git git = new Git(db); git.Add().AddFilepattern(".").Call(); git.Commit().SetMessage("Initial commit").Call(); Write(new FilePath(folder, "folder.txt"), "folder change"); ByteArrayOutputStream os = new ByteArrayOutputStream(); DiffFormatter df = new DiffFormatter(new SafeBufferedOutputStream(os)); df.SetRepository(db); df.SetPathFilter(PathFilter.Create("folder")); DirCacheIterator oldTree = new DirCacheIterator(db.ReadDirCache()); FileTreeIterator newTree = new FileTreeIterator(db); df.Format(oldTree, newTree); df.Flush(); string actual = System.Text.Encoding.UTF8.GetString(os.ToByteArray()); string expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + "index 0119635..95c4c65 100644\n" + "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n" + "@@ -1 +1 @@\n" + "-folder\n" + "\\ No newline at end of file\n" + "+folder change\n" + "\\ No newline at end of file\n"; NUnit.Framework.Assert.AreEqual(expected.ToString(), actual); }
public virtual void TestModified() { WriteTrashFile("file2", "file2"); WriteTrashFile("dir/file3", "dir/file3"); Git git = new Git(db); git.Add().AddFilepattern("file2").AddFilepattern("dir/file3").Call(); WriteTrashFile("dir/file3", "changed"); Tree tree = new Tree(db); tree.AddFile("file2").SetId(ObjectId.FromString("0123456789012345678901234567890123456789" )); tree.AddFile("dir/file3").SetId(ObjectId.FromString("0123456789012345678901234567890123456789" )); NUnit.Framework.Assert.AreEqual(2, tree.MemberCount()); Tree tree2 = (Tree)tree.FindTreeMember("dir"); tree2.SetId(InsertTree(tree2)); tree.SetId(InsertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(2, diff.GetChanged().Count); NUnit.Framework.Assert.IsTrue(diff.GetChanged().Contains("file2")); NUnit.Framework.Assert.IsTrue(diff.GetChanged().Contains("dir/file3")); NUnit.Framework.Assert.AreEqual(1, diff.GetModified().Count); NUnit.Framework.Assert.IsTrue(diff.GetModified().Contains("dir/file3")); NUnit.Framework.Assert.AreEqual(0, diff.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetMissing().Count); NUnit.Framework.Assert.AreEqual(Collections <string> .EMPTY_SET, diff.GetUntrackedFolders() ); }
public virtual void TestAssumeUnchanged() { Git git = new Git(db); string path = "file"; WriteTrashFile(path, "content"); git.Add().AddFilepattern(path).Call(); string path2 = "file2"; WriteTrashFile(path2, "content"); git.Add().AddFilepattern(path2).Call(); git.Commit().SetMessage("commit").Call(); AssumeUnchanged(path2); WriteTrashFile(path, "more content"); WriteTrashFile(path2, "more content"); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(1, diff.GetAssumeUnchanged().Count); NUnit.Framework.Assert.AreEqual(1, diff.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count); NUnit.Framework.Assert.IsTrue(diff.GetAssumeUnchanged().Contains("file2")); NUnit.Framework.Assert.IsTrue(diff.GetModified().Contains("file")); git.Add().AddFilepattern(".").Call(); iterator = new FileTreeIterator(db); diff = new IndexDiff(db, Constants.HEAD, iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(1, diff.GetAssumeUnchanged().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count); NUnit.Framework.Assert.AreEqual(1, diff.GetChanged().Count); NUnit.Framework.Assert.IsTrue(diff.GetAssumeUnchanged().Contains("file2")); NUnit.Framework.Assert.IsTrue(diff.GetChanged().Contains("file")); }
public void CommitDiffs() { Assert.IsNull(nGit.head()); nGit.file_Create("testFile.txt", "some Text"); "head 1 :{0}".info(nGit.head().info()); nGit.add_and_Commit_using_Status(); "head 2 :{0}".info(nGit.head().info()); nGit.file_Write("testFile.txt", "some Text changed"); nGit.add_and_Commit_using_Status(); var head3 = nGit.head(); "head 3 :{0}".info(head3.info()); var workingTreeIt = new FileTreeIterator(nGit.Repository); var indexDiff = new IndexDiff(nGit.Repository, Constants.HEAD, workingTreeIt); indexDiff.Diff(); var result = new Status(indexDiff); "RESULT: {0}".info(result); /*OutputStream outputStream = "Sharpen.dll".assembly().type("ByteArrayOutputStream").ctor(new object[0]).cast<OutputStream>(); * * var diffFormater = new DiffFormatter(outputStream); * diffFormater.SetRepository(nGit.Repository); * //diffFormater.Format(refLog.GetNewId(), refLog.GetOldId()); * diffFormater.Format(refLog.GetOldId(), refLog.GetNewId());*/ }
/// <summary>Construct a content source for a working directory.</summary> /// <remarks> /// Construct a content source for a working directory. /// If the iterator is a /// <see cref="NGit.Treewalk.FileTreeIterator">NGit.Treewalk.FileTreeIterator</see> /// an optimized version is /// used that doesn't require seeking through a TreeWalk. /// </remarks> /// <param name="iterator">the iterator to obtain source files through.</param> /// <returns>a content source wrapping the iterator.</returns> public static ContentSource Create(WorkingTreeIterator iterator) { if (iterator is FileTreeIterator) { FileTreeIterator i = (FileTreeIterator)iterator; return(new ContentSource.FileSource(i.GetDirectory())); } return(new ContentSource.WorkingTreeSource(iterator)); }
public void testEmptyIfRootDoesNotExist() { string path = Path.Combine(trash.FullName, "not-existing-File"); var di = new DirectoryInfo(path); Assert.IsFalse(di.Exists); var fti = new FileTreeIterator(di); Assert.IsTrue(fti.first()); Assert.IsTrue(fti.eof()); }
public void testEmptyIfRootIsFile() { string path = Path.Combine(trash.FullName, Paths[0]); var di = new DirectoryInfo(path); var fi = new FileInfo(path); Assert.IsTrue(fi.Exists); var fti = new FileTreeIterator(di); Assert.IsTrue(fti.first()); AssertHelper.IgnoreOn(AssertedPlatform.Mono, () => Assert.IsTrue(fti.eof()), "Test fails under mono due to http://bugzilla.novell.com/show_bug.cgi?id=539791,Fixed upstream"); }
public virtual void TestUnchangedComplex() { Git git = new Git(db); WriteTrashFile("a.b", "a.b"); WriteTrashFile("a.c", "a.c"); WriteTrashFile("a/b.b/b", "a/b.b/b"); WriteTrashFile("a/b", "a/b"); WriteTrashFile("a/c", "a/c"); WriteTrashFile("a=c", "a=c"); WriteTrashFile("a=d", "a=d"); git.Add().AddFilepattern("a.b").AddFilepattern("a.c").AddFilepattern("a/b.b/b").AddFilepattern ("a/b").AddFilepattern("a/c").AddFilepattern("a=c").AddFilepattern("a=d").Call(); Tree tree = new Tree(db); // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin tree.AddFile("a.b").SetId(ObjectId.FromString("f6f28df96c2b40c951164286e08be7c38ec74851" )); tree.AddFile("a.c").SetId(ObjectId.FromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca" )); tree.AddFile("a/b.b/b").SetId(ObjectId.FromString("8d840bd4e2f3a48ff417c8e927d94996849933fd" )); tree.AddFile("a/b").SetId(ObjectId.FromString("db89c972fc57862eae378f45b74aca228037d415" )); tree.AddFile("a/c").SetId(ObjectId.FromString("52ad142a008aeb39694bafff8e8f1be75ed7f007" )); tree.AddFile("a=c").SetId(ObjectId.FromString("06022365ddbd7fb126761319633bf73517770714" )); tree.AddFile("a=d").SetId(ObjectId.FromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2" )); Tree tree3 = (Tree)tree.FindTreeMember("a/b.b"); tree3.SetId(InsertTree(tree3)); Tree tree2 = (Tree)tree.FindTreeMember("a"); tree2.SetId(InsertTree(tree2)); tree.SetId(InsertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count); NUnit.Framework.Assert.AreEqual(Collections <string> .EMPTY_SET, diff.GetUntrackedFolders() ); }
/// <summary>Update any smudged entries with information from the working tree.</summary> /// <remarks>Update any smudged entries with information from the working tree.</remarks> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> private void UpdateSmudgedEntries() { TreeWalk walk = new TreeWalk(repository); IList <string> paths = new AList <string>(128); try { for (int i = 0; i < entryCnt; i++) { if (sortedEntries[i].IsSmudged) { paths.AddItem(sortedEntries[i].PathString); } } if (paths.IsEmpty()) { return; } walk.Filter = PathFilterGroup.CreateFromStrings(paths); DirCacheIterator iIter = new DirCacheIterator(this); FileTreeIterator fIter = new FileTreeIterator(repository); walk.AddTree(iIter); walk.AddTree(fIter); walk.Recursive = true; while (walk.Next()) { iIter = walk.GetTree <DirCacheIterator>(0); if (iIter == null) { continue; } fIter = walk.GetTree <FileTreeIterator>(1); if (fIter == null) { continue; } DirCacheEntry entry = iIter.GetDirCacheEntry(); if (entry.IsSmudged && iIter.IdEqual(fIter)) { entry.SetLength(fIter.GetEntryLength()); entry.LastModified = fIter.GetEntryLastModified(); } } } finally { walk.Release(); } }
public void testSimpleIterate() { var top = new FileTreeIterator(trash); Assert.IsTrue(top.first()); Assert.IsFalse(top.eof()); Assert.IsTrue(FileMode.RegularFile == top.EntryFileMode); Assert.AreEqual(Paths[0], NameOf(top)); Assert.AreEqual(Paths[0].Length, top.getEntryLength()); Assert.AreEqual(_mtime[0], top.getEntryLastModified()); top.next(1); Assert.IsFalse(top.first()); Assert.IsFalse(top.eof()); Assert.IsTrue(FileMode.RegularFile == top.EntryFileMode); Assert.AreEqual(Paths[1], NameOf(top)); Assert.AreEqual(Paths[1].Length, top.getEntryLength()); Assert.AreEqual(_mtime[1], top.getEntryLastModified()); top.next(1); Assert.IsFalse(top.first()); Assert.IsFalse(top.eof()); Assert.IsTrue(FileMode.Tree == top.EntryFileMode); AbstractTreeIterator sub = top.createSubtreeIterator(db); Assert.IsTrue(sub is FileTreeIterator); var subfti = (FileTreeIterator)sub; Assert.IsTrue(sub.first()); Assert.IsFalse(sub.eof()); Assert.AreEqual(Paths[2], NameOf(sub)); Assert.AreEqual(Paths[2].Length, subfti.getEntryLength()); Assert.AreEqual(_mtime[2], subfti.getEntryLastModified()); sub.next(1); Assert.IsTrue(sub.eof()); top.next(1); Assert.IsFalse(top.first()); Assert.IsFalse(top.eof()); Assert.IsTrue(FileMode.RegularFile == top.EntryFileMode); Assert.AreEqual(Paths[3], NameOf(top)); Assert.AreEqual(Paths[3].Length, top.getEntryLength()); Assert.AreEqual(_mtime[3], top.getEntryLastModified()); top.next(1); Assert.IsTrue(top.eof()); }
public virtual void TestRemovedUntracked() { Git git = new Git(db); string path = "file"; WriteTrashFile(path, "content"); git.Add().AddFilepattern(path).Call(); git.Commit().SetMessage("commit").Call(); RemoveFromIndex(path); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); diff.Diff(); NUnit.Framework.Assert.IsTrue(diff.GetRemoved().Contains(path)); NUnit.Framework.Assert.IsTrue(diff.GetUntracked().Contains(path)); }
public void testComputeFileObjectId() { var top = new FileTreeIterator(trash); MessageDigest md = Constants.newMessageDigest(); md.Update(Constants.encodeASCII(Constants.TYPE_BLOB)); md.Update((byte)' '); md.Update(Constants.encodeASCII(Paths[0].Length)); md.Update(0); md.Update(Constants.encode(Paths[0])); ObjectId expect = ObjectId.FromRaw(md.Digest()); Assert.AreEqual(expect, top.getEntryObjectId()); // Verify it was cached by removing the File and getting it again. File.Delete(Path.Combine(trash.FullName, Paths[0])); Assert.AreEqual(expect, top.getEntryObjectId()); }
public virtual void TestConflicting() { Git git = new Git(db); WriteTrashFile("a", "1\na\n3\n"); WriteTrashFile("b", "1\nb\n3\n"); git.Add().AddFilepattern("a").AddFilepattern("b").Call(); RevCommit initialCommit = git.Commit().SetMessage("initial").Call(); // create side branch with two modifications CreateBranch(initialCommit, "refs/heads/side"); CheckoutBranch("refs/heads/side"); WriteTrashFile("a", "1\na(side)\n3\n"); WriteTrashFile("b", "1\nb\n3\n(side)"); git.Add().AddFilepattern("a").AddFilepattern("b").Call(); RevCommit secondCommit = git.Commit().SetMessage("side").Call(); // update a on master to generate conflict CheckoutBranch("refs/heads/master"); WriteTrashFile("a", "1\na(main)\n3\n"); git.Add().AddFilepattern("a").Call(); git.Commit().SetMessage("main").Call(); // merge side with master MergeCommandResult result = git.Merge().Include(secondCommit.Id).SetStrategy(MergeStrategy .RESOLVE).Call(); NUnit.Framework.Assert.AreEqual(MergeStatus.CONFLICTING, result.GetMergeStatus()); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); diff.Diff(); NUnit.Framework.CollectionAssert.AreEqual("[b]", new TreeSet <string>(diff.GetChanged()).ToString ()); NUnit.Framework.CollectionAssert.IsEmpty(diff.GetAdded()); NUnit.Framework.CollectionAssert.IsEmpty(diff.GetRemoved()); NUnit.Framework.CollectionAssert.IsEmpty(diff.GetMissing()); NUnit.Framework.CollectionAssert.IsEmpty(diff.GetModified()); NUnit.Framework.Assert.AreEqual("a", diff.GetConflicting().First()); NUnit.Framework.Assert.AreEqual(1, diff.GetConflicting().Count()); NUnit.Framework.CollectionAssert.IsEmpty(diff.GetUntrackedFolders()); }
/// <summary>Resets the index to represent exactly some filesystem content.</summary> /// <remarks> /// Resets the index to represent exactly some filesystem content. E.g. the /// following call will replace the index with the working tree content: /// <p> /// <code>resetIndex(new FileSystemIterator(db))</code> /// <p> /// This method can be used by testcases which first prepare a new commit /// somewhere in the filesystem (e.g. in the working-tree) and then want to /// have an index which matches their prepared content. /// </remarks> /// <param name="treeItr"> /// a /// <see cref="NGit.Treewalk.FileTreeIterator">NGit.Treewalk.FileTreeIterator</see> /// which determines which files should /// go into the new index /// </param> /// <exception cref="System.IO.FileNotFoundException">System.IO.FileNotFoundException /// </exception> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> protected internal virtual void ResetIndex(FileTreeIterator treeItr) { ObjectInserter inserter = db.NewObjectInserter(); DirCacheBuilder builder = db.LockDirCache().Builder(); DirCacheEntry dce; while (!treeItr.Eof) { long len = treeItr.GetEntryLength(); dce = new DirCacheEntry(treeItr.EntryPathString); dce.FileMode = treeItr.EntryFileMode; dce.LastModified = treeItr.GetEntryLastModified(); dce.SetLength((int)len); FileInputStream @in = new FileInputStream(treeItr.GetEntryFile()); dce.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, len, @in)); @in.Close(); builder.Add(dce); treeItr.Next(1); } builder.Commit(); inserter.Flush(); inserter.Release(); }
public virtual void TestAdded() { GitIndex index = new GitIndex(db); WriteTrashFile("file1", "file1"); WriteTrashFile("dir/subfile", "dir/subfile"); Tree tree = new Tree(db); tree.SetId(InsertTree(tree)); index.Add(trash, new FilePath(trash, "file1")); index.Add(trash, new FilePath(trash, "dir/subfile")); index.Write(); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.GetId(), iterator); diff.Diff(); NUnit.Framework.Assert.AreEqual(2, diff.GetAdded().Count); NUnit.Framework.Assert.IsTrue(diff.GetAdded().Contains("file1")); NUnit.Framework.Assert.IsTrue(diff.GetAdded().Contains("dir/subfile")); NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count); }
public virtual void TestSameDiff() { Write(new FilePath(db.Directory.GetParent(), "test.txt"), "test"); FilePath folder = new FilePath(db.Directory.GetParent(), "folder"); folder.Mkdir(); Write(new FilePath(folder, "folder.txt"), "\n\n\n\nfolder"); Git git = new Git(db); git.Add().AddFilepattern(".").Call(); git.Commit().SetMessage("Initial commit").Call(); Write(new FilePath(folder, "folder.txt"), "\n\n\n\nfolder change"); PatchIdDiffFormatter df = new PatchIdDiffFormatter(); df.SetRepository(db); df.SetPathFilter(PathFilter.Create("folder")); DirCacheIterator oldTree = new DirCacheIterator(db.ReadDirCache()); FileTreeIterator newTree = new FileTreeIterator(db); df.Format(oldTree, newTree); df.Flush(); NUnit.Framework.Assert.AreEqual("08fca5ac531383eb1da8bf6b6f7cf44411281407", df.GetCalulatedPatchId ().Name); Write(new FilePath(folder, "folder.txt"), "a\n\n\n\nfolder"); git.Add().AddFilepattern(".").Call(); git.Commit().SetMessage("Initial commit").Call(); Write(new FilePath(folder, "folder.txt"), "a\n\n\n\nfolder change"); df = new PatchIdDiffFormatter(); df.SetRepository(db); df.SetPathFilter(PathFilter.Create("folder")); oldTree = new DirCacheIterator(db.ReadDirCache()); newTree = new FileTreeIterator(db); df.Format(oldTree, newTree); df.Flush(); NUnit.Framework.Assert.AreEqual("08fca5ac531383eb1da8bf6b6f7cf44411281407", df.GetCalulatedPatchId ().Name); }
public virtual void TestDiff() { Write(new FilePath(db.Directory.GetParent(), "test.txt"), "test"); FilePath folder = new FilePath(db.Directory.GetParent(), "folder"); folder.Mkdir(); Write(new FilePath(folder, "folder.txt"), "folder"); Git git = new Git(db); git.Add().AddFilepattern(".").Call(); git.Commit().SetMessage("Initial commit").Call(); Write(new FilePath(folder, "folder.txt"), "folder change"); PatchIdDiffFormatter df = new PatchIdDiffFormatter(); df.SetRepository(db); df.SetPathFilter(PathFilter.Create("folder")); DirCacheIterator oldTree = new DirCacheIterator(db.ReadDirCache()); FileTreeIterator newTree = new FileTreeIterator(db); df.Format(oldTree, newTree); df.Flush(); NUnit.Framework.Assert.AreEqual("1ff64e0f9333e9b81967c3e8d7a81362b14d5441", df.GetCalulatedPatchId ().Name); }
/// <exception cref="System.IO.IOException"></exception> private DirCache CreateTemporaryIndex(ObjectId headId, DirCache index) { ObjectInserter inserter = null; // get DirCacheEditor to modify the index if required DirCacheEditor dcEditor = index.Editor(); // get DirCacheBuilder for newly created in-core index to build a // temporary index for this commit DirCache inCoreIndex = DirCache.NewInCore(); DirCacheBuilder dcBuilder = inCoreIndex.Builder(); onlyProcessed = new bool[only.Count]; bool emptyCommit = true; TreeWalk treeWalk = new TreeWalk(repo); int dcIdx = treeWalk.AddTree(new DirCacheIterator(index)); int fIdx = treeWalk.AddTree(new FileTreeIterator(repo)); int hIdx = -1; if (headId != null) { hIdx = treeWalk.AddTree(new RevWalk(repo).ParseTree(headId)); } treeWalk.Recursive = true; while (treeWalk.Next()) { string path = treeWalk.PathString; // check if current entry's path matches a specified path int pos = LookupOnly(path); CanonicalTreeParser hTree = null; if (hIdx != -1) { hTree = treeWalk.GetTree <CanonicalTreeParser>(hIdx); } if (pos >= 0) { // include entry in commit DirCacheIterator dcTree = treeWalk.GetTree <DirCacheIterator>(dcIdx); FileTreeIterator fTree = treeWalk.GetTree <FileTreeIterator>(fIdx); // check if entry refers to a tracked file bool tracked = dcTree != null || hTree != null; if (!tracked) { break; } if (fTree != null) { // create a new DirCacheEntry with data retrieved from disk DirCacheEntry dcEntry = new DirCacheEntry(path); long entryLength = fTree.GetEntryLength(); dcEntry.SetLength(entryLength); dcEntry.LastModified = fTree.GetEntryLastModified(); dcEntry.FileMode = fTree.GetIndexFileMode(dcTree); bool objectExists = (dcTree != null && fTree.IdEqual(dcTree)) || (hTree != null && fTree.IdEqual(hTree)); if (objectExists) { dcEntry.SetObjectId(fTree.EntryObjectId); } else { if (FileMode.GITLINK.Equals(dcEntry.FileMode)) { dcEntry.SetObjectId(fTree.EntryObjectId); } else { // insert object if (inserter == null) { inserter = repo.NewObjectInserter(); } long contentLength = fTree.GetEntryContentLength(); InputStream inputStream = fTree.OpenEntryStream(); try { dcEntry.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, contentLength, inputStream )); } finally { inputStream.Close(); } } } // update index dcEditor.Add(new _PathEdit_375(dcEntry, path)); // add to temporary in-core index dcBuilder.Add(dcEntry); if (emptyCommit && (hTree == null || !hTree.IdEqual(fTree) || hTree.EntryRawMode != fTree.EntryRawMode)) { // this is a change emptyCommit = false; } } else { // if no file exists on disk, remove entry from index and // don't add it to temporary in-core index dcEditor.Add(new DirCacheEditor.DeletePath(path)); if (emptyCommit && hTree != null) { // this is a change emptyCommit = false; } } // keep track of processed path onlyProcessed[pos] = true; } else { // add entries from HEAD for all other paths if (hTree != null) { // create a new DirCacheEntry with data retrieved from HEAD DirCacheEntry dcEntry = new DirCacheEntry(path); dcEntry.SetObjectId(hTree.EntryObjectId); dcEntry.FileMode = hTree.EntryFileMode; // add to temporary in-core index dcBuilder.Add(dcEntry); } } } // there must be no unprocessed paths left at this point; otherwise an // untracked or unknown path has been specified for (int i = 0; i < onlyProcessed.Length; i++) { if (!onlyProcessed[i]) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().entryNotFoundByPath , only[i])); } } // there must be at least one change if (emptyCommit) { throw new JGitInternalException(JGitText.Get().emptyCommit); } // update index dcEditor.Commit(); // finish temporary in-core index used for this commit dcBuilder.Finish(); return(inCoreIndex); }
/// <summary> /// Run the diff operation. Until this is called, all lists will be empty /// </summary> /// <returns>true if anything is different between index, tree, and workdir</returns> private void UpdateDirectory(IEnumerable <string> paths, bool recursive) { RevWalk rw = new RevWalk(Repository); ObjectId id = Repository.Resolve(Constants.HEAD); var commit = id != null?rw.ParseCommit(id) : null; TreeWalk treeWalk = new TreeWalk(Repository); treeWalk.Reset(); treeWalk.Recursive = false; if (commit != null) { treeWalk.AddTree(commit.Tree); } else { treeWalk.AddTree(new EmptyTreeIterator()); } DirCache dc = Repository.ReadDirCache(); treeWalk.AddTree(new DirCacheIterator(dc)); FileTreeIterator workTree = new FileTreeIterator(Repository.WorkTree, Repository.FileSystem, WorkingTreeOptions.KEY.Parse(Repository.GetConfig())); treeWalk.AddTree(workTree); List <TreeFilter> filters = new List <TreeFilter> (); filters.Add(new SkipWorkTreeFilter(1)); var pathFilters = paths.Where(p => p != ".").Select(p => PathFilter.Create(p)).ToArray(); if (pathFilters.Length > 1) { filters.Add(OrTreeFilter.Create(pathFilters)); // Use an OR to join all path filters } else if (pathFilters.Length == 1) { filters.Add(pathFilters[0]); } if (filters.Count > 1) { treeWalk.Filter = AndTreeFilter.Create(filters); } else { treeWalk.Filter = filters[0]; } while (treeWalk.Next()) { AbstractTreeIterator treeIterator = treeWalk.GetTree <AbstractTreeIterator>(0); DirCacheIterator dirCacheIterator = treeWalk.GetTree <DirCacheIterator>(1); WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(2); NGit.FileMode fileModeTree = treeWalk.GetFileMode(0); if (treeWalk.IsSubtree) { treeWalk.EnterSubtree(); continue; } int stage = dirCacheIterator != null?dirCacheIterator.GetDirCacheEntry().Stage : 0; if (stage > 1) { continue; } else if (stage == 1) { MergeConflict.Add(dirCacheIterator.EntryPathString); changesExist = true; continue; } if (treeIterator != null) { if (dirCacheIterator != null) { if (!treeIterator.EntryObjectId.Equals(dirCacheIterator.EntryObjectId)) { // in repo, in index, content diff => changed Modified.Add(dirCacheIterator.EntryPathString); changesExist = true; } } else { // in repo, not in index => removed if (!fileModeTree.Equals(NGit.FileMode.TYPE_TREE)) { Removed.Add(treeIterator.EntryPathString); changesExist = true; } } } else { if (dirCacheIterator != null) { // not in repo, in index => added Added.Add(dirCacheIterator.EntryPathString); changesExist = true; } else { // not in repo, not in index => untracked if (workingTreeIterator != null && !workingTreeIterator.IsEntryIgnored()) { Untracked.Add(workingTreeIterator.EntryPathString); changesExist = true; } } } if (dirCacheIterator != null) { if (workingTreeIterator == null) { // in index, not in workdir => missing Missing.Add(dirCacheIterator.EntryPathString); changesExist = true; } else { // Workaround to file time resolution issues long itime = dirCacheIterator.GetDirCacheEntry().LastModified; long ftime = workingTreeIterator.GetEntryLastModified(); if (itime / 1000 != ftime / 1000) { if (!dirCacheIterator.IdEqual(workingTreeIterator)) { // in index, in workdir, content differs => modified Modified.Add(dirCacheIterator.EntryPathString); changesExist = true; } } } } } }
/// <summary>Apply the changes in a stashed commit to the working directory and index /// </summary> /// <returns>id of stashed commit that was applied</returns> /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException /// </exception> /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException">NGit.Api.Errors.WrongRepositoryStateException /// </exception> public override ObjectId Call() { CheckCallable(); if (repo.GetRepositoryState() != RepositoryState.SAFE) { throw new WrongRepositoryStateException(MessageFormat.Format(JGitText.Get().stashApplyOnUnsafeRepository , repo.GetRepositoryState())); } ObjectId headTree = GetHeadTree(); ObjectId stashId = GetStashId(); ObjectReader reader = repo.NewObjectReader(); try { RevWalk revWalk = new RevWalk(reader); RevCommit stashCommit = revWalk.ParseCommit(stashId); if (stashCommit.ParentCount != 2) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().stashCommitMissingTwoParents , stashId.Name)); } RevTree stashWorkingTree = stashCommit.Tree; RevTree stashIndexTree = revWalk.ParseCommit(stashCommit.GetParent(1)).Tree; RevTree stashHeadTree = revWalk.ParseCommit(stashCommit.GetParent(0)).Tree; CanonicalTreeParser stashWorkingIter = new CanonicalTreeParser(); stashWorkingIter.Reset(reader, stashWorkingTree); CanonicalTreeParser stashIndexIter = new CanonicalTreeParser(); stashIndexIter.Reset(reader, stashIndexTree); CanonicalTreeParser stashHeadIter = new CanonicalTreeParser(); stashHeadIter.Reset(reader, stashHeadTree); CanonicalTreeParser headIter = new CanonicalTreeParser(); headIter.Reset(reader, headTree); DirCache cache = repo.LockDirCache(); DirCacheEditor editor = cache.Editor(); try { DirCacheIterator indexIter = new DirCacheIterator(cache); FileTreeIterator workingIter = new FileTreeIterator(repo); TreeWalk treeWalk = new TreeWalk(reader); treeWalk.Recursive = true; treeWalk.Filter = new StashApplyCommand.StashDiffFilter(); treeWalk.AddTree(stashHeadIter); treeWalk.AddTree(stashIndexIter); treeWalk.AddTree(stashWorkingIter); treeWalk.AddTree(headIter); treeWalk.AddTree(indexIter); treeWalk.AddTree(workingIter); ScanForConflicts(treeWalk); // Reset trees and walk treeWalk.Reset(); stashWorkingIter.Reset(reader, stashWorkingTree); stashIndexIter.Reset(reader, stashIndexTree); stashHeadIter.Reset(reader, stashHeadTree); treeWalk.AddTree(stashHeadIter); treeWalk.AddTree(stashIndexIter); treeWalk.AddTree(stashWorkingIter); ApplyChanges(treeWalk, cache, editor); } finally { editor.Commit(); cache.Unlock(); } } catch (JGitInternalException e) { throw; } catch (IOException e) { throw new JGitInternalException(JGitText.Get().stashApplyFailed, e); } finally { reader.Release(); } return(stashId); }