diff --git a/src/shared/Core/Git.cs b/src/shared/Core/Git.cs index b63e0fe3d..0c58e0159 100644 --- a/src/shared/Core/Git.cs +++ b/src/shared/Core/Git.cs @@ -20,6 +20,12 @@ public interface IGit /// Process object ready to be started. ChildProcess CreateProcess(string args); + /// + /// Returns true if the current Git instance is scoped to a local repository. + /// + /// True if inside a local Git repository, false otherwise. + bool IsInsideRepository(); + /// /// Return the path to the current repository, or null if this instance is not /// scoped to a Git repository. @@ -119,10 +125,26 @@ public IGitConfiguration GetConfiguration() return new GitProcessConfiguration(_trace, this); } + public bool IsInsideRepository() + { + return !string.IsNullOrWhiteSpace(GetCurrentRepositoryInternal(suppressStreams: true)); + } + public string GetCurrentRepository() + { + return GetCurrentRepositoryInternal(suppressStreams: false); + } + + private string GetCurrentRepositoryInternal(bool suppressStreams) { using (var git = CreateProcess("rev-parse --absolute-git-dir")) { + // Redirect standard error to ensure any error messages are captured and not exposed to the user's console + if (suppressStreams) + { + git.StartInfo.RedirectStandardError = true; + } + git.Start(Trace2ProcessClass.Git); string data = git.StandardOutput.ReadToEnd(); git.WaitForExit(); @@ -270,14 +292,5 @@ public GitException(string message, string gitErrorMessage, int exitCode) public static class GitExtensions { - /// - /// Returns true if the current Git instance is scoped to a local repository. - /// - /// Git object. - /// True if inside a local Git repository, false otherwise. - public static bool IsInsideRepository(this IGit git) - { - return !string.IsNullOrWhiteSpace(git.GetCurrentRepository()); - } } } diff --git a/src/shared/TestInfrastructure/Objects/TestGit.cs b/src/shared/TestInfrastructure/Objects/TestGit.cs index 27ddba9b3..f7a93a372 100644 --- a/src/shared/TestInfrastructure/Objects/TestGit.cs +++ b/src/shared/TestInfrastructure/Objects/TestGit.cs @@ -33,6 +33,8 @@ public ChildProcess CreateProcess(string args) throw new NotImplementedException(); } + bool IGit.IsInsideRepository() => !string.IsNullOrWhiteSpace(CurrentRepository); + string IGit.GetCurrentRepository() => CurrentRepository; IEnumerable IGit.GetRemotes() => Remotes;