Esempio n. 1
0
        public void testModified()
        {
            var index = new GitIndex(db);

            index.add(trash, writeTrashFile("file2", "file2"));
            index.add(trash, writeTrashFile("dir/file3", "dir/file3"));

            writeTrashFile("dir/file3", "changed");

            var t = new Tree(db);
            t.AddFile("file2").Id = ObjectId.FromString("0123456789012345678901234567890123456789");
            t.AddFile("dir/file3").Id = ObjectId.FromString("0123456789012345678901234567890123456789");
            Assert.AreEqual(2, t.MemberCount);

            var tree2 = (Tree) t.findTreeMember("dir");
            tree2.Id = new ObjectWriter(db).WriteTree(tree2);
            t.Id = new ObjectWriter(db).WriteTree(t);
            var diff = new IndexDiff(t, index);
            diff.Diff();
            Assert.AreEqual(2, diff.Changed.Count);
            Assert.IsTrue(diff.Changed.Contains("file2"));
            Assert.IsTrue(diff.Changed.Contains("dir/file3"));
            Assert.AreEqual(1, diff.Modified.Count);
            Assert.IsTrue(diff.Modified.Contains("dir/file3"));
            Assert.AreEqual(0, diff.Added.Count);
            Assert.AreEqual(0, diff.Removed.Count);
            Assert.AreEqual(0, diff.Missing.Count);
        }
Esempio n. 2
0
 internal WorkDirCheckout(Repository repo, DirectoryInfo workDir, GitIndex oldIndex, GitIndex newIndex)
     : this()
 {
     _repo = repo;
     _root = workDir;
     _index = oldIndex;
     _merge = repo.MapTree(newIndex.writeTree());
 }
Esempio n. 3
0
 ///	<summary>
 /// Create a checkout class for checking out one tree, merging with the index
 ///	</summary>
 ///	<param name="repo"> </param>
 ///	<param name="root"> workdir </param>
 ///	<param name="index"> current index </param>
 ///	<param name="merge"> tree to check out </param>
 public WorkDirCheckout(Repository repo, DirectoryInfo root, GitIndex index, Tree merge)
     : this()
 {
     this._repo = repo;
     this._root = root;
     this._index = index;
     this._merge = merge;
 }
 void IndexTreeVisitor.VisitEntry(TreeEntry treeEntry, TreeEntry auxEntry, GitIndex.Entry indexEntry, FileInfo file)
 {
     VisitEntryAuxDelegate handler = VisitEntryAux;
     if (handler != null)
     {
         handler(treeEntry, auxEntry, indexEntry, file);
     }
 }
Esempio n. 5
0
 public IndexTreeWalker(GitIndex index, Tree mainTree, Tree newTree, FileSystemInfo root,
                        IndexTreeVisitor visitor)
 {
     _mainTree = mainTree;
     _newTree = newTree;
     _root = root;
     _visitor = visitor;
     _threeTrees = newTree != null;
     _indexMembers = index.Members;
 }
Esempio n. 6
0
		/// <summary>Construct a walker for the index and one tree.</summary>
		/// <remarks>Construct a walker for the index and one tree.</remarks>
		/// <param name="index"></param>
		/// <param name="tree"></param>
		/// <param name="root"></param>
		/// <param name="visitor"></param>
		public IndexTreeWalker(GitIndex index, Tree tree, FilePath root, IndexTreeVisitor
			 visitor)
		{
			//import org.eclipse.jgit.JGitText;
			this.mainTree = tree;
			this.root = root;
			this.visitor = visitor;
			this.newTree = null;
			threeTrees = false;
			indexMembers = index.GetMembers();
		}
Esempio n. 7
0
        public IndexDiff(Tree tree, GitIndex index)
        {
            _anyChanges = false;
            _tree = tree;
            _index = index;

            Added = new HashSet<string>();
            Changed = new HashSet<string>();
            Removed = new HashSet<string>();
            Missing = new HashSet<string>();
            Modified = new HashSet<string>();
        }
