@@ -11,12 +11,12 @@ public class GitVersionContext
1111 {
1212 readonly Config configuration ;
1313
14- public GitVersionContext ( IRepository repository , Config configuration , bool isForTrackingBranchOnly = true )
15- : this ( repository , repository . Head , configuration , isForTrackingBranchOnly )
14+ public GitVersionContext ( IRepository repository , Config configuration , bool isForTrackingBranchOnly = true , string commitId = null )
15+ : this ( repository , repository . Head , configuration , isForTrackingBranchOnly , commitId )
1616 {
1717 }
1818
19- public GitVersionContext ( IRepository repository , Branch currentBranch , Config configuration , bool onlyEvaluateTrackedBranches = true )
19+ public GitVersionContext ( IRepository repository , Branch currentBranch , Config configuration , bool onlyEvaluateTrackedBranches = true , string commitId = null )
2020 {
2121 Repository = repository ;
2222 this . configuration = configuration ;
@@ -25,7 +25,24 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
2525 if ( currentBranch == null )
2626 throw new InvalidOperationException ( "Need a branch to operate on" ) ;
2727
28- CurrentCommit = currentBranch . Tip ;
28+ if ( ! string . IsNullOrWhiteSpace ( commitId ) )
29+ {
30+ Logger . WriteInfo ( string . Format ( "Searching for specific commit '{0}'" , commitId ) ) ;
31+
32+ var commit = repository . Commits . FirstOrDefault ( c => string . Equals ( c . Sha , commitId , StringComparison . OrdinalIgnoreCase ) ) ;
33+ if ( commit != null )
34+ {
35+ CurrentCommit = commit ;
36+ }
37+ }
38+
39+ if ( CurrentCommit == null )
40+ {
41+ Logger . WriteWarning ( "No specific commit specified or found, falling back to latest commit on specified branch" ) ;
42+
43+ CurrentCommit = currentBranch . Tip ;
44+ }
45+
2946 if ( currentBranch . IsDetachedHead ( ) )
3047 {
3148 CurrentBranch = CurrentCommit . GetBranchesContainingCommit ( repository , OnlyEvaluateTrackedBranches ) . OnlyOrDefault ( ) ?? currentBranch ;
0 commit comments