Exemple #1
0
        public void testCreateSimpleSortTestIndex()
        {
            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();

            Assert.AreEqual("a/b", index.GetEntry("a/b").Name);
            Assert.AreEqual("a@b", index.GetEntry("a@b").Name);
            Assert.AreEqual("a.b", index.GetEntry("a.b").Name);
            Assert.IsNull(index.GetEntry("a*b"));

            // Repeat test for re-Read index
            var indexr = new GitIndex(db);

            indexr.Read();
            Assert.AreEqual("a/b", indexr.GetEntry("a/b").Name);
            Assert.AreEqual("a@b", indexr.GetEntry("a@b").Name);
            Assert.AreEqual("a.b", indexr.GetEntry("a.b").Name);
            Assert.IsNull(indexr.GetEntry("a*b"));

            if (CanRunGitStatus)
            {
                Assert.AreEqual(0, System(trash, "git status"));
            }
        }
Exemple #2
0
        private void UpdateSingleFile(string file)
        {
            TreeEntry treeEntry = null;
            var       commit    = Repository.Head.CurrentCommit;

            _tree = commit != null ? commit.Tree : null;
            if (_tree != null)
            {
                treeEntry = _tree.FindBlobMember(file);
            }

            _index = Repository.Index.GitIndex;
            _index.RereadIfNecessary();
            GitIndex.Entry indexEntry = _index.GetEntry(file);

            TreeEntry wdirEntry = null;
            FileInfo  fileInfo  = new FileInfo(Path.Combine(Repository.WorkingDirectory, file.Replace('/', Path.DirectorySeparatorChar)));

            if (fileInfo.Exists && !IgnoreHandler.IsIgnored(file))
            {
                var tree = new Core.Tree(Repository._internal_repo);
                wdirEntry = tree.AddFile(file);
            }

            OnVisitEntry(treeEntry, wdirEntry, indexEntry, fileInfo);
        }
Exemple #3
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"));
        }
		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));
		}
Exemple #5
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));
        }
Exemple #6
0
 public Blob this[string path]
 {
     get
     {
         var e = GitIndex.GetEntry(path);
         if (e == null)
         {
             return(null);
         }
         return(new Blob(_repo, e.ObjectId));
     }
     set
     {
         //todo
     }
 }
Exemple #7
0
 /// <summary>
 /// Check out given paths from the index overwriting files in the working directory. Modified files might be overwritten.
 /// </summary>
 /// <param name="paths"></param>
 public void Checkout(params string[] paths)
 {
     GitIndex.RereadIfNecessary();
     foreach (var absolute_or_relative_path in paths)
     {
         string path = absolute_or_relative_path;
         if (Path.IsPathRooted(absolute_or_relative_path))
         {
             path = Core.Util.PathUtil.RelativePath(_repo.WorkingDirectory, absolute_or_relative_path);
         }
         var e = GitIndex.GetEntry(path);
         if (e == null)
         {
             continue;
         }
         GitIndex.checkoutEntry(new FileInfo(_repo.WorkingDirectory), e);
     }
 }
		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));
		}
        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));
        }
        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));
        }
Exemple #11
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));
        }
Exemple #12
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);
         }
     }
 }
 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"));
 }
Exemple #14
0
        public void testCreateSimpleSortTestIndex()
        {
            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();

            Assert.AreEqual("a/b", index.GetEntry("a/b").Name);
            Assert.AreEqual("a@b", index.GetEntry("a@b").Name);
            Assert.AreEqual("a.b", index.GetEntry("a.b").Name);
            Assert.IsNull(index.GetEntry("a*b"));

            // Repeat test for re-Read index
            var indexr = new GitIndex(db);
            indexr.Read();
            Assert.AreEqual("a/b", indexr.GetEntry("a/b").Name);
            Assert.AreEqual("a@b", indexr.GetEntry("a@b").Name);
            Assert.AreEqual("a.b", indexr.GetEntry("a.b").Name);
            Assert.IsNull(indexr.GetEntry("a*b"));

            if (CanRunGitStatus)
            {
                Assert.AreEqual(0, System(trash, "git status"));
            }
        }
        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"));
        }
Exemple #16
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)));
                }
            }
        }