diff --git a/go.work.sum b/go.work.sum index 9ea026b5..0a5b9b87 100644 --- a/go.work.sum +++ b/go.work.sum @@ -232,6 +232,7 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/testifysec/go-witness v0.1.13/go.mod h1:nyEU+h1Pma91QABbSzDDikIhGfU4YjN7d+S7Cdn/w20= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/vektah/gqlparser/v2 v2.4.3-0.20220508162109-d3d9eb001575/go.mod h1:flJWIR04IMQPGz+BXLrORkrARBxv/rtyIAFvd/MceW0= github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvcciqcxEHac4CYj89xI= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= @@ -300,6 +301,7 @@ golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/time v0.2.0 h1:52I/1L54xyEQAYdtcSuxtiT84KGYTBGXwayxmIpNJhE= golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= diff --git a/subtrees/go-witness/attestation/git/git.go b/subtrees/go-witness/attestation/git/git.go index 668e489c..2978b4d7 100644 --- a/subtrees/go-witness/attestation/git/git.go +++ b/subtrees/go-witness/attestation/git/git.go @@ -17,6 +17,7 @@ package git import ( "crypto" "fmt" + "strings" "time" "github.com/go-git/go-git/v5" @@ -106,6 +107,9 @@ func (a *Attestor) Attest(ctx *attestation.AttestationContext) error { head, err := repo.Head() if err != nil { + if strings.Contains(err.Error(), "reference not found") { + return nil + } return err } diff --git a/subtrees/go-witness/attestation/git/git_test.go b/subtrees/go-witness/attestation/git/git_test.go index 2ca9e998..c4d93dc4 100644 --- a/subtrees/go-witness/attestation/git/git_test.go +++ b/subtrees/go-witness/attestation/git/git_test.go @@ -43,10 +43,10 @@ func TestNameTypeRunType(t *testing.T) { require.Equal(t, RunType, attestor.RunType(), "Expected the attestor's run type") } -func TestRun(t *testing.T) { +func TestRunWorksWithCommits(t *testing.T) { attestor := New() - _, dir, cleanup := createTestRepo(t) + _, dir, cleanup := createTestRepo(t, true) defer cleanup() ctx, err := attestation.NewContext([]attestation.Attestor{attestor}, attestation.WithWorkingDir(dir)) @@ -140,7 +140,23 @@ func TestRun(t *testing.T) { } -func createTestRepo(t *testing.T) (*git.Repository, string, func()) { +func TestRunWorksWithoutCommits(t *testing.T) { + attestor := New() + + _, dir, cleanup := createTestRepo(t, false) + defer cleanup() + + ctx, err := attestation.NewContext([]attestation.Attestor{attestor}, attestation.WithWorkingDir(dir)) + require.NoError(t, err, "Expected no error from NewContext") + + err = ctx.RunAttestors() + require.NoError(t, err, "Expected no error from RunAttestors") + + require.Empty(t, attestor.ParentHashes, "Expected the parent hashes to be set") +} + +// Creates an ephemeral repo for your testing +func createTestRepo(t *testing.T, withCommit bool) (*git.Repository, string, func()) { // Create a temporary directory for the test repository tmpDir, err := os.MkdirTemp("", "test-repo") require.NoError(t, err) @@ -149,7 +165,21 @@ func createTestRepo(t *testing.T) (*git.Repository, string, func()) { repo, err := git.PlainInit(tmpDir, false) require.NoError(t, err) - // Create a new file in the repository + if withCommit { + addCommit(tmpDir, t, repo) + } + + // Return the test repository, the path to the test repository, and a cleanup function + return repo, tmpDir, func() { + err := os.RemoveAll(tmpDir) + require.NoError(t, err) + } +} + +// Create a new file in the repository +// Add the new file to the repository +// Commit the new file to the repository +func addCommit(tmpDir string, t *testing.T, repo *git.Repository) { filePath := filepath.Join(tmpDir, "test.txt") file, err := os.Create(filePath) require.NoError(t, err) @@ -158,13 +188,11 @@ func createTestRepo(t *testing.T) (*git.Repository, string, func()) { err = file.Close() require.NoError(t, err) - // Add the new file to the repository worktree, err := repo.Worktree() require.NoError(t, err) _, err = worktree.Add("test.txt") require.NoError(t, err) - // Commit the new file to the repository _, err = worktree.Commit("Initial commit", &git.CommitOptions{ Author: &object.Signature{ Name: "Test User", @@ -173,12 +201,6 @@ func createTestRepo(t *testing.T) (*git.Repository, string, func()) { }, }) require.NoError(t, err) - - // Return the test repository, the path to the test repository, and a cleanup function - return repo, tmpDir, func() { - err := os.RemoveAll(tmpDir) - require.NoError(t, err) - } } func createTestCommit(t *testing.T, repoPath string, message string) { // Open the Git repository diff --git a/subtrees/witness/docs/attestors/git.md b/subtrees/witness/docs/attestors/git.md index 636e1bd4..7e00d585 100644 --- a/subtrees/witness/docs/attestors/git.md +++ b/subtrees/witness/docs/attestors/git.md @@ -1,8 +1,10 @@ # Git Attestor The Git Attestor records the current state of the objects in the git repository, including untracked objects. + Both staged and unstaged states are recorded. +The Git Attestor assumes you are working from a git repository that has been initialized and has commits. ## Subjects