From 29c12a36ad1c00e1058566c9929adbb2993b905f Mon Sep 17 00:00:00 2001 From: geo Date: Sun, 4 Mar 2018 10:12:13 +0100 Subject: [PATCH] Improved GitBranch detection robusness. It now searches all folders up to root for .git infos. --- UnityLauncher/Tools.cs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/UnityLauncher/Tools.cs b/UnityLauncher/Tools.cs index 931f203..9310842 100644 --- a/UnityLauncher/Tools.cs +++ b/UnityLauncher/Tools.cs @@ -30,17 +30,44 @@ public static void OpenURL(string url) public static string ReadGitBranchInfo(string projectPath) { string results = null; - string branchFile = Path.Combine(projectPath, ".git", "HEAD"); - if (File.Exists(branchFile) == true) + DirectoryInfo gitDirectory = FindDir(".git", projectPath); + if (gitDirectory != null ) { - results = File.ReadAllText(branchFile); - // get branch only - int pos = results.LastIndexOf("/") + 1; - results = results.Substring(pos, results.Length - pos); + string branchFile = Path.Combine(gitDirectory.FullName, "HEAD"); + if (File.Exists(branchFile)) + { + results = File.ReadAllText(branchFile); + // get branch only + int pos = results.LastIndexOf("/") + 1; + results = results.Substring(pos, results.Length - pos); + } } return results; } + /// + /// Searches for a directory beginning with "startPath". + /// If the directory is not found, then parent folders are searched until + /// either it is found or the root folder has been reached. + /// Null is returned if the directory was not found. + /// + /// + /// + /// + public static DirectoryInfo FindDir(string dirName, string startPath) + { + DirectoryInfo dirInfo = new DirectoryInfo(Path.Combine(startPath, dirName)); + while ( !dirInfo.Exists ) + { + if(dirInfo.Parent.Parent == null ) + { + return null; + } + dirInfo = new DirectoryInfo(Path.Combine(dirInfo.Parent.Parent.FullName, dirName)); + } + return dirInfo; + } + /// /// returns last-write-time for a file or folder ///