Example #1
0
        public void testFindContainsGet()
        {
            RefList <global::GitSharp.Core.Ref> list = toList(REF_A, REF_B, REF_c);

            Assert.AreEqual(0, list.find("A"));
            Assert.AreEqual(1, list.find("B"));
            Assert.AreEqual(2, list.find("c"));

            Assert.AreEqual(-1, list.find("0"));
            Assert.AreEqual(-2, list.find("AB"));
            Assert.AreEqual(-3, list.find("a"));
            Assert.AreEqual(-4, list.find("z"));

            Assert.AreSame(REF_A, list.get("A"));
            Assert.AreSame(REF_B, list.get("B"));
            Assert.AreSame(REF_c, list.get("c"));
            Assert.IsNull(list.get("AB"));
            Assert.IsNull(list.get("z"));

            Assert.IsTrue(list.contains("A"));
            Assert.IsTrue(list.contains("B"));
            Assert.IsTrue(list.contains("c"));
            Assert.IsFalse(list.contains("AB"));
            Assert.IsFalse(list.contains("z"));
        }
Example #2
0
        public void testEmptyBuilder()
        {
            RefList <global::GitSharp.Core.Ref> list = new RefList <global::GitSharp.Core.Ref> .Builder <global::GitSharp.Core.Ref>().toRefList();

            Assert.AreEqual(0, list.size());
            Assert.IsFalse(list.iterator().hasNext());
            Assert.AreEqual(-1, list.find("a"));
            Assert.AreEqual(-1, list.find("z"));
            Assert.IsFalse(list.contains("a"));
            Assert.IsNull(list.get("a"));
            Assert.IsTrue(list.asList().Count == 0);
            Assert.AreEqual("[]", list.ToString());

            // default array capacity should be 16, with no bounds checking.
            Assert.IsNull(list.get(16 - 1));
            try
            {
                list.get(16);
                Assert.Fail("default RefList should have 16 element array");
            }
            catch (IndexOutOfRangeException)
            {
                // expected
            }
        }
Example #3
0
        public void testBuilder_AddAll()
        {
            var builder = new RefList <global::GitSharp.Core.Ref> .Builder <global::GitSharp.Core.Ref>(1);

            global::GitSharp.Core.Ref[] src = { REF_A, REF_B, REF_c, REF_A };
            builder.addAll(src, 1, 2);

            RefList <global::GitSharp.Core.Ref> list = builder.toRefList();

            Assert.AreEqual(2, list.size());
            Assert.AreSame(REF_B, list.get(0));
            Assert.AreSame(REF_c, list.get(1));
        }
Example #4
0
        public void testAddToEndOfList()
        {
            RefList <global::GitSharp.Core.Ref> one = toList(REF_A);
            RefList <global::GitSharp.Core.Ref> two = one.add(1, REF_B);

            Assert.AreNotSame(one, two);

            // one is not modified, but two is
            Assert.AreEqual(1, one.size());
            Assert.AreSame(REF_A, one.get(0));
            Assert.AreEqual(2, two.size());
            Assert.AreSame(REF_A, two.get(0));
            Assert.AreSame(REF_B, two.get(1));
        }
Example #5
0
        public void testRemoveEndOfList()
        {
            RefList <global::GitSharp.Core.Ref> one = toList(REF_A, REF_B, REF_c);
            RefList <global::GitSharp.Core.Ref> two = one.remove(2);

            Assert.AreNotSame(one, two);

            Assert.AreEqual(3, one.size());
            Assert.AreSame(REF_A, one.get(0));
            Assert.AreSame(REF_B, one.get(1));
            Assert.AreSame(REF_c, one.get(2));

            Assert.AreEqual(2, two.size());
            Assert.AreSame(REF_A, two.get(0));
            Assert.AreSame(REF_B, two.get(1));
        }
Example #6
0
        public void testCopyLeadingPrefix()
        {
            RefList <global::GitSharp.Core.Ref> one = toList(REF_A, REF_B, REF_c);
            RefList <global::GitSharp.Core.Ref> two = one.copy(2).toRefList();

            Assert.AreNotSame(one, two);

            Assert.AreEqual(3, one.size());
            Assert.AreSame(REF_A, one.get(0));
            Assert.AreSame(REF_B, one.get(1));
            Assert.AreSame(REF_c, one.get(2));

            Assert.AreEqual(2, two.size());
            Assert.AreSame(REF_A, two.get(0));
            Assert.AreSame(REF_B, two.get(1));
        }
