Skip to content
This repository was archived by the owner on May 19, 2021. It is now read-only.

Commit 29c12a3

Browse files
committed
Improved GitBranch detection robusness. It now searches all folders up to root for .git infos.
1 parent 4a143d6 commit 29c12a3

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

UnityLauncher/Tools.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,44 @@ public static void OpenURL(string url)
3030
public static string ReadGitBranchInfo(string projectPath)
3131
{
3232
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 )
3535
{
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+
}
4044
}
4145
return results;
4246
}
4347

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+
4471
/// <summary>
4572
/// returns last-write-time for a file or folder
4673
/// </summary>

0 commit comments

Comments
 (0)