|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Globalization; |
| 7 | +using LibGit2Sharp.Core; |
| 8 | +using LibGit2Sharp.Core.Handles; |
| 9 | +using LibGit2Sharp.Core.Compat; |
| 10 | + |
| 11 | +namespace LibGit2Sharp |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// The collection of <see cref="RefSpec"/>s in a <see cref="Remote"/> |
| 15 | + /// </summary> |
| 16 | + [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| 17 | + public class RefSpecCollection : IEnumerable<RefSpec> |
| 18 | + { |
| 19 | + private readonly Lazy<IList<RefSpec>> refSpecsLazy; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Needed for mocking purposes. |
| 23 | + /// </summary> |
| 24 | + protected RefSpecCollection() |
| 25 | + { } |
| 26 | + |
| 27 | + internal RefSpecCollection(Remote remote) |
| 28 | + { |
| 29 | + Ensure.ArgumentNotNull(remote, "remote"); |
| 30 | + |
| 31 | + refSpecsLazy = new Lazy<IList<RefSpec>>(() => RetrieveRefSpecs(remote)); |
| 32 | + } |
| 33 | + |
| 34 | + private static IList<RefSpec> RetrieveRefSpecs(Remote remote) |
| 35 | + { |
| 36 | + using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(remote.repository.Handle, remote.Name, true)) |
| 37 | + { |
| 38 | + int count = Proxy.git_remote_refspec_count(remoteHandle); |
| 39 | + List<RefSpec> refSpecs = new List<RefSpec>(); |
| 40 | + |
| 41 | + for (int i = 0; i < count; i++) |
| 42 | + { |
| 43 | + using (GitRefSpecHandle handle = Proxy.git_remote_get_refspec(remoteHandle, i)) |
| 44 | + { |
| 45 | + refSpecs.Add(RefSpec.BuildFromPtr(handle)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return refSpecs; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Returns an enumerator that iterates through the collection. |
| 55 | + /// </summary> |
| 56 | + /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns> |
| 57 | + public virtual IEnumerator<RefSpec> GetEnumerator() |
| 58 | + { |
| 59 | + return refSpecsLazy.Value.GetEnumerator(); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Returns an enumerator that iterates through the collection. |
| 64 | + /// </summary> |
| 65 | + /// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns> |
| 66 | + IEnumerator IEnumerable.GetEnumerator() |
| 67 | + { |
| 68 | + return GetEnumerator(); |
| 69 | + } |
| 70 | + |
| 71 | + private string DebuggerDisplay |
| 72 | + { |
| 73 | + get |
| 74 | + { |
| 75 | + return string.Format(CultureInfo.InvariantCulture, |
| 76 | + "Count = {0}", this.Count()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments