| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/git" |
| 11 | "github.com/git-lfs/git-lfs/v3/tr" |
| 12 | ) |
| 13 | |
| 14 | // Status represents the status of a file that appears in the output of `git |
| 15 | // diff-index`. |
| 16 | // |
| 17 | // More information about each of its valid instances can be found: |
| 18 | // https://git-scm.com/docs/git-diff-index |
| 19 | type DiffIndexStatus rune |
| 20 | |
| 21 | const ( |
| 22 | StatusAddition DiffIndexStatus = 'A' |
| 23 | StatusCopy DiffIndexStatus = 'C' |
| 24 | StatusDeletion DiffIndexStatus = 'D' |
| 25 | StatusModification DiffIndexStatus = 'M' |
| 26 | StatusRename DiffIndexStatus = 'R' |
| 27 | StatusTypeChange DiffIndexStatus = 'T' |
| 28 | StatusUnmerged DiffIndexStatus = 'U' |
| 29 | StatusUnknown DiffIndexStatus = 'X' |
| 30 | ) |
| 31 | |
| 32 | // String implements fmt.Stringer by returning a human-readable name for each |
| 33 | // status. |
| 34 | func (s DiffIndexStatus) String() string { |
| 35 | switch s { |
| 36 | case StatusAddition: |
| 37 | return "addition" |
| 38 | case StatusCopy: |
| 39 | return "copy" |
| 40 | case StatusDeletion: |
| 41 | return "deletion" |
| 42 | case StatusModification: |
| 43 | return "modification" |
| 44 | case StatusRename: |
| 45 | return "rename" |
| 46 | case StatusTypeChange: |
| 47 | return "change" |
| 48 | case StatusUnmerged: |
| 49 | return "unmerged" |
| 50 | case StatusUnknown: |
| 51 | return "unknown" |
| 52 | } |
| 53 | return "<unknown>" |
| 54 | } |
| 55 | |
| 56 | // Format implements fmt.Formatter. If printed as "%+d", "%+s", or "%+v", the |
| 57 | // status will be written out as an English word: i.e., "addition", "copy", |
| 58 | // "deletion", etc. |
| 59 | // |
| 60 | // If the '+' flag is not given, the shorthand will be used instead: 'A', 'C', |
| 61 | // and 'D', respectively. |
| 62 | // |
| 63 | // If any other format verb is given, this function will panic(). |
| 64 | func (s DiffIndexStatus) Format(state fmt.State, c rune) { |
| 65 | switch c { |
| 66 | case 'd', 's', 'v': |
| 67 | if state.Flag('+') { |
| 68 | state.Write([]byte(s.String())) |
| 69 | } else { |
| 70 | state.Write([]byte{byte(rune(s))}) |
| 71 | } |
| 72 | default: |
| 73 | panic(tr.Tr.Get("cannot format %v for DiffIndexStatus", c)) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // DiffIndexEntry holds information about a single item in the results of a `git |
| 78 | // diff-index` command. |
| 79 | type DiffIndexEntry struct { |
| 80 | // SrcMode is the file mode of the "src" file, stored as a string-based |
| 81 | // octal. |
| 82 | SrcMode string |
| 83 | // DstMode is the file mode of the "dst" file, stored as a string-based |
| 84 | // octal. |
| 85 | DstMode string |
| 86 | // SrcSha is the Git blob ID of the "src" file. |
| 87 | SrcSha string |
| 88 | // DstSha is the Git blob ID of the "dst" file. |
| 89 | DstSha string |
| 90 | // Status is the status of the file in the index. |
| 91 | Status DiffIndexStatus |
| 92 | // StatusScore is the optional "score" associated with a particular |
| 93 | // status. |
| 94 | StatusScore int |
| 95 | // SrcName is the name of the file in its "src" state as it appears in |
| 96 | // the index. |
| 97 | SrcName string |
| 98 | // DstName is the name of the file in its "dst" state as it appears in |
| 99 | // the index. |
| 100 | DstName string |
| 101 | } |
| 102 | |
| 103 | // DiffIndexScanner scans the output of the `git diff-index` command. |
| 104 | type DiffIndexScanner struct { |
| 105 | // next is the next entry scanned by the Scanner. |
| 106 | next *DiffIndexEntry |
| 107 | // err is any error that the Scanner encountered while scanning. |
| 108 | err error |
| 109 | |
| 110 | // from is the underlying scanner, scanning the `git diff-index` |
| 111 | // command's stdout. |
| 112 | from *bufio.Scanner |
| 113 | } |
| 114 | |
| 115 | // NewDiffIndexScanner initializes a new `DiffIndexScanner` scanning at the |
| 116 | // given ref, "ref". |
| 117 | // |
| 118 | // If "cache" is given, the DiffIndexScanner will scan for differences between |
| 119 | // the given ref and the index. If "cache" is _not_ given, DiffIndexScanner will |
| 120 | // scan for differences between the given ref and the currently checked out |
| 121 | // tree. |
| 122 | // |
| 123 | // If "refresh" is given, the DiffIndexScanner will refresh the index. This is |
| 124 | // probably what you want in all cases except fsck, where invoking a filtering |
| 125 | // operation would be undesirable due to the possibility of corruption. It can |
| 126 | // also be disabled where another operation will have refreshed the index. |
| 127 | // |
| 128 | // If "workingDir" is set, the DiffIndexScanner will be run in the given |
| 129 | // directory. Otherwise, the DiffIndexScanner will be run in the current |
| 130 | // working directory. |
| 131 | // |
| 132 | // If any error was encountered in starting the command or closing its `stdin`, |
| 133 | // that error will be returned immediately. Otherwise, a `*DiffIndexScanner` |
| 134 | // will be returned with a `nil` error. |
| 135 | func NewDiffIndexScanner(ref string, cached bool, refresh bool, workingDir string) (*DiffIndexScanner, error) { |
| 136 | scanner, err := git.DiffIndex(ref, cached, refresh, workingDir) |
| 137 | if err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | return &DiffIndexScanner{ |
| 141 | from: scanner, |
| 142 | }, nil |
| 143 | } |
| 144 | |
| 145 | // Scan advances the scan line and yields either a new value for Entry(), or an |
| 146 | // Err(). It returns true or false, whether or not it can continue scanning for |
| 147 | // more entries. |
| 148 | func (s *DiffIndexScanner) Scan() bool { |
| 149 | if !s.prepareScan() { |
| 150 | return false |
| 151 | } |
| 152 | |
| 153 | s.next, s.err = s.scan(s.from.Text()) |
| 154 | if s.err != nil { |
| 155 | s.err = errors.Wrap(s.err, tr.Tr.Get("`git diff-index` scan")) |
| 156 | } |
| 157 | |
| 158 | return s.err == nil |
| 159 | } |
| 160 | |
| 161 | // Entry returns the last entry that was Scan()'d by the DiffIndexScanner. |
| 162 | func (s *DiffIndexScanner) Entry() *DiffIndexEntry { return s.next } |
| 163 | |
| 164 | // Entry returns the last error that was encountered by the DiffIndexScanner. |
| 165 | func (s *DiffIndexScanner) Err() error { return s.err } |
| 166 | |
| 167 | // prepareScan clears out the results from the last Scan() loop, and advances |
| 168 | // the internal scanner to fetch a new line of Text(). |
| 169 | func (s *DiffIndexScanner) prepareScan() bool { |
| 170 | s.next, s.err = nil, nil |
| 171 | if !s.from.Scan() { |
| 172 | s.err = s.from.Err() |
| 173 | return false |
| 174 | } |
| 175 | |
| 176 | return true |
| 177 | } |
| 178 | |
| 179 | // scan parses the given line and returns a `*DiffIndexEntry` or an error, |
| 180 | // depending on whether or not the parse was successful. |
| 181 | func (s *DiffIndexScanner) scan(line string) (*DiffIndexEntry, error) { |
| 182 | // Format is: |
| 183 | // :100644 100644 c5b3d83a7542255ec7856487baa5e83d65b1624c 9e82ac1b514be060945392291b5b3108c22f6fe3 M foo.gif |
| 184 | // :<old mode> <new mode> <old sha1> <new sha1> <status>\t<file name>[\t<file name>] |
| 185 | |
| 186 | parts := strings.Split(line, "\t") |
| 187 | if len(parts) < 2 { |
| 188 | return nil, errors.New(tr.Tr.Get("invalid line: %s", line)) |
| 189 | } |
| 190 | |
| 191 | desc := strings.Fields(parts[0]) |
| 192 | if len(desc) < 5 { |
| 193 | return nil, errors.New(tr.Tr.Get("invalid description: %s", parts[0])) |
| 194 | } |
| 195 | |
| 196 | entry := &DiffIndexEntry{ |
| 197 | SrcMode: strings.TrimPrefix(desc[0], ":"), |
| 198 | DstMode: desc[1], |
| 199 | SrcSha: desc[2], |
| 200 | DstSha: desc[3], |
| 201 | Status: DiffIndexStatus(rune(desc[4][0])), |
| 202 | SrcName: parts[1], |
| 203 | } |
| 204 | |
| 205 | if score, err := strconv.Atoi(desc[4][1:]); err != nil { |
| 206 | entry.StatusScore = score |
| 207 | } |
| 208 | |
| 209 | if len(parts) > 2 { |
| 210 | entry.DstName = parts[2] |
| 211 | } |
| 212 | |
| 213 | return entry, nil |
| 214 | } |