Repository navigation
refactor(platform): Move GitRuntime into platform/git/exec as Runtime #769
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
Open
manjari25
wants to merge
1
commit into
main
Choose a base branch
from
manjari/gitruntime-platform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright (c) 2026 Uber Technologies, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package gitexec | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // Runtime identifies the explicitly provided Git runtime that commands run on. | ||
| type Runtime struct { | ||
| // Executable is the absolute path to the Git executable. | ||
| Executable string | ||
| // ExecPath is the absolute directory containing Git's helper executables. | ||
| ExecPath string | ||
| // TemplateDir is the absolute directory containing Git's repository | ||
| // templates. | ||
| TemplateDir string | ||
| // PassthroughEnv names additional environment variables to inherit from | ||
| // the parent process, on top of the auth and transport ones always passed | ||
| // through. For a deployment whose remote needs something unusual; leave | ||
| // empty otherwise. Names that could alter merge semantics do not belong | ||
| // here — the scrubbed environment is what keeps a merge reproducible. | ||
| PassthroughEnv []string | ||
| } | ||
|
|
||
| // Validate reports whether every path of the runtime is set and absolute. | ||
| func (r Runtime) Validate() error { | ||
| for _, field := range []struct{ name, path string }{ | ||
| {"executable", r.Executable}, | ||
| {"exec path", r.ExecPath}, | ||
| {"template dir", r.TemplateDir}, | ||
| } { | ||
| if field.path == "" { | ||
| return fmt.Errorf("git runtime %s is required", field.name) | ||
| } | ||
| if !filepath.IsAbs(field.path) { | ||
| return fmt.Errorf("git runtime %s must be absolute: %q", field.name, field.path) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Command constructs a Git command without inheriting the caller's | ||
| // environment. The executable and helper paths come from the pinned runtime; | ||
| // repository-local configuration remains an intentional input. | ||
| func (r Runtime) Command(ctx context.Context, dir string, args ...string) *exec.Cmd { | ||
| gitArgs := make([]string, 0, len(args)+3) | ||
| gitArgs = append(gitArgs, | ||
| "--exec-path="+r.ExecPath, | ||
| "-c", "init.templateDir="+r.TemplateDir, | ||
| ) | ||
| gitArgs = append(gitArgs, args...) | ||
|
|
||
| cmd := exec.CommandContext(ctx, r.Executable, gitArgs...) | ||
| cmd.Dir = dir | ||
| // HOME and XDG_CONFIG_HOME are isolated to the checkout rather than inherited, | ||
| // so the runtime's literals — appended last — override any HOME a deployment | ||
| // passed through. The remaining literals pin git's runtime; the scrub set and | ||
| // transport variables come from Env. | ||
| cmd.Env = Env(EnvOptions{ | ||
| Transport: true, | ||
| Passthrough: r.PassthroughEnv, | ||
| Literal: []string{ | ||
| "HOME=" + filepath.Join(dir, ".submitqueue-git-home"), | ||
| "XDG_CONFIG_HOME=" + filepath.Join(dir, ".submitqueue-git-home", "xdg"), | ||
| "GIT_EXEC_PATH=" + r.ExecPath, | ||
| "GIT_TEMPLATE_DIR=" + r.TemplateDir, | ||
| "LC_ALL=C", | ||
| "LANG=C", | ||
| }, | ||
| }) | ||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright (c) 2026 Uber Technologies, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package gitexec | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func dummyRuntime(t *testing.T) Runtime { | ||
| t.Helper() | ||
| dir := t.TempDir() | ||
| return Runtime{ | ||
| Executable: filepath.Join(dir, "git"), | ||
| ExecPath: filepath.Join(dir, "git-core"), | ||
| TemplateDir: filepath.Join(dir, "templates"), | ||
| } | ||
| } | ||
|
|
||
| func TestRuntimeValidate(t *testing.T) { | ||
| valid := dummyRuntime(t) | ||
| tests := []struct { | ||
| name string | ||
| mutate func(*Runtime) | ||
| wantErr bool | ||
| }{ | ||
| {name: "valid", mutate: func(*Runtime) {}}, | ||
| {name: "missing executable", mutate: func(r *Runtime) { r.Executable = "" }, wantErr: true}, | ||
| {name: "relative exec path", mutate: func(r *Runtime) { r.ExecPath = "git-core" }, wantErr: true}, | ||
| {name: "relative template dir", mutate: func(r *Runtime) { r.TemplateDir = "templates" }, wantErr: true}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| runtime := valid | ||
| tt.mutate(&runtime) | ||
| err := runtime.Validate() | ||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntimeCommandDoesNotInheritEnvironment(t *testing.T) { | ||
| t.Setenv("SUBMITQUEUE_GIT_AMBIENT", "ambient") | ||
| runtime := dummyRuntime(t) | ||
|
|
||
| cmd := runtime.Command(context.Background(), t.TempDir(), "--version") | ||
|
|
||
| assert.Equal(t, runtime.Executable, cmd.Path) | ||
| assert.NotContains(t, cmd.Env, "SUBMITQUEUE_GIT_AMBIENT=ambient") | ||
| assert.Contains(t, cmd.Env, "GIT_CONFIG_NOSYSTEM=1") | ||
| assert.Contains(t, cmd.Env, "GIT_EXEC_PATH="+runtime.ExecPath) | ||
| } | ||
|
|
||
| func TestRuntimeCommandPreservesAuthEnvironment(t *testing.T) { | ||
| // Scrubbing denies git ambient configuration; it must not also deny it the | ||
| // means to reach the remote. Without the agent socket an SSH remote cannot | ||
| // authenticate, and without PATH git cannot even exec ssh. | ||
| t.Setenv("SSH_AUTH_SOCK", "/tmp/agent.sock") | ||
| t.Setenv("PATH", "/usr/bin:/bin") | ||
| t.Setenv("HTTPS_PROXY", "http://proxy.example.com:3128") | ||
| t.Setenv("SUBMITQUEUE_GIT_AMBIENT", "ambient") | ||
|
|
||
| cmd := dummyRuntime(t).Command(context.Background(), t.TempDir(), "--version") | ||
|
|
||
| assert.Contains(t, cmd.Env, "SSH_AUTH_SOCK=/tmp/agent.sock") | ||
| assert.Contains(t, cmd.Env, "PATH=/usr/bin:/bin") | ||
| assert.Contains(t, cmd.Env, "HTTPS_PROXY=http://proxy.example.com:3128") | ||
| assert.NotContains(t, cmd.Env, "SUBMITQUEUE_GIT_AMBIENT=ambient") | ||
| } | ||
|
|
||
| func TestRuntimeCommandOmitsUnsetAuthEnvironment(t *testing.T) { | ||
| // An unset variable must be omitted rather than exported empty: an empty | ||
| // SSH_AUTH_SOCK tells ssh there is no agent instead of letting it look. | ||
| t.Setenv("SSH_AUTH_SOCK", "") | ||
| require.NoError(t, os.Unsetenv("SSH_AUTH_SOCK")) | ||
|
|
||
| cmd := dummyRuntime(t).Command(context.Background(), t.TempDir(), "--version") | ||
|
|
||
| for _, entry := range cmd.Env { | ||
| assert.False(t, strings.HasPrefix(entry, "SSH_AUTH_SOCK="), "unset variable leaked as %q", entry) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntimeCommandPassesThroughExtraEnvironment(t *testing.T) { | ||
| t.Setenv("SUBMITQUEUE_CUSTOM_TRANSPORT", "value") | ||
| runtime := dummyRuntime(t) | ||
| runtime.PassthroughEnv = []string{"SUBMITQUEUE_CUSTOM_TRANSPORT"} | ||
|
|
||
| cmd := runtime.Command(context.Background(), t.TempDir(), "--version") | ||
|
|
||
| assert.Contains(t, cmd.Env, "SUBMITQUEUE_CUSTOM_TRANSPORT=value") | ||
| } | ||
|
|
||
| func TestRuntimeCommandIsolatesHome(t *testing.T) { | ||
| t.Setenv("HOME", "/ambient/home") | ||
| dir := t.TempDir() | ||
|
|
||
| cmd := dummyRuntime(t).Command(context.Background(), dir, "--version") | ||
|
|
||
| assert.Contains(t, cmd.Env, "HOME="+filepath.Join(dir, ".submitqueue-git-home")) | ||
| assert.NotContains(t, cmd.Env, "HOME=/ambient/home") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am wondering if it needs to exit on it's own vs being part of the local gitworkspace impl itself ? do you see any use of it outside?