Example #7
0
        public void testPutNewEntry()
        {
            RefList <global::GitSharp.Core.Ref> one = toList(REF_A, REF_c);
            RefList <global::GitSharp.Core.Ref> two = one.put(REF_B);

            Assert.AreNotSame(one, two);

            // one is not modified, but two is
            Assert.AreEqual(2, one.size());
            Assert.AreSame(REF_A, one.get(0));
            Assert.AreSame(REF_c, one.get(1));

            Assert.AreEqual(3, two.size());
            Assert.AreSame(REF_A, two.get(0));
            Assert.AreSame(REF_B, two.get(1));
            Assert.AreSame(REF_c, two.get(2));
        }
            public void scan(string prefix)
            {
                if (ALL.Equals(prefix))
                {
                    scanOne(Constants.HEAD);
                    scanTree(Constants.R_REFS, _refDirectory.refsDir);

                    // If any entries remain, they are deleted, drop them.
                    if (newLoose == null && curIdx < curLoose.size())
                    {
                        newLoose = curLoose.copy(curIdx);
                    }
                }
                else if (prefix.StartsWith(Constants.R_REFS) && prefix.EndsWith("/"))
                {
                    curIdx = -(curLoose.find(prefix) + 1);
                    DirectoryInfo dir = PathUtil.CombineDirectoryPath(_refDirectory.refsDir, prefix.Substring(Constants.R_REFS.Length));
                    scanTree(prefix, dir);

                    // Skip over entries still within the prefix; these have
                    // been removed from the directory.
                    while (curIdx < curLoose.size())
                    {
                        if (!curLoose.get(curIdx).getName().StartsWith(prefix))
                        {
                            break;
                        }
                        if (newLoose == null)
                        {
                            newLoose = curLoose.copy(curIdx);
                        }
                        curIdx++;
                    }

                    // Keep any entries outside of the prefix space, we
                    // do not know anything about their status.
                    if (newLoose != null)
                    {
                        while (curIdx < curLoose.size())
                        {
                            newLoose.add(curLoose.get(curIdx++));
                        }
                    }
                }
            }
Example #9
0
        public void testBuilder_AddThenSort()
        {
            var builder = new RefList <global::GitSharp.Core.Ref> .Builder <global::GitSharp.Core.Ref>(1);

            builder.add(REF_B);
            builder.add(REF_A);

            RefList <global::GitSharp.Core.Ref> list = builder.toRefList();

            Assert.AreEqual(2, list.size());
            Assert.AreSame(REF_B, list.get(0));
            Assert.AreSame(REF_A, list.get(1));

            builder.sort();
            list = builder.toRefList();
            Assert.AreEqual(2, list.size());
            Assert.AreSame(REF_A, list.get(0));
            Assert.AreSame(REF_B, list.get(1));
        }
Example #10
0
        public void testPutReplaceEntry()
        {
            global::GitSharp.Core.Ref otherc = newRef(REF_c.Name);
            Assert.AreNotSame(REF_c, otherc);

            RefList <global::GitSharp.Core.Ref> one = toList(REF_A, REF_c);
            RefList <global::GitSharp.Core.Ref> two = one.put(otherc);

            Assert.AreNotSame(one, two);

            // one is not modified, but two is
            Assert.AreEqual(2, one.size());
            Assert.AreSame(REF_A, one.get(0));
            Assert.AreSame(REF_c, one.get(1));

            Assert.AreEqual(2, two.size());
            Assert.AreSame(REF_A, two.get(0));
            Assert.AreSame(otherc, two.get(1));
        }
Example #11
0
        public void testAddToMiddleOfListByInsertionPosition()
        {
            RefList <global::GitSharp.Core.Ref> one = toList(REF_A, REF_c);

            Assert.AreEqual(-2, one.find(REF_B.Name));

            RefList <global::GitSharp.Core.Ref> two = one.add(one.find(REF_B.Name), REF_B);

            Assert.AreNotSame(one, two);

            // one is not modified, but two is
            Assert.AreEqual(2, one.size());
            Assert.AreSame(REF_A, one.get(0));
            Assert.AreSame(REF_c, one.get(1));

            Assert.AreEqual(3, two.size());
            Assert.AreSame(REF_A, two.get(0));
            Assert.AreSame(REF_B, two.get(1));
            Assert.AreSame(REF_c, two.get(2));
        }
