public virtual void TestNoPostOrder() { DirCache tree = db.ReadDirCache(); { DirCacheBuilder b = tree.Builder(); b.Add(MakeFile("a")); b.Add(MakeFile("b/c")); b.Add(MakeFile("b/d")); b.Add(MakeFile("q")); b.Finish(); NUnit.Framework.Assert.AreEqual(4, tree.GetEntryCount()); } TreeWalk tw = new TreeWalk(db); tw.PostOrderTraversal = false; tw.AddTree(new DirCacheIterator(tree)); AssertModes("a", FileMode.REGULAR_FILE, tw); AssertModes("b", FileMode.TREE, tw); NUnit.Framework.Assert.IsTrue(tw.IsSubtree); NUnit.Framework.Assert.IsFalse(tw.IsPostChildren); tw.EnterSubtree(); AssertModes("b/c", FileMode.REGULAR_FILE, tw); AssertModes("b/d", FileMode.REGULAR_FILE, tw); AssertModes("q", FileMode.REGULAR_FILE, tw); }
public virtual void TestDF_NoGap() { DirCache tree0 = db.ReadDirCache(); DirCache tree1 = db.ReadDirCache(); { DirCacheBuilder b0 = tree0.Builder(); DirCacheBuilder b1 = tree1.Builder(); b0.Add(CreateEntry("a", REGULAR_FILE)); b0.Add(CreateEntry("a.b", EXECUTABLE_FILE)); b1.Add(CreateEntry("a/b", REGULAR_FILE)); b0.Add(CreateEntry("a0b", SYMLINK)); b0.Finish(); b1.Finish(); NUnit.Framework.Assert.AreEqual(3, tree0.GetEntryCount()); NUnit.Framework.Assert.AreEqual(1, tree1.GetEntryCount()); } NameConflictTreeWalk tw = new NameConflictTreeWalk(db); tw.AddTree(new DirCacheIterator(tree0)); tw.AddTree(new DirCacheIterator(tree1)); AssertModes("a", REGULAR_FILE, TREE, tw); NUnit.Framework.Assert.IsTrue(tw.IsDirectoryFileConflict()); NUnit.Framework.Assert.IsTrue(tw.IsSubtree); tw.EnterSubtree(); AssertModes("a/b", MISSING, REGULAR_FILE, tw); NUnit.Framework.Assert.IsTrue(tw.IsDirectoryFileConflict()); AssertModes("a.b", EXECUTABLE_FILE, MISSING, tw); NUnit.Framework.Assert.IsFalse(tw.IsDirectoryFileConflict()); AssertModes("a0b", SYMLINK, MISSING, tw); NUnit.Framework.Assert.IsFalse(tw.IsDirectoryFileConflict()); }
public virtual void TestTrivialTwoWay_rightDFconflict1() { DirCache treeB = db.ReadDirCache(); DirCache treeO = db.ReadDirCache(); DirCache treeT = db.ReadDirCache(); { DirCacheBuilder b = treeB.Builder(); DirCacheBuilder o = treeO.Builder(); DirCacheBuilder t = treeT.Builder(); b.Add(CreateEntry("d/o", FileMode.REGULAR_FILE)); b.Add(CreateEntry("d/t", FileMode.REGULAR_FILE)); o.Add(CreateEntry("d/o", FileMode.REGULAR_FILE)); o.Add(CreateEntry("d/t", FileMode.REGULAR_FILE, "o !")); t.Add(CreateEntry("d", FileMode.REGULAR_FILE)); b.Finish(); o.Finish(); t.Finish(); } ObjectInserter ow = db.NewObjectInserter(); ObjectId b_1 = Commit(ow, treeB, new ObjectId[] { }); ObjectId o_1 = Commit(ow, treeO, new ObjectId[] { b_1 }); ObjectId t_1 = Commit(ow, treeT, new ObjectId[] { b_1 }); Merger ourMerger = ((ThreeWayMerger)MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.NewMerger (db)); bool merge = ourMerger.Merge(new ObjectId[] { o_1, t_1 }); NUnit.Framework.Assert.IsFalse(merge); }
public virtual void TestPick() { // B---O // \----P---T // // Cherry-pick "T" onto "O". This shouldn't introduce "p-fail", which // was created by "P", nor should it modify "a", which was done by "P". // DirCache treeB = db.ReadDirCache(); DirCache treeO = db.ReadDirCache(); DirCache treeP = db.ReadDirCache(); DirCache treeT = db.ReadDirCache(); { DirCacheBuilder b = treeB.Builder(); DirCacheBuilder o = treeO.Builder(); DirCacheBuilder p = treeP.Builder(); DirCacheBuilder t = treeT.Builder(); b.Add(MakeEntry("a", FileMode.REGULAR_FILE)); o.Add(MakeEntry("a", FileMode.REGULAR_FILE)); o.Add(MakeEntry("o", FileMode.REGULAR_FILE)); p.Add(MakeEntry("a", FileMode.REGULAR_FILE, "q")); p.Add(MakeEntry("p-fail", FileMode.REGULAR_FILE)); t.Add(MakeEntry("a", FileMode.REGULAR_FILE)); t.Add(MakeEntry("t", FileMode.REGULAR_FILE)); b.Finish(); o.Finish(); p.Finish(); t.Finish(); } ObjectInserter ow = db.NewObjectInserter(); ObjectId B = Commit(ow, treeB, new ObjectId[] { }); ObjectId O = Commit(ow, treeO, new ObjectId[] { B }); ObjectId P = Commit(ow, treeP, new ObjectId[] { B }); ObjectId T = Commit(ow, treeT, new ObjectId[] { P }); ThreeWayMerger twm = ((ThreeWayMerger)MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.NewMerger (db)); twm.SetBase(P); bool merge = twm.Merge(new ObjectId[] { O, T }); NUnit.Framework.Assert.IsTrue(merge); TreeWalk tw = new TreeWalk(db); tw.Recursive = true; tw.Reset(twm.GetResultTreeId()); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("a", tw.PathString); AssertCorrectId(treeO, tw); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("o", tw.PathString); AssertCorrectId(treeO, tw); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("t", tw.PathString); AssertCorrectId(treeT, tw); NUnit.Framework.Assert.IsFalse(tw.Next()); }
public virtual void TestRecursiveFiltering() { ObjectInserter odi = db.NewObjectInserter(); ObjectId aSth = odi.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString( "a.sth")); ObjectId aTxt = odi.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString( "a.txt")); ObjectId bSth = odi.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString( "b.sth")); ObjectId bTxt = odi.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString( "b.txt")); DirCache dc = db.ReadDirCache(); DirCacheBuilder builder = dc.Builder(); DirCacheEntry aSthEntry = new DirCacheEntry("a.sth"); aSthEntry.FileMode = FileMode.REGULAR_FILE; aSthEntry.SetObjectId(aSth); DirCacheEntry aTxtEntry = new DirCacheEntry("a.txt"); aTxtEntry.FileMode = FileMode.REGULAR_FILE; aTxtEntry.SetObjectId(aTxt); builder.Add(aSthEntry); builder.Add(aTxtEntry); DirCacheEntry bSthEntry = new DirCacheEntry("sub/b.sth"); bSthEntry.FileMode = FileMode.REGULAR_FILE; bSthEntry.SetObjectId(bSth); DirCacheEntry bTxtEntry = new DirCacheEntry("sub/b.txt"); bTxtEntry.FileMode = FileMode.REGULAR_FILE; bTxtEntry.SetObjectId(bTxt); builder.Add(bSthEntry); builder.Add(bTxtEntry); builder.Finish(); ObjectId treeId = dc.WriteTree(odi); odi.Flush(); TreeWalk tw = new TreeWalk(db); tw.Recursive = true; tw.Filter = PathSuffixFilter.Create(".txt"); tw.AddTree(treeId); IList <string> paths = new List <string>(); while (tw.Next()) { paths.AddItem(tw.PathString); } IList <string> expected = new List <string>(); expected.AddItem("a.txt"); expected.AddItem("sub/b.txt"); NUnit.Framework.Assert.AreEqual(expected, paths); }
/// <summary>adds a new path with the specified stage to the index builder</summary> /// <param name="path"></param> /// <param name="p"></param> /// <param name="stage"></param> /// <returns>the entry which was added to the index</returns> private DirCacheEntry Add(byte[] path, CanonicalTreeParser p, int stage) { if (p != null && !p.EntryFileMode.Equals(FileMode.TREE)) { DirCacheEntry e = new DirCacheEntry(path, stage); e.FileMode = p.EntryFileMode; e.SetObjectId(p.EntryObjectId); builder.Add(e); return(e); } return(null); }
public virtual void TestRevert() { // B---P---T // // Revert P, this should result in a tree with a // from B and t from T as the change to a in P // and addition of t in P is reverted. // // We use the standard merge, but change the order // of the sources. // DirCache treeB = db.ReadDirCache(); DirCache treeP = db.ReadDirCache(); DirCache treeT = db.ReadDirCache(); { DirCacheBuilder b = treeB.Builder(); DirCacheBuilder p = treeP.Builder(); DirCacheBuilder t = treeT.Builder(); b.Add(MakeEntry("a", FileMode.REGULAR_FILE)); p.Add(MakeEntry("a", FileMode.REGULAR_FILE, "q")); p.Add(MakeEntry("p-fail", FileMode.REGULAR_FILE)); t.Add(MakeEntry("a", FileMode.REGULAR_FILE, "q")); t.Add(MakeEntry("p-fail", FileMode.REGULAR_FILE)); t.Add(MakeEntry("t", FileMode.REGULAR_FILE)); b.Finish(); p.Finish(); t.Finish(); } ObjectInserter ow = db.NewObjectInserter(); ObjectId B = Commit(ow, treeB, new ObjectId[] { }); ObjectId P = Commit(ow, treeP, new ObjectId[] { B }); ObjectId T = Commit(ow, treeT, new ObjectId[] { P }); ThreeWayMerger twm = ((ThreeWayMerger)MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.NewMerger (db)); twm.SetBase(P); bool merge = twm.Merge(new ObjectId[] { B, T }); NUnit.Framework.Assert.IsTrue(merge); TreeWalk tw = new TreeWalk(db); tw.Recursive = true; tw.Reset(twm.GetResultTreeId()); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("a", tw.PathString); AssertCorrectId(treeB, tw); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("t", tw.PathString); AssertCorrectId(treeT, tw); NUnit.Framework.Assert.IsFalse(tw.Next()); }
ObjectId WriteWorkingDirectoryTree(RevTree headTree, DirCache index) { DirCache dc = DirCache.NewInCore(); DirCacheBuilder cb = dc.Builder(); ObjectInserter oi = _repo.NewObjectInserter(); try { TreeWalk tw = new TreeWalk(_repo); tw.Reset(); tw.AddTree(new FileTreeIterator(_repo)); tw.AddTree(headTree); tw.AddTree(new DirCacheIterator(index)); while (tw.Next()) { // Ignore untracked files if (tw.IsSubtree) { tw.EnterSubtree(); } else if (tw.GetFileMode(0) != NGit.FileMode.MISSING && (tw.GetFileMode(1) != NGit.FileMode.MISSING || tw.GetFileMode(2) != NGit.FileMode.MISSING)) { WorkingTreeIterator f = tw.GetTree <WorkingTreeIterator>(0); DirCacheIterator dcIter = tw.GetTree <DirCacheIterator>(2); DirCacheEntry currentEntry = dcIter.GetDirCacheEntry(); DirCacheEntry ce = new DirCacheEntry(tw.PathString); if (!f.IsModified(currentEntry, true)) { ce.SetLength(currentEntry.Length); ce.LastModified = currentEntry.LastModified; ce.FileMode = currentEntry.FileMode; ce.SetObjectId(currentEntry.GetObjectId()); } else { long sz = f.GetEntryLength(); ce.SetLength(sz); ce.LastModified = f.GetEntryLastModified(); ce.FileMode = f.EntryFileMode; var data = f.OpenEntryStream(); try { ce.SetObjectId(oi.Insert(Constants.OBJ_BLOB, sz, data)); } finally { data.Close(); } } cb.Add(ce); } } cb.Finish(); return(dc.WriteTree(oi)); } finally { oi.Release(); } }
public virtual void TestTrivialTwoWay_validSubtreeSort() { DirCache treeB = db.ReadDirCache(); DirCache treeO = db.ReadDirCache(); DirCache treeT = db.ReadDirCache(); { DirCacheBuilder b = treeB.Builder(); DirCacheBuilder o = treeO.Builder(); DirCacheBuilder t = treeT.Builder(); b.Add(CreateEntry("libelf-po/a", FileMode.REGULAR_FILE)); b.Add(CreateEntry("libelf/c", FileMode.REGULAR_FILE)); o.Add(CreateEntry("Makefile", FileMode.REGULAR_FILE)); o.Add(CreateEntry("libelf-po/a", FileMode.REGULAR_FILE)); o.Add(CreateEntry("libelf/c", FileMode.REGULAR_FILE)); t.Add(CreateEntry("libelf-po/a", FileMode.REGULAR_FILE)); t.Add(CreateEntry("libelf/c", FileMode.REGULAR_FILE, "blah")); b.Finish(); o.Finish(); t.Finish(); } ObjectInserter ow = db.NewObjectInserter(); ObjectId b_1 = Commit(ow, treeB, new ObjectId[] { }); ObjectId o_1 = Commit(ow, treeO, new ObjectId[] { b_1 }); ObjectId t_1 = Commit(ow, treeT, new ObjectId[] { b_1 }); Merger ourMerger = ((ThreeWayMerger)MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.NewMerger (db)); bool merge = ourMerger.Merge(new ObjectId[] { o_1, t_1 }); NUnit.Framework.Assert.IsTrue(merge); TreeWalk tw = new TreeWalk(db); tw.Recursive = true; tw.Reset(ourMerger.GetResultTreeId()); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("Makefile", tw.PathString); AssertCorrectId(treeO, tw); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("libelf-po/a", tw.PathString); AssertCorrectId(treeO, tw); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("libelf/c", tw.PathString); AssertCorrectId(treeT, tw); NUnit.Framework.Assert.IsFalse(tw.Next()); }
public virtual void TestPathsResetWithUnmerged() { SetupRepository(); string file = "a.txt"; WriteTrashFile(file, "data"); git.Add().AddFilepattern(file).Call(); git.Commit().SetMessage("commit").Call(); DirCache index = db.LockDirCache(); DirCacheBuilder builder = index.Builder(); builder.Add(CreateEntry(file, FileMode.REGULAR_FILE, 1, string.Empty)); builder.Add(CreateEntry(file, FileMode.REGULAR_FILE, 2, string.Empty)); builder.Add(CreateEntry(file, FileMode.REGULAR_FILE, 3, string.Empty)); builder.Add(CreateEntry("b.txt", FileMode.REGULAR_FILE)); NUnit.Framework.Assert.IsTrue(builder.Commit()); NUnit.Framework.Assert.AreEqual("[a.txt, mode:100644, stage:1]" + "[a.txt, mode:100644, stage:2]" + "[a.txt, mode:100644, stage:3]" + "[b.txt, mode:100644]", IndexState(0)); git.Reset().AddPath(file).Call(); NUnit.Framework.Assert.AreEqual("[a.txt, mode:100644]" + "[b.txt, mode:100644]", IndexState(0)); }
public virtual void TestDF_DetectConflict() { DirCache tree0 = db.ReadDirCache(); DirCache tree1 = db.ReadDirCache(); { DirCacheBuilder b0 = tree0.Builder(); DirCacheBuilder b1 = tree1.Builder(); b0.Add(MakeEntry("0", REGULAR_FILE)); b0.Add(MakeEntry("a", REGULAR_FILE)); b1.Add(MakeEntry("0", REGULAR_FILE)); b1.Add(MakeEntry("a.b", REGULAR_FILE)); b1.Add(MakeEntry("a/b", REGULAR_FILE)); b1.Add(MakeEntry("a/c/e", REGULAR_FILE)); b0.Finish(); b1.Finish(); NUnit.Framework.Assert.AreEqual(2, tree0.GetEntryCount()); NUnit.Framework.Assert.AreEqual(4, tree1.GetEntryCount()); } NameConflictTreeWalk tw = new NameConflictTreeWalk(db); tw.AddTree(new DirCacheIterator(tree0)); tw.AddTree(new DirCacheIterator(tree1)); AssertModes("0", REGULAR_FILE, REGULAR_FILE, tw); NUnit.Framework.Assert.IsFalse(tw.IsDirectoryFileConflict()); AssertModes("a", REGULAR_FILE, TREE, tw); NUnit.Framework.Assert.IsTrue(tw.IsSubtree); NUnit.Framework.Assert.IsTrue(tw.IsDirectoryFileConflict()); tw.EnterSubtree(); AssertModes("a/b", MISSING, REGULAR_FILE, tw); NUnit.Framework.Assert.IsTrue(tw.IsDirectoryFileConflict()); AssertModes("a/c", MISSING, TREE, tw); NUnit.Framework.Assert.IsTrue(tw.IsDirectoryFileConflict()); tw.EnterSubtree(); AssertModes("a/c/e", MISSING, REGULAR_FILE, tw); NUnit.Framework.Assert.IsTrue(tw.IsDirectoryFileConflict()); AssertModes("a.b", MISSING, REGULAR_FILE, tw); NUnit.Framework.Assert.IsFalse(tw.IsDirectoryFileConflict()); }
/// <exception cref="System.Exception"></exception> internal CommitBuilder(TestRepository _enclosing, CommitBuilder prior) { this._enclosing = _enclosing; this.branch = prior.branch; DirCacheBuilder b = this.tree.Builder(); for (int i = 0; i < prior.tree.GetEntryCount(); i++) { b.Add(prior.tree.GetEntry(i)); } b.Finish(); this.parents.AddItem(prior.Create()); }
public virtual void TestMixedResetWithUnmerged() { git = new Git(db); string file = "a.txt"; WriteTrashFile(file, "data"); string file2 = "b.txt"; WriteTrashFile(file2, "data"); git.Add().AddFilepattern(file).AddFilepattern(file2).Call(); git.Commit().SetMessage("commit").Call(); DirCache index = db.LockDirCache(); DirCacheBuilder builder = index.Builder(); builder.Add(CreateEntry(file, FileMode.REGULAR_FILE, 1, string.Empty)); builder.Add(CreateEntry(file, FileMode.REGULAR_FILE, 2, string.Empty)); builder.Add(CreateEntry(file, FileMode.REGULAR_FILE, 3, string.Empty)); NUnit.Framework.Assert.IsTrue(builder.Commit()); NUnit.Framework.Assert.AreEqual("[a.txt, mode:100644, stage:1]" + "[a.txt, mode:100644, stage:2]" + "[a.txt, mode:100644, stage:3]", IndexState(0)); git.Reset().SetMode(ResetCommand.ResetType.MIXED).Call(); NUnit.Framework.Assert.AreEqual("[a.txt, mode:100644]" + "[b.txt, mode:100644]", IndexState(0)); }
/// <exception cref="System.IO.IOException"></exception> private DirCacheEntry AddEntryToBuilder(string path, FilePath file, ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage) { FileInputStream inputStream = new FileInputStream(file); ObjectId id = newObjectInserter.Insert(Constants.OBJ_BLOB, file.Length(), inputStream ); inputStream.Close(); DirCacheEntry entry = new DirCacheEntry(path, stage); entry.SetObjectId(id); entry.FileMode = FileMode.REGULAR_FILE; entry.LastModified = file.LastModified(); entry.SetLength((int)file.Length()); builder.Add(entry); return(entry); }
/// <exception cref="System.IO.IOException"></exception> private void ResetIndex(RevCommit commit) { DirCache dc = repo.LockDirCache(); TreeWalk walk = null; try { DirCacheBuilder builder = dc.Builder(); walk = new TreeWalk(repo); walk.AddTree(commit.Tree); walk.AddTree(new DirCacheIterator(dc)); walk.Recursive = true; while (walk.Next()) { AbstractTreeIterator cIter = walk.GetTree <AbstractTreeIterator>(0); if (cIter == null) { // Not in commit, don't add to new index continue; } DirCacheEntry entry = new DirCacheEntry(walk.RawPath); entry.FileMode = cIter.EntryFileMode; entry.SetObjectIdFromRaw(cIter.IdBuffer, cIter.IdOffset); DirCacheIterator dcIter = walk.GetTree <DirCacheIterator>(1); if (dcIter != null && dcIter.IdEqual(cIter)) { DirCacheEntry indexEntry = dcIter.GetDirCacheEntry(); entry.LastModified = indexEntry.LastModified; entry.SetLength(indexEntry.Length); } builder.Add(entry); } builder.Commit(); } finally { dc.Unlock(); if (walk != null) { walk.Release(); } } }
/// <exception cref="System.IO.IOException"></exception> private void Add(int tree, int stage) { AbstractTreeIterator i = GetTree(tree); if (i != null) { if (FileMode.TREE.Equals(tw.GetRawMode(tree))) { builder.AddTree(tw.RawPath, stage, reader, tw.GetObjectId(tree)); } else { DirCacheEntry e; e = new DirCacheEntry(tw.RawPath, stage); e.SetObjectIdFromRaw(i.IdBuffer, i.IdOffset); e.FileMode = tw.GetFileMode(tree); builder.Add(e); } } }
private void ResetIndexForPaths(RevCommit commit) { DirCache dc = null; try { dc = repo.LockDirCache(); DirCacheBuilder builder = dc.Builder(); TreeWalk tw = new TreeWalk(repo); tw.AddTree(new DirCacheBuildIterator(builder)); tw.AddTree(commit.Tree); tw.Filter = PathFilterGroup.CreateFromStrings(filepaths); tw.Recursive = true; while (tw.Next()) { CanonicalTreeParser tree = tw.GetTree <CanonicalTreeParser>(1); // only keep file in index if it's in the commit if (tree != null) { // revert index to commit DirCacheEntry entry = new DirCacheEntry(tw.RawPath); entry.FileMode = tree.EntryFileMode; entry.SetObjectId(tree.EntryObjectId); builder.Add(entry); } } builder.Commit(); } catch (IOException e) { throw new RuntimeException(e); } finally { if (dc != null) { dc.Unlock(); } } }
/// <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(); }
/// <summary> /// Executes the /// <code>Add</code> /// command. Each instance of this class should only /// be used for one invocation of the command. Don't call this method twice /// on an instance. /// </summary> /// <returns>the DirCache after Add</returns> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> /// <exception cref="NGit.Api.Errors.NoFilepatternException"></exception> public override DirCache Call() { if (filepatterns.IsEmpty()) { throw new NoFilepatternException(JGitText.Get().atLeastOnePatternIsRequired); } CheckCallable(); DirCache dc = null; bool addAll = false; if (filepatterns.Contains(".")) { addAll = true; } ObjectInserter inserter = repo.NewObjectInserter(); try { dc = repo.LockDirCache(); DirCacheIterator c; DirCacheBuilder builder = dc.Builder(); TreeWalk tw = new TreeWalk(repo); tw.AddTree(new DirCacheBuildIterator(builder)); if (workingTreeIterator == null) { workingTreeIterator = new FileTreeIterator(repo); } tw.AddTree(workingTreeIterator); tw.Recursive = true; if (!addAll) { tw.Filter = PathFilterGroup.CreateFromStrings(filepatterns); } string lastAddedFile = null; while (tw.Next()) { string path = tw.PathString; WorkingTreeIterator f = tw.GetTree <WorkingTreeIterator>(1); if (tw.GetTree <DirCacheIterator>(0) == null && f != null && f.IsEntryIgnored()) { } else { // file is not in index but is ignored, do nothing // In case of an existing merge conflict the // DirCacheBuildIterator iterates over all stages of // this path, we however want to add only one // new DirCacheEntry per path. if (!(path.Equals(lastAddedFile))) { if (!(update && tw.GetTree <DirCacheIterator>(0) == null)) { c = tw.GetTree <DirCacheIterator>(0); if (f != null) { // the file exists long sz = f.GetEntryLength(); DirCacheEntry entry = new DirCacheEntry(path); if (c == null || c.GetDirCacheEntry() == null || !c.GetDirCacheEntry().IsAssumeValid) { FileMode mode = f.GetIndexFileMode(c); entry.FileMode = mode; if (FileMode.GITLINK != mode) { entry.SetLength(sz); entry.LastModified = f.GetEntryLastModified(); long contentSize = f.GetEntryContentLength(); InputStream @in = f.OpenEntryStream(); try { entry.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, contentSize, @in)); } finally { @in.Close(); } } else { entry.SetObjectId(f.EntryObjectId); } builder.Add(entry); lastAddedFile = path; } else { builder.Add(c.GetDirCacheEntry()); } } else { if (c != null && (!update || FileMode.GITLINK == c.EntryFileMode)) { builder.Add(c.GetDirCacheEntry()); } } } } } } inserter.Flush(); builder.Commit(); SetCallable(false); } catch (IOException e) { throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfAddCommand , e); } finally { inserter.Release(); if (dc != null) { dc.Unlock(); } } return(dc); }
public virtual void TestFindObjects() { DirCache tree0 = DirCache.NewInCore(); DirCacheBuilder b0 = tree0.Builder(); ObjectReader or = db.NewObjectReader(); ObjectInserter oi = db.NewObjectInserter(); DirCacheEntry aDotB = MakeEntry("a.b", EXECUTABLE_FILE); b0.Add(aDotB); DirCacheEntry aSlashB = MakeEntry("a/b", REGULAR_FILE); b0.Add(aSlashB); DirCacheEntry aSlashCSlashD = MakeEntry("a/c/d", REGULAR_FILE); b0.Add(aSlashCSlashD); DirCacheEntry aZeroB = MakeEntry("a0b", SYMLINK); b0.Add(aZeroB); b0.Finish(); NUnit.Framework.Assert.AreEqual(4, tree0.GetEntryCount()); ObjectId tree = tree0.WriteTree(oi); // Find the directories that were implicitly created above. TreeWalk tw = new TreeWalk(or); tw.AddTree(tree); ObjectId a = null; ObjectId aSlashC = null; while (tw.Next()) { if (tw.PathString.Equals("a")) { a = tw.GetObjectId(0); tw.EnterSubtree(); while (tw.Next()) { if (tw.PathString.Equals("a/c")) { aSlashC = tw.GetObjectId(0); break; } } break; } } NUnit.Framework.Assert.AreEqual(a, TreeWalk.ForPath(or, "a", tree).GetObjectId(0) ); NUnit.Framework.Assert.AreEqual(a, TreeWalk.ForPath(or, "a/", tree).GetObjectId(0 )); NUnit.Framework.Assert.AreEqual(null, TreeWalk.ForPath(or, "/a", tree)); NUnit.Framework.Assert.AreEqual(null, TreeWalk.ForPath(or, "/a/", tree)); NUnit.Framework.Assert.AreEqual(aDotB.GetObjectId(), TreeWalk.ForPath(or, "a.b", tree).GetObjectId(0)); NUnit.Framework.Assert.AreEqual(null, TreeWalk.ForPath(or, "/a.b", tree)); NUnit.Framework.Assert.AreEqual(null, TreeWalk.ForPath(or, "/a.b/", tree)); NUnit.Framework.Assert.AreEqual(aDotB.GetObjectId(), TreeWalk.ForPath(or, "a.b/", tree).GetObjectId(0)); NUnit.Framework.Assert.AreEqual(aZeroB.GetObjectId(), TreeWalk.ForPath(or, "a0b", tree).GetObjectId(0)); NUnit.Framework.Assert.AreEqual(aSlashB.GetObjectId(), TreeWalk.ForPath(or, "a/b" , tree).GetObjectId(0)); NUnit.Framework.Assert.AreEqual(aSlashB.GetObjectId(), TreeWalk.ForPath(or, "b", a).GetObjectId(0)); NUnit.Framework.Assert.AreEqual(aSlashC, TreeWalk.ForPath(or, "a/c", tree).GetObjectId (0)); NUnit.Framework.Assert.AreEqual(aSlashC, TreeWalk.ForPath(or, "c", a).GetObjectId (0)); NUnit.Framework.Assert.AreEqual(aSlashCSlashD.GetObjectId(), TreeWalk.ForPath(or, "a/c/d", tree).GetObjectId(0)); NUnit.Framework.Assert.AreEqual(aSlashCSlashD.GetObjectId(), TreeWalk.ForPath(or, "c/d", a).GetObjectId(0)); or.Release(); oi.Release(); }
/// <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); }