Debian Codegraph @

plumbing/format/index/index.go

raw
1 package index
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "path/filepath"
8 "strings"
9 "time"
10
11 "github.com/go-git/go-git/v5/plumbing"
12 "github.com/go-git/go-git/v5/plumbing/filemode"
13 )
14
15 var (
16 // ErrUnsupportedVersion is returned by Decode when the index file version
17 // is not supported.
18 ErrUnsupportedVersion = errors.New("unsupported version")
19 // ErrEntryNotFound is returned by Index.Entry, if an entry is not found.
20 ErrEntryNotFound = errors.New("entry not found")
21
22 indexSignature = []byte{'D', 'I', 'R', 'C'}
23 treeExtSignature = []byte{'T', 'R', 'E', 'E'}
24 resolveUndoExtSignature = []byte{'R', 'E', 'U', 'C'}
25 endOfIndexEntryExtSignature = []byte{'E', 'O', 'I', 'E'}
26 )
27
28 // Stage during merge
29 type Stage int
30
31 const (
32 // Merged is the default stage, fully merged
33 Merged Stage = 1
34 // AncestorMode is the base revision
35 AncestorMode Stage = 1
36 // OurMode is the first tree revision, ours
37 OurMode Stage = 2
38 // TheirMode is the second tree revision, theirs
39 TheirMode Stage = 3
40 )
41
42 // Index contains the information about which objects are currently checked out
43 // in the worktree, having information about the working files. Changes in
44 // worktree are detected using this Index. The Index is also used during merges
45 type Index struct {
46 // Version is index version
47 Version uint32
48 // Entries collection of entries represented by this Index. The order of
49 // this collection is not guaranteed
50 Entries []*Entry
51 // Cache represents the 'Cached tree' extension
52 Cache *Tree
53 // ResolveUndo represents the 'Resolve undo' extension
54 ResolveUndo *ResolveUndo
55 // EndOfIndexEntry represents the 'End of Index Entry' extension
56 EndOfIndexEntry *EndOfIndexEntry
57 // ModTime is the modification time of the index file
58 ModTime time.Time
59 }
60
61 // Add creates a new Entry and returns it. The caller should first check that
62 // another entry with the same path does not exist.
63 func (i *Index) Add(path string) *Entry {
64 e := &Entry{
65 Name: filepath.ToSlash(path),
66 }
67
68 i.Entries = append(i.Entries, e)
69 return e
70 }
71
72 // Entry returns the entry that match the given path, if any.
73 func (i *Index) Entry(path string) (*Entry, error) {
74 path = filepath.ToSlash(path)
75 for _, e := range i.Entries {
76 if e.Name == path {
77 return e, nil
78 }
79 }
80
81 return nil, ErrEntryNotFound
82 }
83
84 // Remove remove the entry that match the give path and returns deleted entry.
85 func (i *Index) Remove(path string) (*Entry, error) {
86 path = filepath.ToSlash(path)
87 for index, e := range i.Entries {
88 if e.Name == path {
89 i.Entries = append(i.Entries[:index], i.Entries[index+1:]...)
90 return e, nil
91 }
92 }
93
94 return nil, ErrEntryNotFound
95 }
96
97 // Glob returns the all entries matching pattern or nil if there is no matching
98 // entry. The syntax of patterns is the same as in filepath.Glob.
99 func (i *Index) Glob(pattern string) (matches []*Entry, err error) {
100 pattern = filepath.ToSlash(pattern)
101 for _, e := range i.Entries {
102 m, err := match(pattern, e.Name)
103 if err != nil {
104 return nil, err
105 }
106
107 if m {
108 matches = append(matches, e)
109 }
110 }
111
112 return
113 }
114
115 // String is equivalent to `git ls-files --stage --debug`
116 func (i *Index) String() string {
117 buf := bytes.NewBuffer(nil)
118 for _, e := range i.Entries {
119 buf.WriteString(e.String())
120 }
121
122 return buf.String()
123 }
124
125 // Entry represents a single file (or stage of a file) in the cache. An entry
126 // represents exactly one stage of a file. If a file path is unmerged then
127 // multiple Entry instances may appear for the same path name.
128 type Entry struct {
129 // Hash is the SHA1 of the represented file
130 Hash plumbing.Hash
131 // Name is the Entry path name relative to top level directory
132 Name string
133 // CreatedAt time when the tracked path was created
134 CreatedAt time.Time
135 // ModifiedAt time when the tracked path was changed
136 ModifiedAt time.Time
137 // Dev and Inode of the tracked path
138 Dev, Inode uint32
139 // Mode of the path
140 Mode filemode.FileMode
141 // UID and GID, userid and group id of the owner
142 UID, GID uint32
143 // Size is the length in bytes for regular files
144 Size uint32
145 // Stage on a merge is defines what stage is representing this entry
146 // https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging
147 Stage Stage
148 // SkipWorktree used in sparse checkouts
149 // https://git-scm.com/docs/git-read-tree#_sparse_checkout
150 SkipWorktree bool
151 // IntentToAdd record only the fact that the path will be added later
152 // https://git-scm.com/docs/git-add ("git add -N")
153 IntentToAdd bool
154 }
155
156 func (e Entry) String() string {
157 buf := bytes.NewBuffer(nil)
158
159 fmt.Fprintf(buf, "%06o %s %d\t%s\n", e.Mode, e.Hash, e.Stage, e.Name)
160 fmt.Fprintf(buf, " ctime: %d:%d\n", e.CreatedAt.Unix(), e.CreatedAt.Nanosecond())
161 fmt.Fprintf(buf, " mtime: %d:%d\n", e.ModifiedAt.Unix(), e.ModifiedAt.Nanosecond())
162 fmt.Fprintf(buf, " dev: %d\tino: %d\n", e.Dev, e.Inode)
163 fmt.Fprintf(buf, " uid: %d\tgid: %d\n", e.UID, e.GID)
164 fmt.Fprintf(buf, " size: %d\tflags: %x\n", e.Size, 0)
165
166 return buf.String()
167 }
168
169 // Tree contains pre-computed hashes for trees that can be derived from the
170 // index. It helps speed up tree object generation from index for a new commit.
171 type Tree struct {
172 Entries []TreeEntry
173 }
174
175 // TreeEntry entry of a cached Tree
176 type TreeEntry struct {
177 // Path component (relative to its parent directory)
178 Path string
179 // Entries is the number of entries in the index that is covered by the tree
180 // this entry represents.
181 Entries int
182 // Trees is the number that represents the number of subtrees this tree has
183 Trees int
184 // Hash object name for the object that would result from writing this span
185 // of index as a tree.
186 Hash plumbing.Hash
187 }
188
189 // ResolveUndo is used when a conflict is resolved (e.g. with "git add path"),
190 // these higher stage entries are removed and a stage-0 entry with proper
191 // resolution is added. When these higher stage entries are removed, they are
192 // saved in the resolve undo extension.
193 type ResolveUndo struct {
194 Entries []ResolveUndoEntry
195 }
196
197 // ResolveUndoEntry contains the information about a conflict when is resolved
198 type ResolveUndoEntry struct {
199 Path string
200 Stages map[Stage]plumbing.Hash
201 }
202
203 // EndOfIndexEntry is the End of Index Entry (EOIE) is used to locate the end of
204 // the variable length index entries and the beginning of the extensions. Code
205 // can take advantage of this to quickly locate the index extensions without
206 // having to parse through all of the index entries.
207 //
208 // Because it must be able to be loaded before the variable length cache
209 // entries and other index extensions, this extension must be written last.
210 type EndOfIndexEntry struct {
211 // Offset to the end of the index entries
212 Offset uint32
213 // Hash is a SHA-1 over the extension types and their sizes (but not
214 // their contents).
215 Hash plumbing.Hash
216 }
217
218 // SkipUnless applies patterns in the form of A, A/B, A/B/C
219 // to the index to prevent the files from being checked out
220 func (i *Index) SkipUnless(patterns []string) {
221 for _, e := range i.Entries {
222 var include bool
223 for _, pattern := range patterns {
224 if strings.HasPrefix(e.Name, pattern) {
225 include = true
226 break
227 }
228 }
229 if !include {
230 e.SkipWorktree = true
231 }
232 }
233 }