• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gittuf / gittuf / 29552153989

17 Jul 2026 03:20AM UTC coverage: 78.033% (+0.2%) from 77.791%
29552153989

push

github

web-flow
Merge pull request #1472 from pjbgf/sha256

feat(sha256): support SHA-256 object format

179 of 220 new or added lines in 11 files covered. (81.36%)

2 existing lines in 2 files now uncovered.

12721 of 16302 relevant lines covered (78.03%)

36.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.09
/pkg/gitinterface/common.go
1
// Copyright The gittuf Authors
2
// SPDX-License-Identifier: Apache-2.0
3

4
package gitinterface
5

6
import (
7
        "os"
8
        "os/exec"
9
        "path/filepath"
10
        "testing"
11
        "time"
12

13
        artifacts "github.com/gittuf/gittuf/internal/testartifacts"
14
        "github.com/jonboulle/clockwork"
15
)
16

17
// ObjectFormat identifies the hash algorithm a Git repository uses for its
18
// object IDs.
19
type ObjectFormat string
20

21
const (
22
        ObjectFormatSHA1   ObjectFormat = "sha1"
23
        ObjectFormatSHA256 ObjectFormat = "sha256"
24
)
25

26
const (
27
        testName  = "Jane Doe"
28
        testEmail = "jane.doe@example.com"
29
)
30

31
var (
32
        testClock = clockwork.NewFakeClockAt(time.Date(1995, time.October, 26, 9, 0, 0, 0, time.UTC))
33
)
34

35
type testRepositoryOptions struct {
36
        objectFormat ObjectFormat
37
}
38

39
// TestRepositoryOption configures the repository created by
40
// CreateTestGitRepository.
41
type TestRepositoryOption func(o *testRepositoryOptions)
42

43
// WithObjectFormat creates the test repository using the specified object
44
// format (hash algorithm).
45
func WithObjectFormat(objectFormat ObjectFormat) TestRepositoryOption {
19✔
46
        return func(o *testRepositoryOptions) {
38✔
47
                o.objectFormat = objectFormat
19✔
48
        }
19✔
49
}
50

51
// WithSHA256Format creates the test repository using the SHA-256 object
52
// format.
53
func WithSHA256Format() TestRepositoryOption {
9✔
54
        return WithObjectFormat(ObjectFormatSHA256)
9✔
55
}
9✔
56

57
// CreateTestGitRepository creates a Git repository in the specified directory,
58
// using the SHA-1 object format unless overridden via options. This is meant
59
// to be used by tests across gittuf packages. This helper also sets up an
60
// SSH RSA signing key that can be used to create reproducible commits.
61
func CreateTestGitRepository(t *testing.T, dir string, bare bool, opts ...TestRepositoryOption) *Repository {
128✔
62
        t.Helper()
128✔
63

128✔
64
        repo, err := createTestGitRepository(dir, t.TempDir(), bare, opts...)
128✔
65
        if err != nil {
128✔
NEW
66
                t.Fatal(err)
×
NEW
67
        }
×
68
        return repo
128✔
69
}
70

71
func createTestGitRepository(dir, signingKeysDir string, bare bool, opts ...TestRepositoryOption) (*Repository, error) {
131✔
72
        options := &testRepositoryOptions{objectFormat: ObjectFormatSHA1}
131✔
73
        for _, fn := range opts {
150✔
74
                fn(options)
19✔
75
        }
19✔
76

77
        repo, err := newTestRepository(dir, bare, options.objectFormat)
131✔
78
        if err != nil {
132✔
79
                return nil, err
1✔
80
        }
1✔
81

82
        // Set up author / committer identity
83
        if err := repo.SetGitConfig("user.name", testName); err != nil {
130✔
NEW
84
                return nil, err
×
85
        }
×
86
        if err := repo.SetGitConfig("user.email", testEmail); err != nil {
130✔
NEW
87
                return nil, err
×
88
        }
×
89

90
        // Set up signing via SSH key
91
        if err := writeSigningKeys(signingKeysDir); err != nil {
131✔
92
                return nil, err
1✔
93
        }
1✔
94

95
        if err := repo.SetGitConfig("user.signingkey", filepath.Join(signingKeysDir, "key.pub")); err != nil {
129✔
NEW
96
                return nil, err
×
UNCOV
97
        }
×
98
        if err := repo.SetGitConfig("gpg.format", "ssh"); err != nil {
129✔
NEW
99
                return nil, err
×
100
        }
×
101

102
        return repo, nil
129✔
103
}
104

105
func setupRepository(t *testing.T, dir string, bare bool, objectFormat ObjectFormat) *Repository {
7✔
106
        t.Helper()
7✔
107

7✔
108
        repo, err := newTestRepository(dir, bare, objectFormat)
7✔
109
        if err != nil {
7✔
NEW
110
                t.Fatal(err)
×
NEW
111
        }
×
112
        return repo
7✔
113
}
114

115
func newTestRepository(dir string, bare bool, objectFormat ObjectFormat) (*Repository, error) {
138✔
116
        var gitDirPath string
138✔
117
        args := []string{"init"}
138✔
118
        if bare {
165✔
119
                args = append(args, "--bare")
27✔
120
                gitDirPath = dir
27✔
121
        } else {
138✔
122
                gitDirPath = filepath.Join(dir, ".git")
111✔
123
        }
111✔
124
        args = append(args, "--object-format", string(objectFormat))
138✔
125
        args = append(args, "-b", "main")
138✔
126
        args = append(args, dir)
138✔
127

138✔
128
        cmd := exec.Command(binary, args...)
138✔
129
        if err := cmd.Run(); err != nil {
139✔
130
                return nil, err
1✔
131
        }
1✔
132

133
        repo := &Repository{gitDirPath: gitDirPath, clock: testClock}
137✔
134

137✔
135
        format, err := repo.readObjectFormat()
137✔
136
        if err != nil {
137✔
NEW
137
                return nil, err
×
NEW
138
        }
×
139
        repo.objectFormat = format
137✔
140

137✔
141
        return repo, nil
137✔
142
}
143

144
func writeSigningKeys(dir string) error {
133✔
145
        sshPrivateKey := artifacts.SSHRSAPrivate
133✔
146
        sshPublicKey := artifacts.SSHRSAPublicSSH
133✔
147

133✔
148
        privateKeyPath := filepath.Join(dir, "key")
133✔
149
        publicKeyPath := filepath.Join(dir, "key.pub")
133✔
150

133✔
151
        if err := os.WriteFile(privateKeyPath, sshPrivateKey, 0o600); err != nil {
135✔
152
                return err
2✔
153
        }
2✔
154

155
        if err := os.WriteFile(publicKeyPath, sshPublicKey, 0o600); err != nil {
132✔
156
                return err
1✔
157
        }
1✔
158

159
        return nil
130✔
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc