/// <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())); }
public virtual void TestBuilder_AddThenSort() { RefListBuilder<Ref> builder = new RefListBuilder<Ref>(1); builder.Add(REF_B); builder.Add(REF_A); 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_A, list.Get(1)); builder.Sort(); 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)); }