Exemplo n.º 1
0
        public virtual void Test007_manyFileLookup()
        {
            Tree t = new Tree(db);
            IList <FileTreeEntry> files = new AList <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);
                    NUnit.Framework.Assert.IsNotNull(f, "File " + n + " added.");
                    NUnit.Framework.Assert.AreEqual(n, f.GetName());
                    files.AddItem(f);
                }
            }
            NUnit.Framework.Assert.AreEqual(files.Count, t.MemberCount());
            TreeEntry[] ents = t.Members();
            NUnit.Framework.Assert.IsNotNull(ents);
            NUnit.Framework.Assert.AreEqual(files.Count, ents.Length);
            for (int k = 0; k < ents.Length; k++)
            {
                NUnit.Framework.Assert.IsTrue(files[k] == ents[k], "File " + files[k].GetName() +
                                              " is at " + k + ".");
            }
        }
Exemplo n.º 2
0
        public virtual void Test002_addFile()
        {
            Tree t = new Tree(db);

            t.SetId(SOME_FAKE_ID);
            NUnit.Framework.Assert.IsTrue(t.GetId() != null, "has id");
            NUnit.Framework.Assert.IsFalse(t.IsModified(), "not modified");
            string        n = "bob";
            FileTreeEntry f = t.AddFile(n);

            NUnit.Framework.Assert.IsNotNull(f, "have file");
            NUnit.Framework.Assert.AreEqual(n, f.GetName(), "name matches");
            NUnit.Framework.Assert.AreEqual(f.GetName(), Sharpen.Runtime.GetStringForBytes(f.
                                                                                           GetNameUTF8(), "UTF-8"), "name matches");
            NUnit.Framework.Assert.AreEqual(n, f.GetFullName(), "full name matches");
            NUnit.Framework.Assert.IsTrue(f.GetId() == null, "no id");
            NUnit.Framework.Assert.IsTrue(t.IsModified(), "is modified");
            NUnit.Framework.Assert.IsTrue(t.GetId() == null, "has no id");
            NUnit.Framework.Assert.IsTrue(t.FindBlobMember(f.GetName()) == f, "found bob");
            TreeEntry[] i = t.Members();
            NUnit.Framework.Assert.IsNotNull(i, "members array not null");
            NUnit.Framework.Assert.IsTrue(i != null && i.Length > 0, "iterator is not empty");
            NUnit.Framework.Assert.IsTrue(i != null && i[0] == f, "iterator returns file");
            NUnit.Framework.Assert.IsTrue(i != null && i.Length == 1, "iterator is empty");
        }
Exemplo n.º 3
0
        public virtual void Test005_addRecursiveFile()
        {
            Tree          t = new Tree(db);
            FileTreeEntry f = t.AddFile("a/b/c");

            NUnit.Framework.Assert.IsNotNull(f, "created f");
            NUnit.Framework.Assert.AreEqual("c", f.GetName());
            NUnit.Framework.Assert.AreEqual("b", f.GetParent().GetName());
            NUnit.Framework.Assert.AreEqual("a", f.GetParent().GetParent().GetName());
            NUnit.Framework.Assert.IsTrue(t == f.GetParent().GetParent().GetParent(), "t is great-grandparent"
                                          );
        }
