/// <exception cref="System.IO.IOException"></exception>
        private RefList <Ref> ParsePackedRefs(BufferedReader br)
        {
            RefListBuilder <Ref> all = new RefListBuilder <Ref>();
            Ref    last     = null;
            bool   peeled   = false;
            bool   needSort = false;
            string p;

            while ((p = br.ReadLine()) != null)
            {
                if (p[0] == '#')
                {
                    if (p.StartsWith(PACKED_REFS_HEADER))
                    {
                        p      = Sharpen.Runtime.Substring(p, PACKED_REFS_HEADER.Length);
                        peeled = p.Contains(PACKED_REFS_PEELED);
                    }
                    continue;
                }
                if (p[0] == '^')
                {
                    if (last == null)
                    {
                        throw new IOException(JGitText.Get().peeledLineBeforeRef);
                    }
                    ObjectId id = ObjectId.FromString(Sharpen.Runtime.Substring(p, 1));
                    last = new ObjectIdRef.PeeledTag(RefStorage.PACKED, last.GetName(), last.GetObjectId
                                                         (), id);
                    all.Set(all.Size() - 1, last);
                    continue;
                }
                int         sp   = p.IndexOf(' ');
                ObjectId    id_1 = ObjectId.FromString(Sharpen.Runtime.Substring(p, 0, sp));
                string      name = Copy(p, sp + 1, p.Length);
                ObjectIdRef cur;
                if (peeled)
                {
                    cur = new ObjectIdRef.PeeledNonTag(RefStorage.PACKED, name, id_1);
                }
                else
                {
                    cur = new ObjectIdRef.Unpeeled(RefStorage.PACKED, name, id_1);
                }
                if (last != null && RefComparator.CompareTo(last, cur) > 0)
                {
                    needSort = true;
                }
                all.Add(cur);
                last = cur;
            }
            if (needSort)
            {
                all.Sort();
            }
            return(all.ToRefList());
        }
        /// <exception cref="System.IO.IOException"></exception>
        public override IDictionary <string, Ref> GetRefs(string prefix)
        {
            RefList <Ref> packed = GetPackedRefs();
            RefList <RefDirectory.LooseRef> oldLoose = looseRefs.Get();

            RefDirectory.LooseScanner scan = new RefDirectory.LooseScanner(this, oldLoose);
            scan.Scan(prefix);
            RefList <RefDirectory.LooseRef> loose;

            if (scan.newLoose != null)
            {
                scan.newLoose.Sort();
                loose = scan.newLoose.ToRefList();
                if (looseRefs.CompareAndSet(oldLoose, loose))
                {
                    modCnt.IncrementAndGet();
                }
            }
            else
            {
                loose = oldLoose;
            }
            FireRefsChanged();
            RefListBuilder <Ref> symbolic = scan.symbolic;

            for (int idx = 0; idx < symbolic.Size();)
            {
                Ref symbolicRef = symbolic.Get(idx);
                Ref resolvedRef = Resolve(symbolicRef, 0, prefix, loose, packed);
                if (resolvedRef != null && resolvedRef.GetObjectId() != null)
                {
                    symbolic.Set(idx, resolvedRef);
                    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.
                    symbolic.Remove(idx);
                    int toRemove = loose.Find(symbolicRef.GetName());
                    if (0 <= toRemove)
                    {
                        loose = loose.Remove(toRemove);
                    }
                }
            }
            symbolic.Sort();
            return(new RefMap(prefix, packed, Upcast(loose), symbolic.ToRefList()));
        }
 internal virtual void Scan(string prefix)
 {
     if (RefDatabase.ALL.Equals(prefix))
     {
         this.ScanOne(Constants.HEAD);
         this.ScanTree(Constants.R_REFS, this._enclosing.refsDir);
         // If any entries remain, they are deleted, drop them.
         if (this.newLoose == null && this.curIdx < this.curLoose.Size())
         {
             this.newLoose = this.curLoose.Copy(this.curIdx);
         }
     }
     else
     {
         if (prefix.StartsWith(Constants.R_REFS) && prefix.EndsWith("/"))
         {
             this.curIdx = -(this.curLoose.Find(prefix) + 1);
             FilePath dir = new FilePath(this._enclosing.refsDir, Sharpen.Runtime.Substring(prefix
                                                                                            , Constants.R_REFS.Length));
             this.ScanTree(prefix, dir);
             // Skip over entries still within the prefix; these have
             // been removed from the directory.
             while (this.curIdx < this.curLoose.Size())
             {
                 if (!this.curLoose.Get(this.curIdx).GetName().StartsWith(prefix))
                 {
                     break;
                 }
                 if (this.newLoose == null)
                 {
                     this.newLoose = this.curLoose.Copy(this.curIdx);
                 }
                 this.curIdx++;
             }
             // Keep any entries outside of the prefix space, we
             // do not know anything about their status.
             if (this.newLoose != null)
             {
                 while (this.curIdx < this.curLoose.Size())
                 {
                     this.newLoose.Add(this.curLoose.Get(this.curIdx++));
                 }
             }
         }
     }
 }
Beispiel #4
0
		private RefList<Ref> ToList(params Ref[] refs)
		{
			RefListBuilder<Ref> b = new RefListBuilder<Ref>(refs.Length);
			b.AddAll(refs, 0, refs.Length);
			return b.ToRefList();
		}
Beispiel #5
0
		public virtual void TestEmptyBuilder()
		{
			RefList<Ref> list = new RefListBuilder<Ref>().ToRefList();
			NUnit.Framework.Assert.AreEqual(0, list.Size());
			NUnit.Framework.Assert.IsFalse(list.Iterator().HasNext());
			NUnit.Framework.Assert.AreEqual(-1, list.Find("a"));
			NUnit.Framework.Assert.AreEqual(-1, list.Find("z"));
			NUnit.Framework.Assert.IsFalse(list.Contains("a"));
			NUnit.Framework.Assert.IsNull(list.Get("a"));
			NUnit.Framework.Assert.IsTrue(list.AsList().IsEmpty());
			NUnit.Framework.Assert.AreEqual("[]", list.ToString());
			// default array capacity should be 16, with no bounds checking.
			NUnit.Framework.Assert.IsNull(list.Get(16 - 1));
			try
			{
				list.Get(16);
				NUnit.Framework.Assert.Fail("default RefList should have 16 element array");
			}
			catch (IndexOutOfRangeException)
			{
			}
		}
Beispiel #6
0
		public virtual void TestCopyConstructorReusesArray()
		{
			RefListBuilder<Ref> one = new RefListBuilder<Ref>();
			one.Add(REF_A);
			RefList<Ref> two = new RefList<Ref>(one.ToRefList());
			one.Set(0, REF_B);
			NUnit.Framework.Assert.AreSame(REF_B, two.Get(0));
		}
Beispiel #7
0
		public virtual void TestBuilder_ToString()
		{
			StringBuilder exp = new StringBuilder();
			exp.Append("[");
			exp.Append(REF_A);
			exp.Append(", ");
			exp.Append(REF_B);
			exp.Append("]");
			RefListBuilder<Ref> list = new RefListBuilder<Ref>();
			list.Add(REF_A);
			list.Add(REF_B);
			NUnit.Framework.Assert.AreEqual(exp.ToString(), list.ToString());
		}
Beispiel #8
0
		public virtual void TestBuilder_Remove()
		{
			RefListBuilder<Ref> builder = new RefListBuilder<Ref>();
			builder.Add(REF_A);
			builder.Add(REF_B);
			builder.Remove(0);
			NUnit.Framework.Assert.AreEqual(1, builder.Size());
			NUnit.Framework.Assert.AreSame(REF_B, builder.Get(0));
		}
Beispiel #9
0
		public virtual void TestBuilder_Set()
		{
			RefListBuilder<Ref> builder = new RefListBuilder<Ref>();
			builder.Add(REF_A);
			builder.Add(REF_A);
			NUnit.Framework.Assert.AreEqual(2, builder.Size());
			NUnit.Framework.Assert.AreSame(REF_A, builder.Get(0));
			NUnit.Framework.Assert.AreSame(REF_A, builder.Get(1));
			RefList<Ref> list = builder.ToRefList();
			NUnit.Framework.Assert.AreEqual(2, list.Size());
			NUnit.Framework.Assert.AreSame(REF_A, list.Get(0));
			NUnit.Framework.Assert.AreSame(REF_A, list.Get(1));
			builder.Set(1, REF_B);
			list = builder.ToRefList();
			NUnit.Framework.Assert.AreEqual(2, list.Size());
			NUnit.Framework.Assert.AreSame(REF_A, list.Get(0));
			NUnit.Framework.Assert.AreSame(REF_B, list.Get(1));
		}
Beispiel #10
0
		public virtual void TestBuilder_AddAll()
		{
			RefListBuilder<Ref> builder = new RefListBuilder<Ref>(1);
			Ref[] src = new Ref[] { REF_A, REF_B, REF_c, REF_A };
			builder.AddAll(src, 1, 2);
			RefList<Ref> list = builder.ToRefList();
			NUnit.Framework.Assert.AreEqual(2, list.Size());
			NUnit.Framework.Assert.AreSame(REF_B, list.Get(0));
			NUnit.Framework.Assert.AreSame(REF_c, list.Get(1));
		}
 private void ScanOne(string name)
 {
     RefDirectory.LooseRef cur;
     if (this.curIdx < this.curLoose.Size())
     {
         do
         {
             cur = this.curLoose.Get(this.curIdx);
             int cmp = RefComparator.CompareTo(cur, name);
             if (cmp < 0)
             {
                 // Reference is not loose anymore, its been deleted.
                 // Skip the name in the new result list.
                 if (this.newLoose == null)
                 {
                     this.newLoose = this.curLoose.Copy(this.curIdx);
                 }
                 this.curIdx++;
                 cur = null;
                 continue;
             }
             if (cmp > 0)
             {
                 // Newly discovered loose reference.
                 cur = null;
             }
             break;
         }while (this.curIdx < this.curLoose.Size());
     }
     else
     {
         cur = null;
     }
     // Newly discovered loose reference.
     RefDirectory.LooseRef n;
     try
     {
         n = this._enclosing.ScanRef(cur, name);
     }
     catch (IOException)
     {
         n = null;
     }
     if (n != null)
     {
         if (cur != n && this.newLoose == null)
         {
             this.newLoose = this.curLoose.Copy(this.curIdx);
         }
         if (this.newLoose != null)
         {
             this.newLoose.Add(n);
         }
         if (n.IsSymbolic())
         {
             this.symbolic.Add(n);
         }
     }
     else
     {
         if (cur != null)
         {
             // Tragically, this file is no longer a loose reference.
             // Kill our cached entry of it.
             if (this.newLoose == null)
             {
                 this.newLoose = this.curLoose.Copy(this.curIdx);
             }
         }
     }
     if (cur != null)
     {
         this.curIdx++;
     }
 }