Debian Codegraph @

plumbing/object/change.go

raw
1 package object
2
3 import (
4 "bytes"
5 "context"
6 "fmt"
7 "strings"
8
9 "github.com/go-git/go-git/v5/utils/merkletrie"
10 )
11
12 // Change values represent a detected change between two git trees. For
13 // modifications, From is the original status of the node and To is its
14 // final status. For insertions, From is the zero value and for
15 // deletions To is the zero value.
16 type Change struct {
17 From ChangeEntry
18 To ChangeEntry
19 }
20
21 var empty ChangeEntry
22
23 // Action returns the kind of action represented by the change, an
24 // insertion, a deletion or a modification.
25 func (c *Change) Action() (merkletrie.Action, error) {
26 if c.From == empty && c.To == empty {
27 return merkletrie.Action(0),
28 fmt.Errorf("malformed change: empty from and to")
29 }
30
31 if c.From == empty {
32 return merkletrie.Insert, nil
33 }
34
35 if c.To == empty {
36 return merkletrie.Delete, nil
37 }
38
39 return merkletrie.Modify, nil
40 }
41
42 // Files returns the files before and after a change.
43 // For insertions from will be nil. For deletions to will be nil.
44 func (c *Change) Files() (from, to *File, err error) {
45 action, err := c.Action()
46 if err != nil {
47 return
48 }
49
50 if action == merkletrie.Insert || action == merkletrie.Modify {
51 to, err = c.To.Tree.TreeEntryFile(&c.To.TreeEntry)
52 if !c.To.TreeEntry.Mode.IsFile() {
53 return nil, nil, nil
54 }
55
56 if err != nil {
57 return
58 }
59 }
60
61 if action == merkletrie.Delete || action == merkletrie.Modify {
62 from, err = c.From.Tree.TreeEntryFile(&c.From.TreeEntry)
63 if !c.From.TreeEntry.Mode.IsFile() {
64 return nil, nil, nil
65 }
66
67 if err != nil {
68 return
69 }
70 }
71
72 return
73 }
74
75 func (c *Change) String() string {
76 action, err := c.Action()
77 if err != nil {
78 return "malformed change"
79 }
80
81 return fmt.Sprintf("<Action: %s, Path: %s>", action, c.name())
82 }
83
84 // Patch returns a Patch with all the file changes in chunks. This
85 // representation can be used to create several diff outputs.
86 func (c *Change) Patch() (*Patch, error) {
87 return c.PatchContext(context.Background())
88 }
89
90 // Patch returns a Patch with all the file changes in chunks. This
91 // representation can be used to create several diff outputs.
92 // If context expires, an non-nil error will be returned
93 // Provided context must be non-nil
94 func (c *Change) PatchContext(ctx context.Context) (*Patch, error) {
95 return getPatchContext(ctx, "", c)
96 }
97
98 func (c *Change) name() string {
99 if c.From != empty {
100 return c.From.Name
101 }
102
103 return c.To.Name
104 }
105
106 // ChangeEntry values represent a node that has suffered a change.
107 type ChangeEntry struct {
108 // Full path of the node using "/" as separator.
109 Name string
110 // Parent tree of the node that has changed.
111 Tree *Tree
112 // The entry of the node.
113 TreeEntry TreeEntry
114 }
115
116 // Changes represents a collection of changes between two git trees.
117 // Implements sort.Interface lexicographically over the path of the
118 // changed files.
119 type Changes []*Change
120
121 func (c Changes) Len() int {
122 return len(c)
123 }
124
125 func (c Changes) Swap(i, j int) {
126 c[i], c[j] = c[j], c[i]
127 }
128
129 func (c Changes) Less(i, j int) bool {
130 return strings.Compare(c[i].name(), c[j].name()) < 0
131 }
132
133 func (c Changes) String() string {
134 var buffer bytes.Buffer
135 buffer.WriteString("[")
136 comma := ""
137 for _, v := range c {
138 buffer.WriteString(comma)
139 buffer.WriteString(v.String())
140 comma = ", "
141 }
142 buffer.WriteString("]")
143
144 return buffer.String()
145 }
146
147 // Patch returns a Patch with all the changes in chunks. This
148 // representation can be used to create several diff outputs.
149 func (c Changes) Patch() (*Patch, error) {
150 return c.PatchContext(context.Background())
151 }
152
153 // Patch returns a Patch with all the changes in chunks. This
154 // representation can be used to create several diff outputs.
155 // If context expires, an non-nil error will be returned
156 // Provided context must be non-nil
157 func (c Changes) PatchContext(ctx context.Context) (*Patch, error) {
158 return getPatchContext(ctx, "", c...)
159 }