FindBlobMember() public method

public FindBlobMember ( string blobName ) : TreeEntry
blobName string
return TreeEntry
Esempio 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);
        }
Esempio n. 2
0
 /// <summary>
 /// Find a Blob or Tree by traversing the tree along the given path. You can access not only direct children
 /// of the tree but any descendant of this tree.
 /// <para/>
 /// The path's directory seperators may be both forward or backslash, it is converted automatically to the internal representation.
 /// <para/>
 /// Throws IOException.
 /// </summary>
 /// <param name="path">Relative path to a file or directory in the git tree. For directories a trailing slash is allowed</param>
 /// <returns>A tree or blob object representing the referenced object</returns>
 public AbstractObject this[string path]
 {
     get
     {
         if (path == "")
         {
             return(this);
         }
         var tree_entry = _internal_tree.FindBlobMember(path);
         if (tree_entry == null)
         {
             tree_entry = _internal_tree.findTreeMember(path);
         }
         if (tree_entry == null)
         {
             return(null);
         }
         if (tree_entry.IsTree)
         {
             return(new Tree(_repo, tree_entry as CoreTree));
         }
         else if (tree_entry.IsBlob)
         {
             return(new Leaf(_repo, tree_entry as FileTreeEntry));
         }
         else                 // if (tree_entry.IsCommit || tree_entry.IsTag)
         {
             return(AbstractObject.Wrap(_repo, tree_entry.Id));
         }
     }
 }
Esempio n. 3
0
        private static bool HasParentBlob(Tree t, string name)
        {
            if (name.IndexOf('/') == -1)
            {
                return(false);
            }

            string parent = name.Slice(0, name.LastIndexOf('/'));

            return(t.FindBlobMember(parent) != null || HasParentBlob(t, parent));
        }
Esempio n. 4
0
        private void CheckoutTwoTrees()
        {
            foreach (string path in Removed)
            {
                _index.remove(_root, new FileInfo(Path.Combine(_root.FullName, path)));
            }

            foreach (KeyValuePair <string, ObjectId> entry in _updated)
            {
                GitIndex.Entry newEntry = _index.addEntry(_merge.FindBlobMember(entry.Key));
                _index.checkoutEntry(_root, newEntry);
            }
        }
Esempio 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);
         }
     }
 }
Esempio n. 6
0
        private static bool HasParentBlob(Tree t, string name)
        {
            if (name.IndexOf('/') == -1)
            {
                return false;
            }

            string parent = name.Slice(0, name.LastIndexOf('/'));
            return t.FindBlobMember(parent) != null || HasParentBlob(t, parent);
        }
Esempio n. 7
0
        public void test006_addDeepTree()
        {
            var t = new Tree(db);

            Tree e = t.AddTree("e");
            Assert.IsNotNull(e);
            Assert.IsTrue(e.Parent == t);
            Tree f = t.AddTree("f");
            Assert.IsNotNull(f);
            Assert.IsTrue(f.Parent == t);
            Tree g = f.AddTree("g");
            Assert.IsNotNull(g);
            Assert.IsTrue(g.Parent == f);
            Tree h = g.AddTree("h");
            Assert.IsNotNull(h);
            Assert.IsTrue(h.Parent == g);

            h.Id = SomeFakeId;
            Assert.IsTrue(!h.IsModified);
            g.Id = SomeFakeId;
            Assert.IsTrue(!g.IsModified);
            f.Id = SomeFakeId;
            Assert.IsTrue(!f.IsModified);
            e.Id = SomeFakeId;
            Assert.IsTrue(!e.IsModified);
            t.Id = SomeFakeId;
            Assert.IsTrue(!t.IsModified);

            Assert.AreEqual("f/g/h", h.FullName);
            Assert.IsTrue(t.findTreeMember(h.FullName) == h);
            Assert.IsTrue(t.FindBlobMember("f/z") == null);
            Assert.IsTrue(t.FindBlobMember("y/z") == null);

            FileTreeEntry i = h.AddFile("i");
            Assert.IsNotNull(i);
            Assert.AreEqual("f/g/h/i", i.FullName);
            Assert.IsTrue(t.FindBlobMember(i.FullName) == i);
            Assert.IsTrue(h.IsModified);
            Assert.IsTrue(g.IsModified);
            Assert.IsTrue(f.IsModified);
            Assert.IsTrue(!e.IsModified);
            Assert.IsTrue(t.IsModified);

            Assert.IsTrue(h.Id == null);
            Assert.IsTrue(g.Id == null);
            Assert.IsTrue(f.Id == null);
            Assert.IsTrue(e.Id != null);
            Assert.IsTrue(t.Id == null);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
 public void test001_createEmpty()
 {
     var t = new Tree(db);
     Assert.IsTrue(t.IsLoaded);
     Assert.IsTrue(t.IsModified);
     Assert.IsTrue(t.Parent == null);
     Assert.IsTrue(t.IsRoot);
     Assert.IsTrue(t.Name == null);
     Assert.IsTrue(t.NameUTF8 == null);
     Assert.IsTrue(t.Members != null);
     Assert.IsTrue(t.Members.Length == 0);
     Assert.AreEqual(string.Empty, t.FullName);
     Assert.IsTrue(t.Id == null);
     Assert.IsTrue(t.TreeEntry == t);
     Assert.IsTrue(t.Repository == db);
     Assert.IsTrue(t.findTreeMember("foo") == null);
     Assert.IsTrue(t.FindBlobMember("foo") == null);
 }
Esempio n. 10
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);
        }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }