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

Commit 7d84837

Browse files
authored
Merge pull request #1736 from github/fixes/1735-remote-config-detritus
Minimize .git/config detritus when fetching
2 parents ee98b3b + 6737407 commit 7d84837

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/GitHub.App/Services/GitClient.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,30 @@ public Task Fetch(IRepository repository, string remoteName)
9696

9797
public Task Fetch(IRepository repo, UriString cloneUrl, params string[] refspecs)
9898
{
99-
var httpsUrl = UriString.ToUriString(cloneUrl.ToRepositoryUrl());
99+
var httpsString = cloneUrl.ToRepositoryUrl().ToString();
100100

101-
var originRemote = repo.Network.Remotes[defaultOriginName];
102-
if (originRemote != null && originRemote.Url == httpsUrl)
101+
foreach (var remote in repo.Network.Remotes)
103102
{
104-
return Fetch(repo, defaultOriginName, refspecs);
103+
var remoteUrl = new UriString(remote.Url);
104+
if (!remoteUrl.IsHypertextTransferProtocol)
105+
{
106+
// Only match http urls
107+
continue;
108+
}
109+
110+
var remoteHttpsString = remoteUrl.ToRepositoryUrl().ToString();
111+
if (remoteHttpsString.Equals(httpsString, StringComparison.OrdinalIgnoreCase))
112+
{
113+
return Fetch(repo, defaultOriginName, refspecs);
114+
}
105115
}
106116

107117
return Task.Factory.StartNew(() =>
108118
{
109119
try
110120
{
111121
var tempRemoteName = cloneUrl.Owner + "-" + Guid.NewGuid();
112-
var remote = repo.Network.Remotes.Add(tempRemoteName, httpsUrl);
122+
var remote = repo.Network.Remotes.Add(tempRemoteName, httpsString);
113123
try
114124
{
115125
#pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.

test/UnitTests/GitHub.App/Services/GitClientTests.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ public async Task FetchUsingHttps(string repoUrl, string expectFetchUrl)
208208
repo.Network.Remotes.Received(1).Add(Arg.Any<string>(), expectFetchUrl);
209209
}
210210

211-
[TestCase("https://github.com/owner/repo", "https://github.com/owner/repo", null)]
212-
[TestCase("https://github.com/fetch/repo", "https://github.com/origin/repo", "https://github.com/fetch/repo")]
213-
[TestCase("[email protected]:owner/repo", "[email protected]:owner/repo", "https://github.com/owner/repo")]
214-
public async Task UseOriginWhenPossible(string fetchUrl, string originUrl, string addUrl = null)
211+
[TestCase("https://github.com/owner/repo", "origin", "https://github.com/owner/repo", null)]
212+
[TestCase("https://github.com/fetch/repo", "origin", "https://github.com/origin/repo", "https://github.com/fetch/repo")]
213+
[TestCase("[email protected]:owner/repo", "origin", "[email protected]:owner/repo", "https://github.com/owner/repo", Description = "Only use http style urls")]
214+
[TestCase("https://github.com/jcansdale/repo", "jcansdale", "https://github.com/jcansdale/repo", null, Description = "Use existing remote")]
215+
[TestCase("https://github.com/jcansdale/repo.git", "jcansdale", "https://github.com/jcansdale/repo", null, Description = "Ignore trailing .git")]
216+
[TestCase("https://github.com/JCANSDALE/REPO", "jcansdale", "https://github.com/jcansdale/repo", null, Description = "Ignore different case")]
217+
public async Task UseExistingRemoteWhenPossible(string fetchUrl, string remoteName, string remoteUrl, string addUrl = null)
215218
{
216-
var remote = Substitute.For<Remote>();
217-
remote.Url.Returns(originUrl);
218-
var repo = Substitute.For<IRepository>();
219-
repo.Network.Remotes["origin"].Returns(remote);
219+
var repo = CreateRepository(remoteName, remoteUrl);
220220
var fetchUri = new UriString(fetchUrl);
221221
var refSpec = "refSpec";
222222
var gitClient = CreateGitClient();
@@ -232,6 +232,18 @@ public async Task UseOriginWhenPossible(string fetchUrl, string originUrl, strin
232232
repo.Network.Remotes.DidNotReceiveWithAnyArgs().Add(null, null);
233233
}
234234
}
235+
236+
static IRepository CreateRepository(string remoteName, string remoteUrl)
237+
{
238+
var remote = Substitute.For<Remote>();
239+
remote.Name.Returns(remoteName);
240+
remote.Url.Returns(remoteUrl);
241+
var remotes = new List<Remote> { remote };
242+
var repo = Substitute.For<IRepository>();
243+
repo.Network.Remotes[remoteName].Returns(remote);
244+
repo.Network.Remotes.GetEnumerator().Returns(_ => remotes.GetEnumerator());
245+
return repo;
246+
}
235247
}
236248

237249
public class TheGetPullRequestMergeBaseMethod : TestBaseClass

0 commit comments

Comments
 (0)