AddFile() public method

Adds a new or existing file with the specified name to this tree. Trees are added if necessary as the name may contain '/':s.
public AddFile ( byte s, int offset ) : FileTreeEntry
s byte an array containing the name
offset int when the name starts in the tree. ///
return FileTreeEntry
Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
 public void testSimpleF1()
 {
     Tree tree = new Tree(db);
     tree.AddFile("x");
     TreeIterator i = MakeIterator(tree);
     Assert.IsTrue(i.hasNext());
     Assert.AreEqual("x", i.next().Name);
 }
Ejemplo n.º 3
0
 private void FillTree(DirectoryInfo dir, Core.Tree tree)
 {
     foreach (var subdir in dir.GetDirectories())
     {
         if (subdir.Name == Constants.DOT_GIT || IgnoreHandler.IsIgnored(tree.FullName + "/" + subdir.Name))
         {
             continue;
         }
         var t = tree.AddTree(subdir.Name);
         FillTree(subdir, t);
     }
     foreach (var file in dir.GetFiles())
     {
         if (IgnoreHandler.IsIgnored(tree.FullName + "/" + file.Name))
         {
             continue;
         }
         tree.AddFile((file.Name));
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a new or existing file with the specified name to this tree.
        ///	Trees are added if necessary as the name may contain '/':s.
        ///	</summary>
        ///	<param name="s"> an array containing the name </param>
        ///	<param name="offset"> when the name starts in the tree.
        ///	</param>
        ///	<returns>A <seealso cref="FileTreeEntry"/> for the added file.</returns>
        ///	<exception cref="IOException"></exception>
        public FileTreeEntry AddFile(byte[] s, int offset)
        {
            int slash;

            for (slash = offset; slash < s.Length && s[slash] != '/'; slash++)
            {
                // search for path component terminator
                // [henon] body is empty by intention!
            }

            EnsureLoaded();
            byte xlast = slash < s.Length ? (byte)'/' : (byte)0;
            int  p     = BinarySearch(_contents, s, xlast, offset, slash);

            if (p >= 0 && slash < s.Length && _contents[p] is Tree)
            {
                return(((Tree)_contents[p]).AddFile(s, slash + 1));
            }

            byte[] newName = SubString(s, offset, slash);
            if (p >= 0)
            {
                throw new EntryExistsException(RawParseUtils.decode(newName));
            }

            if (slash < s.Length)
            {
                Tree t = new Tree(this, newName);
                InsertEntry(p, t);
                return(t.AddFile(s, slash + 1));
            }

            FileTreeEntry f = new FileTreeEntry(this, null, newName, false);

            InsertEntry(p, f);
            return(f);
        }
Ejemplo n.º 5
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);
         }
     }
 }
Ejemplo n.º 6
0
 public void test005_addRecursiveFile()
 {
     var t = new Tree(db);
     FileTreeEntry f = t.AddFile("a/b/c");
     Assert.IsNotNull(f);
     Assert.AreEqual(f.Name, "c");
     Assert.AreEqual(f.Parent.Name, "b");
     Assert.AreEqual(f.Parent.Parent.Name, "a");
     Assert.IsTrue(t == f.Parent.Parent.Parent, "t is great-grandparent");
 }
Ejemplo n.º 7
0
        public void test002_addFile()
        {
            var t = new Tree(db) { Id = SomeFakeId };
            Assert.IsTrue(t.Id != null);
            Assert.IsFalse(t.IsModified);

            const string n = "bob";
            FileTreeEntry f = t.AddFile(n);
            Assert.IsNotNull(f);
            Assert.AreEqual(n, f.Name);
            Assert.AreEqual(f.Name, Constants.CHARSET.GetString(f.NameUTF8));
            Assert.AreEqual(n, f.FullName);
            Assert.IsTrue(f.Id == null);
            Assert.IsTrue(t.IsModified);
            Assert.IsTrue(t.Id == null);
            Assert.IsTrue(t.FindBlobMember(f.Name) == f);

            TreeEntry[] i = t.Members;
            Assert.IsNotNull(i);
            Assert.IsTrue(i != null && i.Length > 0);
            Assert.IsTrue(i != null && i[0] == f);
            Assert.IsTrue(i != null && i.Length == 1);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
		/// <summary>
		/// Adds a new or existing file with the specified name to this tree.
		///	Trees are added if necessary as the name may contain '/':s.
		///	</summary>
		///	<param name="s"> an array containing the name </param>
		///	<param name="offset"> when the name starts in the tree.
		///	</param>
		///	<returns>A <seealso cref="FileTreeEntry"/> for the added file.</returns>
		///	<exception cref="IOException"></exception>
		public FileTreeEntry AddFile(byte[] s, int offset)
		{
			int slash;

			for (slash = offset; slash < s.Length && s[slash] != '/'; slash++)
			{
				// search for path component terminator
				// [henon] body is empty by intention!
			}

			EnsureLoaded();
            byte xlast = slash < s.Length ? (byte)'/' : (byte)0;
			int p = BinarySearch(_contents, s, xlast, offset, slash);
			if (p >= 0 && slash < s.Length && _contents[p] is Tree)
			{
				return ((Tree)_contents[p]).AddFile(s, slash + 1);
			}

			byte[] newName = SubString(s, offset, slash);
			if (p >= 0)
			{
				throw new EntryExistsException(RawParseUtils.decode(newName));
			}

			if (slash < s.Length)
			{
                Tree t = new Tree(this, newName);
				InsertEntry(p, t);
				return t.AddFile(s, slash + 1);
			}

            FileTreeEntry f = new FileTreeEntry(this, null, newName, false);
			InsertEntry(p, f);
			return f;
		}
Ejemplo n.º 10
0
 private Tree BuildTree(Dictionary<string, string> headEntries)
 {
     var tree = new Tree(db);
     if (headEntries != null)
     {
         foreach (var pair in headEntries)
         {
             tree.AddFile(pair.Key).Id = GenSha1(pair.Value);
         }
     }
     return tree;
 }
Ejemplo n.º 11
0
        public void test012_SubtreeExternalSorting()
        {
            ObjectId emptyBlob = new ObjectWriter(db).WriteBlob(new byte[0]);
            var t = new Tree(db);
            FileTreeEntry e0 = t.AddFile("a-");
            FileTreeEntry e1 = t.AddFile("a-b");
            FileTreeEntry e2 = t.AddFile("a/b");
            FileTreeEntry e3 = t.AddFile("a=");
            FileTreeEntry e4 = t.AddFile("a=b");

            e0.Id = emptyBlob;
            e1.Id = emptyBlob;
            e2.Id = emptyBlob;
            e3.Id = emptyBlob;
            e4.Id = emptyBlob;

            t.Accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
            Assert.AreEqual(ObjectId.FromString("b47a8f0a4190f7572e11212769090523e23eb1ea"), t.Id);
        }
Ejemplo n.º 12
0
        public void Write_Simple_Commit()
        {
            var t = new Tree(db);
            FileTreeEntry f = t.AddFile("i-am-a-file");
            writeTrashFile(f.Name, "and this is the data in me\r\n\r\n");
            t.Accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
            //new ObjectChecker().checkBlob(Constants.CHARSET.GetString(db.OpenObject(t.TreeId).getBytes()).ToCharArray());

            string s = new Inspector(db).Inspect(t.Id);
            string s1 = Inspector.Inspect("Resources/single_file_commit", "16c0beaf7523eb3ef5df45bd42dd4fc6343de864");
            string s2 = Inspector.Inspect("Resources/single_file_commit", "917c130bd4fa5bf2df0c399dc1b03401860aa448");
            string s3 = Inspector.Inspect("Resources/single_file_commit", "95ea6a6859af6791464bd8b6de76ad5a6f9fad81");

            //tree 917c130bd4fa5bf2df0c399dc1b03401860aa448\nauthor henon <*****@*****.**> 1245946742 +0200\ncommitter henon <*****@*****.**> 1245946742 +0200\n\nA Commit\n"

            Assert.AreEqual(ObjectId.FromString("917c130bd4fa5bf2df0c399dc1b03401860aa448"), t.Id);

            var c = new Commit(db)
                        {
                            Author = (new PersonIdent("henon", "*****@*****.**", 1245946742000L, 2*60)),
                            Committer = (new PersonIdent("henon", "*****@*****.**", 1245946742000L, 2*60)),
                            Message = ("A Commit\n"),
                            TreeEntry = (t)
                        };
            Assert.AreEqual(t.TreeId, c.TreeId);
            c.Save();

            string s_c = new Inspector(db).Inspect(c.CommitId);
            ObjectId cmtid = ObjectId.FromString("16c0beaf7523eb3ef5df45bd42dd4fc6343de864");
            Assert.AreEqual(cmtid, c.CommitId);

            // Verify the commit we just wrote is in the correct format.
            //using (var xis = new XInputStream(new FileStream(db.ToFile(cmtid).FullName, System.IO.FileMode.Open, FileAccess.Read)))
            //{
            //    Assert.AreEqual(0x78, xis.ReadUInt8());
            //    Assert.AreEqual(0x9c, xis.ReadUInt8());
            //    Assert.IsTrue(0x789c % 31 == 0);
            //}

            // Verify we can Read it.
            Commit c2 = db.MapCommit(cmtid.ToString());
            Assert.IsNotNull(c2);
            Assert.AreEqual(c.Message, c2.Message);
            Assert.AreEqual(c.TreeId, c2.TreeId);
            Assert.AreEqual(c.Author, c2.Author);
            Assert.AreEqual(c.Committer, c2.Committer);
        }
Ejemplo n.º 13
0
 public void test007_manyFileLookup()
 {
     var t = new Tree(db);
     var files = new List<FileTreeEntry>(26 * 26);
     for (char level1 = 'a'; level1 <= 'z'; level1++)
     {
         for (char level2 = 'a'; level2 <= 'z'; level2++)
         {
             String n = "." + level1 + level2 + "9";
             FileTreeEntry f = t.AddFile(n);
             Assert.IsNotNull(f, "File " + n + " added.");
             Assert.AreEqual(n, f.Name);
             files.Add(f);
         }
     }
     Assert.AreEqual(files.Count, t.MemberCount);
     TreeEntry[] ents = t.Members;
     Assert.IsNotNull(ents);
     Assert.AreEqual(files.Count, ents.Length);
     for (int k = 0; k < ents.Length; k++)
     {
         Assert.IsTrue(files[k] == ents[k], "File " + files[k].Name + " is at " + k + ".");
     }
 }
Ejemplo n.º 14
0
        public void test003_WriteShouldBeEmptyTree()
        {
            Tree t = new Tree(db);
            ObjectId emptyId = new ObjectWriter(db).WriteBlob(new byte[0]);
            t.AddFile("should-be-empty").Id = emptyId;
            t.Accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
            Assert.AreEqual("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t.Id.Name);

            var o = new FileInfo(trash_git + "/objects/7b/b943559a305bdd6bdee2cef6e5df2413c3d30a");
            Assert.IsTrue(o.IsFile(), "Exists " + o);
            Assert.IsTrue(o.IsReadOnly, "Read-only " + o);

            o = new FileInfo(trash_git + "/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
            Assert.IsTrue(o.IsFile(), "Exists " + o);
            Assert.IsTrue(o.IsReadOnly, "Read-only " + o);
        }
Ejemplo n.º 15
0
        public void testTricky()
        {
            Tree tree = new Tree(db);
            tree.AddFile("a.b");
            tree.AddFile("a.c");
            tree.AddFile("a/b.b/b");
            tree.AddFile("a/b");
            tree.AddFile("a/c");
            tree.AddFile("a=c");
            tree.AddFile("a=d");

            TreeIterator i = MakeIterator(tree);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a.b", i.next().FullName);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a.c", i.next().FullName);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a/b", i.next().FullName);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a/b.b/b", i.next().FullName);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a/c", i.next().FullName);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a=c", i.next().FullName);
            Assert.IsTrue(i.hasNext());
            Assert.AreEqual("a=d", i.next().FullName);
            Assert.IsFalse(i.hasNext());
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Commit changes in the specified files and update HEAD
        /// </summary>
        /// <param name='message'>The commit message</param>
        /// <param name="author">The author of the content to be committed</param>
        /// <param name="paths">List of files to commit</param>
        /// <returns>Returns the newly created commit</returns>
        public Commit Commit(string message, Author author, params string[] paths)
        {
            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");
            }

            int basePathLength = Path.GetFullPath(WorkingDirectory).TrimEnd('/', '\\').Length;

            // Expand directory paths into file paths. Convert paths to full paths.
            List <string> filePaths = new List <string>();

            foreach (var path in paths)
            {
                string fullPath = path;
                if (!Path.IsPathRooted(fullPath))
                {
                    fullPath = Path.Combine(WorkingDirectory, fullPath);
                }
                fullPath = Path.GetFullPath(fullPath).TrimEnd('/', '\\');
                DirectoryInfo dir = new DirectoryInfo(fullPath);
                if (dir.Exists)
                {
                    filePaths.AddRange(GetDirectoryFiles(dir));
                }
                else
                {
                    filePaths.Add(fullPath);
                }
            }

            // Read the tree of the last commit. We are going to update it.
            GitSharp.Core.Tree tree = _internal_repo.MapTree(CurrentBranch.CurrentCommit._id);

            // Keep a list of trees that have been modified, since they have to be written.
            HashSet <GitSharp.Core.Tree> modifiedTrees = new HashSet <GitSharp.Core.Tree>();

            // Update the tree
            foreach (string fullPath in filePaths)
            {
                string    relPath   = fullPath.Substring(basePathLength + 1).Replace('\\', '/');
                TreeEntry treeEntry = tree.FindBlobMember(relPath);

                if (File.Exists(fullPath))
                {
                    // Looks like an old directory is now a file. Delete the subtree and create a new entry for the file.
                    if (treeEntry != null && !(treeEntry is FileTreeEntry))
                    {
                        treeEntry.Delete();
                    }

                    FileTreeEntry fileEntry  = treeEntry as FileTreeEntry;
                    var           writer     = new ObjectWriter(_internal_repo);
                    bool          executable = GitSharp.Core.Util.FS.canExecute(new FileInfo(fullPath));
                    ObjectId      id         = writer.WriteBlob(new FileInfo(fullPath));
                    if (fileEntry == null)
                    {
                        // It's a new file. Add it.
                        fileEntry = (FileTreeEntry)tree.AddFile(relPath);
                        treeEntry = fileEntry;
                    }
                    else if (fileEntry.Id == id && executable == fileEntry.IsExecutable)
                    {
                        // Same file, ignore it
                        continue;
                    }

                    fileEntry.Id = id;
                    fileEntry.SetExecutable(executable);
                }
                else
                {
                    // Deleted file or directory. Remove from the tree
                    if (treeEntry != null)
                    {
                        GitSharp.Core.Tree ptree = treeEntry.Parent;
                        treeEntry.Delete();
                        // Remove the subtree if it's now empty
                        while (ptree != null && ptree.MemberCount == 0)
                        {
                            GitSharp.Core.Tree nextParent = ptree.Parent;
                            ptree.Delete();
                            ptree = nextParent;
                        }
                    }
                    else
                    {
                        continue; // Already deleted.
                    }
                }
                modifiedTrees.Add(treeEntry.Parent);
            }

            // check if tree is different from current commit's tree
            if (modifiedTrees.Count == 0)
            {
                throw new InvalidOperationException("There are no changes to commit");
            }

            // Create new trees if there is any change
            ObjectId tree_id = SaveTree(tree, modifiedTrees);

            // Create the commit
            var parent = CurrentBranch.CurrentCommit;
            var commit = GitSharp.Commit.Create(message, parent, new Tree(this, tree_id), author);

            Ref.Update("HEAD", commit);

            // Unstage updated files
            Index.Unstage(paths);

            return(commit);
        }
Ejemplo n.º 17
0
        public void testBoth()
        {
            var index = new GitIndex(db);
            var mainTree = new Tree(db);

            index.add(trash, writeTrashFile("a", "a"));
            mainTree.AddFile("b/b");
            index.add(trash, writeTrashFile("c", "c"));
            mainTree.AddFile("c");

            new IndexTreeWalker(index, mainTree, trash, TestIndexTreeVisitor).Walk();
            Assert.IsTrue(IndexOnlyEntriesVisited.Contains("a"));
            Assert.IsTrue(TreeOnlyEntriesVisited.Contains("b/b"));
            Assert.IsTrue(BothVisited.Contains("c"));
        }
Ejemplo n.º 18
0
        public void testTreeOnlyOneLevel()
        {
            var index = new GitIndex(db);
            var mainTree = new Tree(db);
            mainTree.AddFile("foo");
            mainTree.AddFile("bar");

            new IndexTreeWalker(index, mainTree, trash, TestIndexTreeVisitor).Walk();

            Assert.IsTrue(TreeOnlyEntriesVisited[0].Equals("bar"));
            Assert.IsTrue(TreeOnlyEntriesVisited[1].Equals("foo"));
        }
Ejemplo n.º 19
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)));
                }
            }
        }
Ejemplo n.º 20
0
        public void test008_SubtreeInternalSorting()
        {
            var t = new Tree(db);
            FileTreeEntry e0 = t.AddFile("a-b");
            FileTreeEntry e1 = t.AddFile("a-");
            FileTreeEntry e2 = t.AddFile("a=b");
            Tree e3 = t.AddTree("a");
            FileTreeEntry e4 = t.AddFile("a=");

            TreeEntry[] ents = t.Members;
            Assert.AreSame(e1, ents[0]);
            Assert.AreSame(e0, ents[1]);
            Assert.AreSame(e3, ents[2]);
            Assert.AreSame(e4, ents[3]);
            Assert.AreSame(e2, ents[4]);
        }
Ejemplo n.º 21
0
        public void testRemoved()
        {
            var index = new GitIndex(db);
            writeTrashFile("file2", "file2");
            writeTrashFile("dir/file3", "dir/file3");

            var t = new Tree(db);
            t.AddFile("file2");
            t.AddFile("dir/file3");
            Assert.AreEqual(2, t.MemberCount);
            t.FindBlobMember("file2").Id = ObjectId.FromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad");
            var tree2 = (Tree) t.findTreeMember("dir");
            tree2.FindBlobMember("file3").Id = ObjectId.FromString("873fb8d667d05436d728c52b1d7a09528e6eb59b");
            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.Removed.Count);
            Assert.IsTrue(diff.Removed.Contains("file2"));
            Assert.IsTrue(diff.Removed.Contains("dir/file3"));
            Assert.AreEqual(0, diff.Changed.Count);
            Assert.AreEqual(0, diff.Modified.Count);
            Assert.AreEqual(0, diff.Added.Count);
        }
Ejemplo n.º 22
0
        public void test009_CreateCommitOldFormat()
        {
            writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=1\n");
            db.Config.load();
            Assert.AreEqual(true, db.Config.getBoolean("core", "legacyHeaders", false));

            var t = new Tree(db);
            FileTreeEntry f = t.AddFile("i-am-a-file");
            writeTrashFile(f.Name, "and this is the data in me\n");
            t.Accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);

            Assert.AreEqual(ObjectId.FromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"), t.TreeId);

            var c = new Commit(db)
                        {
                            Author = (new PersonIdent(jauthor, 1154236443000L, -4*60)),
                            Committer = (new PersonIdent(jcommitter, 1154236443000L, -4*60)),
                            Message = ("A Commit\n"),
                            TreeEntry = t
                        };
            Assert.AreEqual(t.TreeId, c.TreeId);
            c.Save();

            ObjectId cmtid = ObjectId.FromString("803aec4aba175e8ab1d666873c984c0308179099");
            Assert.AreEqual(cmtid, c.CommitId);

            // Verify the commit we just wrote is in the correct format.
            var xis = new XInputStream(new FileStream(db.ToFile(cmtid).FullName, System.IO.FileMode.Open, FileAccess.Read));
            try
            {
                Assert.AreEqual(0x78, xis.ReadUInt8());
                Assert.AreEqual(0x9c, xis.ReadUInt8());
                Assert.IsTrue(0x789c % 31 == 0);
            }
            finally
            {
                xis.Close();
            }

            // Verify we can Read it.
            Commit c2 = db.MapCommit(cmtid);
            Assert.IsNotNull(c2);
            Assert.AreEqual(c.Message, c2.Message);
            Assert.AreEqual(c.TreeId, c2.TreeId);
            Assert.AreEqual(c.Author, c2.Author);
            Assert.AreEqual(c.Committer, c2.Committer);
        }
Ejemplo n.º 23
0
        public void testUnchangedComplex()
        {
            var index = new GitIndex(db);
            index.add(trash, writeTrashFile("a.b", "a.b"));
            index.add(trash, writeTrashFile("a.c", "a.c"));
            index.add(trash, writeTrashFile("a/b.b/b", "a/b.b/b"));
            index.add(trash, writeTrashFile("a/b", "a/b"));
            index.add(trash, writeTrashFile("a/c", "a/c"));
            index.add(trash, writeTrashFile("a=c", "a=c"));
            index.add(trash, writeTrashFile("a=d", "a=d"));

            var t = new Tree(db);
            t.AddFile("a.b").Id = ObjectId.FromString("f6f28df96c2b40c951164286e08be7c38ec74851");
            t.AddFile("a.c").Id = ObjectId.FromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca");
            t.AddFile("a/b.b/b").Id = ObjectId.FromString("8d840bd4e2f3a48ff417c8e927d94996849933fd");
            t.AddFile("a/b").Id = ObjectId.FromString("db89c972fc57862eae378f45b74aca228037d415");
            t.AddFile("a/c").Id = ObjectId.FromString("52ad142a008aeb39694bafff8e8f1be75ed7f007");
            t.AddFile("a=c").Id = ObjectId.FromString("06022365ddbd7fb126761319633bf73517770714");
            t.AddFile("a=d").Id = ObjectId.FromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2");

            var tree2 = (Tree) t.findTreeMember("a/b.b");
            tree2.Id = new ObjectWriter(db).WriteTree(tree2);

            var tree3 = (Tree) t.findTreeMember("a");
            tree3.Id = new ObjectWriter(db).WriteTree(tree3);
            t.Id = new ObjectWriter(db).WriteTree(t);

            var diff = new IndexDiff(t, index);
            diff.Diff();

            Assert.AreEqual(0, diff.Changed.Count);
            Assert.AreEqual(0, diff.Added.Count);
            Assert.AreEqual(0, diff.Removed.Count);
            Assert.AreEqual(0, diff.Missing.Count);
            Assert.AreEqual(0, diff.Modified.Count);
        }
