Skip to content

Commit 6931c92

Browse files
committed
azrepos: fix error message to stderr when not in repo
Suppress the standard error stream from Git when checking if we're inside a repository or not, because the failure message is otherwise printed to the user's console when we are not (which is what we're trying to decide!). Note that we normally don't suppress or redirect the standard error stream for Git commands as the user may have enabled Git tracing (v1 or v2) to the stderr file and we don't want to mute these for all calls.
1 parent e4284a2 commit 6931c92

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/shared/Core/Git.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public interface IGit
2020
/// <returns>Process object ready to be started.</returns>
2121
ChildProcess CreateProcess(string args);
2222

23+
/// <summary>
24+
/// Returns true if the current Git instance is scoped to a local repository.
25+
/// </summary>
26+
/// <returns>True if inside a local Git repository, false otherwise.</returns>
27+
bool IsInsideRepository();
28+
2329
/// <summary>
2430
/// Return the path to the current repository, or null if this instance is not
2531
/// scoped to a Git repository.
@@ -119,10 +125,26 @@ public IGitConfiguration GetConfiguration()
119125
return new GitProcessConfiguration(_trace, this);
120126
}
121127

128+
public bool IsInsideRepository()
129+
{
130+
return !string.IsNullOrWhiteSpace(GetCurrentRepositoryInternal(suppressStreams: true));
131+
}
132+
122133
public string GetCurrentRepository()
134+
{
135+
return GetCurrentRepositoryInternal(suppressStreams: false);
136+
}
137+
138+
private string GetCurrentRepositoryInternal(bool suppressStreams)
123139
{
124140
using (var git = CreateProcess("rev-parse --absolute-git-dir"))
125141
{
142+
// Redirect standard error to ensure any error messages are captured and not exposed to the user's console
143+
if (suppressStreams)
144+
{
145+
git.StartInfo.RedirectStandardError = true;
146+
}
147+
126148
git.Start(Trace2ProcessClass.Git);
127149
string data = git.StandardOutput.ReadToEnd();
128150
git.WaitForExit();
@@ -270,14 +292,5 @@ public GitException(string message, string gitErrorMessage, int exitCode)
270292

271293
public static class GitExtensions
272294
{
273-
/// <summary>
274-
/// Returns true if the current Git instance is scoped to a local repository.
275-
/// </summary>
276-
/// <param name="git">Git object.</param>
277-
/// <returns>True if inside a local Git repository, false otherwise.</returns>
278-
public static bool IsInsideRepository(this IGit git)
279-
{
280-
return !string.IsNullOrWhiteSpace(git.GetCurrentRepository());
281-
}
282295
}
283296
}

src/shared/TestInfrastructure/Objects/TestGit.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public ChildProcess CreateProcess(string args)
3333
throw new NotImplementedException();
3434
}
3535

36+
bool IGit.IsInsideRepository() => !string.IsNullOrWhiteSpace(CurrentRepository);
37+
3638
string IGit.GetCurrentRepository() => CurrentRepository;
3739

3840
IEnumerable<GitRemote> IGit.GetRemotes() => Remotes;

0 commit comments

Comments
 (0)