Esempio n. 8
0
 public virtual void TestBoth()
 {
     GitIndex index = new GitIndex(db);
     Tree tree = new Tree(db);
     index.Add(trash, WriteTrashFile("a", "a"));
     tree.AddFile("b/b");
     index.Add(trash, WriteTrashFile("c", "c"));
     tree.AddFile("c");
     new IndexTreeWalker(index, tree, trash, new IndexTreeWalkerTest.TestIndexTreeVisitor
         (this)).Walk();
     NUnit.Framework.Assert.IsTrue(indexOnlyEntriesVisited.Contains("a"));
     NUnit.Framework.Assert.IsTrue(treeOnlyEntriesVisited.Contains("b/b"));
     NUnit.Framework.Assert.IsTrue(bothVisited.Contains("c"));
 }
Esempio n. 9
0
		public void ShouldSupportNotModifiedExtensionlessFilesWithoutContentChecking()
		{
			var index = new GitIndex(db);

			writeTrashFile("extensionless-file", "contents");

			var file = new FileInfo(Path.Combine(trash.FullName, "extensionless-file"));

			index.add(trash, file);

			var entry = index.GetEntry("extensionless-file");

			Assert.IsFalse(entry.IsModified(trash));
		}
Esempio n. 10
0
        public void testAdded2()
        {
            var index = new GitIndex(db);
            writeTrashFile("test/für henon.txt", "Weißebier");
            var tree = new Core.Tree(db);

            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "test/für henon.txt")));
            var diff = new IndexDiff(tree, index);
            diff.Diff();

            Assert.AreEqual(1, diff.Added.Count);
            Assert.IsTrue(diff.Added.Contains("test/für henon.txt"));
            Assert.AreEqual(0, diff.Changed.Count);
            Assert.AreEqual(0, diff.Modified.Count);
            Assert.AreEqual(0, diff.Removed.Count);
        }
Esempio n. 11
0
 private static int Compare(TreeEntry t, GitIndex.Entry i)
 {
     if ((t == null) && (i == null))
     {
         return 0;
     }
     if (t == null)
     {
         return 1;
     }
     if (i == null)
     {
         return -1;
     }
     return Tree.CompareNames(t.FullNameUTF8, i.NameUTF8, TreeEntry.LastChar(t), TreeEntry.LastChar(i));
 }
Esempio n. 12
0
        public void testAdded()
        {
            var index = new GitIndex(db);
            writeTrashFile("file1", "file1");
            writeTrashFile("dir/subfile", "dir/subfile");
            var tree = new Tree(db);

            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "file1")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "dir/subfile")));
            var diff = new IndexDiff(tree, index);
            diff.Diff();

            Assert.AreEqual(2, diff.Added.Count);
            Assert.IsTrue(diff.Added.Contains("file1"));
            Assert.IsTrue(diff.Added.Contains("dir/subfile"));
            Assert.AreEqual(0, diff.Changed.Count);
            Assert.AreEqual(0, diff.Modified.Count);
            Assert.AreEqual(0, diff.Removed.Count);
        }
Esempio n. 13
0
 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);
 }
Esempio n. 14
0
		public void ShouldAllowComparingOfAlreadyOpenedFile()
		{
			var index = new GitIndex(db);
			var file = writeTrashFile("extensionless-file", "contents");

			index.add(trash, file);

			var entry = index.GetEntry("extensionless-file");

			// [henon] failed on my windows box (originally only observed on mono/unix) when executed in resharper or with nunit without waiting a second!
			// as the timing is not the point of the test here let's wait a sec anyway.
			Thread.Sleep(TimeSpan.FromSeconds(1));

			// replace contents of file (with same size so it passes the size check)
			using (var writer = file.CreateText())
				writer.Write("stnetnoc");

			// opening the file for reading shoudn't block us from checking the contents
			using (file.OpenRead())
				Assert.IsTrue(entry.IsModified(trash, true));
		}