Example #12
0
        public void testBuilder_Remove()
        {
            var builder = new RefList <global::GitSharp.Core.Ref> .Builder <global::GitSharp.Core.Ref>();

            builder.add(REF_A);
            builder.add(REF_B);
            builder.remove(0);

            Assert.AreEqual(1, builder.size());
            Assert.AreSame(REF_B, builder.get(0));
        }
Example #13
0
        public void testCopyConstructorReusesArray()
        {
            var one = new RefList <global::GitSharp.Core.Ref> .Builder <global::GitSharp.Core.Ref>();

            one.add(REF_A);

            var two = new RefList <global::GitSharp.Core.Ref>(one.toRefList());

            one.set(0, REF_B);
            Assert.AreSame(REF_B, two.get(0));
        }
Example #14
0
        private Ref readRef(string name, RefList <Ref> packed)
        {
            RefList <LooseRef> curList = looseRefs.get();
            int idx = curList.find(name);

            if (0 <= idx)
            {
                LooseRef o = curList.get(idx);
                LooseRef n = scanRef(o, name);
                if (n == null)
                {
                    if (looseRefs.compareAndSet(curList, curList.remove(idx)))
                    {
                        modCnt.incrementAndGet();
                    }
                    return(packed.get(name));
                }

                if (o == n)
                {
                    return(n);
                }
                if (looseRefs.compareAndSet(curList, curList.set(idx, n)))
                {
                    modCnt.incrementAndGet();
                }
                return(n);
            }

            LooseRef n2 = scanRef(null, name);

            if (n2 == null)
            {
                return(packed.get(name));
            }
            if (looseRefs.compareAndSet(curList, curList.add(idx, n2)))
            {
                modCnt.incrementAndGet();
            }
            return(n2);
        }
Example #15
0
        public void testEmpty()
        {
            RefList <global::GitSharp.Core.Ref> list = RefList <global::GitSharp.Core.Ref> .emptyList();

            Assert.AreEqual(0, list.size());
            Assert.IsTrue(list.isEmpty());
            Assert.IsFalse(list.iterator().hasNext());
            Assert.AreEqual(-1, list.find("a"));
            Assert.AreEqual(-1, list.find("z"));
            Assert.IsFalse(list.contains("a"));
            Assert.IsNull(list.get("a"));
            try
            {
                list.get(0);
                Assert.Fail("RefList.emptyList should have 0 element array");
            }
            catch (IndexOutOfRangeException)
            {
                // expected
            }
        }
Example #16
0
        public void testAddToEmptyList()
        {
            RefList <global::GitSharp.Core.Ref> one = toList();
            RefList <global::GitSharp.Core.Ref> two = one.add(0, REF_B);

            Assert.AreNotSame(one, two);

            // one is not modified, but two is
            Assert.AreEqual(0, one.size());
            Assert.AreEqual(1, two.size());
            Assert.IsFalse(two.isEmpty());
            Assert.AreSame(REF_B, two.get(0));
        }
Example #17
0
        private Ref resolve(Ref @ref, int depth, string prefix,
                            RefList <LooseRef> loose, RefList <Ref> packed)
        {
            if (@ref.isSymbolic())
            {
                Ref dst = @ref.getTarget();

                if (MAX_SYMBOLIC_REF_DEPTH <= depth)
                {
                    return(null); // claim it doesn't exist
                }
                // If the cached value can be assumed to be current due to a
                // recent scan of the loose directory, use it.
                if (loose != null && dst.getName().StartsWith(prefix))
                {
                    int idx;
                    if (0 <= (idx = loose.find(dst.getName())))
                    {
                        dst = loose.get(idx);
                    }
                    else if (0 <= (idx = packed.find(dst.getName())))
                    {
                        dst = packed.get(idx);
                    }
                    else
                    {
                        return(@ref);
                    }
                }
                else
                {
                    dst = readRef(dst.getName(), packed);
                    if (dst == null)
                    {
                        return(@ref);
                    }
                }

                dst = resolve(dst, depth + 1, prefix, loose, packed);
                if (dst == null)
                {
                    return(null);
                }
                return(new SymbolicRef(@ref.getName(), dst));
            }
            return(@ref);
        }
