Skip to content

Commit e0c4617

Browse files
committed
Fix linter issues
Signed-off-by: Paulo Gomes <paulo@entire.io> Entire-Checkpoint: abef9edf20cd Entire-Checkpoint: a70d97c363c8
1 parent 9deb0f1 commit e0c4617

6 files changed

Lines changed: 172 additions & 7 deletions

File tree

cmd/entire/cli/checkpoint/committed.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ func GetGitAuthorFromRepo(repo *git.Repository) (name, email string) {
16141614

16151615
// If not found in local config, try global config
16161616
if name == "" || email == "" {
1617+
//nolint:staticcheck // the v6 is not yet released, revisit once it is.
16171618
globalCfg, err := config.LoadConfig(config.GlobalScope)
16181619
if err == nil {
16191620
if name == "" {
@@ -1676,11 +1677,12 @@ func CreateCommit(ctx context.Context, repo *git.Repository, treeHash, parentHas
16761677
// If no signer is registered, signing is disabled via settings, or signing fails,
16771678
// the commit is left unsigned and the error is logged.
16781679
func SignCommitBestEffort(ctx context.Context, commit *object.Commit) {
1679-
if !settings.IsSignCheckpointCommitsEnabled(ctx) {
1680+
// Check plugin availability first (in-memory) before hitting disk for settings.
1681+
if !plugin.Has(plugin.ObjectSigner()) {
16801682
return
16811683
}
16821684

1683-
if !plugin.Has(plugin.ObjectSigner()) {
1685+
if !settings.IsSignCheckpointCommitsEnabled(ctx) {
16841686
return
16851687
}
16861688

@@ -1705,6 +1707,7 @@ func SignCommitBestEffort(ctx context.Context, commit *object.Commit) {
17051707
logging.Warn(ctx, "failed to read encoded commit", slog.String("error", err.Error()))
17061708
return
17071709
}
1710+
defer r.Close()
17081711

17091712
sig, err := signer.Sign(r)
17101713
if err != nil {
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package checkpoint
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
"time"
11+
12+
"github.com/entireio/cli/cmd/entire/cli/paths"
13+
14+
"github.com/go-git/go-git/v6/plumbing"
15+
"github.com/go-git/go-git/v6/plumbing/object"
16+
"github.com/go-git/go-git/v6/x/plugin"
17+
)
18+
19+
type stubSigner struct {
20+
sig []byte
21+
err error
22+
}
23+
24+
func (s *stubSigner) Sign(_ io.Reader) ([]byte, error) {
25+
return s.sig, s.err
26+
}
27+
28+
func setupSigningEnv(t *testing.T, disableSigning bool) {
29+
t.Helper()
30+
31+
dir := t.TempDir()
32+
33+
// Minimal git repo so paths.WorktreeRoot resolves.
34+
if err := os.MkdirAll(filepath.Join(dir, ".git"), 0o755); err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
entireDir := filepath.Join(dir, ".entire")
39+
if err := os.MkdirAll(entireDir, 0o755); err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
if disableSigning {
44+
content := `{"sign_checkpoint_commits": false}`
45+
if err := os.WriteFile(filepath.Join(entireDir, "settings.json"), []byte(content), 0o644); err != nil {
46+
t.Fatal(err)
47+
}
48+
}
49+
50+
paths.ClearWorktreeRootCache()
51+
t.Chdir(dir)
52+
t.Cleanup(func() {
53+
resetPluginEntry("object-signer")
54+
paths.ClearWorktreeRootCache()
55+
})
56+
}
57+
58+
func newTestCommit() *object.Commit {
59+
sig := object.Signature{
60+
Name: "Test",
61+
Email: "test@test.com",
62+
When: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
63+
}
64+
return &object.Commit{
65+
TreeHash: plumbing.ZeroHash,
66+
Author: sig,
67+
Committer: sig,
68+
Message: "test commit",
69+
}
70+
}
71+
72+
func TestSignCommitBestEffort_Signs(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel
73+
setupSigningEnv(t, false)
74+
75+
err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer {
76+
return &stubSigner{sig: []byte("FAKESIG")}
77+
})
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
82+
commit := newTestCommit()
83+
SignCommitBestEffort(context.Background(), commit)
84+
85+
if commit.Signature != "FAKESIG" {
86+
t.Errorf("expected signature %q, got %q", "FAKESIG", commit.Signature)
87+
}
88+
}
89+
90+
func TestSignCommitBestEffort_SkipsWhenDisabled(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel
91+
setupSigningEnv(t, true)
92+
93+
err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer {
94+
t.Fatal("signer should not be called when signing is disabled")
95+
return nil
96+
})
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
101+
commit := newTestCommit()
102+
SignCommitBestEffort(context.Background(), commit)
103+
104+
if commit.Signature != "" {
105+
t.Errorf("expected empty signature, got %q", commit.Signature)
106+
}
107+
}
108+
109+
func TestSignCommitBestEffort_ErrorIsBestEffort(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel
110+
setupSigningEnv(t, false)
111+
112+
err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer {
113+
return &stubSigner{err: errors.New("signing failed")}
114+
})
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
119+
commit := newTestCommit()
120+
SignCommitBestEffort(context.Background(), commit)
121+
122+
if commit.Signature != "" {
123+
t.Errorf("expected empty signature after error, got %q", commit.Signature)
124+
}
125+
}
126+
127+
func TestSignCommitBestEffort_NoSignerRegistered(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel
128+
setupSigningEnv(t, false)
129+
130+
commit := newTestCommit()
131+
SignCommitBestEffort(context.Background(), commit)
132+
133+
if commit.Signature != "" {
134+
t.Errorf("expected empty signature without signer, got %q", commit.Signature)
135+
}
136+
}
137+
138+
func TestSignCommitBestEffort_NilSigner(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel
139+
setupSigningEnv(t, false)
140+
141+
err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer {
142+
return nil
143+
})
144+
if err != nil {
145+
t.Fatal(err)
146+
}
147+
148+
commit := newTestCommit()
149+
SignCommitBestEffort(context.Background(), commit)
150+
151+
if commit.Signature != "" {
152+
t.Errorf("expected empty signature with nil signer, got %q", commit.Signature)
153+
}
154+
}

cmd/entire/cli/checkpoint/global_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func TestMain(m *testing.M) {
1515
// For tests, ensure that go-git always gets empty Configs for both
1616
// system and global scopes. This way the current environment does not
1717
// impact the tests.
18-
err := plugin.Register(plugin.ConfigLoader(), config.NewEmpty)
18+
err := plugin.Register(plugin.ConfigLoader(), func() plugin.ConfigSource {
19+
return config.NewEmpty()
20+
})
1921
if err != nil {
2022
panic(fmt.Errorf("failed to register config storers: %w", err))
2123
}

cmd/entire/cli/global_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func TestMain(m *testing.M) {
1515
// Register a default ConfigSource so tests that call ConfigScoped
1616
// (directly or indirectly via Commit/CreateTag) don't fail with
1717
// "no config loader registered".
18-
err := plugin.Register(plugin.ConfigLoader(), config.NewEmpty)
18+
err := plugin.Register(plugin.ConfigLoader(), func() plugin.ConfigSource {
19+
return config.NewEmpty()
20+
})
1921
if err != nil {
2022
panic(fmt.Errorf("failed to register config storers: %w", err))
2123
}

cmd/entire/cli/strategy/global_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func TestMain(m *testing.M) {
1515
// Register a default ConfigSource so tests that call ConfigScoped
1616
// (directly or indirectly via Commit/CreateTag) don't fail with
1717
// "no config loader registered".
18-
err := plugin.Register(plugin.ConfigLoader(), config.NewEmpty)
18+
err := plugin.Register(plugin.ConfigLoader(), func() plugin.ConfigSource {
19+
return config.NewEmpty()
20+
})
1921
if err != nil {
2022
panic(fmt.Errorf("failed to register config storers: %w", err))
2123
}

cmd/entire/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func main() {
5959
}
6060

6161
func registerObjectSigner() {
62+
//nolint:errcheck,gosec // best-effort; if registration fails, commits are left unsigned
6263
plugin.Register(plugin.ObjectSigner(), func() plugin.Signer {
6364
cfgSource, err := plugin.Get(plugin.ConfigLoader())
6465
if err != nil {
@@ -94,13 +95,14 @@ func registerObjectSigner() {
9495

9596
// connectSSHAgent connects to the SSH agent via SSH_AUTH_SOCK.
9697
// Returns nil if the agent is unavailable.
97-
func connectSSHAgent() agent.Agent {
98+
func connectSSHAgent() agent.Agent { //nolint:ireturn // must return the ssh agent interface
9899
sock := os.Getenv("SSH_AUTH_SOCK")
99100
if sock == "" {
100101
return nil
101102
}
102103

103-
conn, err := net.Dial("unix", sock)
104+
var d net.Dialer
105+
conn, err := d.DialContext(context.Background(), "unix", sock)
104106
if err != nil {
105107
return nil
106108
}

0 commit comments

Comments
 (0)