Skip to content

Commit

Permalink
Merge pull request #121 from x-motemen/refne-git
Browse files Browse the repository at this point in the history
return a function instead of a string when retrieving git
  • Loading branch information
Songmu committed Oct 29, 2023
2 parents 07a8802 + 6228e05 commit 95e9e39
Showing 1 changed file with 10 additions and 12 deletions.
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

0 comments on commit 95e9e39

Please sign in to comment.