diff --git a/NGitLab.Mock/Clients/LibGit2SharpExtensions.cs b/NGitLab.Mock/Clients/LibGit2SharpExtensions.cs index d58a3845..6b86a8c8 100644 --- a/NGitLab.Mock/Clients/LibGit2SharpExtensions.cs +++ b/NGitLab.Mock/Clients/LibGit2SharpExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using NGitLab.Models; @@ -62,7 +63,25 @@ public static Models.CommitInfo ToCommitInfo(this LibGit2Sharp.Commit commit) internal static LibGit2Sharp.Commit GetLastCommitForFileChanges(this LibGit2Sharp.Repository repository, string filePath) { - return repository.Commits.QueryBy(filePath).FirstOrDefault()?.Commit; + try + { + return repository.Commits.QueryBy(filePath).FirstOrDefault()?.Commit; + } + catch (KeyNotFoundException) + { + // LibGit2Sharp sometimes fails with the following exception + // System.Collections.Generic.KeyNotFoundException: The given key '1d08df45e551942eaa70d9f5ab6f5f7665a3f5b3' was not present in the dictionary. + // at System.Collections.Generic.Dictionary`2.get_Item(TKey key) + // at LibGit2Sharp.Core.FileHistory.FullHistory(IRepository repo, String path, CommitFilter filter)+MoveNext() in /_/LibGit2Sharp/Core/FileHistory.cs:line 120 + // at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found) + // at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) + // at NGitLab.Mock.Clients.LibGit2SharpExtensions.GetLastCommitForFileChanges(Repository repository, String filePath) in /_/NGitLab.Mock/Clients/LibGit2SharpExtensions.cs:line 65 + // at NGitLab.Mock.Repository.GetFile(String filePath, String ref) in /_/NGitLab.Mock/Repository.cs:line 485 + // at NGitLab.Mock.Clients.FileClient.Get(String filePath, String ref) in /_/NGitLab.Mock/Clients/FileClient.cs:line 77 + // at NGitLab.Mock.Clients.FileClient.GetAsync(String filePath, String ref, CancellationToken cancellationToken) in /_/NGitLab.Mock/Clients/FileClient.cs:line 125 + } + + return null; } } } diff --git a/NGitLab.Mock/Repository.cs b/NGitLab.Mock/Repository.cs index fb7183f9..9808a9c2 100644 --- a/NGitLab.Mock/Repository.cs +++ b/NGitLab.Mock/Repository.cs @@ -492,7 +492,7 @@ public FileData GetFile(string filePath, string @ref) Ref = @ref, BlobId = ((Blob)commit[filePath].Target).Id.ToString(), CommitId = commit.Sha, - LastCommitId = repo.GetLastCommitForFileChanges(filePath).Sha, + LastCommitId = repo.GetLastCommitForFileChanges(filePath)?.Sha, }; }