예제 #1
0
        public VersionVariables CalculateVersionVariables()
        {
            var buildServer = buildServerResolver.Resolve();

            // Normalize if we are running on build server
            var normalizeGitDirectory = !arguments.NoNormalize && buildServer != null;
            var shouldCleanUpRemotes  = buildServer != null && buildServer.ShouldCleanUpRemotes();

            var currentBranch = ResolveCurrentBranch(buildServer, arguments.TargetBranch, !string.IsNullOrWhiteSpace(arguments.DynamicRepositoryLocation));

            gitPreparer.Prepare(normalizeGitDirectory, currentBranch, shouldCleanUpRemotes);

            var dotGitDirectory = gitPreparer.GetDotGitDirectory();
            var projectRoot     = gitPreparer.GetProjectRootDirectory();

            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 '{arguments.TargetPath}'.");
            }

            return(GetCachedGitVersionInfo(arguments.TargetBranch, arguments.CommitId, arguments.OverrideConfig, arguments.NoCache));
        }
예제 #2
0
        public VersionVariables CalculateVersionVariables()
        {
            gitPreparer.Prepare(); //we need to prepare the repository before using it for version calculation

            var gitVersionOptions = options.Value;

            var cacheKey = cacheKeyFactory.Create(gitVersionOptions.ConfigInfo.OverrideConfig);

            var versionVariables = gitVersionOptions.Settings.NoCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(cacheKey);

                                   if (versionVariables != null)
                                   {
                                       return(versionVariables);
                                   }

                                   var semanticVersion = nextVersionCalculator.FindVersion();

                                   versionVariables = variableProvider.GetVariablesFor(semanticVersion, context.Configuration, context.IsCurrentCommitTagged);

                                   if (gitVersionOptions.Settings.NoCache)
                                   {
                                       return(versionVariables);
                                   }
                                   try
                                   {
                                       gitVersionCache.WriteVariablesToDiskCache(cacheKey, versionVariables);
                                   }
                                   catch (AggregateException e)
                                   {
                                       log.Warning($"One or more exceptions during cache write:{System.Environment.NewLine}{e}");
                                   }

                                   return(versionVariables);
        }
예제 #3
0
 public GitVersionTaskExecutor(IFileSystem fileSystem, ILog log, IBuildServerResolver buildServerResolver, IGitVersionCalculator gitVersionCalculator, IGitPreparer preparer)
 {
     this.fileSystem          = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     this.log                 = log ?? throw new ArgumentNullException(nameof(log));
     this.buildServerResolver = buildServerResolver ?? throw new ArgumentNullException(nameof(buildServerResolver));
     preparer.Prepare();
     versionVariables = gitVersionCalculator.CalculateVersionVariables();
 }
예제 #4
0
        public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
        {
            using var fixture = new EmptyRepositoryFixture();
            var arguments = new Arguments
            {
                TargetPath   = fixture.RepositoryPath,
                TargetUrl    = "https://github.com/GitTools/GitVersion.git",
                TargetBranch = "refs/head/master"
            };

            var gitVersionCalculator = GetGitVersionCalculator(arguments);

            gitPreparer.Prepare();
            gitVersionCalculator.CalculateVersionVariables();
        }
예제 #5
0
        private int VerifyArgumentsAndRun(Arguments arguments)
        {
            try
            {
                if (HandleNonMainCommand(arguments, out var exitCode))
                {
                    return(exitCode);
                }

                gitPreparer.Prepare();
                var variables = gitVersionCalculator.CalculateVersionVariables();
                execCommand.Execute(variables);
            }
            catch (WarningException exception)
            {
                var error = $"An error occurred:{System.Environment.NewLine}{exception.Message}";
                log.Warning(error);
                return(1);
            }
            catch (Exception exception)
            {
                var error = $"An unexpected error occurred:{System.Environment.NewLine}{exception}";
                log.Error(error);

                if (arguments == null)
                {
                    return(1);
                }

                log.Info("Attempting to show the current git graph (please include in issue): ");
                log.Info("Showing max of 100 commits");

                try
                {
                    LibGitExtensions.DumpGraph(arguments.TargetPath, mess => log.Info(mess), 100);
                }
                catch (Exception dumpGraphException)
                {
                    log.Error("Couldn't dump the git graph due to the following error: " + dumpGraphException);
                }
                return(1);
            }

            return(0);
        }
예제 #6
0
        public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.Repository.MakeACommit();

            var gitVersionOptions = new GitVersionOptions
            {
                WorkingDirectory = fixture.RepositoryPath,
                RepositoryInfo   =
                {
                    TargetUrl    = "https://github.com/GitTools/GitVersion.git",
                    TargetBranch = "refs/head/master"
                }
            };

            var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, repository: fixture.Repository);

            gitPreparer.Prepare();
            gitVersionCalculator.CalculateVersionVariables();
        }
예제 #7
0
 private static IRepository InitRepository(IOptions <Arguments> options, IGitPreparer preparer)
 {
     preparer.Prepare(); //we need to prepare the repository before using it for version calculation
     return(new Repository(options.Value.DotGitDirectory));
 }