diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-08-14 14:36:49 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-08-15 23:23:51 +0400 |
commit | aac17c74e73c9e433407a8579906f81c064d57bd (patch) | |
tree | c6583c3aff9349fe1a608f74c47444ee98c66b95 /LibGit2Sharp/ObjectDatabase.cs | |
parent | 7ea504a6ef5bec3e4abcf68b3baa403b36886981 (diff) |
Teach repo.ObjectDatabase to enumerate GitObjects
Diffstat (limited to 'LibGit2Sharp/ObjectDatabase.cs')
-rw-r--r-- | LibGit2Sharp/ObjectDatabase.cs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index 4bb121ff..e7c618f9 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -13,7 +14,7 @@ namespace LibGit2Sharp /// Provides methods to directly work against the Git object database /// without involving the index nor the working directory. /// </summary> - public class ObjectDatabase + public class ObjectDatabase : IEnumerable<GitObject> { private readonly Repository repo; private readonly ObjectDatabaseSafeHandle handle; @@ -32,6 +33,33 @@ namespace LibGit2Sharp repo.RegisterForCleanup(handle); } + #region Implementation of IEnumerable + + /// <summary> + /// Returns an enumerator that iterates through the collection. + /// </summary> + /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns> + public virtual IEnumerator<GitObject> GetEnumerator() + { + ICollection<GitOid> oids = Proxy.git_odb_foreach(handle, + ptr => (GitOid) Marshal.PtrToStructure(ptr, typeof (GitOid))); + + return oids + .Select(gitOid => repo.Lookup<GitObject>(new ObjectId(gitOid))) + .GetEnumerator(); + } + + /// <summary> + /// Returns an enumerator that iterates through the collection. + /// </summary> + /// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns> + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + #endregion + /// <summary> /// Determines if the given object can be found in the object database. /// </summary> |