Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azrepos: fix error message to stderr when not in repo #1583

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading