/// <summary>Open a tree walk and filter to exactly one path.</summary> /// <remarks> /// Open a tree walk and filter to exactly one path. /// <p> /// The returned tree walk is already positioned on the requested path, so /// the caller should not need to invoke /// <see cref="Next()">Next()</see> /// unless they are /// looking for a possible directory/file name conflict. /// </remarks> /// <param name="reader">the reader the walker will obtain tree data from.</param> /// <param name="path">single path to advance the tree walk instance into.</param> /// <param name="trees">one or more trees to walk through, all with the same root.</param> /// <returns> /// a new tree walk configured for exactly this one path; null if no /// path was found in any of the trees. /// </returns> /// <exception cref="System.IO.IOException">reading a pack file or loose object failed. /// </exception> /// <exception cref="NGit.Errors.CorruptObjectException"> /// an tree object could not be read as its data stream did not /// appear to be a tree, or could not be inflated. /// </exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException">an object we expected to be a tree was not a tree. /// </exception> /// <exception cref="NGit.Errors.MissingObjectException">a tree object was not found. /// </exception> public static NGit.Treewalk.TreeWalk ForPath(NGit.ObjectReader reader, string path , params AnyObjectId[] trees) { NGit.Treewalk.TreeWalk tw = new NGit.Treewalk.TreeWalk(reader); PathFilter f = PathFilter.Create(path); tw.Filter = f; tw.Reset(trees); tw.Recursive = false; while (tw.Next()) { if (f.IsDone(tw)) { return(tw); } else { if (tw.IsSubtree) { tw.EnterSubtree(); } } } return(null); }
public override void Revert (FilePath[] localPaths, bool recurse, IProgressMonitor monitor) { foreach (var group in localPaths.GroupBy (f => GetRepository (f))) { var repository = group.Key; var files = group.ToArray (); var c = GetHeadCommit (repository); RevTree tree = c != null ? c.Tree : null; List<FilePath> changedFiles = new List<FilePath> (); List<FilePath> removedFiles = new List<FilePath> (); monitor.BeginTask (GettextCatalog.GetString ("Reverting files"), 3); monitor.BeginStepTask (GettextCatalog.GetString ("Reverting files"), files.Length, 2); DirCache dc = repository.LockDirCache (); DirCacheBuilder builder = dc.Builder (); try { HashSet<string> entriesToRemove = new HashSet<string> (); HashSet<string> foldersToRemove = new HashSet<string> (); // Add the new entries foreach (FilePath fp in files) { string p = repository.ToGitPath (fp); // Register entries to be removed from the index if (Directory.Exists (fp)) foldersToRemove.Add (p); else entriesToRemove.Add (p); TreeWalk tw = tree != null ? TreeWalk.ForPath (repository, p, tree) : null; if (tw == null) { // Removed from the index } else { // Add new entries TreeWalk r; if (tw.IsSubtree) { // It's a directory. Make sure we remove existing index entries of this directory foldersToRemove.Add (p); // We have to iterate through all folder files. We need a new iterator since the // existing rw is not recursive r = new NGit.Treewalk.TreeWalk(repository); r.Reset (tree); r.Filter = PathFilterGroup.CreateFromStrings(new string[]{p}); r.Recursive = true; r.Next (); } else { r = tw; } do { // There can be more than one entry if reverting a whole directory string rpath = repository.FromGitPath (r.PathString); DirCacheEntry e = new DirCacheEntry (r.PathString); e.SetObjectId (r.GetObjectId (0)); e.FileMode = r.GetFileMode (0); if (!Directory.Exists (Path.GetDirectoryName (rpath))) Directory.CreateDirectory (rpath); DirCacheCheckout.CheckoutEntry (repository, rpath, e); builder.Add (e); changedFiles.Add (rpath); } while (r.Next ()); } monitor.Step (1); } // Add entries we want to keep int count = dc.GetEntryCount (); for (int n=0; n<count; n++) { DirCacheEntry e = dc.GetEntry (n); string path = e.PathString; if (!entriesToRemove.Contains (path) && !foldersToRemove.Any (f => IsSubpath (f,path))) builder.Add (e); } builder.Commit (); } catch { dc.Unlock (); throw; } monitor.EndTask (); monitor.BeginTask (null, files.Length); foreach (FilePath p in changedFiles) { FileService.NotifyFileChanged (p); monitor.Step (1); } foreach (FilePath p in removedFiles) { FileService.NotifyFileRemoved (p); monitor.Step (1); } monitor.EndTask (); } }
/// <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 AssertEntry(string sha1string, string path, TreeWalk tw) { NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual(path, tw.PathString); NUnit.Framework.Assert.AreEqual(sha1string, tw.GetObjectId(1).GetName()); }
public virtual void TestMissingSubtree_DetectFileAdded_FileModified() { ObjectInserter inserter = db.NewObjectInserter(); ObjectId aFileId = inserter.Insert(Constants.OBJ_BLOB, Constants.Encode("a")); ObjectId bFileId = inserter.Insert(Constants.OBJ_BLOB, Constants.Encode("b")); ObjectId cFileId1 = inserter.Insert(Constants.OBJ_BLOB, Constants.Encode("c-1")); ObjectId cFileId2 = inserter.Insert(Constants.OBJ_BLOB, Constants.Encode("c-2")); // Create sub-a/empty, sub-c/empty = hello. ObjectId oldTree; { Tree root = new Tree(db); { Tree subA = root.AddTree("sub-a"); subA.AddFile("empty").SetId(aFileId); subA.SetId(inserter.Insert(Constants.OBJ_TREE, subA.Format())); } { Tree subC = root.AddTree("sub-c"); subC.AddFile("empty").SetId(cFileId1); subC.SetId(inserter.Insert(Constants.OBJ_TREE, subC.Format())); } oldTree = inserter.Insert(Constants.OBJ_TREE, root.Format()); } // Create sub-a/empty, sub-b/empty, sub-c/empty. ObjectId newTree; { Tree root = new Tree(db); { Tree subA = root.AddTree("sub-a"); subA.AddFile("empty").SetId(aFileId); subA.SetId(inserter.Insert(Constants.OBJ_TREE, subA.Format())); } { Tree subB = root.AddTree("sub-b"); subB.AddFile("empty").SetId(bFileId); subB.SetId(inserter.Insert(Constants.OBJ_TREE, subB.Format())); } { Tree subC = root.AddTree("sub-c"); subC.AddFile("empty").SetId(cFileId2); subC.SetId(inserter.Insert(Constants.OBJ_TREE, subC.Format())); } newTree = inserter.Insert(Constants.OBJ_TREE, root.Format()); } inserter.Flush(); inserter.Release(); TreeWalk tw = new TreeWalk(db); tw.Reset(oldTree, newTree); tw.Recursive = true; tw.Filter = TreeFilter.ANY_DIFF; NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("sub-b/empty", tw.PathString); NUnit.Framework.Assert.AreEqual(FileMode.MISSING, tw.GetFileMode(0)); NUnit.Framework.Assert.AreEqual(FileMode.REGULAR_FILE, tw.GetFileMode(1)); NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, tw.GetObjectId(0)); NUnit.Framework.Assert.AreEqual(bFileId, tw.GetObjectId(1)); NUnit.Framework.Assert.IsTrue(tw.Next()); NUnit.Framework.Assert.AreEqual("sub-c/empty", tw.PathString); NUnit.Framework.Assert.AreEqual(FileMode.REGULAR_FILE, tw.GetFileMode(0)); NUnit.Framework.Assert.AreEqual(FileMode.REGULAR_FILE, tw.GetFileMode(1)); NUnit.Framework.Assert.AreEqual(cFileId1, tw.GetObjectId(0)); NUnit.Framework.Assert.AreEqual(cFileId2, tw.GetObjectId(1)); NUnit.Framework.Assert.IsFalse(tw.Next()); }
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(); }