Skip to content

Commit

Permalink
azrepos: fix error message to stderr when not in repo (git-ecosystem#…
Browse files Browse the repository at this point in the history
…1583)

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.

Example before:

```shell
% pwd
/Users/mjcheetham
% gcm azure-repos list
fatal: not a git repository (or any of the parent directories): .git
devdiv:
  (global) -> USER@DOMAIN
```

Example now:
```shell
% pwd
/Users/mjcheetham
% gcm azure-repos list
devdiv:
  (global) -> USER@DOMAIN
```
  • Loading branch information
dscho authored Apr 16, 2024
2 parents e4284a2 + 6931c92 commit 1ed7037
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/shared/Core/Git.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public interface IGit
/// <returns>Process object ready to be started.</returns>
ChildProcess CreateProcess(string args);

/// <summary>
/// Returns true if the current Git instance is scoped to a local repository.
/// </summary>
/// <returns>True if inside a local Git repository, false otherwise.</returns>
bool IsInsideRepository();

/// <summary>
/// Return the path to the current repository, or null if this instance is not
/// scoped to a Git repository.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -270,14 +292,5 @@ public GitException(string message, string gitErrorMessage, int exitCode)

public static class GitExtensions
{
/// <summary>
/// Returns true if the current Git instance is scoped to a local repository.
/// </summary>
/// <param name="git">Git object.</param>
/// <returns>True if inside a local Git repository, false otherwise.</returns>
public static bool IsInsideRepository(this IGit git)
{
return !string.IsNullOrWhiteSpace(git.GetCurrentRepository());
}
}
}
2 changes: 2 additions & 0 deletions src/shared/TestInfrastructure/Objects/TestGit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public ChildProcess CreateProcess(string args)
throw new NotImplementedException();
}

bool IGit.IsInsideRepository() => !string.IsNullOrWhiteSpace(CurrentRepository);

string IGit.GetCurrentRepository() => CurrentRepository;

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

0 comments on commit 1ed7037

Please sign in to comment.