Skip to content

Commit f25329b

Browse files
committed
Fix duplicated createCommit and SSH agent connection leak
- Replace strategy.createCommit body with delegation to checkpoint.CreateCommit, eliminating duplicated commit-creation logic and independent signing call - Cache SSH agent connection with sync.Once in connectSSHAgent to prevent leaking a new socket connection on each invocation
1 parent 9deb0f1 commit f25329b

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

cmd/entire/cli/strategy/common.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,40 +1439,9 @@ func ExtractSessionIDFromCommit(commit *object.Commit) string {
14391439
//
14401440
// See push_common.go and session_test.go for usage examples.
14411441

1442-
// createCommit creates a commit object
1442+
// createCommit creates a commit object by delegating to checkpoint.CreateCommit.
14431443
func createCommit(ctx context.Context, repo *git.Repository, treeHash, parentHash plumbing.Hash, message, authorName, authorEmail string) (plumbing.Hash, error) { //nolint:unparam // already present in codebase
1444-
now := time.Now()
1445-
sig := object.Signature{
1446-
Name: authorName,
1447-
Email: authorEmail,
1448-
When: now,
1449-
}
1450-
1451-
commit := &object.Commit{
1452-
TreeHash: treeHash,
1453-
Author: sig,
1454-
Committer: sig,
1455-
Message: message,
1456-
}
1457-
1458-
// Add parent if not a new branch
1459-
if parentHash != plumbing.ZeroHash {
1460-
commit.ParentHashes = []plumbing.Hash{parentHash}
1461-
}
1462-
1463-
checkpoint.SignCommitBestEffort(ctx, commit)
1464-
1465-
obj := repo.Storer.NewEncodedObject()
1466-
if err := commit.Encode(obj); err != nil {
1467-
return plumbing.ZeroHash, fmt.Errorf("failed to encode commit: %w", err)
1468-
}
1469-
1470-
hash, err := repo.Storer.SetEncodedObject(obj)
1471-
if err != nil {
1472-
return plumbing.ZeroHash, fmt.Errorf("failed to store commit: %w", err)
1473-
}
1474-
1475-
return hash, nil
1444+
return checkpoint.CreateCommit(ctx, repo, treeHash, parentHash, message, authorName, authorEmail)
14761445
}
14771446

14781447
// getSessionDescriptionFromTree reads the first line of prompt.txt from a git tree.

cmd/entire/main.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/signal"
1010
"runtime"
1111
"strings"
12+
"sync"
1213
"syscall"
1314

1415
"github.com/entireio/cli/cmd/entire/cli"
@@ -92,20 +93,30 @@ func registerObjectSigner() {
9293
})
9394
}
9495

96+
var (
97+
sshAgentOnce sync.Once
98+
sshAgentClient agent.Agent
99+
)
100+
95101
// connectSSHAgent connects to the SSH agent via SSH_AUTH_SOCK.
102+
// The connection is established at most once per process and reused.
96103
// Returns nil if the agent is unavailable.
97104
func connectSSHAgent() agent.Agent {
98-
sock := os.Getenv("SSH_AUTH_SOCK")
99-
if sock == "" {
100-
return nil
101-
}
105+
sshAgentOnce.Do(func() {
106+
sock := os.Getenv("SSH_AUTH_SOCK")
107+
if sock == "" {
108+
return
109+
}
102110

103-
conn, err := net.Dial("unix", sock)
104-
if err != nil {
105-
return nil
106-
}
111+
conn, err := net.Dial("unix", sock)
112+
if err != nil {
113+
return
114+
}
115+
116+
sshAgentClient = agent.NewClient(conn)
117+
})
107118

108-
return agent.NewClient(conn)
119+
return sshAgentClient
109120
}
110121

111122
var scopeName = map[config.Scope]string{

0 commit comments

Comments
 (0)