Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(gitwork): extract bindSSHRemotes from StartSSH (#358)

fx-kurumic8c719e01b25
diff --git a/internal/gitwork/ssh.go b/internal/gitwork/ssh.go
index 97c28a36..316db3f9 100644
--- a/internal/gitwork/ssh.go
+++ b/internal/gitwork/ssh.go
@@ -94,38 +94,35 @@ func sshHostKeys(remotes []SSHRemote) ([]byte, error) {
return pinned.Bytes(), nil
}
-// StartSSH adds one Git URL rewrite and one OpenSSH identity per repository.
-// Aliases bind each repository to its selected host key.
-// The filesystem contains a socket and public data only. clientRoot is the
-// mount path corresponding to root for a client in a container.
-func StartSSH(environment map[string]string, keys map[string]string, root, clientRoot string) (func(), error) {
- if len(keys) == 0 {
- return func() {}, nil
- }
- type binding struct {
- remote SSHRemote
- alias string
- key string
- }
+// sshBinding ties one repository remote to its host alias and private key.
+type sshBinding struct {
+ remote SSHRemote
+ alias string
+ key string
+}
+
+// bindSSHRemotes appends one Git URL rewrite per repository to environment in
+// sorted URL order and returns the repositories that have an assigned key.
+func bindSSHRemotes(environment map[string]string, keys map[string]string) ([]sshBinding, []SSHRemote, error) {
urls := make([]string, 0, len(keys))
for raw := range keys {
urls = append(urls, raw)
}
sort.Strings(urls)
- var bindings []binding
+ var bindings []sshBinding
var remotes []SSHRemote
configCount, _ := strconv.Atoi(environment["GIT_CONFIG_COUNT"])
for _, raw := range urls {
remote, err := ParseSSHRemote(raw)
if err != nil {
- return nil, err
+ return nil, nil, err
}
destination := raw
if key := keys[raw]; key != "" {
sum := sha256.Sum256([]byte(remote.URL))
alias := fmt.Sprintf("lf-%x", sum[:16])
destination = remote.aliasURL(alias)
- bindings = append(bindings, binding{remote: remote, alias: alias, key: key})
+ bindings = append(bindings, sshBinding{remote: remote, alias: alias, key: key})
remotes = append(remotes, remote)
}
// The exact URL takes priority over the legacy host-wide HTTPS
@@ -136,6 +133,21 @@ func StartSSH(environment map[string]string, keys map[string]string, root, clien
configCount++
}
environment["GIT_CONFIG_COUNT"] = strconv.Itoa(configCount)
+ return bindings, remotes, nil
+}
+
+// StartSSH adds one Git URL rewrite and one OpenSSH identity per repository.
+// Aliases bind each repository to its selected host key.
+// The filesystem contains a socket and public data only. clientRoot is the
+// mount path corresponding to root for a client in a container.
+func StartSSH(environment map[string]string, keys map[string]string, root, clientRoot string) (func(), error) {
+ if len(keys) == 0 {
+ return func() {}, nil
+ }
+ bindings, remotes, err := bindSSHRemotes(environment, keys)
+ if err != nil {
+ return nil, err
+ }
environment["GIT_SSH_VARIANT"] = "ssh"
if len(bindings) == 0 {
// Keep terminals usable for editing without using a host identity.
diff --git a/internal/gitwork/ssh_bind_test.go b/internal/gitwork/ssh_bind_test.go
new file mode 100644
index 00000000..388c009a
--- /dev/null
+++ b/internal/gitwork/ssh_bind_test.go
@@ -0,0 +1,67 @@
+package gitwork
+
+import (
+ "crypto/sha256"
+ "fmt"
+ "testing"
+)
+
+func TestBindSSHRemotesRewritesSortedURLsAfterExistingConfig(t *testing.T) {
+ keyed := "ssh://[email protected]:2222/group/b.git"
+ plain := "[email protected]:group/a.git"
+ env := map[string]string{"GIT_CONFIG_COUNT": "1"}
+ bindings, remotes, err := bindSSHRemotes(env, map[string]string{keyed: "KEY", plain: ""})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(bindings) != 1 || len(remotes) != 1 {
+ t.Fatalf("bindings=%d remotes=%d, want 1/1", len(bindings), len(remotes))
+ }
+ remote, err := ParseSSHRemote(keyed)
+ if err != nil {
+ t.Fatal(err)
+ }
+ sum := sha256.Sum256([]byte(remote.URL))
+ alias := fmt.Sprintf("lf-%x", sum[:16])
+ if got := bindings[0]; got.alias != alias || got.key != "KEY" || got.remote != remote || remotes[0] != remote {
+ t.Fatalf("binding = %+v, want alias %s for %+v", got, alias, remote)
+ }
+ want := map[string]string{
+ "GIT_CONFIG_COUNT": "3",
+ "GIT_CONFIG_KEY_1": "url." + plain + ".insteadOf",
+ "GIT_CONFIG_VALUE_1": plain,
+ "GIT_CONFIG_KEY_2": "url." + remote.aliasURL(alias) + ".insteadOf",
+ "GIT_CONFIG_VALUE_2": keyed,
+ }
+ if len(env) != len(want) {
+ t.Fatalf("env = %v, want %v", env, want)
+ }
+ for k, v := range want {
+ if env[k] != v {
+ t.Fatalf("env[%s] = %q, want %q", k, env[k], v)
+ }
+ }
+}
+
+func TestBindSSHRemotesRejectsInvalidRemote(t *testing.T) {
+ env := map[string]string{}
+ if _, _, err := bindSSHRemotes(env, map[string]string{"https://example.com/a.git": "KEY"}); err == nil {
+ t.Fatal("expected invalid SSH remote error")
+ }
+}
+
+func TestStartSSHWithoutAssignedKeyOnlyRewritesURLs(t *testing.T) {
+ raw := "[email protected]:group/a.git"
+ env := map[string]string{}
+ stop, err := StartSSH(env, map[string]string{raw: ""}, t.TempDir(), "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ stop()
+ if env["GIT_CONFIG_COUNT"] != "1" || env["GIT_CONFIG_VALUE_0"] != raw || env["GIT_SSH_VARIANT"] != "ssh" {
+ t.Fatalf("env = %v", env)
+ }
+ if env["GIT_SSH_COMMAND"] == "" {
+ t.Fatal("expected failing GIT_SSH_COMMAND without assigned key")
+ }
+}