Debian Codegraph @

lfs/extension.go

raw symbols SCIP
1 package lfs
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/hex"
7 "hash"
8 "io"
9 "os"
10 "strings"
11
12 "github.com/git-lfs/git-lfs/v3/config"
13 "github.com/git-lfs/git-lfs/v3/errors"
14 "github.com/git-lfs/git-lfs/v3/subprocess"
15 "github.com/git-lfs/git-lfs/v3/tr"
16 )
17
18 type pipeRequest struct {
19 action string
20 reader io.Reader
21 fileName string
22 extensions []config.Extension
23 }
24
25 type pipeResponse struct {
26 file *os.File
27 results []*pipeExtResult
28 }
29
30 type pipeExtResult struct {
31 name string
32 oidIn string
33 oidOut string
34 }
35
36 type extCommand struct {
37 cmd *subprocess.Cmd
38 out io.WriteCloser
39 err *bytes.Buffer
40 hasher hash.Hash
41 result *pipeExtResult
42 }
43
44 func pipeExtensions(cfg *config.Configuration, request *pipeRequest) (response pipeResponse, err error) {
45 var extcmds []*extCommand
46 defer func() {
47 // In the case of an early return before the end of this
48 // function (in response to an error, etc), kill all running
49 // processes. Errors are ignored since the function has already
50 // returned.
51 //
52 // In the happy path, the commands will have already been
53 // `Wait()`-ed upon and e.cmd.Process.Kill() will return an
54 // error, but we can ignore it.
55 for _, e := range extcmds {
56 if e.cmd.Process != nil {
57 e.cmd.Process.Kill()
58 }
59 }
60 }()
61
62 for _, e := range request.extensions {
63 var pieces []string
64 switch request.action {
65 case "clean":
66 pieces = strings.Split(e.Clean, " ")
67 case "smudge":
68 pieces = strings.Split(e.Smudge, " ")
69 default:
70 err = errors.New(tr.Tr.Get("Invalid action: %s", request.action))
71 return
72 }
73 name := strings.Trim(pieces[0], " ")
74 var args []string
75 for _, value := range pieces[1:] {
76 arg := strings.Replace(value, "%f", request.fileName, -1)
77 args = append(args, arg)
78 }
79 var cmd *subprocess.Cmd
80 cmd, err = subprocess.ExecCommand(name, args...)
81 if err != nil {
82 return
83 }
84 ec := &extCommand{cmd: cmd, result: &pipeExtResult{name: e.Name}}
85 extcmds = append(extcmds, ec)
86 }
87
88 hasher := sha256.New()
89 pipeReader, pipeWriter := io.Pipe()
90 multiWriter := io.MultiWriter(hasher, pipeWriter)
91
92 var input io.Reader
93 var output io.WriteCloser
94 input = pipeReader
95 extcmds[0].cmd.Stdin = input
96 if response.file, err = TempFile(cfg, ""); err != nil {
97 return
98 }
99 defer response.file.Close()
100 output = response.file
101
102 last := len(extcmds) - 1
103 for i, ec := range extcmds {
104 ec.hasher = sha256.New()
105
106 if i == last {
107 ec.cmd.Stdout = io.MultiWriter(ec.hasher, output)
108 ec.out = output
109 continue
110 }
111
112 nextec := extcmds[i+1]
113 var nextStdin io.WriteCloser
114 var stdout io.ReadCloser
115 if nextStdin, err = nextec.cmd.StdinPipe(); err != nil {
116 return
117 }
118 if stdout, err = ec.cmd.StdoutPipe(); err != nil {
119 return
120 }
121
122 ec.cmd.Stdin = input
123 ec.cmd.Stdout = io.MultiWriter(ec.hasher, nextStdin)
124 ec.out = nextStdin
125
126 input = stdout
127
128 var errBuff bytes.Buffer
129 ec.err = &errBuff
130 ec.cmd.Stderr = ec.err
131 }
132
133 for _, ec := range extcmds {
134 if err = ec.cmd.Start(); err != nil {
135 return
136 }
137 }
138
139 if _, err = io.Copy(multiWriter, request.reader); err != nil {
140 return
141 }
142 if err = pipeWriter.Close(); err != nil {
143 return
144 }
145
146 for _, ec := range extcmds {
147 if err = ec.cmd.Wait(); err != nil {
148 if ec.err != nil {
149 errStr := ec.err.String()
150 err = errors.New(tr.Tr.Get("extension '%s' failed with: %s", ec.result.name, errStr))
151 }
152 return
153 }
154 if err = ec.out.Close(); err != nil {
155 return
156 }
157 }
158
159 oid := hex.EncodeToString(hasher.Sum(nil))
160 for _, ec := range extcmds {
161 ec.result.oidIn = oid
162 oid = hex.EncodeToString(ec.hasher.Sum(nil))
163 ec.result.oidOut = oid
164 response.results = append(response.results, ec.result)
165 }
166 return
167 }