@@ -4,17 +4,25 @@ namespace GitVersion
4
4
using System . Linq ;
5
5
using GitVersion . Helpers ;
6
6
7
- public static class ExecuteCore
7
+ using LibGit2Sharp ;
8
+
9
+ public class ExecuteCore
8
10
{
9
- public static VersionVariables ExecuteGitVersion ( IFileSystem fileSystem , string targetUrl , string dynamicRepositoryLocation , Authentication authentication , string targetBranch , bool noFetch , string workingDirectory , string commitId )
11
+ readonly IFileSystem fileSystem ;
12
+ readonly GitVersionCache gitVersionCache ;
13
+
14
+ public ExecuteCore ( IFileSystem fileSystem )
15
+ {
16
+ if ( fileSystem == null ) throw new ArgumentNullException ( "fileSystem" ) ;
17
+
18
+ this . fileSystem = fileSystem ;
19
+ gitVersionCache = new GitVersionCache ( fileSystem ) ;
20
+ }
21
+
22
+ public VersionVariables ExecuteGitVersion ( string targetUrl , string dynamicRepositoryLocation , Authentication authentication , string targetBranch , bool noFetch , string workingDirectory , string commitId )
10
23
{
11
24
// Normalise if we are running on build server
12
25
var gitPreparer = new GitPreparer ( targetUrl , dynamicRepositoryLocation , authentication , noFetch , workingDirectory ) ;
13
- var applicableBuildServers = BuildServerList . GetApplicableBuildServers ( ) ;
14
- var buildServer = applicableBuildServers . FirstOrDefault ( ) ;
15
-
16
- gitPreparer . Initialise ( buildServer != null , ResolveCurrentBranch ( buildServer , targetBranch ) ) ;
17
-
18
26
var dotGitDirectory = gitPreparer . GetDotGitDirectory ( ) ;
19
27
var projectRoot = gitPreparer . GetProjectRootDirectory ( ) ;
20
28
Logger . WriteInfo ( string . Format ( "Project root is: " + projectRoot ) ) ;
@@ -23,28 +31,85 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string
23
31
// TODO Link to wiki article
24
32
throw new Exception ( string . Format ( "Failed to prepare or find the .git directory in path '{0}'." , workingDirectory ) ) ;
25
33
}
26
- VersionVariables variables ;
27
- var versionFinder = new GitVersionFinder ( ) ;
28
- var configuration = ConfigurationProvider . Provide ( projectRoot , fileSystem ) ;
29
-
30
- using ( var repo = RepositoryLoader . GetRepo ( dotGitDirectory ) )
34
+
35
+ using ( var repo = GetRepository ( dotGitDirectory ) )
31
36
{
32
- var gitVersionContext = new GitVersionContext ( repo , configuration , commitId : commitId ) ;
33
- var semanticVersion = versionFinder . FindVersion ( gitVersionContext ) ;
34
- variables = VariableProvider . GetVariablesFor ( semanticVersion , gitVersionContext . Configuration , gitVersionContext . IsCurrentCommitTagged ) ;
37
+ var versionVariables = gitVersionCache . LoadVersionVariablesFromDiskCache ( repo , dotGitDirectory ) ;
38
+ if ( versionVariables == null )
39
+ {
40
+ versionVariables = ExecuteInternal ( targetBranch , commitId , repo , gitPreparer , projectRoot ) ;
41
+ gitVersionCache . WriteVariablesToDiskCache ( repo , dotGitDirectory , versionVariables ) ;
42
+ }
43
+
44
+ return versionVariables ;
35
45
}
46
+ }
36
47
37
- return variables ;
48
+ public bool TryGetVersion ( string directory , out VersionVariables versionVariables , bool noFetch , Authentication authentication )
49
+ {
50
+ try
51
+ {
52
+ versionVariables = ExecuteGitVersion ( null , null , authentication , null , noFetch , directory , null ) ;
53
+ return true ;
54
+ }
55
+ catch ( Exception ex )
56
+ {
57
+ Logger . WriteWarning ( "Could not determine assembly version: " + ex ) ;
58
+ versionVariables = null ;
59
+ return false ;
60
+ }
38
61
}
39
62
40
- private static string ResolveCurrentBranch ( IBuildServer buildServer , string targetBranch )
63
+ static string ResolveCurrentBranch ( IBuildServer buildServer , string targetBranch )
41
64
{
42
- if ( buildServer == null ) return targetBranch ;
65
+ if ( buildServer == null )
66
+ {
67
+ return targetBranch ;
68
+ }
43
69
44
70
var currentBranch = buildServer . GetCurrentBranch ( ) ?? targetBranch ;
45
71
Logger . WriteInfo ( "Branch from build environment: " + currentBranch ) ;
46
72
47
73
return currentBranch ;
48
74
}
75
+
76
+ VersionVariables ExecuteInternal ( string targetBranch , string commitId , IRepository repo , GitPreparer gitPreparer , string projectRoot )
77
+ {
78
+ var applicableBuildServers = BuildServerList . GetApplicableBuildServers ( ) ;
79
+ var buildServer = applicableBuildServers . FirstOrDefault ( ) ;
80
+
81
+ gitPreparer . Initialise ( buildServer != null , ResolveCurrentBranch ( buildServer , targetBranch ) ) ;
82
+
83
+ var versionFinder = new GitVersionFinder ( ) ;
84
+ var configuration = ConfigurationProvider . Provide ( projectRoot , fileSystem ) ;
85
+
86
+ var gitVersionContext = new GitVersionContext ( repo , configuration , commitId : commitId ) ;
87
+ var semanticVersion = versionFinder . FindVersion ( gitVersionContext ) ;
88
+
89
+ return VariableProvider . GetVariablesFor ( semanticVersion , gitVersionContext . Configuration , gitVersionContext . IsCurrentCommitTagged ) ;
90
+ }
91
+
92
+ IRepository GetRepository ( string gitDirectory )
93
+ {
94
+ try
95
+ {
96
+ var repository = new Repository ( gitDirectory ) ;
97
+
98
+ var branch = repository . Head ;
99
+ if ( branch . Tip == null )
100
+ {
101
+ throw new WarningException ( "No Tip found. Has repo been initialized?" ) ;
102
+ }
103
+ return repository ;
104
+ }
105
+ catch ( Exception exception )
106
+ {
107
+ if ( exception . Message . Contains ( "LibGit2Sharp.Core.NativeMethods" ) || exception . Message . Contains ( "FilePathMarshaler" ) )
108
+ {
109
+ throw new WarningException ( "Restart of the process may be required to load an updated version of LibGit2Sharp." ) ;
110
+ }
111
+ throw ;
112
+ }
113
+ }
49
114
}
50
115
}
0 commit comments