Skip to content

Commit bbebe8a

Browse files
authored
Set environment when running git commands (#3103)
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.
1 parent 28408f4 commit bbebe8a

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

private/buf/cmd/buf/command/push/push.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func getGitMetadataUploadOptions(
443443
if err := validateInputIsValidDirAndGitCheckout(ctx, runner, container, input); err != nil {
444444
return nil, err
445445
}
446-
uncommittedFiles, err := git.CheckForUncommittedGitChanges(ctx, runner, input)
446+
uncommittedFiles, err := git.CheckForUncommittedGitChanges(ctx, runner, container, input)
447447
if err != nil {
448448
return nil, err
449449
}
@@ -457,7 +457,7 @@ func getGitMetadataUploadOptions(
457457
}
458458
return nil, err
459459
}
460-
currentGitCommit, err := git.GetCurrentHEADGitCommit(ctx, runner, input)
460+
currentGitCommit, err := git.GetCurrentHEADGitCommit(ctx, runner, container, input)
461461
if err != nil {
462462
return nil, err
463463
}

private/pkg/git/git.go

+6
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,13 @@ func CheckDirectoryIsValidGitCheckout(
247247
func CheckForUncommittedGitChanges(
248248
ctx context.Context,
249249
runner command.Runner,
250+
envContainer app.EnvContainer,
250251
dir string,
251252
) ([]string, error) {
252253
stdout := bytes.NewBuffer(nil)
253254
stderr := bytes.NewBuffer(nil)
254255
var modifiedFiles []string
256+
envMap := app.EnvironMap(envContainer)
255257
// Unstaged changes
256258
if err := runner.Run(
257259
ctx,
@@ -260,6 +262,7 @@ func CheckForUncommittedGitChanges(
260262
command.RunWithStdout(stdout),
261263
command.RunWithStderr(stderr),
262264
command.RunWithDir(dir),
265+
command.RunWithEnv(envMap),
263266
); err != nil {
264267
return nil, fmt.Errorf("failed to get unstaged changes: %w: %s", err, stderr.String())
265268
}
@@ -275,6 +278,7 @@ func CheckForUncommittedGitChanges(
275278
command.RunWithStdout(stdout),
276279
command.RunWithStderr(stderr),
277280
command.RunWithDir(dir),
281+
command.RunWithEnv(envMap),
278282
); err != nil {
279283
return nil, fmt.Errorf("failed to get staged changes: %w: %s", err, stderr.String())
280284
}
@@ -287,6 +291,7 @@ func CheckForUncommittedGitChanges(
287291
func GetCurrentHEADGitCommit(
288292
ctx context.Context,
289293
runner command.Runner,
294+
envContainer app.EnvContainer,
290295
dir string,
291296
) (string, error) {
292297
stdout := bytes.NewBuffer(nil)
@@ -298,6 +303,7 @@ func GetCurrentHEADGitCommit(
298303
command.RunWithStdout(stdout),
299304
command.RunWithStderr(stderr),
300305
command.RunWithDir(dir),
306+
command.RunWithEnv(app.EnvironMap(envContainer)),
301307
); err != nil {
302308
return "", fmt.Errorf("failed to get current HEAD commit: %w: %s", err, stderr.String())
303309
}

private/pkg/git/remote.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ func getRemote(
134134
hostname, repositoryPath, err := getRemoteURLMetadata(
135135
ctx,
136136
runner,
137+
envContainer,
137138
dir,
138139
name,
139140
)
140141
if err != nil {
141-
return nil, err
142+
return nil, fmt.Errorf("failed to get remote URL metadata: %w", err)
142143
}
143144
headBranch, err := getRemoteHEADBranch(ctx, runner, envContainer, dir, name)
144145
if err != nil {
145-
return nil, err
146+
return nil, fmt.Errorf("failed to get remote HEAD branch: %w", err)
146147
}
147148
return newRemote(
148149
name,
@@ -185,6 +186,7 @@ func validateRemoteExists(
185186
func getRemoteURLMetadata(
186187
ctx context.Context,
187188
runner command.Runner,
189+
envContainer app.EnvContainer,
188190
dir string,
189191
remote string,
190192
) (string, string, error) {
@@ -199,6 +201,7 @@ func getRemoteURLMetadata(
199201
command.RunWithStdout(stdout),
200202
command.RunWithStderr(stderr),
201203
command.RunWithDir(dir),
204+
command.RunWithEnv(app.EnvironMap(envContainer)),
202205
); err != nil {
203206
return "", "", err
204207
}

0 commit comments

Comments
 (0)