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

Commit 6ad8d84

Browse files
Splitting functionality of switching remotes from forking repos
1 parent b49d64d commit 6ad8d84

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

src/GitHub.App/Services/RepositoryForkService.cs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using GitHub.Api;
66
using GitHub.Logging;
77
using GitHub.Models;
8+
using LibGit2Sharp;
89
using Octokit;
910
using ReactiveUI;
1011
using Serilog;
12+
using Repository = Octokit.Repository;
1113

1214
namespace GitHub.Services
1315
{
@@ -27,33 +29,71 @@ public RepositoryForkService(IGitClient gitClient, IVSGitServices vsGitServices)
2729
this.vsGitServices = vsGitServices;
2830
}
2931

30-
public IObservable<Repository> ForkRepository(IApiClient apiClient, IRepositoryModel sourceRepository, NewRepositoryFork repositoryFork, bool resetMasterTracking, bool addUpstream, bool updateOrigin)
32+
public IObservable<Repository> ForkRepository(IApiClient apiClient, IRepositoryModel sourceRepository, NewRepositoryFork repositoryFork, bool updateOrigin, bool addUpstream, bool trackMasterUpstream)
3133
{
3234
return Observable.Defer(() => apiClient.ForkRepository(sourceRepository.Owner, sourceRepository.Name, repositoryFork)
3335
.ObserveOn(RxApp.MainThreadScheduler)
34-
.Select(remoteRepo => new { RemoteRepo = remoteRepo, LocalRepo = vsGitServices.GetActiveRepo() }))
36+
.Select(remoteRepo => new { RemoteRepo = remoteRepo, ActiveRepo = vsGitServices.GetActiveRepo() }))
3537
.SelectMany(async repo =>
3638
{
37-
using (repo.LocalRepo)
39+
using (repo.ActiveRepo)
3840
{
39-
if (updateOrigin)
40-
{
41-
await gitClient.SetRemote(repo.LocalRepo, "origin", new Uri(repo.RemoteRepo.CloneUrl));
42-
}
41+
var originUri = repo.RemoteRepo != null ? new Uri(repo.RemoteRepo.CloneUrl) : null;
42+
var upstreamUri = addUpstream ? sourceRepository.CloneUrl.ToUri() : null;
43+
44+
await SwitchRemotes(repo.ActiveRepo, originUri, upstreamUri, trackMasterUpstream);
45+
46+
return repo.RemoteRepo;
47+
}
48+
});
49+
}
50+
51+
public IObservable<object> SwitchRemotes(IRepositoryModel destinationRepository, bool updateOrigin, bool addUpstream, bool trackMasterUpstream)
52+
{
53+
return Observable.Defer(() => Observable.Return(new object())
54+
.ObserveOn(RxApp.MainThreadScheduler)
55+
.Select(_ => vsGitServices.GetActiveRepo()))
56+
.SelectMany(async activeRepo =>
57+
{
58+
using (activeRepo)
59+
{
60+
Uri currentOrigin = null;
4361

4462
if (addUpstream)
4563
{
46-
await gitClient.SetRemote(repo.LocalRepo, "upstream", sourceRepository.CloneUrl.ToUri());
47-
48-
if (resetMasterTracking)
49-
{
50-
await gitClient.SetTrackingBranch(repo.LocalRepo, "master", "upstream");
51-
}
64+
var remote = await gitClient.GetHttpRemote(activeRepo, "origin");
65+
currentOrigin = new Uri(remote.Url);
5266
}
53-
}
5467

55-
return repo.RemoteRepo;
68+
await SwitchRemotes(activeRepo, updateOrigin ? destinationRepository.CloneUrl.ToUri() : null,
69+
currentOrigin, trackMasterUpstream);
70+
71+
return new object();
72+
}
5673
});
5774
}
75+
76+
private async Task SwitchRemotes(IRepository repository, Uri originUri = null, Uri upstreamUri = null, bool trackMasterUpstream = false)
77+
{
78+
if (originUri != null || upstreamUri != null)
79+
{
80+
if (originUri != null)
81+
{
82+
await gitClient.SetRemote(repository, "origin", originUri);
83+
}
84+
85+
if (upstreamUri != null)
86+
{
87+
await gitClient.SetRemote(repository, "upstream", upstreamUri);
88+
89+
await gitClient.Fetch(repository, "upstream");
90+
91+
if (trackMasterUpstream)
92+
{
93+
await gitClient.SetTrackingBranch(repository, "master", "upstream");
94+
}
95+
}
96+
}
97+
}
5898
}
5999
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using GitHub.Api;
34
using GitHub.Models;
45
using Octokit;
@@ -7,6 +8,7 @@ namespace GitHub.Services
78
{
89
public interface IRepositoryForkService
910
{
10-
IObservable<Repository> ForkRepository(IApiClient apiClient, IRepositoryModel sourceRepository, NewRepositoryFork repositoryFork, bool resetMasterTracking, bool addUpstream, bool updateOrigin);
11+
IObservable<Repository> ForkRepository(IApiClient apiClient, IRepositoryModel sourceRepository, NewRepositoryFork repositoryFork, bool updateOrigin, bool addUpstream, bool trackMasterUpstream);
12+
IObservable<object> SwitchRemotes(IRepositoryModel destinationRepository, bool updateOrigin, bool addUpstream, bool trackMasterUpstream);
1113
}
1214
}

0 commit comments

Comments
 (0)