public bool GitSystemConfig(Installation installation, out string path) { if (installation != null && Storage.FileExists(installation.Config)) { path = installation.Path; return(true); } // find Git on the local disk - the system config is stored relative to it else { List <Installation> installations; if (FindGitInstallations(out installations) && Storage.FileExists(installations[0].Config)) { path = installations[0].Config; return(true); } } path = null; return(false); }
public bool FindGitInstallation(string path, KnownDistribution distro, out Installation installation) { installation = new Installation(Context, path, distro); return(installation.IsValid()); }
/// <summary> /// Finds and returns path(s) to Git installation(s) in common locations. /// <para/> /// Returns `<see langword="true"/>` if successful; otherwise `<see langword="false"/>`. /// </summary> /// <param name="installations">The list of found Git installation if successful; otherwise `<see langword="null"/>`.</param> public static bool FindGitInstallations(out List <Installation> installations) { const string GitAppName = @"Git"; const string GitSubkeyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Git_is1"; const string GitValueName = "InstallLocation"; installations = null; var programFiles32Path = string.Empty; var programFiles64Path = string.Empty; var appDataRoamingPath = string.Empty; var appDataLocalPath = string.Empty; var programDataPath = string.Empty; var reg32HklmPath = string.Empty; var reg64HklmPath = string.Empty; var reg32HkcuPath = string.Empty; var reg64HkcuPath = string.Empty; var shellPathValue = string.Empty; using (var reg32HklmKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)) using (var reg32HkcuKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32)) using (var reg32HklmSubKey = reg32HklmKey?.OpenSubKey(GitSubkeyName)) using (var reg32HkcuSubKey = reg32HkcuKey?.OpenSubKey(GitSubkeyName)) { reg32HklmPath = reg32HklmSubKey?.GetValue(GitValueName, reg32HklmPath) as string; reg32HkcuPath = reg32HkcuSubKey?.GetValue(GitValueName, reg32HkcuPath) as string; } if ((programFiles32Path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)) != null) { programFiles32Path = Path.Combine(programFiles32Path, GitAppName); } if (Environment.Is64BitOperatingSystem) { using (var reg64HklmKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) using (var reg64HkcuKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) using (var reg64HklmSubKey = reg64HklmKey?.OpenSubKey(GitSubkeyName)) using (var reg64HkcuSubKey = reg64HkcuKey?.OpenSubKey(GitSubkeyName)) { reg64HklmPath = reg64HklmSubKey?.GetValue(GitValueName, reg64HklmPath) as string; reg64HkcuPath = reg64HkcuSubKey?.GetValue(GitValueName, reg64HkcuPath) as string; } if ((programFiles64Path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)) != null) { programFiles64Path = Path.Combine(programFiles64Path, GitAppName); } } if ((appDataRoamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) != null) { appDataRoamingPath = Path.Combine(appDataRoamingPath, GitAppName); } if ((appDataLocalPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) != null) { appDataLocalPath = Path.Combine(appDataLocalPath, GitAppName); } if ((programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)) != null) { programDataPath = Path.Combine(programDataPath, GitAppName); } List <Installation> candidates = new List <Installation>(); // Add candidate locations in order of preference. if (Where.FindApp(GitAppName, out shellPathValue)) { // `Where.App` returns the path to the executable, truncate to the installation root if (shellPathValue.EndsWith(Installation.AllVersionCmdPath, StringComparison.OrdinalIgnoreCase)) { shellPathValue = shellPathValue.Substring(0, shellPathValue.Length - Installation.AllVersionCmdPath.Length); } candidates.Add(new Installation(shellPathValue, KnownDistribution.GitForWindows64v2)); candidates.Add(new Installation(shellPathValue, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(shellPathValue, KnownDistribution.GitForWindows32v1)); } if (!string.IsNullOrEmpty(reg64HklmPath)) { candidates.Add(new Installation(reg64HklmPath, KnownDistribution.GitForWindows64v2)); } if (!string.IsNullOrEmpty(programFiles32Path)) { candidates.Add(new Installation(programFiles64Path, KnownDistribution.GitForWindows64v2)); } if (!string.IsNullOrEmpty(reg64HkcuPath)) { candidates.Add(new Installation(reg64HkcuPath, KnownDistribution.GitForWindows64v2)); } if (!string.IsNullOrEmpty(reg32HklmPath)) { candidates.Add(new Installation(reg32HklmPath, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(reg32HklmPath, KnownDistribution.GitForWindows32v1)); } if (!string.IsNullOrEmpty(programFiles32Path)) { candidates.Add(new Installation(programFiles32Path, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(programFiles32Path, KnownDistribution.GitForWindows32v1)); } if (!string.IsNullOrEmpty(reg32HkcuPath)) { candidates.Add(new Installation(reg32HkcuPath, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(reg32HkcuPath, KnownDistribution.GitForWindows32v1)); } if (!string.IsNullOrEmpty(programDataPath)) { candidates.Add(new Installation(programDataPath, KnownDistribution.GitForWindows64v2)); candidates.Add(new Installation(programDataPath, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(programDataPath, KnownDistribution.GitForWindows32v1)); } if (!string.IsNullOrEmpty(appDataLocalPath)) { candidates.Add(new Installation(appDataLocalPath, KnownDistribution.GitForWindows64v2)); candidates.Add(new Installation(appDataLocalPath, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(appDataLocalPath, KnownDistribution.GitForWindows32v1)); } if (!string.IsNullOrEmpty(appDataRoamingPath)) { candidates.Add(new Installation(appDataRoamingPath, KnownDistribution.GitForWindows64v2)); candidates.Add(new Installation(appDataRoamingPath, KnownDistribution.GitForWindows32v2)); candidates.Add(new Installation(appDataRoamingPath, KnownDistribution.GitForWindows32v1)); } HashSet <Installation> pathSet = new HashSet <Installation>(); foreach (var candidate in candidates) { if (Installation.IsValid(candidate)) { pathSet.Add(candidate); } } installations = pathSet.ToList(); Git.Trace.WriteLine($"found {installations.Count} Git installation(s)."); return(installations.Count > 0); }