Skip to content

Commit 795ff4b

Browse files
committed
Add better ModTime in gitfs and ArchGitTime helper (for SOURCE_DATE_EPOCH)
1 parent 2d67127 commit 795ff4b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

cmd/bashbrew/git.go

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

1516
"github.com/urfave/cli"
1617

@@ -110,6 +111,21 @@ func (r Repo) archGitFS(arch string, entry *manifest.Manifest2822Entry) (fs.FS,
110111
return fs.Sub(gitFS, entry.ArchDirectory(arch))
111112
}
112113

114+
// returns the timestamp of the ArchGitCommit -- useful for SOURCE_DATE_EPOCH
115+
func (r Repo) ArchGitTime(arch string, entry *manifest.Manifest2822Entry) (time.Time, error) {
116+
f, err := r.archGitFS(arch, entry)
117+
if err != nil {
118+
return time.Time{}, err
119+
}
120+
121+
fi, err := fs.Stat(f, ".")
122+
if err != nil {
123+
return time.Time{}, err
124+
}
125+
126+
return fi.ModTime(), nil
127+
}
128+
113129
func gitCommitFS(commit string) (fs.FS, error) {
114130
if err := ensureGitInit(); err != nil {
115131
return nil, err

pkg/gitfs/fs.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import (
1717

1818
// https://github.com/go-git/go-git/issues/296
1919

20+
func CommitTime(commit *goGitPlumbingObject.Commit) time.Time {
21+
if commit.Committer.When.After(commit.Author.When) {
22+
return commit.Committer.When
23+
} else {
24+
return commit.Author.When
25+
}
26+
}
27+
2028
func CommitHash(repo *goGit.Repository, commit string) (fs.FS, error) {
2129
gitCommit, err := repo.CommitObject(goGitPlumbing.NewHash(commit))
2230
if err != nil {
@@ -31,6 +39,7 @@ func CommitHash(repo *goGit.Repository, commit string) (fs.FS, error) {
3139
storer: repo.Storer,
3240
tree: tree,
3341
name: ".",
42+
Mod: CommitTime(gitCommit),
3443
},
3544
}, nil
3645
}
@@ -49,6 +58,8 @@ type gitFS struct {
4958
tree *goGitPlumbingObject.Tree
5059
entry *goGitPlumbingObject.TreeEntry // might be nil ("." at the top-level of the repo)
5160

61+
Mod time.Time
62+
5263
// cached values
5364
name string // full path from the repository root
5465
size int64 // Tree.Size value for non-directories (more efficient than opening/reading the blob)
@@ -342,7 +353,7 @@ func (f gitFS) Mode() fs.FileMode {
342353

343354
// https://pkg.go.dev/io/fs#FileInfo: modification time
344355
func (f gitFS) ModTime() time.Time {
345-
return time.Time{} // TODO maybe pass down whichever is more recent of commit.Author.When vs commit.Committer.When ?
356+
return f.Mod
346357
}
347358

348359
// https://pkg.go.dev/io/fs#FileInfo: abbreviation for Mode().IsDir()

pkg/gitfs/tarscrub_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,35 @@ func ExampleGitVarnish() {
4242
fmt.Printf("%x\n", h.Sum(nil))
4343
// Output: 3aef5ac859b23d65dfe5e9f2a47750e9a32852222829cfba762a870c1473fad6
4444
}
45+
46+
// this example is nice because it has a different committer vs author timestamp
47+
// https://github.com/tianon/docker-bash/commit/eb7e541caccc813d297e77cf4068f89553256673
48+
// https://github.com/docker-library/official-images/blob/8718b8afb62ff1a001d99bb4f77d95fe352ba187/library/bash
49+
func ExampleGitBash() {
50+
repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
51+
URL: "https://github.com/tianon/docker-bash.git",
52+
SingleBranch: true,
53+
})
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
commit, err := gitfs.CommitHash(repo, "eb7e541caccc813d297e77cf4068f89553256673")
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
f, err := fs.Sub(commit, "5.2")
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
h := sha256.New()
69+
70+
if err := tarscrub.WriteTar(f, h); err != nil {
71+
panic(err)
72+
}
73+
74+
fmt.Printf("%x\n", h.Sum(nil))
75+
// Output: 011c8eda9906b94916a14e7969cfcff3974f1fbf2ff7b8e5c1867ff491dc01d3
76+
}

0 commit comments

Comments
 (0)