Skip to content

Commit

Permalink
Add better ModTime in gitfs and ArchGitTime helper (for `SOURCE…
Browse files Browse the repository at this point in the history
…_DATE_EPOCH`)
  • Loading branch information
tianon committed Jan 9, 2024
1 parent 8fbedb3 commit 39d42ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
16 changes: 16 additions & 0 deletions cmd/bashbrew/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/urfave/cli"

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

// returns the timestamp of the ArchGitCommit -- useful for SOURCE_DATE_EPOCH
func (r Repo) ArchGitTime(arch string, entry *manifest.Manifest2822Entry) (time.Time, error) {
f, err := r.archGitFS(arch, entry)
if err != nil {
return time.Time{}, err
}

fi, err := fs.Stat(f, ".")
if err != nil {
return time.Time{}, err
}

return fi.ModTime(), nil
}

func gitCommitFS(commit string) (fs.FS, error) {
if err := ensureGitInit(); err != nil {
return nil, err
Expand Down
16 changes: 15 additions & 1 deletion pkg/gitfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import (

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

func CommitTime(commit *goGitPlumbingObject.Commit) time.Time {
if commit == nil {
return time.Time{}
}
if commit.Author.When.After(commit.Committer.When) {
return commit.Author.When
} else {
return commit.Committer.When
}
}

func CommitHash(repo *goGit.Repository, commit string) (fs.FS, error) {
gitCommit, err := repo.CommitObject(goGitPlumbing.NewHash(commit))
if err != nil {
Expand All @@ -31,6 +42,7 @@ func CommitHash(repo *goGit.Repository, commit string) (fs.FS, error) {
storer: repo.Storer,
tree: tree,
name: ".",
Mod: CommitTime(gitCommit),
},
}, nil
}
Expand All @@ -49,6 +61,8 @@ type gitFS struct {
tree *goGitPlumbingObject.Tree
entry *goGitPlumbingObject.TreeEntry // might be nil ("." at the top-level of the repo)

Mod time.Time

// cached values
name string // full path from the repository root
size int64 // Tree.Size value for non-directories (more efficient than opening/reading the blob)
Expand Down Expand Up @@ -343,7 +357,7 @@ func (f gitFS) Mode() fs.FileMode {

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

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

0 comments on commit 39d42ec

Please sign in to comment.