1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // An Op is a single operation to execute to apply a patch.
12 Src string // source file
13 Dst string // destination file
14 Mode int // mode for destination (if non-zero)
15 Data []byte // data for destination (if non-nil)
18 // Apply applies the patch set to the files named in the patch set,
19 // constructing an in-memory copy of the new file state.
20 // It is the client's job to write the changes to the file system
23 // The function readFile should return the contents of the named file.
24 // Typically this function will be io.ReadFile.
26 func (set *Set) Apply(readFile func(string) ([]byte, os.Error)) ([]Op, os.Error) {
27 op := make([]Op, len(set.File))
29 for i, f := range set.File {
35 if f.Diff != NoDiff || o.Verb != Edit {
36 // Clients assume o.Data == nil means no data diff.
37 // Start with a non-nil data.
38 var old []byte = make([]byte, 0) // not nil
41 old, err = readFile(f.Src)
43 return nil, &os.PathError{string(f.Verb), f.Src, err}
46 o.Data, err = f.Diff.Apply(old)
48 return nil, &os.PathError{string(f.Verb), f.Src, err}