@@ -30,17 +30,44 @@ public static void OpenURL(string url)
30
30
public static string ReadGitBranchInfo ( string projectPath )
31
31
{
32
32
string results = null ;
33
- string branchFile = Path . Combine ( projectPath , ".git" , "HEAD" ) ;
34
- if ( File . Exists ( branchFile ) == true )
33
+ DirectoryInfo gitDirectory = FindDir ( ".git" , projectPath ) ;
34
+ if ( gitDirectory != null )
35
35
{
36
- results = File . ReadAllText ( branchFile ) ;
37
- // get branch only
38
- int pos = results . LastIndexOf ( "/" ) + 1 ;
39
- results = results . Substring ( pos , results . Length - pos ) ;
36
+ string branchFile = Path . Combine ( gitDirectory . FullName , "HEAD" ) ;
37
+ if ( File . Exists ( branchFile ) )
38
+ {
39
+ results = File . ReadAllText ( branchFile ) ;
40
+ // get branch only
41
+ int pos = results . LastIndexOf ( "/" ) + 1 ;
42
+ results = results . Substring ( pos , results . Length - pos ) ;
43
+ }
40
44
}
41
45
return results ;
42
46
}
43
47
48
+ /// <summary>
49
+ /// Searches for a directory beginning with "startPath".
50
+ /// If the directory is not found, then parent folders are searched until
51
+ /// either it is found or the root folder has been reached.
52
+ /// Null is returned if the directory was not found.
53
+ /// </summary>
54
+ /// <param name="dirName"></param>
55
+ /// <param name="startPath"></param>
56
+ /// <returns></returns>
57
+ public static DirectoryInfo FindDir ( string dirName , string startPath )
58
+ {
59
+ DirectoryInfo dirInfo = new DirectoryInfo ( Path . Combine ( startPath , dirName ) ) ;
60
+ while ( ! dirInfo . Exists )
61
+ {
62
+ if ( dirInfo . Parent . Parent == null )
63
+ {
64
+ return null ;
65
+ }
66
+ dirInfo = new DirectoryInfo ( Path . Combine ( dirInfo . Parent . Parent . FullName , dirName ) ) ;
67
+ }
68
+ return dirInfo ;
69
+ }
70
+
44
71
/// <summary>
45
72
/// returns last-write-time for a file or folder
46
73
/// </summary>
0 commit comments