/
save.go
103 lines (85 loc) · 2.3 KB
/
save.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
base58 "github.com/jbenet/go-base58"
attrs "github.com/mildred/doc/attrs"
ignore "github.com/mildred/doc/ignore"
repo "github.com/mildred/doc/repo"
)
const saveUsage string = `doc save [FILE]
For each modified file in DIR or the current directory, computes a checksum and
store it in the extended attributes. A PAR2 archive is also created and stored
separately in the .dirstore directory.
Options:
`
func mainSave(args []string) int {
f := flag.NewFlagSet("save", flag.ExitOnError)
opt_force := f.Bool("force", false, "Force writing xattrs on read only files")
opt_nodocignore := f.Bool("no-docignore", false, "Don't respect .docignore")
f.Usage = func() {
fmt.Print(saveUsage)
f.PrintDefaults()
}
f.Parse(args)
dir := f.Arg(0)
if dir == "" {
dir = "."
}
dirstore := repo.GetRepo(dir)
if dirstore == nil {
fmt.Fprintf(os.Stderr, "%s: Could not find repository, please run doc init\n", dir)
os.Exit(1)
}
status := 0
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err.Error())
status = 1
return err
}
if !*opt_nodocignore && ignore.IsIgnored(path) {
return filepath.SkipDir
}
// Skip .dirstore/ at root
if filepath.Base(path) == attrs.DirStoreName && filepath.Dir(path) == dir && info.IsDir() {
return filepath.SkipDir
} else if info.IsDir() || !info.Mode().IsRegular() {
return nil
}
hashTime, err := repo.GetHashTime(path)
if err != nil && !repo.IsNoData(err) {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err.Error())
return nil
}
var digest []byte
if err != nil || hashTime != info.ModTime() {
digest, err = commitFile(path, info, *opt_force)
if err != nil {
status = 1
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err.Error())
} else if digest != nil {
fmt.Printf("%s %s\n", base58.Encode(digest), path)
}
} else {
digest, err = attrs.Get(path, repo.XattrHash)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
return nil
}
}
err = dirstore.Create(path, digest)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
return nil
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
return status
}