diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-01-09 19:40:43 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-01-24 02:16:38 +0400 |
commit | c3f37afe160faf2c684b4551ec7997d7991edee2 (patch) | |
tree | ae7c1c50fe50c8499133219c73ae7a6f242a6d59 | |
parent | 1ccfc718b8e2919430b2942a6c1c14cb1284029d (diff) |
Refactor reference building
-rw-r--r-- | LibGit2Sharp/DirectReference.cs | 5 | ||||
-rw-r--r-- | LibGit2Sharp/Reference.cs | 35 | ||||
-rw-r--r-- | LibGit2Sharp/SymbolicReference.cs | 19 |
3 files changed, 48 insertions, 11 deletions
diff --git a/LibGit2Sharp/DirectReference.cs b/LibGit2Sharp/DirectReference.cs index 9929fa35..7f4b9c25 100644 --- a/LibGit2Sharp/DirectReference.cs +++ b/LibGit2Sharp/DirectReference.cs @@ -15,9 +15,10 @@ namespace LibGit2Sharp protected DirectReference() { } - internal DirectReference(Lazy<GitObject> targetBuilder) + internal DirectReference(string canonicalName, Repository repo, ObjectId targetId) + : base(canonicalName, targetId.Sha) { - this.targetBuilder = targetBuilder; + targetBuilder = new Lazy<GitObject>(() => repo.Lookup(targetId)); ; } /// <summary> diff --git a/LibGit2Sharp/Reference.cs b/LibGit2Sharp/Reference.cs index 49d91466..253851e8 100644 --- a/LibGit2Sharp/Reference.cs +++ b/LibGit2Sharp/Reference.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.Globalization; using LibGit2Sharp.Core; -using LibGit2Sharp.Core.Compat; using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp @@ -16,10 +15,20 @@ namespace LibGit2Sharp private static readonly LambdaEqualityHelper<Reference> equalityHelper = new LambdaEqualityHelper<Reference>(x => x.CanonicalName, x => x.TargetIdentifier); + private readonly string canonicalName; + private readonly string targetIdentifier; + /// <summary> - /// Gets the full name of this reference. + /// Needed for mocking purposes. /// </summary> - public virtual string CanonicalName { get; protected set; } + protected Reference() + { } + + protected Reference(string canonicalName, string targetIdentifier) + { + this.canonicalName = canonicalName; + this.targetIdentifier = targetIdentifier; + } internal static T BuildFromPtr<T>(ReferenceSafeHandle handle, Repository repo) where T : Reference { @@ -37,20 +46,19 @@ namespace LibGit2Sharp { if (resolvedHandle == null) { - reference = new SymbolicReference { CanonicalName = name, Target = null, TargetIdentifier = targetIdentifier }; + reference = new SymbolicReference(name, targetIdentifier, null); break; } var targetRef = BuildFromPtr<DirectReference>(resolvedHandle, repo); - reference = new SymbolicReference { CanonicalName = name, Target = targetRef, TargetIdentifier = targetIdentifier }; + reference = new SymbolicReference(name, targetIdentifier, targetRef); break; } case GitReferenceType.Oid: ObjectId targetOid = Proxy.git_reference_oid(handle); - var targetBuilder = new Lazy<GitObject>(() => repo.Lookup(targetOid)); - reference = new DirectReference(targetBuilder) { CanonicalName = name, TargetIdentifier = targetOid.Sha }; + reference = new DirectReference(name, repo, targetOid); break; default: @@ -61,6 +69,14 @@ namespace LibGit2Sharp } /// <summary> + /// Gets the full name of this reference. + /// </summary> + public virtual string CanonicalName + { + get { return canonicalName; } + } + + /// <summary> /// Recursively peels the target of the reference until a direct reference is encountered. /// </summary> /// <returns>The <see cref = "DirectReference" /> this <see cref = "Reference" /> points to.</returns> @@ -74,7 +90,10 @@ namespace LibGit2Sharp /// </para> /// </summary> // TODO: Maybe find a better name for this property. - public virtual string TargetIdentifier { get; private set; } + public virtual string TargetIdentifier + { + get { return targetIdentifier; } + } /// <summary> /// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Reference" />. diff --git a/LibGit2Sharp/SymbolicReference.cs b/LibGit2Sharp/SymbolicReference.cs index b7ff5c23..d98a92ed 100644 --- a/LibGit2Sharp/SymbolicReference.cs +++ b/LibGit2Sharp/SymbolicReference.cs @@ -9,10 +9,27 @@ namespace LibGit2Sharp [DebuggerDisplay("{DebuggerDisplay,nq}")] public class SymbolicReference : Reference { + private readonly Reference target; + + /// <summary> + /// Needed for mocking purposes. + /// </summary> + protected SymbolicReference() + { } + + internal SymbolicReference(string canonicalName, string targetIdentifier, Reference target) + : base(canonicalName, targetIdentifier) + { + this.target = target; + } + /// <summary> /// Gets the target of this <see cref = "SymbolicReference" /> /// </summary> - public Reference Target { get; internal set; } + public virtual Reference Target + { + get { return target; } + } /// <summary> /// Recursively peels the target of the reference until a direct reference is encountered. |