33#tool "nuget:?package=GitVersion.CommandLine&version=5.12.0"
44#tool "nuget:?package=NuGet.CommandLine&version=7.0.1"
55
6+ #addin "nuget:?package=LibGit2Sharp&version=0.31.0"
7+
68//-------------------------------------------------------------
79
810public class GeneralContext : BuildContextWithItemsBase
@@ -88,7 +90,9 @@ public class VersionContext : BuildContextBase
8890 var repositoryName = generalContext . Solution . Name ;
8991 var dynamicRepositoryPath = System . IO . Path . Combine ( System . IO . Path . GetTempPath ( ) , repositoryName ) ;
9092
91- if ( ClearCache )
93+ // Note: for now (until we fix the dynamic cloning in the code below), clear cache must be true
94+ var clearCache = ClearCache || true ;
95+ if ( clearCache )
9296 {
9397 CakeContext . Warning ( "Cleaning the cloned temp directory, disable by setting 'GitVersion_ClearCache' to 'false'" ) ;
9498
@@ -115,15 +119,79 @@ public class VersionContext : BuildContextBase
115119
116120 CakeContext . Information ( $ "Fetching dynamic repository from url '{ generalContext . Repository . Url } ' => '{ dynamicRepositoryPath } '") ;
117121
118- // Dynamic repository
119- gitVersionSettings . UserName = generalContext . Repository . Username ;
120- gitVersionSettings . Password = generalContext . Repository . Password ;
121- gitVersionSettings . Url = generalContext . Repository . Url ;
122- gitVersionSettings . Branch = generalContext . Repository . BranchName ;
123- gitVersionSettings . Commit = generalContext . Repository . CommitId ;
124- gitVersionSettings . NoFetch = false ;
125- gitVersionSettings . WorkingDirectory = generalContext . RootDirectory ;
126- gitVersionSettings . DynamicRepositoryPath = dynamicRepositoryPath ;
122+ // Note: starting with GitVersion 6.x, we need to handle dynamic repos ourselves,
123+ // and we will be using LibGit2Sharp directly to support cloning a specific commit id
124+
125+ // var gitCloneSettings = new GitCloneSettings
126+ // {
127+ // //BranchName = generalContext.Repository.BranchName,
128+ // BranchName = generalContext.Repository.CommitId,
129+ // Checkout = true,
130+ // IsBare = false,
131+ // RecurseSubmodules = false,
132+ // };
133+
134+ var cloneOptions = new LibGit2Sharp . CloneOptions
135+ {
136+ IsBare = false ,
137+ Checkout = true ,
138+ BranchName = generalContext . Repository . BranchName ,
139+ RecurseSubmodules = false
140+ } ;
141+
142+ if ( ! string . IsNullOrWhiteSpace ( generalContext . Repository . Username ) &&
143+ ! string . IsNullOrWhiteSpace ( generalContext . Repository . Password ) )
144+ {
145+ CakeContext . Information ( "Cloning with authentication" ) ;
146+
147+ cloneOptions . FetchOptions . CredentialsProvider =
148+ ( url , user , cred ) => new LibGit2Sharp . UsernamePasswordCredentials
149+ {
150+ Username = generalContext . Repository . Username ,
151+ Password = generalContext . Repository . Password
152+ } ;
153+ }
154+ else
155+ {
156+ CakeContext . Information ( "Cloning without authentication" ) ;
157+ }
158+
159+ LibGit2Sharp . Repository . Clone ( generalContext . Repository . Url , dynamicRepositoryPath , cloneOptions ) ;
160+
161+ if ( ! CakeContext . GitIsValidRepository ( dynamicRepositoryPath ) )
162+ {
163+ throw new Exception ( $ "Cloned repository at '{ dynamicRepositoryPath } ' is not a valid repository") ;
164+ }
165+
166+ CakeContext . Information ( "Switching to correct commit ID" ) ;
167+
168+ // According to docs, to not get into a detached head state, we need to:
169+ //
170+ // git checkout -B 'branch' 'commit id'
171+ //
172+ // This seems impossible via Cake.Git (and LibGit2Sharp directly), so we will
173+ // just invoke git.exe directly here
174+ //
175+ //CakeContext.GitCheckout(dynamicRepositoryPath, generalContext.Repository.CommitId);
176+
177+ var gitExe = CakeContext . Tools . Resolve ( "git.exe" ) . FullPath ;
178+
179+ using ( var process = CakeContext . StartAndReturnProcess ( gitExe ,
180+ new ProcessSettings
181+ {
182+ WorkingDirectory = dynamicRepositoryPath ,
183+ Arguments = $ "checkout -B { generalContext . Repository . BranchName } { generalContext . Repository . CommitId } ",
184+ } ) )
185+ {
186+ process . WaitForExit ( ) ;
187+
188+ // This should output 0 as valid arguments supplied
189+ CakeContext . Information ( "Exit code: {0}" , process . GetExitCode ( ) ) ;
190+ }
191+
192+ CakeContext . Information ( "Preparing GitVersion settings" ) ;
193+
194+ gitVersionSettings . RepositoryPath = dynamicRepositoryPath ;
127195 gitVersionSettings . Verbosity = GitVersionVerbosity . Verbose ;
128196 }
129197
0 commit comments