예제 #1
0
        /// <summary>
        /// Attempts to check if the repository has uncommitted changes.
        /// </summary>
        /// <param name="gitRunner"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public static GitInfo GetGitStatus(IGitRunner gitRunner, ITaskLogger logger)
        {
            GitInfo status = new GitInfo();

            try
            {
                string statusText = gitRunner.GetTextFromProcess(GitArgument.Status);
                Match  match      = StatusBranchSearch.Match(statusText);
                if (match.Success && match.Groups.Count > 1)
                {
                    string branch = match.Groups[1].Value;
                    status.Branch = branch;
                }
                else
                {
                    Match detatchedMatch = DetatchedBranchSearch.Match(statusText);
                    if (detatchedMatch.Success && detatchedMatch.Groups.Count > 1)
                    {
                        string branch = detatchedMatch.Groups[1].Value;
                        status.Branch = branch;
                        if (branch.StartsWith("pull/"))
                        {
                            status.IsPullRequest = true;
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(status.Branch))
                {
                    logger.LogMessage(MessageImportance.High, $"Unable to retrieve branch name from status text: \n{statusText}");
                }
                statusText = statusText.ToUpper();
                if (statusText.Contains(UnmodifiedText) || statusText.Contains(UntrackedOnlyText))
                {
                    status.Modified = "Unmodified";
                }
                else
                {
                    status.Modified = "Modified";
                }
            }
            catch (GitRunnerException ex)
            {
                logger?.LogWarning($"Error getting 'git status': {ex.Message}");
            }
            try
            {
                status.OriginUrl = gitRunner.GetTextFromProcess(GitArgument.OriginUrl);
                if (status.OriginUrl != null && status.OriginUrl.Length > 0)
                {
                    status.GitUser = GetGitHubUser(status.OriginUrl);
                }
            }
            catch (GitRunnerException ex)
            {
                logger?.LogWarning($"Error getting git origin URL: {ex.Message}");
            }
            return(status);
        }
        private GitVersionCalculator CreateGitVersionCalculator(out IGitRunner fakeGitRunner)
        {
            GitVersionCalculator gitVersionCalculator = new GitVersionCalculator();

            fakeGitRunner = A.Fake <IGitRunner>();
            gitVersionCalculator.GetType()
            .GetField(
                "gitRunner",
                System.Reflection.BindingFlags.NonPublic
                | System.Reflection.BindingFlags.Instance)
            .SetValue(gitVersionCalculator, fakeGitRunner);
            return(gitVersionCalculator);
        }
예제 #3
0
 /// <summary>
 /// Attempts to retrieve the git commit hash using the 'git' program.
 /// </summary>
 /// <param name="gitRunner"></param>
 /// <param name="logger"></param>
 /// <param name="commitHash"></param>
 /// <returns></returns>
 public static bool TryGetGitCommit(IGitRunner gitRunner, ITaskLogger logger, out string commitHash)
 {
     commitHash = null;
     try
     {
         string outText = gitRunner.GetTextFromProcess(GitArgument.CommitHash);
         if (outText.Length > 0)
         {
             commitHash = outText;
             return(true);
         }
     }
     catch (GitRunnerException ex)
     {
         logger?.LogWarning($"Error getting commit hash from 'git' command: {ex.Message}");
     }
     return(false);
 }
예제 #4
0
        /// <summary>
        /// Attempts to check if the repository has uncommitted changes.
        /// </summary>
        /// <param name="gitRunner"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public static GitInfo GetGitStatus(IGitRunner gitRunner, ITaskLogger logger)
        {
            GitInfo status = new GitInfo();

            try
            {
                string statusText = gitRunner.GetTextFromProcess(GitArgument.Status);
                Match  match      = StatusBranchSearch.Match(statusText);
                if (match.Success && match.Groups.Count > 1)
                {
                    string branch = match.Groups[1].Value;
                    status.Branch = branch;
                }
                statusText = statusText.ToUpper();
                if (statusText.Contains(UnmodifiedText) || statusText.Contains(UntrackedOnlyText))
                {
                    status.Modified = "Unmodified";
                }
                else
                {
                    status.Modified = "Modified";
                }
            }
            catch (GitRunnerException ex)
            {
                logger?.LogWarning($"Error getting 'git status': {ex.Message}");
            }
            try
            {
                status.OriginUrl = gitRunner.GetTextFromProcess(GitArgument.OriginUrl);
                if (status.OriginUrl != null && status.OriginUrl.Length > 0)
                {
                    status.GitUser = GetGitHubUser(status.OriginUrl);
                }
            }
            catch (GitRunnerException ex)
            {
                logger?.LogWarning($"Error getting git origin URL: {ex.Message}");
            }
            return(status);
        }
예제 #5
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>true if successful</returns>
        public override bool Execute()
        {
            if (this.BuildEngine != null)
            {
                Logger = new LogWrapper(Log);
            }
            else
            {
                Logger = new MockTaskLogger();
            }
            if (GitRunner == null)
            {
                GitRunner = new GitCommandRunner(ProjectDir);
            }
            CommitHash = "local";
            string errorCode = null;

            string[] gitPaths = new string[] {
                Path.GetFullPath(Path.Combine(ProjectDir, GitDirectory)),
                Path.GetFullPath(Path.Combine(ProjectDir, "..", GitDirectory))
            };
            try
            {
                string commitHash = null;
                if (!NoGit && TryGetGitCommit(GitRunner, Logger, out commitHash) && commitHash.Length > 0)
                {
                    CommitHash = commitHash.Substring(0, Math.Min(commitHash.Length, HashLength));
                    if (!SkipStatus)
                    {
                        GitInfo gitStatus = GetGitStatus(GitRunner, Logger);
                        if (!string.IsNullOrWhiteSpace(gitStatus.Branch))
                        {
                            Branch = gitStatus.Branch;
                        }
                        if (!string.IsNullOrWhiteSpace(gitStatus.Modified))
                        {
                            Modified = gitStatus.Modified;
                        }
                        if (!string.IsNullOrWhiteSpace(gitStatus.OriginUrl))
                        {
                            OriginUrl = gitStatus.OriginUrl;
                        }
                        if (!string.IsNullOrWhiteSpace(gitStatus.GitUser))
                        {
                            GitUser = gitStatus.GitUser;
                        }
                    }
                    if (string.IsNullOrWhiteSpace(Branch))
                    {
                        if (TryGetCommitManual(gitPaths, out GitInfo manualInfo))
                        {
                            Branch = manualInfo.Branch;
                        }
                    }
                }
                else
                {
                    if (TryGetCommitManual(gitPaths, out GitInfo gitInfo))
                    {
                        commitHash = gitInfo.CommitHash;
                        if (commitHash.Length > 0)
                        {
                            CommitHash = commitHash
                                         .Substring(0,
                                                    Math.Min(commitHash.Length, HashLength));
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.Branch))
                        {
                            Branch = gitInfo.Branch;
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.Modified))
                        {
                            Modified = gitInfo.Modified;
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.OriginUrl))
                        {
                            OriginUrl = gitInfo.OriginUrl;
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.GitUser))
                        {
                            GitUser = gitInfo.GitUser;
                        }
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(errorCode))
                {
                    errorCode = MessageCodes.GetCommitInfo.GitFailed;
                }
                if (BuildEngine != null)
                {
                    int line   = BuildEngine.LineNumberOfTaskNode;
                    int column = BuildEngine.ColumnNumberOfTaskNode;
                    Logger.LogMessage(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column,
                                      MessageImportance.High, $"Error in {GetType().Name}: {ex.Message}");
                }
                else
                {
                    Logger.LogMessage(null, errorCode, null, null, 0, 0, 0, 0,
                                      MessageImportance.High, $"Error in {GetType().Name}: {ex.Message}");
                }
            }
#pragma warning restore CA1031 // Do not catch general exception types
            if (CommitHash == "local")
            {
                if (BuildEngine != null)
                {
                    errorCode = MessageCodes.GetCommitInfo.GitNoRepository;
                    int line   = BuildEngine.LineNumberOfTaskNode;
                    int column = BuildEngine.ColumnNumberOfTaskNode;
                    Logger.LogMessage(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column,
                                      MessageImportance.High, "Project does not appear to be in a git repository.");
                }
                else
                {
                    Logger.LogMessage(null, errorCode, null, null, 0, 0, 0, 0,
                                      MessageImportance.High, "Project does not appear to be in a git repository.");
                }
            }
            return(true);
        }
예제 #6
0
 /// <summary>
 /// Creates a new instance of the version calculator
 /// </summary>
 public GitVersionCalculator()
 {
     gitRunner = new GitRunner();
 }