-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchup.go
84 lines (67 loc) · 1.91 KB
/
touchup.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
package sdk
import (
"fmt"
"path/filepath"
"time"
"github.com/julian7/redact/files"
"github.com/julian7/redact/gitutil"
"github.com/spf13/afero"
)
// TouchUp touches and checks out encrypted files, generally fixing them.
func TouchUp(masterkey *files.MasterKey, softErrHandler func(string, error)) error {
files, err := gitutil.LsFiles(nil)
if err != nil {
return fmt.Errorf("list git files: %w", err)
}
err = files.CheckAttrs()
if err != nil {
return fmt.Errorf("check git files' attributes: %w", err)
}
affectedFiles := make([]string, 0, len(files.Items))
for _, entry := range files.Items {
if entry.Filter == AttrName && entry.Status != gitutil.StatusOther {
affectedFiles = append(affectedFiles, entry.Name)
}
}
if softErrHandler != nil {
for _, err := range files.Errors {
softErrHandler(err.Name, err.Orig)
}
}
return TouchUpFiles(masterkey, affectedFiles, softErrHandler)
}
// TouchUpFiles force-checkouts specific files in a repo
func TouchUpFiles(masterkey *files.MasterKey, files []string, softErrHandler func(string, error)) error {
if len(files) < 1 {
return nil
}
touched := make([]string, 0, len(files))
for _, entry := range files {
fullpath := filepath.Join(masterkey.RepoInfo.Toplevel, entry)
if err := TouchFile(masterkey.Fs, fullpath); err != nil && softErrHandler != nil {
softErrHandler(entry, err)
continue
}
touched = append(touched, fullpath)
}
if len(touched) > 0 {
issues, err := gitutil.Checkout(touched)
if len(issues) > 0 && softErrHandler != nil {
for _, issue := range issues {
softErrHandler(issue.Name, issue.Orig)
}
}
if err != nil {
return err
}
}
return nil
}
// TouchFile touches a single file
func TouchFile(filesystem afero.Fs, fullpath string) error {
touchTime := time.Now()
if err := filesystem.Chtimes(fullpath, touchTime, touchTime); err != nil {
return fmt.Errorf("touch file: %w", err)
}
return nil
}