Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return a function instead of a string when retrieving git #121

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,33 +234,31 @@ func asEntry(atomEntry *atom.Entry, err error) (*entry, error) {
return entryFromAtom(atomEntry)
}

var getGit = sync.OnceValue(func() string {
var getGit = sync.OnceValue(func() func(...string) (string, error) {
g, err := exec.LookPath("git")
if err != nil {
return ""
if err != nil || g == "" {
return nil
}
return func(args ...string) (string, error) {
bb, err := exec.Command(g, args...).Output()
return strings.TrimSpace(string(bb)), err
}
return g
})

func doCommand(name string, args ...string) (string, error) {
bb, err := exec.Command(name, args...).Output()
return strings.TrimSpace(string(bb)), err
}

func modTime(fpath string) (time.Time, error) {
fi, err := os.Stat(fpath)
if err != nil {
return time.Time{}, err
}
ti := fi.ModTime()

if git := getGit(); git != "" {
if git := getGit(); git != nil {
// ref. https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-emaiem
// %aI means "author date, strict ISO 8601 format"
timeStr, err := doCommand(git, "log", "-1", "--format=%aI", "--", fpath)
timeStr, err := git("log", "-1", "--format=%aI", "--", fpath)
if err == nil && timeStr != "" {
// check if the file is clean (not modified) on git
out, err := doCommand(git, "status", "-s", "--", fpath)
out, err := git("status", "-s", "--", fpath)
// if the output is empty, it means the file has not been modified
if err == nil && out == "" {
t, err := time.Parse(time.RFC3339, timeStr)
Expand Down
Loading