From 98bd461fefc927015e3821a05f2de5b19b0cf171 Mon Sep 17 00:00:00 2001 From: Artur Stolear Date: Sat, 2 Nov 2019 16:03:19 +0200 Subject: [PATCH 1/2] GH-1874, fix the nofetch behaviour, it was not correctly initialized --- .../Configuration/NamedConfigFileLocator.cs | 3 +- src/GitVersionCore/GitPreparer.cs | 52 +++++++++---------- .../Helpers/GitRepositoryHelper.cs | 8 +-- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs b/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs index abd5748ebf..95c630dc74 100644 --- a/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs +++ b/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs @@ -10,8 +10,7 @@ public class NamedConfigFileLocator : ConfigFileLocator { public NamedConfigFileLocator(IFileSystem fileSystem, ILog log, IOptions options) : base(fileSystem, log) { - var arguments = options.Value; - var filePath = arguments.ConfigFile; + var filePath = options.Value.ConfigFile; if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException(nameof(filePath), "Empty file path provided!"); FilePath = filePath; } diff --git a/src/GitVersionCore/GitPreparer.cs b/src/GitVersionCore/GitPreparer.cs index e9a2cfd657..1254ee3218 100644 --- a/src/GitVersionCore/GitPreparer.cs +++ b/src/GitVersionCore/GitPreparer.cs @@ -13,7 +13,6 @@ public class GitPreparer : IGitPreparer private readonly ILog log; private readonly IEnvironment environment; private readonly string dynamicRepositoryLocation; - private readonly bool noFetch; private readonly Arguments arguments; private const string DefaultRemoteName = "origin"; @@ -29,7 +28,6 @@ public GitPreparer(ILog log, IEnvironment environment, IOptions optio WorkingDirectory = arguments.TargetPath.TrimEnd('/', '\\'); dynamicRepositoryLocation = arguments.DynamicRepositoryLocation; - noFetch = arguments.NoFetch; } public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false) @@ -39,23 +37,24 @@ public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shoul Username = arguments.Authentication?.Username, Password = arguments.Authentication?.Password }; - if (string.IsNullOrWhiteSpace(TargetUrl)) + if (!string.IsNullOrWhiteSpace(TargetUrl)) { - if (!normalizeGitDirectory) return; - using (log.IndentLog($"Normalizing git directory for branch '{currentBranch}'")) + var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation); + + dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch); + } + else + { + if (normalizeGitDirectory) { if (shouldCleanUpRemotes) { CleanupDuplicateOrigin(); } - GitRepositoryHelper.NormalizeGitDirectory(log, environment, GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository()); + + NormalizeGitDirectory(authentication, currentBranch, GetDotGitDirectory(), IsDynamicGitRepository()); } - return; } - - var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation); - - dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch); } public TResult WithRepository(Func action) @@ -108,7 +107,7 @@ private void CleanupDuplicateOrigin() { var remoteToKeep = DefaultRemoteName; - var repo = new Repository(GetDotGitDirectory()); + using var repo = new Repository(GetDotGitDirectory()); // check that we have a remote that matches defaultRemoteName if not take the first remote if (!repo.Network.Remotes.Any(remote => remote.Name.Equals(DefaultRemoteName, StringComparison.InvariantCultureIgnoreCase))) @@ -176,29 +175,28 @@ private string CreateDynamicRepository(string targetPath, AuthenticationInfo aut using (log.IndentLog($"Creating dynamic repository at '{targetPath}'")) { var gitDirectory = Path.Combine(targetPath, ".git"); - if (Directory.Exists(targetPath)) + if (!Directory.Exists(targetPath)) { - log.Info("Git repository already exists"); - using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'")) - { - GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, noFetch, targetBranch, true); - } - - return gitDirectory; + CloneRepository(repositoryUrl, gitDirectory, auth); } - - CloneRepository(repositoryUrl, gitDirectory, auth); - - using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'")) + else { - // Normalize (download branches) before using the branch - GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, noFetch, targetBranch, true); + log.Info("Git repository already exists"); } - + NormalizeGitDirectory(auth, targetBranch, gitDirectory, true); return gitDirectory; } } + private void NormalizeGitDirectory(AuthenticationInfo auth, string targetBranch, string gitDirectory, bool isDynamicRepository) + { + using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'")) + { + // Normalize (download branches) before using the branch + GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, arguments.NoFetch, targetBranch, isDynamicRepository); + } + } + private void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo auth) { Credentials credentials = null; diff --git a/src/GitVersionCore/Helpers/GitRepositoryHelper.cs b/src/GitVersionCore/Helpers/GitRepositoryHelper.cs index fc3f7cb7c7..7529c99190 100644 --- a/src/GitVersionCore/Helpers/GitRepositoryHelper.cs +++ b/src/GitVersionCore/Helpers/GitRepositoryHelper.cs @@ -10,8 +10,8 @@ namespace GitVersion.Helpers public static class GitRepositoryHelper { /// - /// Normalisation of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways. - /// It is not recommended to run normalisation against a local repository + /// Normalization of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways. + /// It is not recommended to run normalization against a local repository /// public static void NormalizeGitDirectory(ILog log, IEnvironment environment, string gitDirectory, AuthenticationInfo authentication, bool noFetch, string currentBranch, bool isDynamicRepository) @@ -265,7 +265,7 @@ private static IEnumerable GetRemoteTipsForAnonymousUser(Reposi private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(ILog log, Repository repo, string remoteName) { var prefix = $"refs/remotes/{remoteName}/"; - var remoteHeadCanonicalName = $"{prefix}{"HEAD"}"; + var remoteHeadCanonicalName = $"{prefix}HEAD"; foreach (var remoteTrackingReference in repo.Refs.FromGlob(prefix + "*").Where(r => r.CanonicalName != remoteHeadCanonicalName)) { @@ -299,7 +299,7 @@ private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(ILog log, } } - public static Remote EnsureOnlyOneRemoteIsDefined(ILog log, IRepository repo) + private static Remote EnsureOnlyOneRemoteIsDefined(ILog log, IRepository repo) { var remotes = repo.Network.Remotes; var howMany = remotes.Count(); From ca52e0ec44789cf6118d335b4183e202bc1c5c03 Mon Sep 17 00:00:00 2001 From: Artur Stolear Date: Sat, 2 Nov 2019 20:42:21 +0200 Subject: [PATCH 2/2] build with ubuntu 16.04 agents --- azure-pipelines.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 6906d59247..37730ad7d8 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -9,7 +9,7 @@ stages: 'Windows': VM_IMAGE: 'windows-latest' 'Linux': - VM_IMAGE: 'ubuntu-latest' + VM_IMAGE: 'ubuntu-16.04' 'macOS': VM_IMAGE: 'macOS-latest' pool: @@ -26,7 +26,7 @@ stages: - template: build/stages/artifacts-test.yml parameters: name: Linux - vmImage: 'ubuntu-latest' + vmImage: 'ubuntu-16.04' - template: build/stages/artifacts-test.yml parameters: name: Windows @@ -40,7 +40,7 @@ stages: - template: build/stages/docker.yml parameters: name: Linux - vmImage: 'ubuntu-latest' + vmImage: 'ubuntu-16.04' - template: build/stages/docker.yml parameters: name: Windows