55using GitHub . Api ;
66using GitHub . Logging ;
77using GitHub . Models ;
8+ using LibGit2Sharp ;
89using Octokit ;
910using ReactiveUI ;
1011using Serilog ;
12+ using Repository = Octokit . Repository ;
1113
1214namespace 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}
0 commit comments