Skip to content

Commit

Permalink
Set environment when running git commands (#3103)
Browse files Browse the repository at this point in the history
Git commands often need to shell out to other commands, and so running
consistently with the environment set (in particular PATH) fixes issues
running in certain environments.

Additionally, wrap a few errors to make it easier to determine the
reason for failures with the --git-metadata flag.
  • Loading branch information
pkwarren committed Jun 21, 2024
1 parent 28408f4 commit bbebe8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions private/buf/cmd/buf/command/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func getGitMetadataUploadOptions(
if err := validateInputIsValidDirAndGitCheckout(ctx, runner, container, input); err != nil {
return nil, err
}
uncommittedFiles, err := git.CheckForUncommittedGitChanges(ctx, runner, input)
uncommittedFiles, err := git.CheckForUncommittedGitChanges(ctx, runner, container, input)
if err != nil {
return nil, err
}
Expand All @@ -457,7 +457,7 @@ func getGitMetadataUploadOptions(
}
return nil, err
}
currentGitCommit, err := git.GetCurrentHEADGitCommit(ctx, runner, input)
currentGitCommit, err := git.GetCurrentHEADGitCommit(ctx, runner, container, input)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions private/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,13 @@ func CheckDirectoryIsValidGitCheckout(
func CheckForUncommittedGitChanges(
ctx context.Context,
runner command.Runner,
envContainer app.EnvContainer,
dir string,
) ([]string, error) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
var modifiedFiles []string
envMap := app.EnvironMap(envContainer)
// Unstaged changes
if err := runner.Run(
ctx,
Expand All @@ -260,6 +262,7 @@ func CheckForUncommittedGitChanges(
command.RunWithStdout(stdout),
command.RunWithStderr(stderr),
command.RunWithDir(dir),
command.RunWithEnv(envMap),
); err != nil {
return nil, fmt.Errorf("failed to get unstaged changes: %w: %s", err, stderr.String())
}
Expand All @@ -275,6 +278,7 @@ func CheckForUncommittedGitChanges(
command.RunWithStdout(stdout),
command.RunWithStderr(stderr),
command.RunWithDir(dir),
command.RunWithEnv(envMap),
); err != nil {
return nil, fmt.Errorf("failed to get staged changes: %w: %s", err, stderr.String())
}
Expand All @@ -287,6 +291,7 @@ func CheckForUncommittedGitChanges(
func GetCurrentHEADGitCommit(
ctx context.Context,
runner command.Runner,
envContainer app.EnvContainer,
dir string,
) (string, error) {
stdout := bytes.NewBuffer(nil)
Expand All @@ -298,6 +303,7 @@ func GetCurrentHEADGitCommit(
command.RunWithStdout(stdout),
command.RunWithStderr(stderr),
command.RunWithDir(dir),
command.RunWithEnv(app.EnvironMap(envContainer)),
); err != nil {
return "", fmt.Errorf("failed to get current HEAD commit: %w: %s", err, stderr.String())
}
Expand Down
7 changes: 5 additions & 2 deletions private/pkg/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ func getRemote(
hostname, repositoryPath, err := getRemoteURLMetadata(
ctx,
runner,
envContainer,
dir,
name,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get remote URL metadata: %w", err)
}
headBranch, err := getRemoteHEADBranch(ctx, runner, envContainer, dir, name)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get remote HEAD branch: %w", err)
}
return newRemote(
name,
Expand Down Expand Up @@ -185,6 +186,7 @@ func validateRemoteExists(
func getRemoteURLMetadata(
ctx context.Context,
runner command.Runner,
envContainer app.EnvContainer,
dir string,
remote string,
) (string, string, error) {
Expand All @@ -199,6 +201,7 @@ func getRemoteURLMetadata(
command.RunWithStdout(stdout),
command.RunWithStderr(stderr),
command.RunWithDir(dir),
command.RunWithEnv(app.EnvironMap(envContainer)),
); err != nil {
return "", "", err
}
Expand Down

0 comments on commit bbebe8a

Please sign in to comment.