public virtual void TestPathsResetOnDirs() { SetupRepository(); DirCacheEntry preReset = DirCache.Read(db.GetIndexFile(), db.FileSystem).GetEntry ("dir/b.txt"); NUnit.Framework.Assert.IsNotNull(preReset); git.Add().AddFilepattern(untrackedFile.GetName()).Call(); // 'dir/b.txt' has already been modified in setupRepository git.Reset().AddPath("dir").Call(); DirCacheEntry postReset = DirCache.Read(db.GetIndexFile(), db.FileSystem).GetEntry ("dir/b.txt"); NUnit.Framework.Assert.IsNotNull(postReset); NUnit.Framework.Assert.AreNotSame(preReset.GetObjectId(), postReset.GetObjectId() ); // check that HEAD hasn't moved ObjectId head = db.Resolve(Constants.HEAD); NUnit.Framework.Assert.AreEqual(secondCommit, head); // check if files still exist NUnit.Framework.Assert.IsTrue(untrackedFile.Exists()); NUnit.Framework.Assert.IsTrue(InHead("dir/b.txt")); NUnit.Framework.Assert.IsTrue(InIndex("dir/b.txt")); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.NoFilepatternException"></exception> /// <exception cref="NGit.Api.Errors.NoHeadException"></exception> /// <exception cref="NGit.Api.Errors.NoMessageException"></exception> /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException"></exception> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException"></exception> public virtual void SetupRepository() { // create initial commit git = new Git(db); initialCommit = git.Commit().SetMessage("initial commit").Call(); // create file indexFile = new FilePath(db.WorkTree, "a.txt"); FileUtils.CreateNewFile(indexFile); PrintWriter writer = new PrintWriter(indexFile); writer.Write("content"); writer.Flush(); // add file and commit it git.Add().AddFilepattern("a.txt").Call(); secondCommit = git.Commit().SetMessage("adding a.txt").Call(); prestage = DirCache.Read(db.GetIndexFile(), db.FileSystem).GetEntry(indexFile.GetName ()); // modify file and add to index writer.Write("new content"); writer.Close(); git.Add().AddFilepattern("a.txt").Call(); // create a file not added to the index untrackedFile = new FilePath(db.WorkTree, "notAddedToIndex.txt"); FileUtils.CreateNewFile(untrackedFile); PrintWriter writer2 = new PrintWriter(untrackedFile); writer2.Write("content"); writer2.Close(); }
public virtual void TestUpdateSmudgedEntries() { git.BranchCreate().SetName("test2").Call(); RefUpdate rup = db.UpdateRef(Constants.HEAD); rup.Link("refs/heads/test2"); FilePath file = new FilePath(db.WorkTree, "Test.txt"); long size = file.Length(); long mTime = file.LastModified() - 5000L; NUnit.Framework.Assert.IsTrue(file.SetLastModified(mTime)); DirCache cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem); DirCacheEntry entry = cache.GetEntry("Test.txt"); NUnit.Framework.Assert.IsNotNull(entry); entry.SetLength(0); entry.LastModified = 0; cache.Write(); NUnit.Framework.Assert.IsTrue(cache.Commit()); cache = DirCache.Read(db.GetIndexFile(), db.FileSystem); entry = cache.GetEntry("Test.txt"); NUnit.Framework.Assert.IsNotNull(entry); NUnit.Framework.Assert.AreEqual(0, entry.Length); NUnit.Framework.Assert.AreEqual(0, entry.LastModified); db.GetIndexFile().SetLastModified(db.GetIndexFile().LastModified() - 5000); NUnit.Framework.Assert.IsNotNull(git.Checkout().SetName("test").Call()); cache = DirCache.Read(db.GetIndexFile(), db.FileSystem); entry = cache.GetEntry("Test.txt"); NUnit.Framework.Assert.IsNotNull(entry); NUnit.Framework.Assert.AreEqual(size, entry.Length); NUnit.Framework.Assert.AreEqual(mTime, entry.LastModified); }
public virtual void CommitUpdatesSmudgedEntries() { Git git = new Git(db); FilePath file1 = WriteTrashFile("file1.txt", "content1"); NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() - 5000)); FilePath file2 = WriteTrashFile("file2.txt", "content2"); NUnit.Framework.Assert.IsTrue(file2.SetLastModified(file2.LastModified() - 5000)); FilePath file3 = WriteTrashFile("file3.txt", "content3"); NUnit.Framework.Assert.IsTrue(file3.SetLastModified(file3.LastModified() - 5000)); NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("file1.txt").AddFilepattern ("file2.txt").AddFilepattern("file3.txt").Call()); RevCommit commit = git.Commit().SetMessage("add files").Call(); NUnit.Framework.Assert.IsNotNull(commit); DirCache cache = DirCache.Read(db.GetIndexFile(), db.FileSystem); int file1Size = cache.GetEntry("file1.txt").Length; int file2Size = cache.GetEntry("file2.txt").Length; int file3Size = cache.GetEntry("file3.txt").Length; ObjectId file2Id = cache.GetEntry("file2.txt").GetObjectId(); ObjectId file3Id = cache.GetEntry("file3.txt").GetObjectId(); NUnit.Framework.Assert.IsTrue(file1Size > 0); NUnit.Framework.Assert.IsTrue(file2Size > 0); NUnit.Framework.Assert.IsTrue(file3Size > 0); // Smudge entries cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem); cache.GetEntry("file1.txt").SetLength(0); cache.GetEntry("file2.txt").SetLength(0); cache.GetEntry("file3.txt").SetLength(0); cache.Write(); NUnit.Framework.Assert.IsTrue(cache.Commit()); // Verify entries smudged cache = DirCache.Read(db.GetIndexFile(), db.FileSystem); NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file1.txt").Length); NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file2.txt").Length); NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file3.txt").Length); long indexTime = db.GetIndexFile().LastModified(); db.GetIndexFile().SetLastModified(indexTime - 5000); Write(file1, "content4"); NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() + 2500)); NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("edit file").SetOnly("file1.txt" ).Call()); cache = db.ReadDirCache(); NUnit.Framework.Assert.AreEqual(file1Size, cache.GetEntry("file1.txt").Length); NUnit.Framework.Assert.AreEqual(file2Size, cache.GetEntry("file2.txt").Length); NUnit.Framework.Assert.AreEqual(file3Size, cache.GetEntry("file3.txt").Length); NUnit.Framework.Assert.AreEqual(file2Id, cache.GetEntry("file2.txt").GetObjectId( )); NUnit.Framework.Assert.AreEqual(file3Id, cache.GetEntry("file3.txt").GetObjectId( )); }
public virtual void TestHardResetOnTag() { SetupRepository(); string tagName = "initialtag"; git.Tag().SetName(tagName).SetObjectId(secondCommit).SetMessage("message").Call(); DirCacheEntry preReset = DirCache.Read(db.GetIndexFile(), db.FileSystem).GetEntry (indexFile.GetName()); NUnit.Framework.Assert.IsNotNull(preReset); git.Add().AddFilepattern(untrackedFile.GetName()).Call(); git.Reset().SetRef(tagName).SetMode(ResetCommand.ResetType.HARD).Call(); ObjectId head = db.Resolve(Constants.HEAD); NUnit.Framework.Assert.AreEqual(secondCommit, head); }
public virtual void TestReadIndex_LsFiles() { IDictionary<string, DirCacheCGitCompatabilityTest.CGitIndexRecord> ls = ReadLsFiles (); DirCache dc = new DirCache(index, FS.DETECTED); NUnit.Framework.Assert.AreEqual(0, dc.GetEntryCount()); dc.Read(); NUnit.Framework.Assert.AreEqual(ls.Count, dc.GetEntryCount()); { Iterator<DirCacheCGitCompatabilityTest.CGitIndexRecord> rItr = ls.Values.Iterator (); for (int i = 0; rItr.HasNext(); i++) { AssertEqual(rItr.Next(), dc.GetEntry(i)); } } }
/// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> private void AssertIndex(Dictionary <string, string> i) { string expectedValue; string path; DirCache read = DirCache.Read(db.GetIndexFile(), db.FileSystem); NUnit.Framework.Assert.AreEqual(i.Count, read.GetEntryCount(), "Index has not the right size." ); for (int j = 0; j < read.GetEntryCount(); j++) { path = read.GetEntry(j).PathString; expectedValue = i.Get(path); NUnit.Framework.Assert.IsNotNull(expectedValue, "found unexpected entry for path " + path + " in index"); NUnit.Framework.Assert.IsTrue(Arrays.Equals(db.Open(read.GetEntry(j).GetObjectId( )).GetCachedBytes(), Sharpen.Runtime.GetBytesForString(i.Get(path))), "unexpected content for path " + path + " in index. Expected: <" + expectedValue + ">"); } }
public virtual void TestPathsResetWithRef() { SetupRepository(); DirCacheEntry preReset = DirCache.Read(db.GetIndexFile(), db.FileSystem).GetEntry (indexFile.GetName()); NUnit.Framework.Assert.IsNotNull(preReset); git.Add().AddFilepattern(untrackedFile.GetName()).Call(); // 'a.txt' has already been modified in setupRepository // 'notAddedToIndex.txt' has been added to repository // reset to the inital commit git.Reset().SetRef(initialCommit.GetName()).AddPath(indexFile.GetName()).AddPath( untrackedFile.GetName()).Call(); // check that HEAD hasn't moved ObjectId head = db.Resolve(Constants.HEAD); NUnit.Framework.Assert.AreEqual(secondCommit, head); // check if files still exist NUnit.Framework.Assert.IsTrue(untrackedFile.Exists()); NUnit.Framework.Assert.IsTrue(indexFile.Exists()); NUnit.Framework.Assert.IsTrue(InHead(indexFile.GetName())); NUnit.Framework.Assert.IsFalse(InIndex(indexFile.GetName())); NUnit.Framework.Assert.IsFalse(InIndex(untrackedFile.GetName())); }
/// <summary>Checks if a file with the given path exists in the index</summary> /// <param name="path"></param> /// <returns>true if the file exists</returns> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> private bool InIndex(string path) { DirCache dc = DirCache.Read(db.GetIndexFile(), db.FileSystem); return(dc.GetEntry(path) != null); }
public virtual void TestTreeWalk_LsFiles() { Repository db = CreateBareRepository(); IDictionary<string, DirCacheCGitCompatabilityTest.CGitIndexRecord> ls = ReadLsFiles (); DirCache dc = new DirCache(index, db.FileSystem); NUnit.Framework.Assert.AreEqual(0, dc.GetEntryCount()); dc.Read(); NUnit.Framework.Assert.AreEqual(ls.Count, dc.GetEntryCount()); { Iterator<DirCacheCGitCompatabilityTest.CGitIndexRecord> rItr = ls.Values.Iterator (); TreeWalk tw = new TreeWalk(db); tw.Recursive = true; tw.AddTree(new DirCacheIterator(dc)); while (rItr.HasNext()) { DirCacheIterator dcItr; NUnit.Framework.Assert.IsTrue(tw.Next()); dcItr = tw.GetTree<DirCacheIterator>(0); NUnit.Framework.Assert.IsNotNull(dcItr); AssertEqual(rItr.Next(), dcItr.GetDirCacheEntry()); } } }
public virtual void TestReadWriteV3() { FilePath file = PathOf("gitgit.index.v3"); DirCache dc = new DirCache(file, FS.DETECTED); dc.Read(); NUnit.Framework.Assert.AreEqual(10, dc.GetEntryCount()); AssertV3TreeEntry(0, "dir1/file1.txt", false, false, dc); AssertV3TreeEntry(1, "dir2/file2.txt", true, false, dc); AssertV3TreeEntry(2, "dir3/file3.txt", false, false, dc); AssertV3TreeEntry(3, "dir3/file3a.txt", true, false, dc); AssertV3TreeEntry(4, "dir4/file4.txt", true, false, dc); AssertV3TreeEntry(5, "dir4/file4a.txt", false, false, dc); AssertV3TreeEntry(6, "file.txt", true, false, dc); AssertV3TreeEntry(7, "newdir1/newfile1.txt", false, true, dc); AssertV3TreeEntry(8, "newdir1/newfile2.txt", false, true, dc); AssertV3TreeEntry(9, "newfile.txt", false, true, dc); ByteArrayOutputStream bos = new ByteArrayOutputStream(); dc.WriteTo(bos); byte[] indexBytes = bos.ToByteArray(); byte[] expectedBytes = IOUtil.ReadFully(file); CollectionAssert.AreEquivalent(expectedBytes, indexBytes); }
public virtual void TestReadIndex_DirCacheTree() { IDictionary<string, DirCacheCGitCompatabilityTest.CGitIndexRecord> cList = ReadLsFiles (); IDictionary<string, DirCacheCGitCompatabilityTest.CGitLsTreeRecord> cTree = ReadLsTree (); DirCache dc = new DirCache(index, FS.DETECTED); NUnit.Framework.Assert.AreEqual(0, dc.GetEntryCount()); dc.Read(); NUnit.Framework.Assert.AreEqual(cList.Count, dc.GetEntryCount()); DirCacheTree jTree = dc.GetCacheTree(false); NUnit.Framework.Assert.IsNotNull(jTree); NUnit.Framework.Assert.AreEqual(string.Empty, jTree.GetNameString()); NUnit.Framework.Assert.AreEqual(string.Empty, jTree.GetPathString()); NUnit.Framework.Assert.IsTrue(jTree.IsValid()); NUnit.Framework.Assert.AreEqual(ObjectId.FromString("698dd0b8d0c299f080559a1cffc7fe029479a408" ), jTree.GetObjectId()); NUnit.Framework.Assert.AreEqual(cList.Count, jTree.GetEntrySpan()); AList<DirCacheCGitCompatabilityTest.CGitLsTreeRecord> subtrees = new AList<DirCacheCGitCompatabilityTest.CGitLsTreeRecord >(); foreach (DirCacheCGitCompatabilityTest.CGitLsTreeRecord r in cTree.Values) { if (FileMode.TREE.Equals(r.mode)) { subtrees.AddItem(r); } } NUnit.Framework.Assert.AreEqual(subtrees.Count, jTree.GetChildCount()); for (int i = 0; i < jTree.GetChildCount(); i++) { DirCacheTree sj = jTree.GetChild(i); DirCacheCGitCompatabilityTest.CGitLsTreeRecord sc = subtrees[i]; NUnit.Framework.Assert.AreEqual(sc.path, sj.GetNameString()); NUnit.Framework.Assert.AreEqual(sc.path + "/", sj.GetPathString()); NUnit.Framework.Assert.IsTrue(sj.IsValid()); NUnit.Framework.Assert.AreEqual(sc.id, sj.GetObjectId()); } }
public virtual void TestCorruptChecksumAtFooter() { DirCache dc = new DirCache(PathOf("gitgit.index.badchecksum"), FS.DETECTED); try { dc.Read(); NUnit.Framework.Assert.Fail("Cache loaded despite corrupt checksum"); } catch (CorruptObjectException err) { NUnit.Framework.Assert.AreEqual("DIRC checksum mismatch", err.Message); } }
public virtual void TestUnsupportedRequiredExtension() { DirCache dc = new DirCache(PathOf("gitgit.index.aaaa"), FS.DETECTED); try { dc.Read(); NUnit.Framework.Assert.Fail("Cache loaded an unsupported extension"); } catch (CorruptObjectException err) { NUnit.Framework.Assert.AreEqual("DIRC extension 'aaaa'" + " not supported by this version." , err.Message); } }
public virtual void TestUnsupportedOptionalExtension() { DirCache dc = new DirCache(PathOf("gitgit.index.ZZZZ"), FS.DETECTED); dc.Read(); NUnit.Framework.Assert.AreEqual(1, dc.GetEntryCount()); NUnit.Framework.Assert.AreEqual("A", dc.GetEntry(0).PathString); }