Example #18
0
        public override IDictionary <string, Ref> getRefs(string prefix)
        {
            RefList <Ref>      packed   = getPackedRefs();
            RefList <LooseRef> oldLoose = looseRefs.get();

            var scan = new LooseScanner(oldLoose, this);

            scan.scan(prefix);

            RefList <LooseRef> loose;

            if (scan.newLoose != null)
            {
                loose = scan.newLoose.toRefList();
                if (looseRefs.compareAndSet(oldLoose, loose))
                {
                    modCnt.incrementAndGet();
                }
            }
            else
            {
                loose = oldLoose;
            }
            fireRefsChanged();

            RefList <Ref> .Builder <Ref> symbolic = scan.symbolic;
            for (int idx = 0; idx < symbolic.size();)
            {
                Ref @ref = symbolic.get(idx);
                @ref = resolve(@ref, 0, prefix, loose, packed);
                if (@ref != null && @ref.getObjectId() != null)
                {
                    symbolic.set(idx, @ref);
                    idx++;
                }
                else
                {
                    // A broken symbolic reference, we have to drop it from the
                    // collections the client is about to receive. Should be a
                    // rare occurrence so pay a copy penalty.
                    loose = loose.remove(idx);
                    symbolic.remove(idx);
                }
            }

            return(new RefMap(prefix, packed, upcast(loose), symbolic.toRefList()));
        }
Example #19
0
        public override Ref peel(Ref @ref)
        {
            Ref leaf = @ref.getLeaf();

            if (leaf.isPeeled() || leaf.getObjectId() == null)
            {
                return(@ref);
            }

            RevWalk.RevWalk rw  = new RevWalk.RevWalk(getRepository());
            RevObject       obj = rw.parseAny(leaf.getObjectId());
            ObjectIdRef     newLeaf;

            if (obj is RevTag)
            {
                do
                {
                    obj = rw.parseAny(((RevTag)obj).getObject());
                } while (obj is RevTag);

                newLeaf = new PeeledTag(leaf.getStorage(), leaf
                                        .getName(), leaf.getObjectId(), obj.Copy());
            }
            else
            {
                newLeaf = new PeeledNonTag(leaf.getStorage(), leaf
                                           .getName(), leaf.getObjectId());
            }

            // Try to remember this peeling in the cache, so we don't have to do
            // it again in the future, but only if the reference is unchanged.
            if (leaf.getStorage().IsLoose)
            {
                RefList <LooseRef> curList = looseRefs.get();
                int idx = curList.find(leaf.getName());
                if (0 <= idx && curList.get(idx) == leaf)
                {
                    LooseRef           asPeeled = ((LooseRef)leaf).peel(newLeaf);
                    RefList <LooseRef> newList  = curList.set(idx, asPeeled);
                    looseRefs.compareAndSet(curList, newList);
                }
            }

            return(recreate(@ref, newLeaf));
        }
Example #20
0
        public void testPut_WithPrefix()
        {
            global::GitSharp.Core.Ref refA_one = newRef("refs/heads/A", ID_ONE);
            global::GitSharp.Core.Ref refA_two = newRef("refs/heads/A", ID_TWO);

            packed = toList(refA_one);

            RefMap map = new RefMap("refs/heads/", packed, loose, resolved);

            Assert.AreSame(refA_one, map.get("A"));
            Assert.AreSame(refA_one, map.put("A", refA_two));

            // map changed, but packed, loose did not
            Assert.AreSame(refA_two, map.get("A"));
            Assert.AreSame(refA_one, packed.get(0));
            Assert.AreEqual(0, loose.size());

            Assert.AreSame(refA_two, map.put("A", refA_one));
            Assert.AreSame(refA_one, map.get("A"));
        }
Example #21
0
        public void testIterable()
        {
            RefList <global::GitSharp.Core.Ref> list = toList(REF_A, REF_B, REF_c);

            int idx = 0;

            foreach (global::GitSharp.Core.Ref @ref in list)
            {
                Assert.AreSame(list.get(idx++), @ref);
            }
            Assert.AreEqual(3, idx);

            var i = RefList <global::GitSharp.Core.Ref> .emptyList().iterator();

            try
            {
                i.next();
                Assert.Fail("did not throw NoSuchElementException");
            }
            catch (IndexOutOfRangeException)
            {
                // expected
            }

            i = list.iterator();
            Assert.IsTrue(i.hasNext());
            Assert.AreSame(REF_A, i.next());
            try
            {
                i.remove();
                Assert.Fail("did not throw UnsupportedOperationException");
            }
            catch (NotSupportedException)
            {
                // expected
            }
        }
