forked from rosylilly/git-set-mtime
/
main.go
54 lines (43 loc) · 1006 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
const rfc2822 = "Mon, 2 Jan 2006 15:04:05 -0700"
func main() {
lsFiles := exec.Command("git", "ls-files", "-z")
out, err := lsFiles.Output()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
files := strings.Split(strings.TrimRight(string(out), "\x00"), "\x00")
for _, file := range files {
gitLog := exec.Command(
"/bin/sh", "-c",
fmt.Sprintf(`git log -n 1 --date=rfc2822 "%s" | head -n 3 | tail -n 1`, file),
)
out, err := gitLog.Output()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
mStr := strings.TrimSpace(strings.TrimLeft(string(out), "Date:"))
mTime, err := time.Parse(rfc2822, mStr)
if err != nil {
fmt.Fprintf(os.Stderr, "%s on %s", err, file)
os.Exit(1)
}
mTimeval := syscall.NsecToTimeval(mTime.UnixNano())
times := []syscall.Timeval{
mTimeval,
mTimeval,
}
syscall.Utimes(file, times)
fmt.Printf("%s: %s\n", file, mTime)
}
}