public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId) { // Normalise if we are running on build server var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory); gitPreparer.Initialise(BuildServerList.GetApplicableBuildServers().Any()); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); Logger.WriteInfo(string.Format("Project root is: " + projectRoot)); if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot)) { // TODO Link to wiki article throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory)); } VersionVariables variables; var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem); using (var repo = RepositoryLoader.GetRepo(dotGitDirectory)) { var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId); var semanticVersion = versionFinder.FindVersion(gitVersionContext); var config = gitVersionContext.Configuration; variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged); } return(variables); }
public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId) { // Normalise if we are running on build server var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory); var applicableBuildServers = BuildServerList.GetApplicableBuildServers(); var buildServer = applicableBuildServers.FirstOrDefault(); var currentBranch = buildServer == null ? null : buildServer.GetCurrentBranch(); if (!string.IsNullOrEmpty(currentBranch)) { Logger.WriteInfo("Branch from build environment: " + currentBranch); } gitPreparer.Initialise(buildServer != null, currentBranch ?? targetBranch); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); Logger.WriteInfo(string.Format("Project root is: " + projectRoot)); if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot)) { // TODO Link to wiki article throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory)); } VersionVariables variables; var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem); using (var repo = RepositoryLoader.GetRepo(dotGitDirectory)) { var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId); var semanticVersion = versionFinder.FindVersion(gitVersionContext); var config = gitVersionContext.Configuration; variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged); } return variables; }
public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null, bool noCache = false, bool noNormalize = false) { BuildServerList.Init(environment, log); // Normalize if we are running on build server var applicableBuildServers = BuildServerList.GetApplicableBuildServers(log); var buildServer = applicableBuildServers.FirstOrDefault(); var normalizeGitDirectory = !noNormalize && buildServer != null; var fetch = noFetch || buildServer != null && buildServer.PreventFetch(); var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes(); var gitPreparer = new GitPreparer(log, targetUrl, dynamicRepositoryLocation, authentication, fetch, workingDirectory); gitPreparer.Initialise(normalizeGitDirectory, ResolveCurrentBranch(buildServer, targetBranch, !string.IsNullOrWhiteSpace(dynamicRepositoryLocation)), shouldCleanUpRemotes); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); // TODO Can't use this, it still needs work //var gitRepository = GitRepositoryFactory.CreateRepository(new RepositoryInfo //{ // Url = targetUrl, // Branch = targetBranch, // Authentication = new AuthenticationInfo // { // Username = authentication.Username, // Password = authentication.Password // }, // Directory = workingDirectory //}); log.Info($"Project root is: {projectRoot}"); log.Info($"DotGit directory is: {dotGitDirectory}"); if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot)) { // TODO Link to wiki article throw new Exception($"Failed to prepare or find the .git directory in path '{workingDirectory}'."); } var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, log, gitPreparer, overrideConfig, configFileLocator); var versionVariables = noCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey); if (versionVariables == null) { versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, overrideConfig); if (!noCache) { try { gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables); } catch (AggregateException e) { log.Warning($"One or more exceptions during cache write:{Environment.NewLine}{e}"); } } } return(versionVariables); }
public void CacheKeySameAfterReNormalizing() { var versionAndBranchFinder = new ExecuteCore(fileSystem); RepositoryScope(versionAndBranchFinder, (fixture, vv) => { var targetUrl = "https://github.com/GitTools/GitVersion.git"; var targetBranch = "refs/head/master"; var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath); gitPreparer.Initialise(true, targetBranch); var cacheKey1 = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null); gitPreparer.Initialise(true, targetBranch); var cacheKey2 = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null); cacheKey2.Value.ShouldBe(cacheKey1.Value); }); }
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot, IBuildServer buildServer) { gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, gitPreparer.IsDynamicGitRepository)); var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem); var gitVersionContext = new GitVersionContext(repo, configuration, commitId : commitId); var semanticVersion = versionFinder.FindVersion(gitVersionContext); return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged); }
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot, IBuildServer buildServer, Config overrideConfig = null) { gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, gitPreparer.IsDynamicGitRepository)); var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem, overrideConfig: overrideConfig); var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId); var semanticVersion = versionFinder.FindVersion(gitVersionContext); return(VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged)); }
public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null, bool noCache = false) { // Normalise if we are running on build server var applicableBuildServers = BuildServerList.GetApplicableBuildServers(); var buildServer = applicableBuildServers.FirstOrDefault(); var fetch = noFetch || (buildServer != null && buildServer.PreventFetch()); var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, fetch, workingDirectory); gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, !string.IsNullOrWhiteSpace(dynamicRepositoryLocation))); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); // TODO Can't use this, it still needs work //var gitRepository = GitRepositoryFactory.CreateRepository(new RepositoryInfo //{ // Url = targetUrl, // Branch = targetBranch, // Authentication = new AuthenticationInfo // { // Username = authentication.Username, // Password = authentication.Password // }, // Directory = workingDirectory //}); Logger.WriteInfo(string.Format("Project root is: {0}", projectRoot)); Logger.WriteInfo(string.Format("DotGit directory is: {0}", dotGitDirectory)); if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot)) { // TODO Link to wiki article throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory)); } var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, overrideConfig); var versionVariables = noCache ? default(VersionVariables) : gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey); if (versionVariables == null) { versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig); if (!noCache) { try { gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables); } catch (AggregateException e) { Logger.WriteWarning(string.Format("One or more exceptions during cache write:{0}{1}", Environment.NewLine, e)); } } } return versionVariables; }
public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null) { // Normalise if we are running on build server var applicableBuildServers = BuildServerList.GetApplicableBuildServers(); var buildServer = applicableBuildServers.FirstOrDefault(); var fetch = noFetch || (buildServer != null && buildServer.PreventFetch()); var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, fetch, workingDirectory); gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, !string.IsNullOrWhiteSpace(dynamicRepositoryLocation))); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); // TODO Can't use this, it still needs work //var gitRepository = GitRepositoryFactory.CreateRepository(new RepositoryInfo //{ // Url = targetUrl, // Branch = targetBranch, // Authentication = new AuthenticationInfo // { // Username = authentication.Username, // Password = authentication.Password // }, // Directory = workingDirectory //}); Logger.WriteInfo(string.Format("Project root is: {0}", projectRoot)); Logger.WriteInfo(string.Format("DotGit directory is: {0}", dotGitDirectory)); if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot)) { // TODO Link to wiki article throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory)); } var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, overrideConfig); var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey); if (versionVariables == null) { versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig); try { gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables); } catch (AggregateException e) { Logger.WriteWarning(string.Format("One or more exceptions during cache write:{0}{1}", Environment.NewLine, e)); } } return(versionVariables); }
public void UpdatesExistingDynamicRepository() { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); string dynamicRepositoryPath = null; try { using (var mainRepositoryFixture = new EmptyRepositoryFixture(new Config())) { mainRepositoryFixture.Repository.MakeCommits(1); var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir); gitPreparer.Initialise(false, "master"); dynamicRepositoryPath = gitPreparer.GetDotGitDirectory(); var newCommit = mainRepositoryFixture.Repository.MakeACommit(); gitPreparer.Initialise(false, "master"); using (var repository = new Repository(dynamicRepositoryPath)) { mainRepositoryFixture.Repository.DumpGraph(); repository.DumpGraph(); repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha); } } } finally { Directory.Delete(tempDir, true); if (dynamicRepositoryPath != null) DeleteHelper.DeleteGitRepository(dynamicRepositoryPath); } }
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName) { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); string dynamicRepositoryPath = null; try { using (var fixture = new EmptyRepositoryFixture(new Config())) { var expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last()); fixture.Repository.MakeCommits(5); fixture.Repository.CreateFileAndCommit("TestFile.txt"); fixture.Repository.CreateBranch(SpecificBranchName); // Copy contents into working directory File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt")); var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir); gitPreparer.Initialise(false, branchName); dynamicRepositoryPath = gitPreparer.GetDotGitDirectory(); gitPreparer.IsDynamicGitRepository.ShouldBe(true); gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "\\.git"); using (var repository = new Repository(dynamicRepositoryPath)) { var currentBranch = repository.Head.CanonicalName; currentBranch.ShouldEndWith(expectedBranchName); } } } finally { Directory.Delete(tempDir, true); if (dynamicRepositoryPath != null) DeleteHelper.DeleteGitRepository(dynamicRepositoryPath); } }
public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken() { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); string expectedDynamicRepoLocation = null; try { using (var fixture = new EmptyRepositoryFixture(new Config())) { fixture.Repository.CreateFileAndCommit("TestFile.txt"); File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt")); expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last()); Directory.CreateDirectory(expectedDynamicRepoLocation); var arguments = new Arguments { TargetPath = tempDir, TargetUrl = fixture.RepositoryPath }; var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath); gitPreparer.Initialise(false); gitPreparer.IsDynamicGitRepository.ShouldBe(true); gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git"); } } finally { Directory.Delete(tempDir, true); if (expectedDynamicRepoLocation != null) Directory.Delete(expectedDynamicRepoLocation, true); if (expectedDynamicRepoLocation != null) DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1"); } }
public void TestErrorThrownForInvalidRepository() { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); try { var arguments = new Arguments { TargetPath = tempDir, TargetUrl = "http://127.0.0.1/testrepo.git" }; var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath); Assert.Throws<Exception>(() => gitPreparer.Initialise(true)); } finally { Directory.Delete(tempDir, true); } }
public void UsingDynamicRepositoryWithoutTargetBranchFails() { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); try { using (var mainRepositoryFixture = new EmptyRepositoryFixture(new Config())) { var commitId = mainRepositoryFixture.Repository.MakeACommit().Id.Sha; var arguments = new Arguments { TargetPath = tempDir, TargetUrl = mainRepositoryFixture.RepositoryPath, CommitId = commitId }; var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath); gitPreparer.Initialise(true); Assert.Throws<Exception>(() => gitPreparer.Initialise(true)); } } finally { Directory.Delete(tempDir, true); } }
public void TestErrorThrownForInvalidRepository() { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); try { var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), false, tempDir); Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master")); } finally { Directory.Delete(tempDir, true); } }
public void UsingDynamicRepositoryWithoutTargetBranchFails() { var repoName = Guid.NewGuid().ToString(); var tempPath = Path.GetTempPath(); var tempDir = Path.Combine(tempPath, repoName); Directory.CreateDirectory(tempDir); try { using (var mainRepositoryFixture = new EmptyRepositoryFixture(new Config())) { mainRepositoryFixture.Repository.MakeACommit(); var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir); Should.Throw<Exception>(() => gitPreparer.Initialise(true, null)); } } finally { Directory.Delete(tempDir, true); } }