Exemplo n.º 4
0
        public virtual void Test006_addDeepTree()
        {
            Tree t = new Tree(db);
            Tree e = t.AddTree("e");

            NUnit.Framework.Assert.IsNotNull(e, "have e");
            NUnit.Framework.Assert.IsTrue(e.GetParent() == t, "e.parent == t");
            Tree f = t.AddTree("f");

            NUnit.Framework.Assert.IsNotNull(f, "have f");
            NUnit.Framework.Assert.IsTrue(f.GetParent() == t, "f.parent == t");
            Tree g = f.AddTree("g");

            NUnit.Framework.Assert.IsNotNull(g, "have g");
            NUnit.Framework.Assert.IsTrue(g.GetParent() == f, "g.parent == f");
            Tree h = g.AddTree("h");

            NUnit.Framework.Assert.IsNotNull(h, "have h");
            NUnit.Framework.Assert.IsTrue(h.GetParent() == g, "h.parent = g");
            h.SetId(SOME_FAKE_ID);
            NUnit.Framework.Assert.IsTrue(!h.IsModified(), "h not modified");
            g.SetId(SOME_FAKE_ID);
            NUnit.Framework.Assert.IsTrue(!g.IsModified(), "g not modified");
            f.SetId(SOME_FAKE_ID);
            NUnit.Framework.Assert.IsTrue(!f.IsModified(), "f not modified");
            e.SetId(SOME_FAKE_ID);
            NUnit.Framework.Assert.IsTrue(!e.IsModified(), "e not modified");
            t.SetId(SOME_FAKE_ID);
            NUnit.Framework.Assert.IsTrue(!t.IsModified(), "t not modified.");
            NUnit.Framework.Assert.AreEqual("f/g/h", h.GetFullName(), "full path of h ok");
            NUnit.Framework.Assert.IsTrue(t.FindTreeMember(h.GetFullName()) == h, "Can find h"
                                          );
            NUnit.Framework.Assert.IsTrue(t.FindBlobMember("f/z") == null, "Can't find f/z");
            NUnit.Framework.Assert.IsTrue(t.FindBlobMember("y/z") == null, "Can't find y/z");
            FileTreeEntry i = h.AddFile("i");

            NUnit.Framework.Assert.IsNotNull(i);
            NUnit.Framework.Assert.AreEqual("f/g/h/i", i.GetFullName(), "full path of i ok");
            NUnit.Framework.Assert.IsTrue(t.FindBlobMember(i.GetFullName()) == i, "Can find i"
                                          );
            NUnit.Framework.Assert.IsTrue(h.IsModified(), "h modified");
            NUnit.Framework.Assert.IsTrue(g.IsModified(), "g modified");
            NUnit.Framework.Assert.IsTrue(f.IsModified(), "f modified");
            NUnit.Framework.Assert.IsTrue(!e.IsModified(), "e not modified");
            NUnit.Framework.Assert.IsTrue(t.IsModified(), "t modified");
            NUnit.Framework.Assert.IsTrue(h.GetId() == null, "h no id");
            NUnit.Framework.Assert.IsTrue(g.GetId() == null, "g no id");
            NUnit.Framework.Assert.IsTrue(f.GetId() == null, "f no id");
            NUnit.Framework.Assert.IsTrue(e.GetId() != null, "e has id");
            NUnit.Framework.Assert.IsTrue(t.GetId() == null, "t no id");
        }
Exemplo n.º 5
0
        public virtual void Test008_SubtreeInternalSorting()
        {
            Tree          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();
            NUnit.Framework.Assert.AreSame(e1, ents[0]);
            NUnit.Framework.Assert.AreSame(e0, ents[1]);
            NUnit.Framework.Assert.AreSame(e3, ents[2]);
            NUnit.Framework.Assert.AreSame(e4, ents[3]);
            NUnit.Framework.Assert.AreSame(e2, ents[4]);
        }
Exemplo n.º 6
0
		/// <exception cref="System.IO.IOException"></exception>
		public override void VisitFile(FileTreeEntry f)
		{
			FilePath path = new FilePath(GetCurrentDirectory(), f.GetName());
			FileInputStream @in = new FileInputStream(path);
			try
			{
				long sz = @in.GetChannel().Size();
				f.SetId(inserter.Insert(Constants.OBJ_BLOB, sz, @in));
				inserter.Flush();
			}
			finally
			{
				inserter.Release();
				@in.Close();
			}
		}
