diff --git a/src/HgVersion/VCS/HgRepository.cs b/src/HgVersion/VCS/HgRepository.cs index a2000a7..567dc4b 100644 --- a/src/HgVersion/VCS/HgRepository.cs +++ b/src/HgVersion/VCS/HgRepository.cs @@ -29,11 +29,7 @@ public HgRepository(Repository repository) /// public IEnumerable Log(ILogQuery query) { - if (query == null) - throw new ArgumentNullException(nameof(query)); - - if (!(query is HgLogQuery hgQuery)) - throw new InvalidOperationException($"{query.GetType()} is not supported."); + var hgQuery = CastTo(query); return _repository .Log(new LogCommand() @@ -56,12 +52,7 @@ public IEnumerable Log(Func select) /// public int Count(ILogQuery query) { - if (query == null) - throw new ArgumentNullException(nameof(query)); - - if (!(query is HgLogQuery hgQuery)) - throw new InvalidOperationException($"{query.GetType()} is not supported."); - + var hgQuery = CastTo(query); var command = new CountCommand() .WithRevision(hgQuery.Revision); @@ -146,9 +137,7 @@ public string Commit(string message) /// public IEnumerable Parents(ICommit commit) { - if (!(commit is HgCommit hgCommit)) - throw new InvalidOperationException($"{commit.GetType()} is not supported."); - + var hgCommit = CastTo(commit); var command = new ParentsCommand() .WithRevision(hgCommit); @@ -202,5 +191,16 @@ public void Update(RevSpec rev) /// public static implicit operator HgRepository(Repository repository) => new HgRepository(repository); + + private static T CastTo(object from) + { + if (from == null) + throw new ArgumentNullException(nameof(from)); + + if (!(from is T to)) + throw new InvalidCastException($"{from.GetType()} is not supported."); + + return to; + } } } \ No newline at end of file