Example #1
0
        protected override StepResult HandleResult(string result, Queue<ConfigInitWizardStep> steps, Config config, string workingDirectory)
        {
            int parsed;
            if (int.TryParse(result, out parsed))
            {
                if (parsed == 0)
                {
                    steps.Enqueue(new EditConfigStep(Console, FileSystem));
                    return StepResult.Ok();
                }

                try
                {
                    var foundBranch = OrderedBranches(config).ElementAt(parsed - 1);
                    var branchConfig = foundBranch.Value;
                    if (branchConfig == null)
                    {
                        branchConfig = new BranchConfig();
                        config.Branches.Add(foundBranch.Key, branchConfig);
                    }
                    steps.Enqueue(new ConfigureBranch(foundBranch.Key, branchConfig, Console, FileSystem));
                    return StepResult.Ok();
                }
                catch (ArgumentOutOfRangeException)
                { }
            }

            return StepResult.InvalidResponseSelected();
        }
Example #2
0
 public ConfigureBranch WithData(string configName, BranchConfig config)
 {
     this.branchConfig = config;
     this.name         = configName;
     return(this);
 }
    public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config configuration, BranchConfig currentBranchConfig)
    {
        var name = currentBranchConfig.Name;

        if (!currentBranchConfig.VersioningMode.HasValue)
        {
            throw new Exception($"Configuration value for 'Versioning mode' for branch {name} has no value. (this should not happen, please report an issue)");
        }
        if (!currentBranchConfig.Increment.HasValue)
        {
            throw new Exception($"Configuration value for 'Increment' for branch {name} has no value. (this should not happen, please report an issue)");
        }
        if (!currentBranchConfig.PreventIncrementOfMergedBranchVersion.HasValue)
        {
            throw new Exception($"Configuration value for 'PreventIncrementOfMergedBranchVersion' for branch {name} has no value. (this should not happen, please report an issue)");
        }
        if (!currentBranchConfig.TrackMergeTarget.HasValue)
        {
            throw new Exception($"Configuration value for 'TrackMergeTarget' for branch {name} has no value. (this should not happen, please report an issue)");
        }
        if (!currentBranchConfig.TracksReleaseBranches.HasValue)
        {
            throw new Exception($"Configuration value for 'TracksReleaseBranches' for branch {name} has no value. (this should not happen, please report an issue)");
        }
        if (!currentBranchConfig.IsReleaseBranch.HasValue)
        {
            throw new Exception($"Configuration value for 'IsReleaseBranch' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.AssemblyVersioningScheme.HasValue)
        {
            throw new Exception("Configuration value for 'AssemblyVersioningScheme' has no value. (this should not happen, please report an issue)");
        }
        if (!configuration.AssemblyFileVersioningScheme.HasValue)
        {
            throw new Exception("Configuration value for 'AssemblyFileVersioningScheme' has no value. (this should not happen, please report an issue)");
        }
        if (!configuration.CommitMessageIncrementing.HasValue)
        {
            throw new Exception("Configuration value for 'CommitMessageIncrementing' has no value. (this should not happen, please report an issue)");
        }
        if (!configuration.LegacySemVerPadding.HasValue)
        {
            throw new Exception("Configuration value for 'LegacySemVerPadding' has no value. (this should not happen, please report an issue)");
        }
        if (!configuration.BuildMetaDataPadding.HasValue)
        {
            throw new Exception("Configuration value for 'BuildMetaDataPadding' has no value. (this should not happen, please report an issue)");
        }
        if (!configuration.CommitsSinceVersionSourcePadding.HasValue)
        {
            throw new Exception("Configuration value for 'CommitsSinceVersionSourcePadding' has no value. (this should not happen, please report an issue)");
        }
        if (!configuration.TagPreReleaseWeight.HasValue)
        {
            throw new Exception("Configuration value for 'TagPreReleaseWeight' has no value. (this should not happen, please report an issue)");
        }

        var versioningMode    = currentBranchConfig.VersioningMode.Value;
        var tag               = currentBranchConfig.Tag;
        var tagNumberPattern  = currentBranchConfig.TagNumberPattern;
        var incrementStrategy = currentBranchConfig.Increment.Value;
        var preventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
        var trackMergeTarget = currentBranchConfig.TrackMergeTarget.Value;
        var preReleaseWeight = currentBranchConfig.PreReleaseWeight ?? 0;

        var nextVersion = configuration.NextVersion;
        var assemblyVersioningScheme     = configuration.AssemblyVersioningScheme.Value;
        var assemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value;
        var assemblyInformationalFormat  = configuration.AssemblyInformationalFormat;
        var assemblyVersioningFormat     = configuration.AssemblyVersioningFormat;
        var assemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
        var gitTagPrefix        = configuration.TagPrefix;
        var majorMessage        = configuration.MajorVersionBumpMessage;
        var minorMessage        = configuration.MinorVersionBumpMessage;
        var patchMessage        = configuration.PatchVersionBumpMessage;
        var noBumpMessage       = configuration.NoBumpMessage;
        var commitDateFormat    = configuration.CommitDateFormat;
        var updateBuildNumber   = configuration.UpdateBuildNumber ?? true;
        var tagPreReleaseWeight = configuration.TagPreReleaseWeight.Value;

        var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? configuration.CommitMessageIncrementing.Value;

        return(new EffectiveConfiguration(
                   assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix,
                   tag, nextVersion, incrementStrategy,
                   currentBranchConfig.Regex,
                   preventIncrementForMergedBranchVersion,
                   tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
                   trackMergeTarget,
                   majorMessage, minorMessage, patchMessage, noBumpMessage,
                   commitMessageVersionBump,
                   configuration.LegacySemVerPadding.Value,
                   configuration.BuildMetaDataPadding.Value,
                   configuration.CommitsSinceVersionSourcePadding.Value,
                   configuration.Ignore.ToFilters(),
                   currentBranchConfig.TracksReleaseBranches.Value,
                   currentBranchConfig.IsReleaseBranch.Value,
                   commitDateFormat,
                   updateBuildNumber,
                   preReleaseWeight,
                   tagPreReleaseWeight));
    }
 public ConfigureBranch(string name, BranchConfig branchConfig, IConsole console, IFileSystem fileSystem)
     : base(console, fileSystem)
 {
     this.branchConfig = branchConfig;
     this.name         = name;
 }
Example #5
0
 public SetBranchTag WithData(string name, BranchConfig branchConfig)
 {
     this.branchConfig = branchConfig;
     this.name         = name;
     return(this);
 }
 public SetBranchIncrementMode(string name, BranchConfig branchConfig, IConsole console, IFileSystem fileSystem)
     : base(console, fileSystem)
 {
     this.name         = name;
     this.branchConfig = branchConfig;
 }
    private static BranchConfig?ChooseMainOrDevelopIncrementStrategyIfTheChosenBranchIsOneOfThem(IBranch chosenBranch, BranchConfig branchConfiguration, Config config)
    {
        BranchConfig?mainOrDevelopConfig = null;
        var          developBranchRegex  = config.Branches[Config.DevelopBranchKey]?.Regex;
        var          mainBranchRegex     = config.Branches[Config.MainBranchKey]?.Regex;

        if (Regex.IsMatch(chosenBranch.Name.Friendly, developBranchRegex, RegexOptions.IgnoreCase))
        {
            // Normally we would not expect this to happen but for safety we add a check
            if (config.Branches[Config.DevelopBranchKey]?.Increment !=
                IncrementStrategy.Inherit)
            {
                mainOrDevelopConfig = new BranchConfig(branchConfiguration)
                {
                    Increment = config.Branches[Config.DevelopBranchKey]?.Increment
                };
            }
        }
        else if (Regex.IsMatch(chosenBranch.Name.Friendly, mainBranchRegex, RegexOptions.IgnoreCase))
        {
            // Normally we would not expect this to happen but for safety we add a check
            if (config.Branches[Config.MainBranchKey]?.Increment !=
                IncrementStrategy.Inherit)
            {
                mainOrDevelopConfig = new BranchConfig(branchConfiguration)
                {
                    Increment = config.Branches[Config.DevelopBranchKey]?.Increment
                };
            }
        }
        return(mainOrDevelopConfig);
    }
Example #8
0
 public SetBranchTag(string name, BranchConfig branchConfig)
 {
     this.name = name;
     this.branchConfig = branchConfig;
 }
 public SetBranchIncrementMode(string name, BranchConfig branchConfig)
 {
     this.name = name;
     this.branchConfig = branchConfig;
 }
 public SetBranchTag(string name, BranchConfig branchConfig, IConsole console, IFileSystem fileSystem, ILog log)
     : base(console, fileSystem, log)
 {
     this.name         = name;
     this.branchConfig = branchConfig;
 }
    public EffectiveConfiguration(Config configuration, BranchConfig currentBranchConfig)
    {
        Configuration = configuration.NotNull();
        currentBranchConfig.NotNull();

        var name = currentBranchConfig.Name;

        if (!currentBranchConfig.VersioningMode.HasValue)
        {
            throw new Exception($"Configuration value for 'Versioning mode' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!currentBranchConfig.Increment.HasValue)
        {
            throw new Exception($"Configuration value for 'Increment' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!currentBranchConfig.PreventIncrementOfMergedBranchVersion.HasValue)
        {
            throw new Exception($"Configuration value for 'PreventIncrementOfMergedBranchVersion' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!currentBranchConfig.TrackMergeTarget.HasValue)
        {
            throw new Exception($"Configuration value for 'TrackMergeTarget' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!currentBranchConfig.TracksReleaseBranches.HasValue)
        {
            throw new Exception($"Configuration value for 'TracksReleaseBranches' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!currentBranchConfig.IsReleaseBranch.HasValue)
        {
            throw new Exception($"Configuration value for 'IsReleaseBranch' for branch {name} has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.AssemblyVersioningScheme.HasValue)
        {
            throw new Exception("Configuration value for 'AssemblyVersioningScheme' has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.AssemblyFileVersioningScheme.HasValue)
        {
            throw new Exception("Configuration value for 'AssemblyFileVersioningScheme' has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.CommitMessageIncrementing.HasValue)
        {
            throw new Exception("Configuration value for 'CommitMessageIncrementing' has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.LegacySemVerPadding.HasValue)
        {
            throw new Exception("Configuration value for 'LegacySemVerPadding' has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.BuildMetaDataPadding.HasValue)
        {
            throw new Exception("Configuration value for 'BuildMetaDataPadding' has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.CommitsSinceVersionSourcePadding.HasValue)
        {
            throw new Exception("Configuration value for 'CommitsSinceVersionSourcePadding' has no value. (this should not happen, please report an issue)");
        }

        if (!configuration.TagPreReleaseWeight.HasValue)
        {
            throw new Exception("Configuration value for 'TagPreReleaseWeight' has no value. (this should not happen, please report an issue)");
        }

        AssemblyVersioningScheme     = configuration.AssemblyVersioningScheme.Value;
        AssemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value;
        AssemblyInformationalFormat  = configuration.AssemblyInformationalFormat;
        AssemblyVersioningFormat     = configuration.AssemblyVersioningFormat;
        AssemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
        VersioningMode     = currentBranchConfig.VersioningMode.Value;
        GitTagPrefix       = configuration.TagPrefix;
        Tag                = currentBranchConfig.Tag;
        NextVersion        = configuration.NextVersion;
        Increment          = currentBranchConfig.Increment.Value;
        BranchPrefixToTrim = currentBranchConfig.Regex;
        PreventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
        TagNumberPattern = currentBranchConfig.TagNumberPattern;
        ContinuousDeploymentFallbackTag = configuration.ContinuousDeploymentFallbackTag;
        TrackMergeTarget                 = currentBranchConfig.TrackMergeTarget.Value;
        MajorVersionBumpMessage          = configuration.MajorVersionBumpMessage;
        MinorVersionBumpMessage          = configuration.MinorVersionBumpMessage;
        PatchVersionBumpMessage          = configuration.PatchVersionBumpMessage;
        NoBumpMessage                    = configuration.NoBumpMessage;
        CommitMessageIncrementing        = currentBranchConfig.CommitMessageIncrementing ?? configuration.CommitMessageIncrementing.Value;
        LegacySemVerPadding              = configuration.LegacySemVerPadding.Value;
        BuildMetaDataPadding             = configuration.BuildMetaDataPadding.Value;
        CommitsSinceVersionSourcePadding = configuration.CommitsSinceVersionSourcePadding.Value;
        VersionFilters                   = configuration.Ignore.ToFilters();
        TracksReleaseBranches            = currentBranchConfig.TracksReleaseBranches.Value;
        IsCurrentBranchRelease           = currentBranchConfig.IsReleaseBranch.Value;
        CommitDateFormat                 = configuration.CommitDateFormat;
        UpdateBuildNumber                = configuration.UpdateBuildNumber ?? true;
        PreReleaseWeight                 = currentBranchConfig.PreReleaseWeight ?? 0;
        TagPreReleaseWeight              = configuration.TagPreReleaseWeight.Value;
    }
Example #12
0
        public void UsesFirstBranchConfigWhenMultipleMatch()
        {
            using var fixture = new EmptyRepositoryFixture();

            var branchConfig = new BranchConfig
            {
                VersioningMode = VersioningMode.Mainline,
                Increment      = IncrementStrategy.None,
                PreventIncrementOfMergedBranchVersion = false,
                TrackMergeTarget      = false,
                TracksReleaseBranches = false,
                IsReleaseBranch       = false,
                SourceBranches        = new HashSet <string>()
            };
            var config = new ConfigurationBuilder()
                         .Add(new Config
            {
                VersioningMode = VersioningMode.ContinuousDelivery,
                Branches       =
                {
                    { "release/latest", new BranchConfig(branchConfig)
                                          {
                                              Increment = IncrementStrategy.None, Regex = "release/latest"
                                          } },
                    { "release", new BranchConfig(branchConfig)
                                          {
                                              Increment = IncrementStrategy.Patch, Regex = "releases?[/-]"
                                          } }
                }
            })
                         .Build();

            var releaseLatestBranch = new MockBranch("release/latest")
            {
                new MockCommit {
                    CommitterEx = Generate.SignatureNow()
                }
            };
            var releaseVersionBranch = new MockBranch("release/1.0.0")
            {
                new MockCommit {
                    CommitterEx = Generate.SignatureNow()
                }
            };

            var mockRepository = new MockRepository
            {
                Branches = new MockBranchCollection
                {
                    releaseLatestBranch,
                    releaseVersionBranch
                },
                Head = releaseLatestBranch
            };

            var latestContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseLatestBranch.CanonicalName, config);

            latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None);

            mockRepository.Head = releaseVersionBranch;
            var versionContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseVersionBranch.CanonicalName, config);

            versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch);
        }
Example #13
0
 public ConfigureBranch WithData(string name, BranchConfig branchConfig)
 {
     this.branchConfig = branchConfig;
     this.name         = name;
     return(this);
 }
Example #14
0
 public ConfigureBranch(string name, BranchConfig branchConfig)
 {
     this.branchConfig = branchConfig;
     this.name = name;
 }
    // TODO I think we need to take a fresh approach to this.. it's getting really complex with heaps of edge cases
    private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConfig branchConfiguration, ICommit?currentCommit, Config configuration, IList <IBranch>?excludedInheritBranches)
    {
        using (this.log.IndentLog("Attempting to inherit branch configuration from parent branch"))
        {
            var excludedBranches = new[] { targetBranch };
            // Check if we are a merge commit. If so likely we are a pull request
            var parentCount = currentCommit?.Parents.Count();
            if (parentCount == 2)
            {
                excludedBranches = CalculateWhenMultipleParents(currentCommit !, ref targetBranch, excludedBranches);
            }

            excludedInheritBranches ??= this.repositoryStore.GetExcludedInheritBranches(configuration).ToList();

            excludedBranches = excludedBranches.Where(b => excludedInheritBranches.All(bte => !b.Equals(bte))).ToArray();
            // Add new excluded branches.
            foreach (var excludedBranch in excludedBranches)
            {
                excludedInheritBranches.Add(excludedBranch);
            }
            var branchesToEvaluate = this.repositoryStore.ExcludingBranches(excludedInheritBranches)
                                     .Distinct(new LocalRemoteBranchEqualityComparer())
                                     .ToList();

            var branchPoint = this.repositoryStore
                              .FindCommitBranchWasBranchedFrom(targetBranch, configuration, excludedInheritBranches.ToArray());
            List <IBranch> possibleParents;
            if (branchPoint == BranchCommit.Empty)
            {
                possibleParents = this.repositoryStore.GetBranchesContainingCommit(targetBranch.Tip, branchesToEvaluate)
                                  // It fails to inherit Increment branch configuration if more than 1 parent;
                                  // therefore no point to get more than 2 parents
                                  .Take(2)
                                  .ToList();
            }
            else
            {
                var branches = this.repositoryStore.GetBranchesContainingCommit(branchPoint.Commit, branchesToEvaluate).ToList();
                if (branches.Count > 1)
                {
                    var currentTipBranches = this.repositoryStore.GetBranchesContainingCommit(currentCommit, branchesToEvaluate).ToList();
                    possibleParents = branches.Except(currentTipBranches).ToList();
                }
                else
                {
                    possibleParents = branches;
                }
            }

            this.log.Info("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.ToString())));

            if (possibleParents.Count == 1)
            {
                var branchConfig = GetBranchConfiguration(possibleParents[0], currentCommit, configuration, excludedInheritBranches);
                // If we have resolved a fallback config we should not return that we have got config
                if (branchConfig.Name != FallbackConfigName)
                {
                    return(new BranchConfig(branchConfiguration)
                    {
                        Increment = branchConfig.Increment,
                        PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion,
                        // If we are inheriting from develop then we should behave like develop
                        TracksReleaseBranches = branchConfig.TracksReleaseBranches
                    });
                }
            }

            // If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
            // if develop exists and main if not
            var errorMessage = possibleParents.Count == 0
                ? "Failed to inherit Increment branch configuration, no branches found."
                : "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.ToString()));

            var chosenBranch = this.repositoryStore.GetChosenBranch(configuration);
            if (chosenBranch == null)
            {
                // TODO We should call the build server to generate this exception, each build server works differently
                // for fetch issues and we could give better warnings.
                throw new InvalidOperationException("Gitversion could not determine which branch to treat as the development branch (default is 'develop') nor releaseable branch (default is 'main' or 'master'), either locally or remotely. Ensure the local clone and checkout match the requirements or considering using 'GitVersion Dynamic Repositories'");
            }

            this.log.Warning($"{errorMessage}{System.Environment.NewLine}Falling back to {chosenBranch} branch config");

            // To prevent infinite loops, make sure that a new branch was chosen.
            if (targetBranch.Equals(chosenBranch))
            {
                var developOrMainConfig =
                    ChooseMainOrDevelopIncrementStrategyIfTheChosenBranchIsOneOfThem(
                        chosenBranch, branchConfiguration, configuration);
                if (developOrMainConfig != null)
                {
                    return(developOrMainConfig);
                }

                this.log.Warning("Fallback branch wants to inherit Increment branch configuration from itself. Using patch increment instead.");
                return(new BranchConfig(branchConfiguration)
                {
                    Increment = IncrementStrategy.Patch
                });
            }

            var inheritingBranchConfig = GetBranchConfiguration(chosenBranch, currentCommit, configuration, excludedInheritBranches) !;
            var configIncrement        = inheritingBranchConfig.Increment;
            if (inheritingBranchConfig.Name !.IsEquivalentTo(FallbackConfigName) && configIncrement == IncrementStrategy.Inherit)
            {
                this.log.Warning("Fallback config inherits by default, dropping to patch increment");
                configIncrement = IncrementStrategy.Patch;
            }
            return(new BranchConfig(branchConfiguration)
            {
                Increment = configIncrement,
                PreventIncrementOfMergedBranchVersion = inheritingBranchConfig.PreventIncrementOfMergedBranchVersion,
                // If we are inheriting from develop then we should behave like develop
                TracksReleaseBranches = inheritingBranchConfig.TracksReleaseBranches
            });
        }
    }
Example #16
0
 public SetBranchIncrementMode WithData(string name, BranchConfig branchConfig)
 {
     this.branchConfig = branchConfig;
     this.name         = name;
     return(this);
 }
 public ConfigureBranch(string name, BranchConfig branchConfig, IConsole console, IFileSystem fileSystem) 
     : base(console, fileSystem)
 {
     this.branchConfig = branchConfig;
     this.name = name;
 }
Example #18
0
 public SetBranchTag(string name, BranchConfig branchConfig)
 {
     this.name         = name;
     this.branchConfig = branchConfig;
 }
 public SetBranchIncrementMode(string name, BranchConfig branchConfig, IConsole console, IFileSystem fileSystem)
     : base(console, fileSystem)
 {
     this.name = name;
     this.branchConfig = branchConfig;
 }
Example #20
0
 public IdentityClient(BranchConfig options) : base(options)
 {
 }