Esempio n. 15
0
 public virtual void TestCheckingOutWithConflicts()
 {
     GitIndex index = new GitIndex(db);
     index.Add(trash, WriteTrashFile("bar", "bar"));
     index.Add(trash, WriteTrashFile("foo/bar/baz/qux", "foo/bar"));
     RecursiveDelete(new FilePath(trash, "bar"));
     RecursiveDelete(new FilePath(trash, "foo"));
     WriteTrashFile("bar/baz/qux/foo", "another nasty one");
     WriteTrashFile("foo", "troublesome little bugger");
     try
     {
         WorkDirCheckout workDirCheckout = new WorkDirCheckout(db, trash, index, index);
         workDirCheckout.Checkout();
         NUnit.Framework.Assert.Fail("Should have thrown exception");
     }
     catch (NGit.Errors.CheckoutConflictException)
     {
     }
     // all is well
     WorkDirCheckout workDirCheckout_1 = new WorkDirCheckout(db, trash, index, index);
     workDirCheckout_1.SetFailOnConflict(false);
     workDirCheckout_1.Checkout();
     NUnit.Framework.Assert.IsTrue(new FilePath(trash, "bar").IsFile());
     NUnit.Framework.Assert.IsTrue(new FilePath(trash, "foo/bar/baz/qux").IsFile());
     GitIndex index2 = new GitIndex(db);
     RecursiveDelete(new FilePath(trash, "bar"));
     RecursiveDelete(new FilePath(trash, "foo"));
     index2.Add(trash, WriteTrashFile("bar/baz/qux/foo", "bar"));
     WriteTrashFile("bar/baz/qux/bar", "evil? I thought it said WEEVIL!");
     index2.Add(trash, WriteTrashFile("foo", "lalala"));
     workDirCheckout_1 = new WorkDirCheckout(db, trash, index2, index);
     workDirCheckout_1.SetFailOnConflict(false);
     workDirCheckout_1.Checkout();
     NUnit.Framework.Assert.IsTrue(new FilePath(trash, "bar").IsFile());
     NUnit.Framework.Assert.IsTrue(new FilePath(trash, "foo/bar/baz/qux").IsFile());
     NUnit.Framework.Assert.IsNotNull(index2.GetEntry("bar"));
     NUnit.Framework.Assert.IsNotNull(index2.GetEntry("foo/bar/baz/qux"));
     NUnit.Framework.Assert.IsNull(index2.GetEntry("bar/baz/qux/foo"));
     NUnit.Framework.Assert.IsNull(index2.GetEntry("foo"));
 }
Esempio n. 16
0
        public void testCheckingOutWithConflicts()
        {
            var index = new GitIndex(db);
            index.add(trash, writeTrashFile("bar", "bar"));
            index.add(trash, writeTrashFile("foo/bar/baz/qux", "foo/bar"));
            recursiveDelete(new FileInfo(Path.Combine(trash.FullName, "bar")));
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "foo")));
            writeTrashFile("bar/baz/qux/foo", "another nasty one");
            writeTrashFile("foo", "troublesome little bugger");
            
            var workDirCheckout1 = new WorkDirCheckout(db, trash, index, index);

            AssertHelper.Throws<CheckoutConflictException>(workDirCheckout1.checkout);


            var workDirCheckout2 = new WorkDirCheckout(db, trash, index, index) { FailOnConflict = false };
            workDirCheckout2.checkout();

            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "bar")).IsFile());
            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "foo/bar/baz/qux")).IsFile());

            var index2 = new GitIndex(db);
            recursiveDelete(new FileInfo(Path.Combine(trash.FullName, "bar")));
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "foo")));
            index2.add(trash, writeTrashFile("bar/baz/qux/foo", "bar"));
            writeTrashFile("bar/baz/qux/bar", "evil? I thought it said WEEVIL!");
            index2.add(trash, writeTrashFile("foo", "lalala"));

            workDirCheckout2 = new WorkDirCheckout(db, trash, index2, index) { FailOnConflict = false };
            workDirCheckout2.checkout();

            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "bar")).IsFile());
            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "foo/bar/baz/qux")).IsFile());
            Assert.IsNotNull(index2.GetEntry("bar"));
            Assert.IsNotNull(index2.GetEntry("foo/bar/baz/qux"));
            Assert.IsNull(index2.GetEntry("bar/baz/qux/foo"));
            Assert.IsNull(index2.GetEntry("foo"));
        }