Ejemplo n.º 24
0
        public void test026_CreateCommitMultipleparents()
        {
            db.Config.load();

            var t = new Tree(db);
            FileTreeEntry f = t.AddFile("i-am-a-file");
            writeTrashFile(f.Name, "and this is the data in me\n");
            t.Accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
            Assert.AreEqual(ObjectId.FromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
                    t.TreeId);

            var c1 = new Commit(db)
                     	{
                     		Author = new PersonIdent(jauthor, 1154236443000L, -4*60),
                     		Committer = new PersonIdent(jcommitter, 1154236443000L, -4*60),
                     		Message = "A Commit\n",
                     		TreeEntry = t
                     	};

            Assert.AreEqual(t.TreeId, c1.TreeId);
            c1.Save();
            ObjectId cmtid1 = ObjectId.FromString("803aec4aba175e8ab1d666873c984c0308179099");
            Assert.AreEqual(cmtid1, c1.CommitId);

            var c2 = new Commit(db)
                     	{
                     		Author = new PersonIdent(jauthor, 1154236443000L, -4*60),
                     		Committer = new PersonIdent(jcommitter, 1154236443000L, -4*60),
                     		Message = "A Commit 2\n",
                     		TreeEntry = t
                     	};

            Assert.AreEqual(t.TreeId, c2.TreeId);
            c2.ParentIds = new[] { c1.CommitId };
            c2.Save();
            ObjectId cmtid2 = ObjectId.FromString("95d068687c91c5c044fb8c77c5154d5247901553");
            Assert.AreEqual(cmtid2, c2.CommitId);

            Commit rm2 = db.MapCommit(cmtid2);
            Assert.AreNotSame(c2, rm2); // assert the parsed objects is not from the cache
            Assert.AreEqual(c2.Author, rm2.Author);
            Assert.AreEqual(c2.CommitId, rm2.CommitId);
            Assert.AreEqual(c2.Message, rm2.Message);
            Assert.AreEqual(c2.TreeEntry.TreeId, rm2.TreeEntry.TreeId);
            Assert.AreEqual(1, rm2.ParentIds.Length);
            Assert.AreEqual(c1.CommitId, rm2.ParentIds[0]);

            var c3 = new Commit(db)
                     	{
                     		Author = new PersonIdent(jauthor, 1154236443000L, -4*60),
                     		Committer = new PersonIdent(jcommitter, 1154236443000L, -4*60),
                     		Message = "A Commit 3\n",
                     		TreeEntry = t
                     	};

            Assert.AreEqual(t.TreeId, c3.TreeId);
            c3.ParentIds = new[] { c1.CommitId, c2.CommitId };
            c3.Save();
            ObjectId cmtid3 = ObjectId.FromString("ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
            Assert.AreEqual(cmtid3, c3.CommitId);

            Commit rm3 = db.MapCommit(cmtid3);
            Assert.AreNotSame(c3, rm3); // assert the parsed objects is not from the cache
            Assert.AreEqual(c3.Author, rm3.Author);
            Assert.AreEqual(c3.CommitId, rm3.CommitId);
            Assert.AreEqual(c3.Message, rm3.Message);
            Assert.AreEqual(c3.TreeEntry.TreeId, rm3.TreeEntry.TreeId);
            Assert.AreEqual(2, rm3.ParentIds.Length);
            Assert.AreEqual(c1.CommitId, rm3.ParentIds[0]);
            Assert.AreEqual(c2.CommitId, rm3.ParentIds[1]);

            var c4 = new Commit(db)
                     	{
                     		Author = new PersonIdent(jauthor, 1154236443000L, -4*60),
                     		Committer = new PersonIdent(jcommitter, 1154236443000L, -4*60),
                     		Message = "A Commit 4\n",
                     		TreeEntry = t
                     	};

            Assert.AreEqual(t.TreeId, c3.TreeId);
            c4.ParentIds = new[] { c1.CommitId, c2.CommitId, c3.CommitId };
            c4.Save();
            ObjectId cmtid4 = ObjectId.FromString("d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
            Assert.AreEqual(cmtid4, c4.CommitId);

            Commit rm4 = db.MapCommit(cmtid4);
            Assert.AreNotSame(c4, rm3); // assert the parsed objects is not from the cache
            Assert.AreEqual(c4.Author, rm4.Author);
            Assert.AreEqual(c4.CommitId, rm4.CommitId);
            Assert.AreEqual(c4.Message, rm4.Message);
            Assert.AreEqual(c4.TreeEntry.TreeId, rm4.TreeEntry.TreeId);
            Assert.AreEqual(3, rm4.ParentIds.Length);
            Assert.AreEqual(c1.CommitId, rm4.ParentIds[0]);
            Assert.AreEqual(c2.CommitId, rm4.ParentIds[1]);
            Assert.AreEqual(c3.CommitId, rm4.ParentIds[2]);
        }
Ejemplo n.º 25
0
        public void testUnchangedSimple()
        {
            var index = new GitIndex(db);

            index.add(trash, writeTrashFile("a.b", "a.b"));
            index.add(trash, writeTrashFile("a.c", "a.c"));
            index.add(trash, writeTrashFile("a=c", "a=c"));
            index.add(trash, writeTrashFile("a=d", "a=d"));

            var t = new Tree(db);
            t.AddFile("a.b").Id = ObjectId.FromString("f6f28df96c2b40c951164286e08be7c38ec74851");
            t.AddFile("a.c").Id = ObjectId.FromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca");
            t.AddFile("a=c").Id = ObjectId.FromString("06022365ddbd7fb126761319633bf73517770714");
            t.AddFile("a=d").Id = ObjectId.FromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2");
            t.Id = new ObjectWriter(db).WriteTree(t);

            var diff = new IndexDiff(t, index);
            diff.Diff();

            Assert.AreEqual(0, diff.Changed.Count);
            Assert.AreEqual(0, diff.Added.Count);
            Assert.AreEqual(0, diff.Removed.Count);
            Assert.AreEqual(0, diff.Missing.Count);
            Assert.AreEqual(0, diff.Modified.Count);
        }
Ejemplo n.º 26
0
        public void Write_Tree()
        {
            var t = new Tree(db);
            FileTreeEntry f = t.AddFile("i-am-a-file");
            writeTrashFile(f.Name, "and this is the data in me\r\n\r\n");
            Assert.AreEqual(File.ReadAllText("Resources/single_file_commit/i-am-a-file"), File.ReadAllText(trash + "/i-am-a-file"));
            t.Accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
            var id = t.Id;

            var b1 = new BinaryReader(new Inspector(db).ContentStream(id));
            b1.BaseStream.Position = b1.BaseStream.Length - 21;

            var b2 = new BinaryReader(Inspector.ContentStream("Resources/single_file_commit", "917c130bd4fa5bf2df0c399dc1b03401860aa448"));
            b2.BaseStream.Position = b2.BaseStream.Length - 21;
            Assert.AreEqual(b2.ReadByte(), b1.ReadByte());

            var git_w1=b2.ReadInt32();
            var git_w2=b2.ReadInt32();
            var git_w3 = b2.ReadInt32();
            var git_w4 = b2.ReadInt32();
            var git_w5 = b2.ReadInt32();
            b2.BaseStream.Position = b2.BaseStream.Length-20;
            var git_id = ObjectId.FromRaw(b2.ReadBytes(20));
            var w1 = b1.ReadInt32();
            var w2= b1.ReadInt32();
            b1.Close();
            b2.Close();

            Assert.AreEqual(git_w1,w1);
            Assert.AreEqual(git_w2, w2);

            Assert.AreEqual("917c130bd4fa5bf2df0c399dc1b03401860aa448", id.ToString());
            var s_git = Inspector.Inspect("Resources/single_file_commit", "917c130bd4fa5bf2df0c399dc1b03401860aa448");
            var s = new Inspector(db).Inspect(id);
            Assert.AreEqual(s_git, s);
        }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
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)));
            }
        }
Ejemplo n.º 29
0
 public void testRules1thru3_NoIndexEntry()
 {
     var index = new GitIndex(db);
     var head = new Tree(db);
     FileTreeEntry entry = head.AddFile("foo");
     ObjectId expected = ObjectId.FromString("ba78e065e2c261d4f7b8f42107588051e87e18e9");
     entry.Id = expected;
     var merge = new Tree(db);
     var checkout = new WorkDirCheckout(db, trash, head, index, merge);
     checkout.PrescanTwoTrees();
     Assert.IsTrue(checkout.Removed.Contains("foo"));
     checkout = new WorkDirCheckout(db, trash, merge, index, head);
     checkout.PrescanTwoTrees();
     Assert.AreEqual(expected, checkout.Updated["foo"]);
     ObjectId id2 = ObjectId.FromString("ba78e065e2c261d4f7b8f42107588051e87e18ee");
     merge.AddFile("foo").Id = id2;
     checkout = new WorkDirCheckout(db, trash, head, index, merge);
     checkout.PrescanTwoTrees();
     Assert.AreEqual(id2, checkout.Updated["foo"]);
 }