Exemplo n.º 7
0
        /// <summary>Adds a new or existing file with the specified name to this tree.</summary>
        /// <remarks>
        /// Adds a new or existing file with the specified name to this tree.
        /// Trees are added if necessary as the name may contain '/':s.
        /// </remarks>
        /// <param name="s">an array containing the name</param>
        /// <param name="offset">when the name starts in the tree.</param>
        /// <returns>
        /// a
        /// <see cref="FileTreeEntry">FileTreeEntry</see>
        /// for the added file.
        /// </returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public virtual FileTreeEntry AddFile(byte[] s, int offset)
        {
            int slash;
            int p;

            for (slash = offset; slash < s.Length && s[slash] != '/'; slash++)
            {
            }
            // search for path component terminator
            EnsureLoaded();
            byte xlast = slash < s.Length ? unchecked ((byte)(byte)('/')) : (byte)0;

            p = BinarySearch(contents, s, xlast, offset, slash);
            if (p >= 0 && slash < s.Length && contents[p] is NGit.Tree)
            {
                return(((NGit.Tree)contents[p]).AddFile(s, slash + 1));
            }
            byte[] newName = Substring(s, offset, slash);
            if (p >= 0)
            {
                throw new EntryExistsException(RawParseUtils.Decode(newName));
            }
            else
            {
                if (slash < s.Length)
                {
                    NGit.Tree t = new NGit.Tree(this, newName);
                    InsertEntry(p, t);
                    return(t.AddFile(s, slash + 1));
                }
                else
                {
                    FileTreeEntry f = new FileTreeEntry(this, null, newName, false);
                    InsertEntry(p, f);
                    return(f);
                }
            }
        }
        /// <summary>Construct and write tree out of index.</summary>
        /// <remarks>Construct and write tree out of index.</remarks>
        /// <returns>SHA-1 of the constructed tree</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public virtual ObjectId WriteTree()
        {
            CheckWriteOk();
            ObjectInserter inserter = db.NewObjectInserter();

            try
            {
                Tree         current = new Tree(db);
                Stack <Tree> trees   = new Stack <Tree>();
                trees.Push(current);
                string[] prevName = new string[0];
                foreach (GitIndex.Entry e in entries.Values)
                {
                    if (e.GetStage() != 0)
                    {
                        continue;
                    }
                    string[] newName = SplitDirPath(e.GetName());
                    int      c       = LongestCommonPath(prevName, newName);
                    while (c < trees.Count - 1)
                    {
                        current.SetId(inserter.Insert(Constants.OBJ_TREE, current.Format()));
                        trees.Pop();
                        current = trees.IsEmpty() ? null : (Tree)trees.Peek();
                    }
                    while (trees.Count < newName.Length)
                    {
                        if (!current.ExistsTree(newName[trees.Count - 1]))
                        {
                            current = new Tree(current, Constants.Encode(newName[trees.Count - 1]));
                            current.GetParent().AddEntry(current);
                            trees.Push(current);
                        }
                        else
                        {
                            current = (Tree)current.FindTreeMember(newName[trees.Count - 1]);
                            trees.Push(current);
                        }
                    }
                    FileTreeEntry ne = new FileTreeEntry(current, e.sha1, Constants.Encode(newName[newName
                                                                                                   .Length - 1]), (e.mode & FileMode.EXECUTABLE_FILE.GetBits()) == FileMode.EXECUTABLE_FILE
                                                         .GetBits());
                    current.AddEntry(ne);
                }
                while (!trees.IsEmpty())
                {
                    current.SetId(inserter.Insert(Constants.OBJ_TREE, current.Format()));
                    trees.Pop();
                    if (!trees.IsEmpty())
                    {
                        current = trees.Peek();
                    }
                }
                inserter.Flush();
                return(current.GetId());
            }
            finally
            {
                inserter.Release();
            }
        }
Exemplo n.º 9
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadTree(byte[] raw)
        {
            int rawSize = raw.Length;
            int rawPtr  = 0;

            TreeEntry[] temp;
            int         nextIndex = 0;

            while (rawPtr < rawSize)
            {
                while (rawPtr < rawSize && raw[rawPtr] != 0)
                {
                    rawPtr++;
                }
                rawPtr++;
                rawPtr += Constants.OBJECT_ID_LENGTH;
                nextIndex++;
            }
            temp      = new TreeEntry[nextIndex];
            rawPtr    = 0;
            nextIndex = 0;
            while (rawPtr < rawSize)
            {
                int c = raw[rawPtr++];
                if (c < '0' || c > '7')
                {
                    throw new CorruptObjectException(GetId(), JGitText.Get().corruptObjectInvalidEntryMode
                                                     );
                }
                int mode = c - '0';
                for (; ;)
                {
                    c = raw[rawPtr++];
                    if (' ' == c)
                    {
                        break;
                    }
                    else
                    {
                        if (c < '0' || c > '7')
                        {
                            throw new CorruptObjectException(GetId(), JGitText.Get().corruptObjectInvalidMode
                                                             );
                        }
                    }
                    mode <<= 3;
                    mode  += c - '0';
                }
                int nameLen = 0;
                while (raw[rawPtr + nameLen] != 0)
                {
                    nameLen++;
                }
                byte[] name = new byte[nameLen];
                System.Array.Copy(raw, rawPtr, name, 0, nameLen);
                rawPtr += nameLen + 1;
                ObjectId id = ObjectId.FromRaw(raw, rawPtr);
                rawPtr += Constants.OBJECT_ID_LENGTH;
                TreeEntry ent;
                if (FileMode.REGULAR_FILE.Equals(mode))
                {
                    ent = new FileTreeEntry(this, id, name, false);
                }
                else
                {
                    if (FileMode.EXECUTABLE_FILE.Equals(mode))
                    {
                        ent = new FileTreeEntry(this, id, name, true);
                    }
                    else
                    {
                        if (FileMode.TREE.Equals(mode))
                        {
                            ent = new NGit.Tree(this, id, name);
                        }
                        else
                        {
                            if (FileMode.SYMLINK.Equals(mode))
                            {
                                ent = new SymlinkTreeEntry(this, id, name);
                            }
                            else
                            {
                                if (FileMode.GITLINK.Equals(mode))
                                {
                                    ent = new GitlinkTreeEntry(this, id, name);
                                }
                                else
                                {
                                    throw new CorruptObjectException(GetId(), MessageFormat.Format(JGitText.Get().corruptObjectInvalidMode2
                                                                                                   , Sharpen.Extensions.ToOctalString(mode)));
                                }
                            }
                        }
                    }
                }
                temp[nextIndex++] = ent;
            }
            contents = temp;
        }
