@@ -15,6 +15,8 @@ public class GitPreparer
1515 bool noFetch ;
1616 string targetPath ;
1717
18+ const string defaultRemoteName = "origin" ;
19+
1820 public GitPreparer ( string targetPath ) : this ( null , null , null , false , targetPath ) { }
1921 public GitPreparer ( string targetUrl , string dynamicRepositoryLocation , Authentication authentication , bool noFetch , string targetPath )
2022 {
@@ -76,10 +78,25 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch, bool sh
7678
7779 private void CleanupDuplicateOrigin ( )
7880 {
81+ var remoteToKeep = defaultRemoteName ;
82+
7983 var repo = new Repository ( GetDotGitDirectory ( ) ) ;
80- if ( repo . Network . Remotes . Any ( remote => remote . Name == "origin1" ) )
84+
85+ // check that we have a remote that matches defaultRemoteName if not take the first remote
86+ if ( ! repo . Network . Remotes . Any ( remote => remote . Name . Equals ( defaultRemoteName , StringComparison . InvariantCultureIgnoreCase ) ) )
87+ {
88+ remoteToKeep = repo . Network . Remotes . First ( ) . Name ;
89+ }
90+
91+ var duplicateRepos = repo . Network
92+ . Remotes
93+ . Where ( remote => ! remote . Name . Equals ( remoteToKeep , StringComparison . InvariantCultureIgnoreCase ) )
94+ . Select ( remote => remote . Name ) ;
95+
96+ // remove all remotes that are considered duplicates
97+ foreach ( var repoName in duplicateRepos )
8198 {
82- repo . Network . Remotes . Remove ( "origin1" ) ;
99+ repo . Network . Remotes . Remove ( repoName ) ;
83100 }
84101 }
85102
0 commit comments