Skip to content

Commit d1b64fc

Browse files
authored
Set correct timestamp for commits created via puregoCommit (#328)
Signed-off-by: Gabriel De Obieta <[email protected]>
1 parent c801a8c commit d1b64fc

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

pkg/repo/clone.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"os/exec"
1313
"strings"
14+
"time"
1415

1516
billy "github.com/go-git/go-billy/v6"
1617
git "github.com/go-git/go-git/v6"
@@ -211,6 +212,7 @@ func (c *Clone) puregoCommit(opts *options.CommitOptions) error {
211212
copts.Author = &object.Signature{
212213
Name: opts.Name,
213214
Email: opts.Email,
215+
When: time.Now(),
214216
}
215217
}
216218
_ = copts.Validate(c.repo) //nolint:errcheck // This just loads the user details

pkg/repo/clone_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-FileCopyrightText: Copyright 2025 The SLSA Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package repo
5+
6+
import (
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
memfs "github.com/go-git/go-billy/v6/memfs"
12+
git "github.com/go-git/go-git/v6"
13+
"github.com/go-git/go-git/v6/storage/memory"
14+
15+
"github.com/slsa-framework/source-tool/pkg/repo/options"
16+
"github.com/slsa-framework/source-tool/pkg/sourcetool/models"
17+
)
18+
19+
// TestCloneAddFiles_CreatesValidCommit is an integration test that verifies
20+
// the entire workflow of adding files and committing works correctly with timestamps.
21+
func TestCloneAddFiles_CreatesValidCommit(t *testing.T) {
22+
// Create an in-memory repository
23+
storer := memory.NewStorage()
24+
fs := memfs.New()
25+
26+
repo, err := git.Init(storer, git.WithWorkTree(fs))
27+
if err != nil {
28+
t.Fatalf("Failed to initialize test repository: %v", err)
29+
}
30+
31+
clone := &Clone{
32+
Repository: models.Repository{
33+
Hostname: "github.com",
34+
Path: "test/repo",
35+
},
36+
repo: repo,
37+
fs: fs,
38+
}
39+
40+
// Add files using the AddFiles method
41+
files := []*PullRequestFileEntry{
42+
{
43+
Path: "workflow.yaml",
44+
Reader: strings.NewReader("name: Test Workflow\non: push\n"),
45+
},
46+
}
47+
48+
err = clone.AddFiles(clone, files)
49+
if err != nil {
50+
t.Fatalf("AddFiles failed: %v", err)
51+
}
52+
53+
// Create a commit
54+
useGit := false
55+
commitOpts := &options.CommitOptions{
56+
Name: "Workflow Bot",
57+
58+
Message: "Add SLSA Source Provenance Workflow",
59+
UseGit: &useGit,
60+
}
61+
62+
beforeCommit := time.Now()
63+
err = clone.Commit(commitOpts)
64+
if err != nil {
65+
t.Fatalf("Commit failed: %v", err)
66+
}
67+
afterCommit := time.Now()
68+
69+
// Verify the commit
70+
ref, err := repo.Head()
71+
if err != nil {
72+
t.Fatalf("Failed to get HEAD reference: %v", err)
73+
}
74+
75+
commit, err := repo.CommitObject(ref.Hash())
76+
if err != nil {
77+
t.Fatalf("Failed to get commit object: %v", err)
78+
}
79+
80+
// Check the commit is not from epoch
81+
epochTime := time.Unix(0, 0)
82+
year1970 := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
83+
84+
if commit.Author.When.Year() == 1970 {
85+
t.Errorf("Commit has 1970 timestamp: %v (this is the bug we're fixing)", commit.Author.When)
86+
}
87+
88+
if commit.Author.When.Equal(epochTime) || commit.Author.When.Equal(year1970) {
89+
t.Errorf("Commit timestamp is epoch (Jan 1, 1970): %v", commit.Author.When)
90+
}
91+
92+
// Verify timestamp is current
93+
if commit.Author.When.Before(beforeCommit.Add(-1*time.Minute)) ||
94+
commit.Author.When.After(afterCommit.Add(1*time.Minute)) {
95+
t.Errorf("Commit timestamp %v is not close to current time [%v, %v]",
96+
commit.Author.When, beforeCommit, afterCommit)
97+
}
98+
99+
// Verify the file was added
100+
tree, err := commit.Tree()
101+
if err != nil {
102+
t.Fatalf("Failed to get commit tree: %v", err)
103+
}
104+
105+
_, err = tree.File("workflow.yaml")
106+
if err != nil {
107+
t.Errorf("workflow.yaml not found in commit tree: %v", err)
108+
}
109+
}

0 commit comments

Comments
 (0)