3
3
using System ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
+ using System . Text . RegularExpressions ;
6
7
using System . Windows . Forms ;
7
8
8
9
namespace SamirBoulema . TGit . Helpers
@@ -11,7 +12,7 @@ public static class FileHelper
11
12
{
12
13
public static string GetTortoiseGitProc ( )
13
14
{
14
- return ( string ) Registry . GetValue ( @"HKEY_LOCAL_MACHINE\SOFTWARE\TortoiseGit" , "ProcPath" , @"C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" ) ;
15
+ return ( string ) Registry . GetValue ( @"HKEY_LOCAL_MACHINE\SOFTWARE\TortoiseGit" , "ProcPath" , @"C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" ) ;
15
16
}
16
17
17
18
public static string GetTortoiseGitPlink ( )
@@ -30,17 +31,39 @@ public static void SaveAllFiles(DTE dte)
30
31
dte . ExecuteCommand ( "File.SaveAll" ) ;
31
32
}
32
33
34
+ /// <summary>
35
+ /// Check if we have an open solution or a folder and try to find the base repository/solution dir
36
+ /// </summary>
37
+ /// <param name="dte"></param>
38
+ /// <returns>Path to the base repository/solution dir</returns>
33
39
public static string GetSolutionDir ( DTE dte )
34
40
{
35
- string fileName = dte . Solution . FullName ;
36
- if ( ! string . IsNullOrEmpty ( fileName ) )
41
+ var fullName = dte . Solution . FullName ;
42
+
43
+ if ( string . IsNullOrEmpty ( fullName ) )
44
+ {
45
+ return string . Empty ;
46
+ }
47
+
48
+ if ( File . Exists ( fullName ) )
37
49
{
38
- var path = Path . GetDirectoryName ( fileName ) ;
50
+ var path = Path . GetDirectoryName ( fullName ) ;
39
51
return FindGitdir ( path ) ;
40
52
}
53
+
54
+ if ( Directory . Exists ( fullName ) )
55
+ {
56
+ return FindGitdir ( fullName ) ;
57
+ }
58
+
41
59
return string . Empty ;
42
60
}
43
61
62
+ /// <summary>
63
+ /// Start at the solution dir and traverse up to find a .git folder or file
64
+ /// </summary>
65
+ /// <param name="path">Path to start traversing from.</param>
66
+ /// <returns>Path to the .git folder or file.</returns>
44
67
private static string FindGitdir ( string path )
45
68
{
46
69
try
@@ -50,6 +73,18 @@ private static string FindGitdir(string path)
50
73
{
51
74
return di . FullName ;
52
75
}
76
+
77
+ var gitFilePath = Path . Combine ( path , ".git" ) ;
78
+ if ( File . Exists ( gitFilePath ) )
79
+ {
80
+ var text = File . ReadAllText ( gitFilePath ) ;
81
+ var match = Regex . Match ( text , "gitdir:(.*)" ) ;
82
+ if ( match . Success )
83
+ {
84
+ return match . Groups [ 1 ] . Value . Trim ( ) ;
85
+ }
86
+ }
87
+
53
88
if ( di . Parent != null )
54
89
{
55
90
return FindGitdir ( di . Parent . FullName ) ;
0 commit comments