Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Attempts to retrieve the git commit hash by reading git files.
 /// </summary>
 /// <param name="gitPaths"></param>
 /// <param name="gitInfo"></param>
 /// <returns></returns>
 public static bool TryGetCommitManual(string[] gitPaths, out GitInfo gitInfo)
 {
     gitInfo = new GitInfo();
     for (int i = 0; i < gitPaths.Length; i++)
     {
         if (TryGetCommitManual(gitPaths[i], out GitInfo retVal))
         {
             gitInfo = retVal;
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempts to retrieve the git commit hash by reading git files.
        /// </summary>
        /// <param name="gitPath"></param>
        /// <param name="gitInfo"></param>
        /// <returns></returns>
        public static bool TryGetCommitManual(string gitPath, out GitInfo gitInfo)
        {
            gitInfo = new GitInfo();
            bool   success    = false;
            string headPath   = Path.Combine(gitPath, "HEAD");
            string configPath = Path.Combine(gitPath, "config");

            if (File.Exists(headPath))
            {
                string headContents = File.ReadAllText(headPath);
                if (!string.IsNullOrEmpty(headContents) && headContents.StartsWith("ref:"))
                {
                    headPath = Path.Combine(gitPath, headContents.Replace("ref:", "").Trim());
                }
                gitInfo.Branch = headPath.Substring(headPath.LastIndexOf('/') + 1);
                if (File.Exists(headPath))
                {
                    headContents = File.ReadAllText(headPath);
                    if (headContents.Length >= 0)
                    {
                        gitInfo.CommitHash = headContents.Trim();
                        success            = true;
                    }
                }
            }
            if (File.Exists(configPath))
            {
                string configContents = File.ReadAllText(configPath);
                if (!string.IsNullOrEmpty(configContents))
                {
                    Match match = OriginSearch.Match(configContents);
                    if (match.Success && match.Groups.Count > 1)
                    {
                        gitInfo.OriginUrl = match.Groups[1].Value?.Trim();
                        if (gitInfo.OriginUrl != null && gitInfo.OriginUrl.Length > 0)
                        {
                            gitInfo.GitUser = GetGitHubUser(gitInfo.OriginUrl);
                        }
                    }
                }
            }
            return(success);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }