Skip to content

Commit

Permalink
Merge 52a67ea into 56a19d0
Browse files Browse the repository at this point in the history
  • Loading branch information
snechaev authored Dec 23, 2019
2 parents 56a19d0 + 52a67ea commit 64963f1
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 20 deletions.
124 changes: 109 additions & 15 deletions TortoiseGitToolbar/Config/Constants/PathConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Win32;

namespace MattDavies.TortoiseGitToolbar.Config.Constants
Expand All @@ -12,6 +14,9 @@ public static class PathConfiguration
private const string TortoiseGitx86 = @"C:\Program Files (x86)\TortoiseGit\bin\TortoiseGitProc.exe";
private const string GitBashx86 = @"C:\Program Files (x86)\Git\bin\sh.exe";
private const string GitBashx64 = @"C:\Program Files\Git\bin\sh.exe";
private const string GitExex86 = @"C:\Program Files (x86)\Git\bin\git.exe";
private const string GitExex64 = @"C:\Program Files\Git\bin\git.exe";


private static readonly RegistryKey TortoiseGitProcRegistryRoot = Registry.LocalMachine;
private const string TortoiseGitProcRegistryPath = @"SOFTWARE\TortoiseGit";
Expand Down Expand Up @@ -43,35 +48,114 @@ public static string GetGitBashPath()
: null;
}

public static string GetGitExePath()
{
var path = GetGitExePathFromRegistry();
if (path != null)
return path;
return File.Exists(GitExex64) ? GitExex64
: File.Exists(GitExex86) ? GitExex86
: null;
}

public static string GetSolutionPath(Solution2 solution)
{
if (solution != null && solution.IsOpen)
{
var solutionPathFromSln = Path.GetDirectoryName(solution.FullName);
Debug.WriteLine("Solution path is: " + solutionPathFromSln);

var solutionPathInfo = new DirectoryInfo(solutionPathFromSln);
Debug.WriteLine("Solution path is: " + solutionPathInfo.FullName);

// find parent folder that holds the .git folder
while (!Directory.Exists(Path.Combine(solutionPathInfo.FullName, ".git")))
var repositoryRootPath = GetRepositoryRootGit(solutionPathFromSln);
if (repositoryRootPath == null)
{
Debug.WriteLine("No .git folder found in solution path.");
if (solutionPathInfo.Parent == null)
Debug.WriteLine("Failed to get root path from git. Trying to filesystem-based approach");
repositoryRootPath = GetRepositoryRootFs(solutionPathFromSln);
if (repositoryRootPath == null)
{
Debug.WriteLine("No parent folder found. Using original path: " + solutionPathFromSln);
return solutionPathFromSln;
}

solutionPathInfo = solutionPathInfo.Parent;
}

Debug.WriteLine("Using solution path: " + solutionPathInfo.FullName);
return solutionPathInfo.FullName;
Debug.WriteLine("Using solution path: " + repositoryRootPath);
return repositoryRootPath;
}

return null;
}

/// <summary>
/// Find repository root by calling "git rev-parse --show-toplevel" command
/// </summary>
/// <param name="solutionPath">Path inside repository (working path for git command)</param>
/// <returns> Path to repository root, if found. Otherwise null.</returns>
private static string GetRepositoryRootGit(string solutionPath)
{
var gitPath = GetGitExePath();
if (gitPath == null)
{
return null;
}

var procInfo = new ProcessStartInfo
{
FileName = gitPath,
Arguments = "rev-parse --show-toplevel",
UseShellExecute = false,
WorkingDirectory = solutionPath,
RedirectStandardOutput = true,
CreateNoWindow = true,
};

using (var process = Process.Start(procInfo))
{
var stdOut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
var errCode = process.ExitCode;

Debug.WriteLine($"git rev-parse --show-toplevel exited with code {errCode} and stdout: {stdOut}");
if (errCode != 0 || string.IsNullOrWhiteSpace(stdOut))
{
return null;
}

try
{
return Path.GetFullPath(stdOut);
}
catch (Exception e)
{
Debug.WriteLine($"GetFullPath failed for {stdOut}, with {e} reason: {e.Message}");
return null;
}
}
}


/// <summary>
/// Find repository root basing on filesystem by finding parent directory that contains .git folder
/// </summary>
/// <param name="solutionPath">Directory to start search from.</param>
/// <returns> Path to repository root, if found. Otherwise null.</returns>
private static string GetRepositoryRootFs(string solutionPath)
{
var solutionPathInfo = new DirectoryInfo(solutionPath);

// find parent folder that holds the .git folder
while (!Directory.Exists(Path.Combine(solutionPathInfo.FullName, ".git")))
{
Debug.WriteLine("No .git folder found in solution path.");
if (solutionPathInfo.Parent == null)
{
return null;
}

solutionPathInfo = solutionPathInfo.Parent;
}

return solutionPathInfo.FullName;
}

public static string GetOpenedFilePath(Solution2 solution)
{
if (solution != null && solution.DTE != null)
Expand Down Expand Up @@ -131,6 +215,16 @@ public static string GetTortoiseGitPathFromRegistry()
}

public static string GetGitBashPathFromRegistry()
{
return GetGitExecutablePathFromRegisty("sh.exe");
}

public static string GetGitExePathFromRegistry()
{
return GetGitExecutablePathFromRegisty("git.exe");
}

private static string GetGitExecutablePathFromRegisty(string executableName)
{
var path = GetValueFromRegistry(GitBashRegistryRoot, GitBashRegistryPath, GitBashRegistryKeyName);
Debug.WriteLine("Git bash path from registry: " + (path ?? "(null)"));
Expand All @@ -139,11 +233,11 @@ public static string GetGitBashPathFromRegistry()
{
Debug.WriteLine("Git bash path from registry exists.");

var shPath = Path.Combine(path, "sh.exe");
if (File.Exists(shPath))
var exePath = Path.Combine(path, executableName);
if (File.Exists(exePath))
{
Debug.WriteLine("Git bash path sh.exe exists: " + shPath);
return shPath;
Debug.WriteLine($"Git bash path {executableName} exists: " + exePath);
return exePath;
}
}

Expand Down
8 changes: 4 additions & 4 deletions TortoiseGitToolbar/TortoiseGitToolbar.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0">
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props')" />
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.props" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.props')" />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down Expand Up @@ -231,10 +231,10 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.targets'))" />
</Target>
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets')" />
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.targets" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.16.5.1027\build\Microsoft.VSSDK.BuildTools.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
2 changes: 1 addition & 1 deletion TortoiseGitToolbar/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.VSSDK.BuildTools" version="15.5.100" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.VSSDK.BuildTools" version="16.5.1027" targetFramework="net452" developmentDependency="true" />
<package id="VSSDK.DTE" version="7.0.4" targetFramework="net45" />
<package id="VSSDK.GraphModel" version="11.0.4" targetFramework="net45" />
<package id="VSSDK.IDE" version="7.0.4" targetFramework="net45" />
Expand Down

0 comments on commit 64963f1

Please sign in to comment.