Skip to content

Commit

Permalink
Use a shared var to save the value for git existence (dotnet#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicancy authored Sep 29, 2017
1 parent 9440979 commit 95589ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 83 deletions.
91 changes: 22 additions & 69 deletions src/Microsoft.DocAsCode.Common/Git/GitUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
// 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<bool> _existGitCommand =
new Lazy<bool>(() => CommandUtility.ExistCommand(CommandName));

private static readonly string GetRepoRootCommand = "rev-parse --show-toplevel";
private static readonly string GetLocalBranchCommand = "rev-parse --abbrev-ref HEAD";
private static readonly string GetLocalBranchCommitIdCommand = "rev-parse HEAD";
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[]
{
Expand All @@ -39,6 +38,9 @@ public static class GitUtility

private static readonly ConcurrentDictionary<string, GitRepoInfo> Cache = new ConcurrentDictionary<string, GitRepoInfo>();

private static bool? GitCommandExists = null;
private static object SyncRoot = new object();

public static GitDetail TryGetFileDetail(string filePath)
{
GitDetail detail = null;
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -356,8 +298,19 @@ private static void RunGitCommand(string repoPath, string arguments, Action<stri

private static bool ExistGitCommand()
{
return _existGitCommand.Value;
if (GitCommandExists == null)
{
lock (SyncRoot)
{
if (GitCommandExists == null)
{
GitCommandExists = CommandUtility.ExistCommand(CommandName);
}
}
}
return GitCommandExists.Value;
}

#endregion
}
}
14 changes: 0 additions & 14 deletions test/Microsoft.DocAsCode.Common.Tests/GitUtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,5 @@ public void Environment_ForBranchName()
var info = GitUtility.GetFileDetail(Directory.GetCurrentDirectory());
Assert.Equal("special-branch", info.RemoteBranch);
}

[Fact]
public void GetDeletedFile()
{
var repoInfo = GitUtility.GetRepoInfo(Directory.GetCurrentDirectory());
var deletedExistingFile = Path.Combine(repoInfo.RepoRootPath, @"src/docfx.website.themes/angular/README.md");
var deletedNotExistingFile = Path.Combine(repoInfo.RepoRootPath, @"NOTEXISTING.md");

var content = GitUtility.GetDeletedFileContent(deletedExistingFile);
Assert.NotNull(content);

content = GitUtility.GetDeletedFileContent(deletedNotExistingFile);
Assert.Null(content);
}
}
}

0 comments on commit 95589ca

Please sign in to comment.