]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/patch/apply.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / patch / apply.go
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.
4
5 package patch
6
7 import "os"
8
9 // An Op is a single operation to execute to apply a patch.
10 type Op struct {
11 Verb Verb // action
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)
16 }
17
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
21 // if desired.
22 //
23 // The function readFile should return the contents of the named file.
24 // Typically this function will be io.ReadFile.
25 //
26 func (set *Set) Apply(readFile func(string) ([]byte, os.Error)) ([]Op, os.Error) {
27 op := make([]Op, len(set.File))
28
29 for i, f := range set.File {
30 o := &op[i]
31 o.Verb = f.Verb
32 o.Src = f.Src
33 o.Dst = f.Dst
34 o.Mode = f.NewMode
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
39 var err os.Error
40 if f.Src != "" {
41 old, err = readFile(f.Src)
42 if err != nil {
43 return nil, &os.PathError{string(f.Verb), f.Src, err}
44 }
45 }
46 o.Data, err = f.Diff.Apply(old)
47 if err != nil {
48 return nil, &os.PathError{string(f.Verb), f.Src, err}
49 }
50 }
51 }
52
53 return op, nil
54 }