Example #22
0
        public override bool isNameConflicting(string name)
        {
            RefList <Ref>      packed = getPackedRefs();
            RefList <LooseRef> loose  = getLooseRefs();

            // Cannot be nested within an existing reference.
            int lastSlash = name.LastIndexOf('/');

            while (0 < lastSlash)
            {
                string needle = name.Slice(0, lastSlash);
                if (loose.contains(needle) || packed.contains(needle))
                {
                    return(true);
                }
                lastSlash = name.LastIndexOf('/', lastSlash - 1);
            }

            // Cannot be the container of an existing reference.
            string prefix = name + '/';
            int    idx;

            idx = -(packed.find(prefix) + 1);
            if (idx < packed.size() && packed.get(idx).getName().StartsWith(prefix))
            {
                return(true);
            }

            idx = -(loose.find(prefix) + 1);
            if (idx < loose.size() && loose.get(idx).getName().StartsWith(prefix))
            {
                return(true);
            }

            return(false);
        }
Example #23
0
        public void testPut_WithPrefix()
        {
            global::GitSharp.Core.Ref refA_one = newRef("refs/heads/A", ID_ONE);
            global::GitSharp.Core.Ref refA_two = newRef("refs/heads/A", ID_TWO);

            packed = toList(refA_one);

            RefMap map = new RefMap("refs/heads/", packed, loose, resolved);
            Assert.AreSame(refA_one, map.get("A"));
            Assert.AreSame(refA_one, map.put("A", refA_two));

            // map changed, but packed, loose did not
            Assert.AreSame(refA_two, map.get("A"));
            Assert.AreSame(refA_one, packed.get(0));
            Assert.AreEqual(0, loose.size());

            Assert.AreSame(refA_two, map.put("A", refA_one));
            Assert.AreSame(refA_one, map.get("A"));
        }
Example #24
0
        public void testEmptyBuilder()
        {
            RefList<global::GitSharp.Core.Ref> list = new RefList<global::GitSharp.Core.Ref>.Builder<global::GitSharp.Core.Ref>().toRefList();
            Assert.AreEqual(0, list.size());
            Assert.IsFalse(list.iterator().hasNext());
            Assert.AreEqual(-1, list.find("a"));
            Assert.AreEqual(-1, list.find("z"));
            Assert.IsFalse(list.contains("a"));
            Assert.IsNull(list.get("a"));
            Assert.IsTrue(list.asList().Count == 0);
            Assert.AreEqual("[]", list.ToString());

            // default array capacity should be 16, with no bounds checking.
            Assert.IsNull(list.get(16 - 1));
            try
            {
                list.get(16);
                Assert.Fail("default RefList should have 16 element array");
            }
            catch (IndexOutOfRangeException err)
            {
                // expected
            }
        }
Example #25
0
        public void testBuilder_Set()
        {
            var builder = new RefList<global::GitSharp.Core.Ref>.Builder<global::GitSharp.Core.Ref>();
            builder.add(REF_A);
            builder.add(REF_A);

            Assert.AreEqual(2, builder.size());
            Assert.AreSame(REF_A, builder.get(0));
            Assert.AreSame(REF_A, builder.get(1));

            RefList<global::GitSharp.Core.Ref> list = builder.toRefList();
            Assert.AreEqual(2, list.size());
            Assert.AreSame(REF_A, list.get(0));
            Assert.AreSame(REF_A, list.get(1));
            builder.set(1, REF_B);

            list = builder.toRefList();
            Assert.AreEqual(2, list.size());
            Assert.AreSame(REF_A, list.get(0));
            Assert.AreSame(REF_B, list.get(1));
        }
Example #26
0
        public void testCopyConstructorReusesArray()
        {
            var one = new RefList<global::GitSharp.Core.Ref>.Builder<global::GitSharp.Core.Ref>();
            one.add(REF_A);

            var two = new RefList<global::GitSharp.Core.Ref>(one.toRefList());
            one.set(0, REF_B);
            Assert.AreSame(REF_B, two.get(0));
        }
Example #27
0
        public void testBuilder_Remove()
        {
            var builder = new RefList<global::GitSharp.Core.Ref>.Builder<global::GitSharp.Core.Ref>();
            builder.add(REF_A);
            builder.add(REF_B);
            builder.remove(0);

            Assert.AreEqual(1, builder.size());
            Assert.AreSame(REF_B, builder.get(0));
        }