Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 6f1a890

Browse files
committed
Dispose after using PullRequestSessionService.GetRepository
1 parent 7cfbda3 commit 6f1a890

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

src/GitHub.InlineReviews/Services/PullRequestSessionService.cs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,19 @@ public PullRequestSessionService(
5353
/// <inheritdoc/>
5454
public virtual async Task<IReadOnlyList<DiffChunk>> Diff(ILocalRepositoryModel repository, string baseSha, string headSha, string relativePath)
5555
{
56-
var repo = await GetRepository(repository);
57-
return await diffService.Diff(repo, baseSha, headSha, relativePath);
56+
using (var repo = await GetRepository(repository))
57+
{
58+
return await diffService.Diff(repo, baseSha, headSha, relativePath);
59+
}
5860
}
5961

6062
/// <inheritdoc/>
6163
public virtual async Task<IReadOnlyList<DiffChunk>> Diff(ILocalRepositoryModel repository, string baseSha, string headSha, string relativePath, byte[] contents)
6264
{
63-
var repo = await GetRepository(repository);
64-
return await diffService.Diff(repo, baseSha, headSha, relativePath, contents);
65+
using (var repo = await GetRepository(repository))
66+
{
67+
return await diffService.Diff(repo, baseSha, headSha, relativePath, contents);
68+
}
6569
}
6670

6771
/// <inheritdoc/>
@@ -175,18 +179,22 @@ public ITextDocument GetDocument(ITextBuffer buffer)
175179
/// <inheritdoc/>
176180
public virtual async Task<string> GetTipSha(ILocalRepositoryModel repository)
177181
{
178-
var repo = await GetRepository(repository);
179-
return repo.Head.Tip.Sha;
182+
using (var repo = await GetRepository(repository))
183+
{
184+
return repo.Head.Tip.Sha;
185+
}
180186
}
181187

182188
/// <inheritdoc/>
183189
public async Task<bool> IsUnmodifiedAndPushed(ILocalRepositoryModel repository, string relativePath, byte[] contents)
184190
{
185-
var repo = await GetRepository(repository);
186-
var modified = await gitClient.IsModified(repo, relativePath, contents);
187-
var pushed = await gitClient.IsHeadPushed(repo);
191+
using (var repo = await GetRepository(repository))
192+
{
193+
var modified = await gitClient.IsModified(repo, relativePath, contents);
194+
var pushed = await gitClient.IsHeadPushed(repo);
188195

189-
return !modified && pushed;
196+
return !modified && pushed;
197+
}
190198
}
191199

192200
public async Task<byte[]> ExtractFileFromGit(
@@ -195,17 +203,18 @@ public async Task<byte[]> ExtractFileFromGit(
195203
string sha,
196204
string relativePath)
197205
{
198-
var repo = await GetRepository(repository);
199-
200-
try
201-
{
202-
return await gitClient.ExtractFileBinary(repo, sha, relativePath);
203-
}
204-
catch (FileNotFoundException)
206+
using (var repo = await GetRepository(repository))
205207
{
206-
var pullHeadRef = $"refs/pull/{pullRequestNumber}/head";
207-
await gitClient.Fetch(repo, "origin", sha, pullHeadRef);
208-
return await gitClient.ExtractFileBinary(repo, sha, relativePath);
208+
try
209+
{
210+
return await gitClient.ExtractFileBinary(repo, sha, relativePath);
211+
}
212+
catch (FileNotFoundException)
213+
{
214+
var pullHeadRef = $"refs/pull/{pullRequestNumber}/head";
215+
await gitClient.Fetch(repo, "origin", sha, pullHeadRef);
216+
return await gitClient.ExtractFileBinary(repo, sha, relativePath);
217+
}
209218
}
210219
}
211220

@@ -242,21 +251,23 @@ public virtual async Task<string> GetPullRequestMergeBase(ILocalRepositoryModel
242251
return mergeBase;
243252
}
244253

245-
var repo = await GetRepository(repository);
246-
var targetUrl = pullRequest.Base.RepositoryCloneUrl;
247-
var headUrl = pullRequest.Head.RepositoryCloneUrl;
248-
var baseRef = pullRequest.Base.Ref;
249-
var pullNumber = pullRequest.Number;
250-
try
254+
using (var repo = await GetRepository(repository))
251255
{
252-
mergeBase = await gitClient.GetPullRequestMergeBase(repo, targetUrl, baseSha, headSha, baseRef, pullNumber);
253-
}
254-
catch (NotFoundException ex)
255-
{
256-
throw new NotFoundException("The Pull Request failed to load. Please check your network connection and click refresh to try again. If this issue persists, please let us know at [email protected]", ex);
257-
}
256+
var targetUrl = pullRequest.Base.RepositoryCloneUrl;
257+
var headUrl = pullRequest.Head.RepositoryCloneUrl;
258+
var baseRef = pullRequest.Base.Ref;
259+
var pullNumber = pullRequest.Number;
260+
try
261+
{
262+
mergeBase = await gitClient.GetPullRequestMergeBase(repo, targetUrl, baseSha, headSha, baseRef, pullNumber);
263+
}
264+
catch (NotFoundException ex)
265+
{
266+
throw new NotFoundException("The Pull Request failed to load. Please check your network connection and click refresh to try again. If this issue persists, please let us know at [email protected]", ex);
267+
}
258268

259-
return mergeBaseCache[key] = mergeBase;
269+
return mergeBaseCache[key] = mergeBase;
270+
}
260271
}
261272

262273
/// <inheritdoc/>

test/GitHub.InlineReviews.UnitTests/TestDoubles/FakeDiffService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public string AddFile(string path, string contents, string commitAlias)
5454
public void Dispose()
5555
{
5656
var path = repository.Info.WorkingDirectory;
57-
repository.Dispose();
57+
58+
// NOTE: IDiffService doesn't own the Repository object
59+
//repository.Dispose();
5860

5961
// The .git folder has some files marked as readonly, meaning that a simple
6062
// Directory.Delete doesn't work here.

0 commit comments

Comments
 (0)