Exemplo n.º 10
0
		/// <exception cref="System.IO.IOException"></exception>
		private void ReadTree(byte[] raw)
		{
			int rawSize = raw.Length;
			int rawPtr = 0;
			TreeEntry[] temp;
			int nextIndex = 0;
			while (rawPtr < rawSize)
			{
				while (rawPtr < rawSize && raw[rawPtr] != 0)
				{
					rawPtr++;
				}
				rawPtr++;
				rawPtr += Constants.OBJECT_ID_LENGTH;
				nextIndex++;
			}
			temp = new TreeEntry[nextIndex];
			rawPtr = 0;
			nextIndex = 0;
			while (rawPtr < rawSize)
			{
				int c = raw[rawPtr++];
				if (c < '0' || c > '7')
				{
					throw new CorruptObjectException(GetId(), JGitText.Get().corruptObjectInvalidEntryMode
						);
				}
				int mode = c - '0';
				for (; ; )
				{
					c = raw[rawPtr++];
					if (' ' == c)
					{
						break;
					}
					else
					{
						if (c < '0' || c > '7')
						{
							throw new CorruptObjectException(GetId(), JGitText.Get().corruptObjectInvalidMode
								);
						}
					}
					mode <<= 3;
					mode += c - '0';
				}
				int nameLen = 0;
				while (raw[rawPtr + nameLen] != 0)
				{
					nameLen++;
				}
				byte[] name = new byte[nameLen];
				System.Array.Copy(raw, rawPtr, name, 0, nameLen);
				rawPtr += nameLen + 1;
				ObjectId id = ObjectId.FromRaw(raw, rawPtr);
				rawPtr += Constants.OBJECT_ID_LENGTH;
				TreeEntry ent;
				if (FileMode.REGULAR_FILE.Equals(mode))
				{
					ent = new FileTreeEntry(this, id, name, false);
				}
				else
				{
					if (FileMode.EXECUTABLE_FILE.Equals(mode))
					{
						ent = new FileTreeEntry(this, id, name, true);
					}
					else
					{
						if (FileMode.TREE.Equals(mode))
						{
							ent = new NGit.Tree(this, id, name);
						}
						else
						{
							if (FileMode.SYMLINK.Equals(mode))
							{
								ent = new SymlinkTreeEntry(this, id, name);
							}
							else
							{
								if (FileMode.GITLINK.Equals(mode))
								{
									ent = new GitlinkTreeEntry(this, id, name);
								}
								else
								{
									throw new CorruptObjectException(GetId(), MessageFormat.Format(JGitText.Get().corruptObjectInvalidMode2
										, Sharpen.Extensions.ToOctalString(mode)));
								}
							}
						}
					}
				}
				temp[nextIndex++] = ent;
			}
			contents = temp;
		}
Exemplo n.º 11
0
		/// <summary>Adds a new or existing file with the specified name to this tree.</summary>
		/// <remarks>
		/// Adds a new or existing file with the specified name to this tree.
		/// Trees are added if necessary as the name may contain '/':s.
		/// </remarks>
		/// <param name="s">an array containing the name</param>
		/// <param name="offset">when the name starts in the tree.</param>
		/// <returns>
		/// a
		/// <see cref="FileTreeEntry">FileTreeEntry</see>
		/// for the added file.
		/// </returns>
		/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
		public virtual FileTreeEntry AddFile(byte[] s, int offset)
		{
			int slash;
			int p;
			for (slash = offset; slash < s.Length && s[slash] != '/'; slash++)
			{
			}
			// search for path component terminator
			EnsureLoaded();
			byte xlast = slash < s.Length ? unchecked((byte)(byte)('/')) : (byte)0;
			p = BinarySearch(contents, s, xlast, offset, slash);
			if (p >= 0 && slash < s.Length && contents[p] is NGit.Tree)
			{
				return ((NGit.Tree)contents[p]).AddFile(s, slash + 1);
			}
			byte[] newName = Substring(s, offset, slash);
			if (p >= 0)
			{
				throw new EntryExistsException(RawParseUtils.Decode(newName));
			}
			else
			{
				if (slash < s.Length)
				{
					NGit.Tree t = new NGit.Tree(this, newName);
					InsertEntry(p, t);
					return t.AddFile(s, slash + 1);
				}
				else
				{
					FileTreeEntry f = new FileTreeEntry(this, null, newName, false);
					InsertEntry(p, f);
					return f;
				}
			}
		}
		public abstract void VisitFile(FileTreeEntry arg1);