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

Commit 3f8e31d

Browse files
authored
Merge pull request #1250 from github/fixes/1249-temp-remote
Fix race condition when fetching from and deleting a temporary remote
2 parents 75be49c + 5f1f3d8 commit 3f8e31d

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/GitHub.App/Services/GitClient.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,41 @@ public Task Fetch(IRepository repository, string remoteName)
8686
});
8787
}
8888

89+
public Task Fetch(IRepository repo, UriString cloneUrl, params string[] refspecs)
90+
{
91+
var httpsUrl = UriString.ToUriString(cloneUrl.ToRepositoryUrl());
92+
93+
var originRemote = repo.Network.Remotes[defaultOriginName];
94+
if (originRemote != null && originRemote.Url == httpsUrl)
95+
{
96+
return Fetch(repo, defaultOriginName, refspecs);
97+
}
98+
99+
return Task.Factory.StartNew(() =>
100+
{
101+
try
102+
{
103+
var tempRemoteName = cloneUrl.Owner + "-" + Guid.NewGuid();
104+
var remote = repo.Network.Remotes.Add(tempRemoteName, httpsUrl);
105+
try
106+
{
107+
repo.Network.Fetch(remote, refspecs, fetchOptions);
108+
}
109+
finally
110+
{
111+
repo.Network.Remotes.Remove(tempRemoteName);
112+
}
113+
}
114+
catch (Exception ex)
115+
{
116+
log.Error("Failed to fetch", ex);
117+
#if DEBUG
118+
throw;
119+
#endif
120+
}
121+
});
122+
}
123+
89124
public Task Fetch(IRepository repository, string remoteName, params string[] refspecs)
90125
{
91126
Guard.ArgumentNotNull(repository, nameof(repository));
@@ -430,26 +465,6 @@ public Task<bool> IsHeadPushed(IRepository repo)
430465
});
431466
}
432467

433-
public Task Fetch(IRepository repo, UriString cloneUrl, params string[] refspecs)
434-
{
435-
var httpsUrl = UriString.ToUriString(cloneUrl.ToRepositoryUrl());
436-
if (repo.Network.Remotes[defaultOriginName]?.Url == httpsUrl)
437-
{
438-
return Fetch(repo, defaultOriginName, refspecs);
439-
}
440-
441-
var tempRemoteName = cloneUrl.Owner + "-" + Guid.NewGuid();
442-
repo.Network.Remotes.Add(tempRemoteName, httpsUrl);
443-
try
444-
{
445-
return Fetch(repo, tempRemoteName, refspecs);
446-
}
447-
finally
448-
{
449-
repo.Network.Remotes.Remove(tempRemoteName);
450-
}
451-
}
452-
453468
static bool IsCanonical(string s)
454469
{
455470
Guard.ArgumentNotEmptyString(s, nameof(s));

0 commit comments

Comments
 (0)