Esempio n. 17
0
        public void ShouldAllowComparingOfAlreadyOpenedFile()
        {
            var index = new GitIndex(db);
            var file = writeTrashFile("extensionless-file", "contents");

            index.add(trash, file);

            var entry = index.GetEntry("extensionless-file");

            if (AssertHelper.IsRunningOnMono())
            {
                // File timestamps on Unix based systems are only precise to the second
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            // replace contents of file (with same size so it passes the size check)
            using (var writer = file.CreateText())
                writer.Write("stnetnoc");

            // opening the file for reading shoudn't block us from checking the contents
            using (file.OpenRead())
                Assert.IsTrue(entry.IsModified(trash, true));
        }
Esempio n. 18
0
        public void testWriteTree()
        {
            var index = new GitIndex(db);

            writeTrashFile("a/b", "data:a/b");
            writeTrashFile("a@b", "data:a:b");
            writeTrashFile("a.b", "data:a.b");
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b")));
            index.write();

            ObjectId id = index.writeTree();

            Assert.AreEqual("0036d433dc4f10ec47b61abc3ec5033c78d34f84", id.Name);

            writeTrashFile("a/b", "data:a/b");
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b")));

            if (CanRunGitStatus)
            {
                Assert.AreEqual(0, System(trash, "git status"));
            }
        }
Esempio n. 19
0
 void WriteTree(ObjectWriter writer, Core.Tree headTree, GitIndex index, Core.Tree tree, DirectoryInfo dir)
 {
     foreach (var fsi in dir.GetFileSystemInfos())
     {
         if (fsi is FileInfo)
         {
             // Exclude untracked files
             string gname   = _repo.ToGitPath(fsi.FullName);
             bool   inIndex = index.GetEntry(gname) != null;
             bool   inHead  = headTree.FindBlobMember(gname) != null;
             if (inIndex || inHead)
             {
                 var entry = tree.AddFile(fsi.Name);
                 entry.Id = writer.WriteBlob((FileInfo)fsi);
             }
         }
         else if (fsi.Name != Constants.DOT_GIT)
         {
             var child = tree.AddTree(fsi.Name);
             WriteTree(writer, headTree, index, child, (DirectoryInfo)fsi);
             child.Id = writer.WriteTree(child);
         }
     }
 }
Esempio n. 20
0
        public Commit CommitChanges(string message, Author author)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("Commit message must not be null or empty!", "message");
            }
            if (string.IsNullOrEmpty(author.Name))
            {
                throw new ArgumentException("Author name must not be null or empty!", "author");
            }
            GitIndex.RereadIfNecessary();
            var tree_id = GitIndex.writeTree();
            // check if tree is different from current commit's tree
            var parent = _repo.CurrentBranch.CurrentCommit;

            if ((parent == null && GitIndex.Members.Count == 0) || (parent != null && parent.Tree._id == tree_id))
            {
                throw new InvalidOperationException("There are no changes to commit");
            }
            var commit = Commit.Create(message, parent, new Tree(_repo, tree_id), author);

            Ref.Update("HEAD", commit);
            return(commit);
        }
        public void Refresh()
        {
            cache.Clear();
            if (!string.IsNullOrEmpty(initFolder))
            {
                try
                {
                    this.repository = Repository.Open(initFolder);

                    if (this.repository != null)
                    {
                        var id = repository.Resolve("HEAD");
                        var commit = repository.MapCommit(id);
                        this.commitTree = (commit != null ? commit.TreeEntry : new Tree(repository));
                        this.index = repository.Index;
                        this.index.RereadIfNecessary();
                        //this.ignoreHandler = new IgnoreHandler(repository);
                        //this.watcher = new FileSystemWatcher(this.repository.WorkingDirectory.FullName);
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
Esempio n. 22
0
 /// <summary>
 /// Writes the index to the disk.
 /// </summary>
 public void Write()
 {
     GitIndex.write();
 }
Esempio n. 23
0
		/// <summary>Helper for accessing tree/blob/index methods.</summary>
		/// <remarks>Helper for accessing tree/blob/index methods.</remarks>
		/// <param name="i"></param>
		/// <returns>'/' for Tree entries and NUL for non-treeish objects</returns>
		public static int LastChar(GitIndex.Entry i)
		{
			// FIXME, gitlink etc. Currently Trees cannot appear in the
			// index so '\0' is always returned, except maybe for submodules
			// which we do not support yet.
			return FileMode.TREE.Equals(i.GetModeBits()) ? '/' : '\0';
		}
Esempio n. 24
0
 // [henon] we do not publicly expose checking out into a custom directory, as this is an unrealistic use case and conflicts with checking out paths.
 // it is possible anyway by iterating over the Entries and writing the contents of each entry into a custom directory!
 private void Checkout(string directory)
 {
     GitIndex.RereadIfNecessary();
     GitIndex.checkout(new FileInfo(directory));
 }
Esempio n. 25
0
 public virtual void TestFindingConflicts()
 {
     GitIndex index = new GitIndex(db);
     index.Add(trash, WriteTrashFile("bar", "bar"));
     index.Add(trash, WriteTrashFile("foo/bar/baz/qux", "foo/bar"));
     RecursiveDelete(new FilePath(trash, "bar"));
     RecursiveDelete(new FilePath(trash, "foo"));
     WriteTrashFile("bar/baz/qux/foo", "another nasty one");
     WriteTrashFile("foo", "troublesome little bugger");
     WorkDirCheckout workDirCheckout = new WorkDirCheckout(db, trash, index, index);
     workDirCheckout.PrescanOneTree();
     IList<string> conflictingEntries = workDirCheckout.GetConflicts();
     IList<string> removedEntries = workDirCheckout.GetRemoved();
     NUnit.Framework.Assert.AreEqual("bar/baz/qux/foo", conflictingEntries[0]);
     NUnit.Framework.Assert.AreEqual("foo", conflictingEntries[1]);
     GitIndex index2 = new GitIndex(db);
     RecursiveDelete(new FilePath(trash, "bar"));
     RecursiveDelete(new FilePath(trash, "foo"));
     index2.Add(trash, WriteTrashFile("bar/baz/qux/foo", "bar"));
     index2.Add(trash, WriteTrashFile("foo", "lalala"));
     workDirCheckout = new WorkDirCheckout(db, trash, index2, index);
     workDirCheckout.PrescanOneTree();
     conflictingEntries = workDirCheckout.GetConflicts();
     removedEntries = workDirCheckout.GetRemoved();
     NUnit.Framework.Assert.IsTrue(conflictingEntries.IsEmpty());
     NUnit.Framework.Assert.IsTrue(removedEntries.Contains("bar/baz/qux/foo"));
     NUnit.Framework.Assert.IsTrue(removedEntries.Contains("foo"));
 }
Esempio n. 26
0
        private void ProcessEntry(TreeEntry h, TreeEntry m, GitIndex.Entry i)
        {
            ObjectId iId = (i == null ? null : i.ObjectId);
            ObjectId mId = (m == null ? null : m.Id);
            ObjectId hId = (h == null ? null : h.Id);

            string name = (i != null ? i.Name : (h != null ? h.FullName : m.FullName));

            if (i == null)
            {
                //                    
                //				    I (index)                H        M        Result
                //			        -------------------------------------------------------
                //			        0 nothing             nothing  nothing  (does not happen)
                //			        1 nothing             nothing  exists   use M
                //			        2 nothing             exists   nothing  remove path from index
                //			        3 nothing             exists   exists   use M 

                if (h == null)
                {
                    _updated.Add(name, mId);
                }
                else if (m == null)
                {
                    Removed.Add(name);
                }
                else
                {
                    _updated.Add(name, mId);
                }
            }
            else if (h == null)
            {
                //                    
                //					  clean I==H  I==M       H        M        Result
                //			         -----------------------------------------------------
                //			        4 yes   N/A   N/A     nothing  nothing  keep index
                //			        5 no    N/A   N/A     nothing  nothing  keep index
                //			
                //			        6 yes   N/A   yes     nothing  exists   keep index
                //			        7 no    N/A   yes     nothing  exists   keep index
                //			        8 yes   N/A   no      nothing  exists   fail
                //			        9 no    N/A   no      nothing  exists   fail       

                if (m == null || mId.Equals(iId))
                {
                    if (HasParentBlob(_merge, name))
                    {
                        if (i.IsModified(_root, true))
                        {
                            Conflicts.Add(name);
                        }
                        else
                        {
                            Removed.Add(name);
                        }
                    }
                }
                else
                {
                    Conflicts.Add(name);
                }
            }
            else if (m == null)
            {
                //                    
                //					10 yes   yes   N/A     exists   nothing  remove path from index
                //			        11 no    yes   N/A     exists   nothing  fail
                //			        12 yes   no    N/A     exists   nothing  fail
                //			        13 no    no    N/A     exists   nothing  fail
                //					 

                if (hId.Equals(iId))
                {
                    if (i.IsModified(_root, true))
                    {
                        Conflicts.Add(name);
                    }
                    else
                    {
                        Removed.Add(name);
                    }
                }
                else
                {
                    Conflicts.Add(name);
                }
            }
            else
            {
                if (!hId.Equals(mId) && !hId.Equals(iId) && !mId.Equals(iId))
                {
                    Conflicts.Add(name);
                }
                else if (hId.Equals(iId) && !mId.Equals(iId))
                {
                    if (i.IsModified(_root, true))
                    {
                        Conflicts.Add(name);
                    }
                    else
                    {
                        _updated.Add(name, mId);
                    }
                }
            }
        }
Esempio n. 27
0
            public override void VisitEntry(TreeEntry treeEntry, GitIndex.Entry indexEntry, FilePath
				 file)
            {
                if (treeEntry == null)
                {
                    this._enclosing.indexOnlyEntriesVisited.AddItem(indexEntry.GetName());
                }
                else
                {
                    if (indexEntry == null)
                    {
                        this._enclosing.treeOnlyEntriesVisited.AddItem(treeEntry.GetFullName());
                    }
                    else
                    {
                        this._enclosing.bothVisited.AddItem(indexEntry.GetName());
                    }
                }
            }
Esempio n. 28
0
 public virtual void TestIndexOnlySubDirs()
 {
     GitIndex index = new GitIndex(db);
     Tree tree = new Tree(db);
     index.Add(trash, WriteTrashFile("foo/bar/baz", "foobar"));
     index.Add(trash, WriteTrashFile("asdf", "asdf"));
     new IndexTreeWalker(index, tree, trash, new IndexTreeWalkerTest.TestIndexTreeVisitor
         (this)).Walk();
     NUnit.Framework.Assert.AreEqual("asdf", indexOnlyEntriesVisited[0]);
     NUnit.Framework.Assert.AreEqual("foo/bar/baz", indexOnlyEntriesVisited[1]);
 }
Esempio n. 29
0
        private void UpdateDirectoryNotRecursive(string path)
        {
            _index = Repository.Index.GitIndex;

            // Tree that will hold the working dir file entries
            var wtree = new Core.Tree(Repository._internal_repo);

            // Get a list of a leaves in the path

            Tree commitTree = null;
            var  commit     = Repository.Head.CurrentCommit;

            commitTree = commit != null ? commit.Tree : null;
            if (commitTree != null)
            {
                commitTree = commitTree[path] as Tree;
            }

            Dictionary <string, Leaf> commitEntries;

            if (commitTree != null)
            {
                commitEntries = commitTree.Leaves.ToDictionary(l => l.Path);
            }
            else
            {
                commitEntries = new Dictionary <string, Leaf> ();
            }

            HashSet <string> visited = new HashSet <string> ();

            // Compare commited files and working tree files

            DirectoryInfo dir = new DirectoryInfo(Repository.FromGitPath(path));

            if (dir.Exists)
            {
                foreach (FileInfo fileInfo in dir.GetFiles())
                {
                    string file = path + "/" + fileInfo.Name;

                    Leaf lf;
                    if (commitEntries.TryGetValue(file, out lf))
                    {
                        // Remove from the collection. At the end of the loop, entries still remaining in the
                        // collection will be processed as not having a corresponding working dir file
                        commitEntries.Remove(file);
                    }

                    TreeEntry wdirEntry = null;
                    if (!IgnoreHandler.IsIgnored(file) && fileInfo.Exists)
                    {
                        wdirEntry = wtree.AddFile(file);
                    }

                    GitIndex.Entry indexEntry = _index.GetEntry(file);

                    OnVisitEntry(lf != null ? lf.InternalEntry : null, wdirEntry, indexEntry, fileInfo);
                    visited.Add(file);
                }
            }

            // Now visit entries for which a working dir file was not found

            foreach (var lf in commitEntries)
            {
                string         file       = lf.Key;
                FileInfo       fileInfo   = new FileInfo(Repository.FromGitPath(file));
                GitIndex.Entry indexEntry = _index.GetEntry(file);
                OnVisitEntry(lf.Value.InternalEntry, null, indexEntry, fileInfo);
                visited.Add(file);
            }

            // Finally, visit remaining index entries which are not in the working dir nor in the commit

            foreach (var ie in _index.Members)
            {
                string file = ie.Name;
                // Exclude entries in subdirectories of _root_path
                int    i    = file.LastIndexOf('/');
                string fdir = i != -1 ? file.Substring(0, i) : string.Empty;
                if (fdir == _root_path && !visited.Contains(file))
                {
                    OnVisitEntry(null, null, ie, new FileInfo(Repository.FromGitPath(file)));
                }
            }
        }
Esempio n. 30
0
        internal void Apply(Stash stash)
        {
            Commit wip   = _repo.Get <Commit> (stash.CommitId);
            Commit index = wip.Parents.Last();

            Tree     wipTree          = wip.Tree;
            Tree     headTree         = _repo.CurrentBranch.CurrentCommit.Tree;
            GitIndex currentIndex     = _repo.Index.GitIndex;
            Tree     currentIndexTree = new Tree(_repo, _repo._internal_repo.MapTree(currentIndex.writeTree()));

            WorkDirCheckout co = new WorkDirCheckout(_repo._internal_repo, _repo._internal_repo.WorkingDirectory, headTree.InternalTree, currentIndex, wip.Tree.InternalTree);

            co.checkout();

            currentIndex.write();

            List <DirCacheEntry> toAdd = new List <DirCacheEntry> ();

            DirCache dc = DirCache.Lock(_repo._internal_repo);

            try {
                var cacheEditor = dc.editor();

                // The WorkDirCheckout class doesn't check if there are conflicts in modified files,
                // so we have to do it here.

                foreach (var c in co.Updated)
                {
                    var baseEntry   = wip.Parents.First().Tree[c.Key] as Leaf;
                    var oursEntry   = wipTree [c.Key] as Leaf;
                    var theirsEntry = headTree [c.Key] as Leaf;

                    if (baseEntry != null && oursEntry != null && currentIndexTree [c.Key] == null)
                    {
                        // If a file was reported as updated but that file is not present in the stashed index,
                        // it means that the file was scheduled to be deleted.
                        cacheEditor.@add(new DirCacheEditor.DeletePath(c.Key));
                        File.Delete(_repo.FromGitPath(c.Key));
                    }
                    else if (baseEntry != null && oursEntry != null && theirsEntry != null)
                    {
                        MergeResult    res = MergeAlgorithm.merge(new RawText(baseEntry.RawData), new RawText(oursEntry.RawData), new RawText(theirsEntry.RawData));
                        MergeFormatter f   = new MergeFormatter();
                        using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(_repo.FromGitPath(c.Key)))) {
                            f.formatMerge(bw, res, "Base", "Stash", "Head", Constants.CHARSET.WebName);
                        }
                        if (res.containsConflicts())
                        {
                            // Remove the entry from the index. It will be added later on.
                            cacheEditor.@add(new DirCacheEditor.DeletePath(c.Key));

                            // Generate index entries for each merge stage
                            // Those entries can't be added right now to the index because a DirCacheEditor
                            // can't be used at the same time as a DirCacheBuilder.
                            var e = new DirCacheEntry(c.Key, DirCacheEntry.STAGE_1);
                            e.setObjectId(baseEntry.InternalEntry.Id);
                            e.setFileMode(baseEntry.InternalEntry.Mode);
                            toAdd.Add(e);

                            e = new DirCacheEntry(c.Key, DirCacheEntry.STAGE_2);
                            e.setObjectId(oursEntry.InternalEntry.Id);
                            e.setFileMode(oursEntry.InternalEntry.Mode);
                            toAdd.Add(e);

                            e = new DirCacheEntry(c.Key, DirCacheEntry.STAGE_3);
                            e.setObjectId(theirsEntry.InternalEntry.Id);
                            e.setFileMode(theirsEntry.InternalEntry.Mode);
                            toAdd.Add(e);
                        }
                    }
                }

                cacheEditor.finish();

                if (toAdd.Count > 0)
                {
                    // Add the index entries generated above
                    var cacheBuilder = dc.builder();
                    for (int n = 0; n < dc.getEntryCount(); n++)
                    {
                        cacheBuilder.@add(dc.getEntry(n));
                    }
                    foreach (var entry in toAdd)
                    {
                        cacheBuilder.@add(entry);
                    }
                    cacheBuilder.finish();
                }

                dc.write();
                dc.commit();
            } catch {
                dc.unlock();
                throw;
            }
        }
Esempio n. 31
0
 ///	<summary>
 /// Create a checkout class for merging and checking our two trees and the index.
 ///	</summary>
 ///	<param name="repo"> </param>
 ///	<param name="root"> workdir </param>
 ///	<param name="head"> </param>
 ///	<param name="index"> </param>
 ///	<param name="merge"> </param>
 public WorkDirCheckout(Repository repo, DirectoryInfo root, Core.Tree head, GitIndex index, Core.Tree merge)
     : this(repo, root, index, merge)
 {
     this._head = head;
 }
Esempio n. 32
0
 public void testReadWithNoIndex()
 {
     var index = new GitIndex(db);
     index.Read();
     Assert.AreEqual(0, index.Members.Count);
 }
Esempio n. 33
0
 public virtual void TestLeavingTree()
 {
     GitIndex index = new GitIndex(db);
     index.Add(trash, WriteTrashFile("foo/bar", "foo/bar"));
     index.Add(trash, WriteTrashFile("foobar", "foobar"));
     new IndexTreeWalker(index, db.MapTree(index.WriteTree()), trash, new _AbstractIndexTreeVisitor_144
         ()).Walk();
 }
Esempio n. 34
0
        public void testUpdateSimpleSortTestIndex()
        {
            var index = new GitIndex(db);
            writeTrashFile("a/b", "data:a/b");
            writeTrashFile("a@b", "data:a:b");
            writeTrashFile("a.b", "data:a.b");
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b")));
            writeTrashFile("a/b", "data:a/b modified");
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b")));
            index.write();

            if (CanRunGitStatus)
            {
                Assert.AreEqual(0, System(trash, "git status"));
            }
        }
Esempio n. 35
0
            public override void VisitEntry(TreeEntry entry, GitIndex.Entry indexEntry, FilePath
				 f)
            {
                if (entry == null || indexEntry == null)
                {
                    NUnit.Framework.Assert.Fail();
                }
            }
Esempio n. 36
0
        public void testWriteTree()
        {
            var index = new GitIndex(db);
            writeTrashFile("a/b", "data:a/b");
            writeTrashFile("a@b", "data:a:b");
            writeTrashFile("a.b", "data:a.b");
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b")));
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b")));
            index.write();

            ObjectId id = index.writeTree();
            Assert.AreEqual("0036d433dc4f10ec47b61abc3ec5033c78d34f84", id.Name);

            writeTrashFile("a/b", "data:a/b");
            index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b")));

            if (CanRunGitStatus)
            {
                Assert.AreEqual(0, System(trash, "git status"));
            }
        }
Esempio n. 37
0
 public virtual void TestIndexOnlyOneLevel()
 {
     GitIndex index = new GitIndex(db);
     Tree tree = new Tree(db);
     index.Add(trash, WriteTrashFile("foo", "foo"));
     index.Add(trash, WriteTrashFile("bar", "bar"));
     new IndexTreeWalker(index, tree, trash, new IndexTreeWalkerTest.TestIndexTreeVisitor
         (this)).Walk();
     NUnit.Framework.Assert.IsTrue(indexOnlyEntriesVisited[0].Equals("bar"));
     NUnit.Framework.Assert.IsTrue(indexOnlyEntriesVisited[1].Equals("foo"));
 }
Esempio n. 38
0
 /// <summary>
 /// Reads the index from the disk
 /// </summary>
 public void Read()
 {
     GitIndex.Read();
 }