Skip to content

Commit

Permalink
Workaround for an exception in libgit2sharp (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou authored Jul 11, 2023
1 parent 08bdbdd commit 04c632b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion NGitLab.Mock/Clients/LibGit2SharpExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NGitLab.Models;

Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion NGitLab.Mock/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down

0 comments on commit 04c632b

Please sign in to comment.