fork of go-gitdiff with jj support
1package gitdiff
2
3import (
4 "fmt"
5 "os"
6)
7
8// File describes changes to a single file. It can be either a text file or a
9// binary file.
10type File struct {
11 OldName string
12 NewName string
13
14 IsNew bool
15 IsDelete bool
16 IsCopy bool
17 IsRename bool
18
19 OldMode os.FileMode
20 NewMode os.FileMode
21
22 OldOIDPrefix string
23 NewOIDPrefix string
24 Score int
25
26 // TextFragments contains the fragments describing changes to a text file. It
27 // may be empty if the file is empty or if only the mode changes.
28 TextFragments []*TextFragment
29
30 // IsBinary is true if the file is a binary file. If the patch includes
31 // binary data, BinaryFragment will be non-nil and describe the changes to
32 // the data. If the patch is reversible, ReverseBinaryFragment will also be
33 // non-nil and describe the changes needed to restore the original file
34 // after applying the changes in BinaryFragment.
35 IsBinary bool
36 BinaryFragment *BinaryFragment
37 ReverseBinaryFragment *BinaryFragment
38}
39
40// TextFragment describes changed lines starting at a specific line in a text file.
41type TextFragment struct {
42 Comment string
43
44 OldPosition int64
45 OldLines int64
46
47 NewPosition int64
48 NewLines int64
49
50 LinesAdded int64
51 LinesDeleted int64
52
53 LeadingContext int64
54 TrailingContext int64
55
56 Lines []Line
57}
58
59// Header returns the canonical header of this fragment.
60func (f *TextFragment) Header() string {
61 return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
62}
63
64// Line is a line in a text fragment.
65type Line struct {
66 Op LineOp
67 Line string
68}
69
70func (fl Line) String() string {
71 return fl.Op.String() + fl.Line
72}
73
74// LineOp describes the type of a text fragment line: context, added, or removed.
75type LineOp int
76
77const (
78 // OpContext indicates a context line
79 OpContext LineOp = iota
80 // OpDelete indicates a deleted line
81 OpDelete
82 // OpAdd indicates an added line
83 OpAdd
84)
85
86func (op LineOp) String() string {
87 switch op {
88 case OpContext:
89 return " "
90 case OpDelete:
91 return "-"
92 case OpAdd:
93 return "+"
94 }
95 return "?"
96}
97
98// BinaryFragment describes changes to a binary file.
99type BinaryFragment struct {
100 Method BinaryPatchMethod
101 Size int64
102 Data []byte
103}
104
105// BinaryPatchMethod is the method used to create and apply the binary patch.
106type BinaryPatchMethod int
107
108const (
109 // BinaryPatchDelta indicates the data uses Git's packfile encoding
110 BinaryPatchDelta BinaryPatchMethod = iota
111 // BinaryPatchLiteral indicates the data is the exact file content
112 BinaryPatchLiteral
113)