diff options
author | Keith Dahlby <dahlbyk@gmail.com> | 2014-09-01 07:38:46 +0400 |
---|---|---|
committer | Keith Dahlby <dahlbyk@gmail.com> | 2014-09-05 20:16:07 +0400 |
commit | c9157e4f8f57a7f745078f4c51a9153d6364d844 (patch) | |
tree | 03863e237e7ff89b07ef130aea95ad8e259d1c0f | |
parent | 547daba273b2b23febb9860406453c8bc3ab4290 (diff) |
Add IBelongToARepository with some implementations
-rw-r--r-- | LibGit2Sharp.Tests/BranchFixture.cs | 2 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/CheckoutFixture.cs | 3 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/MetaFixture.cs | 25 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/RemoteFixture.cs | 2 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/SubmoduleFixture.cs | 1 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs | 6 | ||||
-rw-r--r-- | LibGit2Sharp/GitObject.cs | 4 | ||||
-rw-r--r-- | LibGit2Sharp/IBelongToARepository.cs | 26 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/Reference.cs | 4 | ||||
-rw-r--r-- | LibGit2Sharp/ReferenceWrapper.cs | 4 | ||||
-rw-r--r-- | LibGit2Sharp/Remote.cs | 4 | ||||
-rw-r--r-- | LibGit2Sharp/Submodule.cs | 4 |
13 files changed, 80 insertions, 6 deletions
diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index d954e6a5..0aef1a27 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -75,6 +75,7 @@ namespace LibGit2Sharp.Tests // The branch now exists... Branch orphan = repo.Branches["orphan"]; Assert.NotNull(orphan); + AssertBelongsToARepository(repo, orphan); // ...and points to that newly created commit Assert.Equal(c, orphan.Tip); @@ -489,6 +490,7 @@ namespace LibGit2Sharp.Tests Assert.True(master.IsTracking); Assert.NotNull(master.TrackedBranch); + AssertBelongsToARepository(repo, master.TrackedBranch); Assert.NotNull(master.TrackingDetails); Assert.Equal(9, master.TrackingDetails.AheadBy); diff --git a/LibGit2Sharp.Tests/CheckoutFixture.cs b/LibGit2Sharp.Tests/CheckoutFixture.cs index abe4ad9a..b3a09564 100644 --- a/LibGit2Sharp.Tests/CheckoutFixture.cs +++ b/LibGit2Sharp.Tests/CheckoutFixture.cs @@ -32,9 +32,11 @@ namespace LibGit2Sharp.Tests Branch branch = repo.Branches[branchName]; Assert.NotNull(branch); + AssertBelongsToARepository(repo, branch); Branch test = repo.Checkout(branch); Assert.False(repo.Info.IsHeadDetached); + AssertBelongsToARepository(repo, test); Assert.False(test.IsRemote); Assert.True(test.IsCurrentRepositoryHead); @@ -114,6 +116,7 @@ namespace LibGit2Sharp.Tests Assert.False(repo.Index.RetrieveStatus().IsDirty); var commit = repo.Lookup<Commit>(commitPointer); + AssertBelongsToARepository(repo, commit); Branch detachedHead = checkoutByCommitOrBranchSpec ? repo.Checkout(commitPointer) : repo.Checkout(commit); diff --git a/LibGit2Sharp.Tests/MetaFixture.cs b/LibGit2Sharp.Tests/MetaFixture.cs index 3cff83cc..fb1b567c 100644 --- a/LibGit2Sharp.Tests/MetaFixture.cs +++ b/LibGit2Sharp.Tests/MetaFixture.cs @@ -12,6 +12,11 @@ namespace LibGit2Sharp.Tests { public class MetaFixture { + private static readonly HashSet<Type> explicitOnlyInterfaces = new HashSet<Type> + { + typeof(IBelongToARepository), + }; + [Fact] public void PublicTestMethodsAreFactsOrTheories() { @@ -114,7 +119,7 @@ namespace LibGit2Sharp.Tests var methodsMissingFromInterfaces = from t in Assembly.GetAssembly(typeof(IRepository)).GetExportedTypes() where !t.IsInterface - where t.GetInterfaces().Any(i => i.IsPublic && i.Namespace == typeof(IRepository).Namespace) + where t.GetInterfaces().Any(i => i.IsPublic && i.Namespace == typeof(IRepository).Namespace && !explicitOnlyInterfaces.Contains(i)) let interfaceTargetMethods = from i in t.GetInterfaces() from im in t.GetInterfaceMap(i).TargetMethods select im @@ -127,6 +132,24 @@ namespace LibGit2Sharp.Tests } [Fact] + public void LibGit2SharpExplicitOnlyInterfacesAreIndeedExplicitOnly() + { + var methodsMissingFromInterfaces = + from t in Assembly.GetAssembly(typeof(IRepository)).GetExportedTypes() + where t.GetInterfaces().Any(explicitOnlyInterfaces.Contains) + let interfaceTargetMethods = from i in t.GetInterfaces() + where explicitOnlyInterfaces.Contains(i) + from im in t.GetInterfaceMap(i).TargetMethods + select im + from tm in t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) + where interfaceTargetMethods.Contains(tm) + select t.Name + " has public method " + tm.Name + " which should be explicitly implemented."; + + Assert.Equal("", string.Join(Environment.NewLine, + methodsMissingFromInterfaces.ToArray())); + } + + [Fact] public void EnumsWithFlagsHaveMutuallyExclusiveValues() { var flagsEnums = Assembly.GetAssembly(typeof(IRepository)).GetExportedTypes() diff --git a/LibGit2Sharp.Tests/RemoteFixture.cs b/LibGit2Sharp.Tests/RemoteFixture.cs index 72a2fa67..0299860a 100644 --- a/LibGit2Sharp.Tests/RemoteFixture.cs +++ b/LibGit2Sharp.Tests/RemoteFixture.cs @@ -14,6 +14,7 @@ namespace LibGit2Sharp.Tests { Remote origin = repo.Network.Remotes["origin"]; Assert.NotNull(origin); + AssertBelongsToARepository(repo, origin); Assert.Equal("origin", origin.Name); Assert.Equal("c:/GitHub/libgit2sharp/Resources/testrepo.git", origin.Url); } @@ -38,6 +39,7 @@ namespace LibGit2Sharp.Tests foreach (Remote remote in repo.Network.Remotes) { Assert.NotNull(remote); + AssertBelongsToARepository(repo, remote); count++; } diff --git a/LibGit2Sharp.Tests/SubmoduleFixture.cs b/LibGit2Sharp.Tests/SubmoduleFixture.cs index e1b378b7..2463912f 100644 --- a/LibGit2Sharp.Tests/SubmoduleFixture.cs +++ b/LibGit2Sharp.Tests/SubmoduleFixture.cs @@ -60,6 +60,7 @@ namespace LibGit2Sharp.Tests { var submodule = repo.Submodules[name]; Assert.NotNull(submodule); + AssertBelongsToARepository(repo, submodule); Assert.Equal(name, submodule.Name); Assert.Equal((ObjectId)headId, submodule.HeadCommitId); diff --git a/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs b/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs index b01d04c4..0200a256 100644 --- a/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs +++ b/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs @@ -412,5 +412,11 @@ namespace LibGit2Sharp.Tests.TestHelpers return true; } + + public void AssertBelongsToARepository<T>(IRepository repo, T instance) + where T : IBelongToARepository + { + Assert.Same(repo, ((IBelongToARepository)instance).Repository); + } } } diff --git a/LibGit2Sharp/GitObject.cs b/LibGit2Sharp/GitObject.cs index ad4614dc..5e11489a 100644 --- a/LibGit2Sharp/GitObject.cs +++ b/LibGit2Sharp/GitObject.cs @@ -11,7 +11,7 @@ namespace LibGit2Sharp /// A GitObject /// </summary> [DebuggerDisplay("{DebuggerDisplay,nq}")] - public abstract class GitObject : IEquatable<GitObject> + public abstract class GitObject : IEquatable<GitObject>, IBelongToARepository { internal static IDictionary<Type, ObjectType> TypeToKindMap = new Dictionary<Type, ObjectType> @@ -154,5 +154,7 @@ namespace LibGit2Sharp { get { return Id.ToString(7); } } + + IRepository IBelongToARepository.Repository { get { return repo; } } } } diff --git a/LibGit2Sharp/IBelongToARepository.cs b/LibGit2Sharp/IBelongToARepository.cs new file mode 100644 index 00000000..f0297c6c --- /dev/null +++ b/LibGit2Sharp/IBelongToARepository.cs @@ -0,0 +1,26 @@ +namespace LibGit2Sharp +{ + /// <summary> + /// Can be used to reference the <see cref="IRepository" /> from which + /// an instance was created. + /// <para> + /// While convenient in some situations (e.g. Checkout branch bound to UI element), + /// it is important to ensure instances created from an <see cref="IRepository" /> + /// are not used after it is disposed. + /// </para> + /// <para> + /// It's generally better to create <see cref="IRepository" /> and dependant instances + /// on demand, with a short lifespan. + /// </para> + /// </summary> + public interface IBelongToARepository + { + /// <summary> + /// The <see cref="IRepository" /> from which this instance was created. + /// <para> + /// The returned value should not be disposed. + /// </para> + /// </summary> + IRepository Repository { get; } + } +} diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 67929273..ad733e41 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -84,6 +84,7 @@ <Compile Include="Core\Handles\IndexNameEntrySafeHandle.cs" /> <Compile Include="Core\Handles\IndexReucEntrySafeHandle.cs" /> <Compile Include="EntryExistsException.cs" /> + <Compile Include="IBelongToARepository.cs" /> <Compile Include="IndexNameEntryCollection.cs" /> <Compile Include="ContentChangeStats.cs" /> <Compile Include="BuiltInFeatures.cs" /> diff --git a/LibGit2Sharp/Reference.cs b/LibGit2Sharp/Reference.cs index 94ac2f31..8ab7d1b7 100644 --- a/LibGit2Sharp/Reference.cs +++ b/LibGit2Sharp/Reference.cs @@ -10,7 +10,7 @@ namespace LibGit2Sharp /// A Reference to another git object /// </summary> [DebuggerDisplay("{DebuggerDisplay,nq}")] - public abstract class Reference : IEquatable<Reference> + public abstract class Reference : IEquatable<Reference>, IBelongToARepository { private static readonly LambdaEqualityHelper<Reference> equalityHelper = new LambdaEqualityHelper<Reference>(x => x.CanonicalName, x => x.TargetIdentifier); @@ -209,5 +209,7 @@ namespace LibGit2Sharp "{0} => \"{1}\"", CanonicalName, TargetIdentifier); } } + + IRepository IBelongToARepository.Repository { get { return repo; } } } } diff --git a/LibGit2Sharp/ReferenceWrapper.cs b/LibGit2Sharp/ReferenceWrapper.cs index aaf56070..583c68d4 100644 --- a/LibGit2Sharp/ReferenceWrapper.cs +++ b/LibGit2Sharp/ReferenceWrapper.cs @@ -10,7 +10,7 @@ namespace LibGit2Sharp /// </summary> /// <typeparam name="TObject">The type of the referenced Git object.</typeparam> [DebuggerDisplay("{DebuggerDisplay,nq}")] - public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TObject>> where TObject : GitObject + public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TObject>>, IBelongToARepository where TObject : GitObject { /// <summary> /// The repository. @@ -160,5 +160,7 @@ namespace LibGit2Sharp (TargetObject != null) ? TargetObject.Id.ToString(7) : "?"); } } + + IRepository IBelongToARepository.Repository { get { return repo; } } } } diff --git a/LibGit2Sharp/Remote.cs b/LibGit2Sharp/Remote.cs index e999d08d..dfff15fe 100644 --- a/LibGit2Sharp/Remote.cs +++ b/LibGit2Sharp/Remote.cs @@ -12,7 +12,7 @@ namespace LibGit2Sharp /// A remote repository whose branches are tracked. /// </summary> [DebuggerDisplay("{DebuggerDisplay,nq}")] - public class Remote : IEquatable<Remote> + public class Remote : IEquatable<Remote>, IBelongToARepository { private static readonly LambdaEqualityHelper<Remote> equalityHelper = new LambdaEqualityHelper<Remote>(x => x.Name, x => x.Url); @@ -174,5 +174,7 @@ namespace LibGit2Sharp "{0} => {1}", Name, Url); } } + + IRepository IBelongToARepository.Repository { get { return repository; } } } } diff --git a/LibGit2Sharp/Submodule.cs b/LibGit2Sharp/Submodule.cs index 3d0b3562..c15e224d 100644 --- a/LibGit2Sharp/Submodule.cs +++ b/LibGit2Sharp/Submodule.cs @@ -9,7 +9,7 @@ namespace LibGit2Sharp /// A Submodule. /// </summary> [DebuggerDisplay("{DebuggerDisplay,nq}")] - public class Submodule : IEquatable<Submodule> + public class Submodule : IEquatable<Submodule>, IBelongToARepository { private static readonly LambdaEqualityHelper<Submodule> equalityHelper = new LambdaEqualityHelper<Submodule>(x => x.Name, x => x.HeadCommitId); @@ -155,5 +155,7 @@ namespace LibGit2Sharp "{0} => {1}", Name, Url); } } + + IRepository IBelongToARepository.Repository { get { return repo; } } } } |