Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2011-06-23 23:37:56 +0400
committernulltoken <emeric.fermas@gmail.com>2011-06-23 23:37:56 +0400
commit608c8aef7e34ea7489f6136bc268d622f419d1ac (patch)
treee64d1123dbf618a2e4a634920d8a46401f28aa5d
parentdb9c4950f1874981388a422a664604c344bddf51 (diff)
Leverage Lazy<T> to unify implementation of deferred execution
-rw-r--r--LibGit2Sharp/Commit.cs25
-rw-r--r--LibGit2Sharp/Repository.cs8
-rwxr-xr-xLibGit2Sharp/TreeEntry.cs5
3 files changed, 17 insertions, 21 deletions
diff --git a/LibGit2Sharp/Commit.cs b/LibGit2Sharp/Commit.cs
index 45cefa57..3be4d3c5 100644
--- a/LibGit2Sharp/Commit.cs
+++ b/LibGit2Sharp/Commit.cs
@@ -11,13 +11,13 @@ namespace LibGit2Sharp
public class Commit : GitObject
{
private readonly Repository repo;
- private List<Commit> parents;
- private Tree tree;
- private readonly ObjectId treeId;
+ private readonly Lazy<IEnumerable<Commit>> parents;
+ private readonly Lazy<Tree> tree;
internal Commit(ObjectId id, ObjectId treeId, Repository repo) : base(id)
{
- this.treeId = treeId;
+ this.tree = new Lazy<Tree>(() => repo.Lookup<Tree>(treeId));
+ this.parents = new Lazy<IEnumerable<Commit>>(() => RetrieveParentsOfCommit(id.Oid));
this.repo = repo;
}
@@ -44,26 +44,23 @@ namespace LibGit2Sharp
/// <summary>
/// Gets the Tree associated to this commit.
/// </summary>
- public Tree Tree { get { return tree ?? (tree = repo.Lookup<Tree>(treeId)); } }
+ public Tree Tree { get { return tree.Value; } }
/// <summary>
/// Gets the parents of this commit. This property is lazy loaded and can throw an exception if the commit no longer exists in the repo.
/// </summary>
- public IEnumerable<Commit> Parents
- {
- get { return parents ?? (parents = RetrieveParentsOfCommit(Id.Oid)); }
- }
+ public IEnumerable<Commit> Parents { get { return parents.Value; } }
- private List<Commit> RetrieveParentsOfCommit(GitOid oid) //TODO: Convert to a ParentEnumerator
+ private IEnumerable<Commit> RetrieveParentsOfCommit(GitOid oid) //TODO: Convert to a ParentEnumerator
{
IntPtr obj;
var res = NativeMethods.git_object_lookup(out obj, repo.Handle, ref oid, GitObjectType.Commit);
Ensure.Success(res);
+ var parentsOfCommits = new List<Commit>();
+
try
{
- parents = new List<Commit>();
-
uint parentsCount = NativeMethods.git_commit_parentcount(obj);
for (uint i = 0; i < parentsCount; i++)
@@ -71,7 +68,7 @@ namespace LibGit2Sharp
IntPtr parentCommit;
res = NativeMethods.git_commit_parent(out parentCommit, obj, i);
Ensure.Success(res);
- parents.Add((Commit)CreateFromPtr(parentCommit, ObjectIdOf(parentCommit), repo));
+ parentsOfCommits.Add((Commit)CreateFromPtr(parentCommit, ObjectIdOf(parentCommit), repo));
}
}
finally
@@ -79,7 +76,7 @@ namespace LibGit2Sharp
NativeMethods.git_object_close(obj);
}
- return parents;
+ return parentsOfCommits;
}
internal static Commit BuildFromPtr(IntPtr obj, ObjectId id, Repository repo)
diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs
index ef80c7f7..156ed419 100644
--- a/LibGit2Sharp/Repository.cs
+++ b/LibGit2Sharp/Repository.cs
@@ -15,7 +15,7 @@ namespace LibGit2Sharp
private readonly Index index;
private readonly ReferenceCollection refs;
private readonly TagCollection tags;
- private RepositoryInformation info;
+ private readonly Lazy<RepositoryInformation> info;
private readonly bool isBare;
/// <summary>
@@ -41,6 +41,7 @@ namespace LibGit2Sharp
refs = new ReferenceCollection(this);
branches = new BranchCollection(this);
tags = new TagCollection(this);
+ info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
}
internal RepositorySafeHandle Handle
@@ -111,10 +112,7 @@ namespace LibGit2Sharp
/// <summary>
/// Provides high level information about this repository.
/// </summary>
- public RepositoryInformation Info
- {
- get { return info ?? (info = new RepositoryInformation(this, isBare)); }
- }
+ public RepositoryInformation Info { get { return info.Value; } }
#region IDisposable Members
diff --git a/LibGit2Sharp/TreeEntry.cs b/LibGit2Sharp/TreeEntry.cs
index aa057a96..caebe642 100755
--- a/LibGit2Sharp/TreeEntry.cs
+++ b/LibGit2Sharp/TreeEntry.cs
@@ -11,7 +11,7 @@ namespace LibGit2Sharp
{
private readonly ObjectId parentTreeId;
private readonly Repository repo;
- private GitObject target;
+ private readonly Lazy<GitObject> target;
private readonly ObjectId targetOid;
private static readonly LambdaEqualityHelper<TreeEntry> equalityHelper =
@@ -24,6 +24,7 @@ namespace LibGit2Sharp
IntPtr gitTreeEntryId = NativeMethods.git_tree_entry_id(obj);
targetOid = new ObjectId((GitOid)Marshal.PtrToStructure(gitTreeEntryId, typeof(GitOid)));
Type = NativeMethods.git_tree_entry_type(obj);
+ target = new Lazy<GitObject>(RetreiveTreeEntryTarget);
Attributes = NativeMethods.git_tree_entry_attributes(obj);
Name = NativeMethods.git_tree_entry_name(obj).MarshallAsString();
@@ -43,7 +44,7 @@ namespace LibGit2Sharp
/// <summary>
/// Gets the <see cref="GitObject"/> being pointed at.
/// </summary>
- public GitObject Target { get { return target ?? (target = RetreiveTreeEntryTarget()); } }
+ public GitObject Target { get { return target.Value; } }
/// <summary>
/// Gets the <see cref="GitObjectType"/> of the <see cref="Target"/> being pointed at.