From 95589ca1524914486a35b11baf219ff76841690c Mon Sep 17 00:00:00 2001 From: "Liangying.Wei" Date: Fri, 29 Sep 2017 10:26:09 +0800 Subject: [PATCH] Use a shared var to save the value for git existence (#2090) --- .../Git/GitUtility.cs | 91 +++++-------------- .../GitUtilityTest.cs | 14 --- 2 files changed, 22 insertions(+), 83 deletions(-) diff --git a/src/Microsoft.DocAsCode.Common/Git/GitUtility.cs b/src/Microsoft.DocAsCode.Common/Git/GitUtility.cs index 581d1785deb..2198db0ef6e 100644 --- a/src/Microsoft.DocAsCode.Common/Git/GitUtility.cs +++ b/src/Microsoft.DocAsCode.Common/Git/GitUtility.cs @@ -2,19 +2,19 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. namespace Microsoft.DocAsCode.Common.Git -{ +{ using System; using System.Collections.Concurrent; using System.Diagnostics; using System.IO; using System.Text; + using Microsoft.DocAsCode.Plugins; + public static class GitUtility { private static readonly string CommandName = "git"; private static readonly int GitTimeOut = 1000; - private static readonly Lazy _existGitCommand = - new Lazy(() => CommandUtility.ExistCommand(CommandName)); private static readonly string GetRepoRootCommand = "rev-parse --show-toplevel"; private static readonly string GetLocalBranchCommand = "rev-parse --abbrev-ref HEAD"; @@ -22,10 +22,9 @@ public static class GitUtility private static readonly string GetRemoteBranchCommand = "rev-parse --abbrev-ref @{u}"; private static readonly string GetDeletedFileContentCommand = "show {0}^:{1}"; private static readonly string GetFileLastCommitIdCommand = "rev-list --max-count=1 --all -- {0}"; + // TODO: only get default remote's url currently. private static readonly string GetOriginUrlCommand = "config --get remote.origin.url"; - private static readonly string GetLocalHeadIdCommand = "rev-parse HEAD"; - private static readonly string GetRemoteHeadIdCommand = "rev-parse @{u}"; private static readonly string[] BuildSystemBranchName = new[] { @@ -39,6 +38,9 @@ public static class GitUtility private static readonly ConcurrentDictionary Cache = new ConcurrentDictionary(); + private static bool? GitCommandExists = null; + private static object SyncRoot = new object(); + public static GitDetail TryGetFileDetail(string filePath) { GitDetail detail = null; @@ -55,45 +57,15 @@ public static GitDetail TryGetFileDetail(string filePath) public static GitDetail GetFileDetail(string filePath) { - if (string.IsNullOrEmpty(filePath)) + if (string.IsNullOrEmpty(filePath) || !ExistGitCommand()) { return null; } - if (!Path.IsPathRooted(filePath)) - { - throw new GitException($"{nameof(filePath)} should be an absolute path"); - } + var path = Path.Combine(EnvironmentContext.BaseDirectory, filePath).ToNormalizedPath(); - if (!ExistGitCommand()) - { - throw new GitException("Can't find git command in current environment"); - } - - filePath = PathUtility.NormalizePath(filePath); - var detail = GetFileDetailCore(filePath); + var detail = GetFileDetailCore(path); return detail; - } - - public static string GetDeletedFileContent(string filePath) - { - if (string.IsNullOrEmpty(filePath)) - { - return null; - } - - if (!Path.IsPathRooted(filePath)) - { - throw new GitException($"{nameof(filePath)} should be an absolute path"); - } - - if (!ExistGitCommand()) - { - throw new GitException("Can't find git command in current environment"); - } - - filePath = PathUtility.NormalizePath(filePath); - return GetDeletedFileContentCore(filePath); } public static GitRepoInfo GetRepoInfo(string directory) @@ -118,36 +90,6 @@ public static GitRepoInfo GetRepoInfo(string directory) } #region Private Methods - private static string GetDeletedFileContentCore(string filePath) - { - string directory; - if (PathUtility.IsDirectory(filePath)) - { - directory = filePath; - } - else - { - directory = Path.GetDirectoryName(filePath); - } - - var repoInfo = Cache.GetOrAdd(directory, GetRepoInfo); - - if (repoInfo == null) - { - return null; - } - - var getFileLastCommitIdCommand = string.Format(GetFileLastCommitIdCommand, PathUtility.MakeRelativePath(repoInfo.RepoRootPath, filePath)); - var lastCommitId = TryRunGitCommandAndGetLastLine(repoInfo.RepoRootPath, getFileLastCommitIdCommand); - - if (lastCommitId == null) - { - return null; - } - - var getDeletedFileContentCommand = string.Format(GetDeletedFileContentCommand, lastCommitId, PathUtility.MakeRelativePath(repoInfo.RepoRootPath, filePath)); - return TryRunGitCommand(repoInfo.RepoRootPath, getDeletedFileContentCommand); - } private static bool IsGitRoot(string directory) { @@ -356,8 +298,19 @@ private static void RunGitCommand(string repoPath, string arguments, Action