Skip to content

Commit 1225890

Browse files
committed
Starting to pull real execution flow into something which can go into core
1 parent 3fa14ba commit 1225890

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed

GitVersionExe.Tests/GitPreparerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
5555
arguments.TargetBranch = branchName;
5656
}
5757

58-
var gitPreparer = new GitPreparer(arguments);
58+
var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
5959
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
6060
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
6161

@@ -100,7 +100,7 @@ public void UpdatesExistingDynamicRepository()
100100
TargetBranch = "master"
101101
};
102102

103-
var gitPreparer = new GitPreparer(arguments);
103+
var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
104104
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
105105
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
106106

@@ -147,7 +147,7 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
147147
TargetUrl = fixture.RepositoryPath
148148
};
149149

150-
var gitPreparer = new GitPreparer(arguments);
150+
var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
151151
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
152152

153153
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
@@ -174,7 +174,7 @@ public void WorksCorrectlyWithLocalRepository()
174174
TargetPath = tempDir
175175
};
176176

177-
var gitPreparer = new GitPreparer(arguments);
177+
var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
178178
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
179179

180180
dynamicRepositoryPath.ShouldBe(null);

GitVersionExe/GitPreparer.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@
77

88
public class GitPreparer
99
{
10-
Arguments arguments;
11-
12-
public GitPreparer(Arguments arguments)
10+
string targetUrl;
11+
string dynamicRepositoryLocation;
12+
Authentication authentication;
13+
string targetBranch;
14+
bool noFetch;
15+
string targetPath;
16+
17+
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string targetPath)
1318
{
14-
this.arguments = arguments;
19+
this.targetUrl = targetUrl;
20+
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
21+
this.authentication = authentication;
22+
this.targetBranch = targetBranch;
23+
this.noFetch = noFetch;
24+
this.targetPath = targetPath;
1525
}
1626

1727
public bool IsDynamicGitRepository
@@ -23,13 +33,13 @@ public bool IsDynamicGitRepository
2333

2434
public void InitialiseDynamicRepositoryIfNeeded()
2535
{
26-
if (string.IsNullOrWhiteSpace(arguments.TargetUrl)) return;
36+
if (string.IsNullOrWhiteSpace(targetUrl)) return;
2737

28-
var targetPath = CalculateTemporaryRepositoryPath(arguments.TargetUrl, arguments.DynamicRepositoryLocation);
29-
DynamicGitRepositoryPath = CreateDynamicRepository(targetPath, arguments.Authentication, arguments.TargetUrl, arguments.TargetBranch, arguments.NoFetch);
38+
var targetPath = CalculateTemporaryRepositoryPath(targetUrl, dynamicRepositoryLocation);
39+
DynamicGitRepositoryPath = CreateDynamicRepository(targetPath, authentication, targetUrl, targetBranch, noFetch);
3040
}
3141

32-
string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
42+
static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
3343
{
3444
var userTemp = dynamicRepositoryLocation ?? Path.GetTempPath();
3545
var repositoryName = targetUrl.Split('/', '\\').Last().Replace(".git", string.Empty);
@@ -75,7 +85,15 @@ public string GetDotGitDirectory()
7585
if (IsDynamicGitRepository)
7686
return DynamicGitRepositoryPath;
7787

78-
return GitDirFinder.TreeWalkForDotGitDir(arguments.TargetPath);
88+
return GitDirFinder.TreeWalkForDotGitDir(targetPath);
89+
}
90+
91+
public string GetProjectRootDirectory()
92+
{
93+
if (IsDynamicGitRepository)
94+
return targetPath;
95+
96+
return Directory.GetParent(GitDirFinder.TreeWalkForDotGitDir(targetPath)).FullName;
7997
}
8098

8199
static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)

GitVersionExe/SpecifiedArgumentRunner.cs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,18 @@ class SpecifiedArgumentRunner
1111

1212
public static void Run(Arguments arguments, IFileSystem fileSystem)
1313
{
14-
var gitPreparer = new GitPreparer(arguments);
15-
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
16-
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
17-
if (string.IsNullOrEmpty(dotGitDirectory))
18-
{
19-
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath));
20-
}
21-
var applicableBuildServers = GetApplicableBuildServers(arguments.Authentication).ToList();
22-
23-
foreach (var buildServer in applicableBuildServers)
24-
{
25-
buildServer.PerformPreProcessingSteps(dotGitDirectory, arguments.NoFetch);
26-
}
27-
VersionVariables variables;
28-
var versionFinder = new GitVersionFinder();
29-
var configuration = ConfigurationProvider.Provide(arguments.TargetPath, fileSystem);
30-
31-
using (var repo = RepositoryLoader.GetRepo(dotGitDirectory))
32-
{
33-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: arguments.CommitId);
34-
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
35-
var config = gitVersionContext.Configuration;
36-
variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged);
37-
}
14+
var noFetch = arguments.NoFetch;
15+
var authentication = arguments.Authentication;
16+
var targetPath = arguments.TargetPath;
17+
var targetUrl = arguments.TargetUrl;
18+
var dynamicRepositoryLocation = arguments.DynamicRepositoryLocation;
19+
var targetBranch = arguments.TargetBranch;
20+
var commitId = arguments.CommitId;
21+
var variables = ExecuteGitVersion(fileSystem, targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, targetPath, commitId);
3822

3923
if (arguments.Output == OutputType.BuildServer)
4024
{
41-
foreach (var buildServer in applicableBuildServers)
25+
foreach (var buildServer in BuildServerList.GetApplicableBuildServers(authentication))
4226
{
4327
buildServer.WriteIntegration(Console.WriteLine, variables);
4428
}
@@ -63,10 +47,10 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
6347
}
6448
}
6549

66-
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, arguments.TargetPath, variables, fileSystem))
50+
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, targetPath, variables, fileSystem))
6751
{
68-
var execRun = RunExecCommandIfNeeded(arguments, arguments.TargetPath, variables);
69-
var msbuildRun = RunMsBuildIfNeeded(arguments, arguments.TargetPath, variables);
52+
var execRun = RunExecCommandIfNeeded(arguments, targetPath, variables);
53+
var msbuildRun = RunMsBuildIfNeeded(arguments, targetPath, variables);
7054
if (!execRun && !msbuildRun)
7155
{
7256
assemblyInfoUpdate.DoNotRestoreAssemblyInfo();
@@ -81,9 +65,35 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
8165
}
8266
}
8367

84-
static IEnumerable<IBuildServer> GetApplicableBuildServers(Authentication authentication)
68+
static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
8569
{
86-
return BuildServerList.GetApplicableBuildServers(authentication);
70+
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory);
71+
gitPreparer.InitialiseDynamicRepositoryIfNeeded();
72+
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
73+
var projectRoot = gitPreparer.GetProjectRootDirectory();
74+
if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot))
75+
{
76+
// TODO Link to wiki article
77+
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
78+
}
79+
80+
foreach (var buildServer in BuildServerList.GetApplicableBuildServers(authentication))
81+
{
82+
buildServer.PerformPreProcessingSteps(dotGitDirectory, noFetch);
83+
}
84+
VersionVariables variables;
85+
var versionFinder = new GitVersionFinder();
86+
var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem);
87+
88+
using (var repo = RepositoryLoader.GetRepo(dotGitDirectory))
89+
{
90+
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
91+
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
92+
var config = gitVersionContext.Configuration;
93+
variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged);
94+
}
95+
96+
return variables;
8797
}
8898

8999
static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables)

0 commit comments

Comments
 (0)