forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_filter_process.go
More file actions
110 lines (89 loc) · 2.89 KB
/
command_filter_process.go
File metadata and controls
110 lines (89 loc) · 2.89 KB
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
104
105
106
107
108
109
110
package commands
import (
"fmt"
"io"
"os"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/filepathfilter"
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfs"
"github.com/spf13/cobra"
)
const (
// cleanFilterBufferCapacity is the desired capacity of the
// `*git.PacketWriter`'s internal buffer when the filter protocol
// dictates the "clean" command. 512 bytes is (in most cases) enough to
// hold an entire LFS pointer in memory.
cleanFilterBufferCapacity = 512
// smudgeFilterBufferCapacity is the desired capacity of the
// `*git.PacketWriter`'s internal buffer when the filter protocol
// dictates the "smudge" command.
smudgeFilterBufferCapacity = git.MaxPacketLength
)
// filterSmudgeSkip is a command-line flag owned by the `filter-process` command
// dictating whether or not to skip the smudging process, leaving pointers as-is
// in the working tree.
var filterSmudgeSkip bool
func filterCommand(cmd *cobra.Command, args []string) {
requireStdin("This command should be run by the Git filter process")
lfs.InstallHooks(false)
s := git.NewFilterProcessScanner(os.Stdin, os.Stdout)
if err := s.Init(); err != nil {
ExitWithError(err)
}
if err := s.NegotiateCapabilities(); err != nil {
ExitWithError(err)
}
skip := filterSmudgeSkip || cfg.Os.Bool("GIT_LFS_SKIP_SMUDGE", false)
filter := filepathfilter.New(cfg.FetchIncludePaths(), cfg.FetchExcludePaths())
var malformed []string
for s.Scan() {
var err error
var w *git.PktlineWriter
req := s.Request()
s.WriteStatus(statusFromErr(nil))
switch req.Header["command"] {
case "clean":
w = git.NewPktlineWriter(os.Stdout, cleanFilterBufferCapacity)
err = clean(w, req.Payload, req.Header["pathname"])
case "smudge":
w = git.NewPktlineWriter(os.Stdout, smudgeFilterBufferCapacity)
err = smudge(w, req.Payload, req.Header["pathname"], skip, filter)
default:
ExitWithError(fmt.Errorf("Unknown command %q", req.Header["command"]))
}
if errors.IsNotAPointerError(err) {
malformed = append(malformed, req.Header["pathname"])
err = nil
}
var status string
if ferr := w.Flush(); ferr != nil {
status = statusFromErr(ferr)
} else {
status = statusFromErr(err)
}
s.WriteStatus(status)
}
if len(malformed) > 0 {
fmt.Fprintf(os.Stderr, "Encountered %d file(s) that should have been pointers, but weren't:\n", len(malformed))
for _, m := range malformed {
fmt.Fprintf(os.Stderr, "\t%s\n", m)
}
}
if err := s.Err(); err != nil && err != io.EOF {
ExitWithError(err)
}
}
// statusFromErr returns the status code that should be sent over the filter
// protocol based on a given error, "err".
func statusFromErr(err error) string {
if err != nil && err != io.EOF {
return "error"
}
return "success"
}
func init() {
RegisterCommand("filter-process", filterCommand, func(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&filterSmudgeSkip, "skip", "s", false, "")
})
}