| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | |
| 12 | "github.com/git-lfs/git-lfs/v3/git" |
| 13 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 14 | "github.com/git-lfs/git-lfs/v3/tr" |
| 15 | "github.com/spf13/cobra" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | pointerFile string |
| 20 | pointerCompare string |
| 21 | pointerStdin bool |
| 22 | pointerCheck bool |
| 23 | pointerStrict bool |
| 24 | pointerNoStrict bool |
| 25 | pointerNoExtensions bool |
| 26 | ) |
| 27 | |
| 28 | func pointerCommand(cmd *cobra.Command, args []string) { |
| 29 | var comparePointer *lfs.Pointer |
| 30 | comparing := false |
| 31 | something := false |
| 32 | hasExtensionsConfig := false |
| 33 | buildOid := "" |
| 34 | compareOid := "" |
| 35 | |
| 36 | if pointerCheck { |
| 37 | var r io.ReadCloser |
| 38 | var err error |
| 39 | |
| 40 | if pointerStrict && pointerNoStrict { |
| 41 | ExitWithError(errors.New(tr.Tr.Get("Cannot combine --strict with --no-strict"))) |
| 42 | } |
| 43 | |
| 44 | if len(pointerCompare) > 0 { |
| 45 | ExitWithError(errors.New(tr.Tr.Get("Cannot combine --check with --pointer"))) |
| 46 | } |
| 47 | |
| 48 | if len(pointerFile) > 0 { |
| 49 | if pointerStdin { |
| 50 | ExitWithError(errors.New(tr.Tr.Get("With --check, --file cannot be combined with --stdin"))) |
| 51 | } |
| 52 | r, err = os.Open(pointerFile) |
| 53 | if err != nil { |
| 54 | ExitWithError(err) |
| 55 | } |
| 56 | } else if pointerStdin { |
| 57 | r = io.NopCloser(os.Stdin) |
| 58 | } else { |
| 59 | ExitWithError(errors.New(tr.Tr.Get("Must specify either --file or --stdin with --check"))) |
| 60 | } |
| 61 | |
| 62 | p, err := lfs.DecodePointer(r) |
| 63 | if err != nil { |
| 64 | ExitWithCode(1) |
| 65 | } |
| 66 | if pointerStrict && !p.Canonical { |
| 67 | ExitWithCode(2) |
| 68 | } |
| 69 | r.Close() |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | if len(pointerCompare) > 0 || pointerStdin { |
| 74 | comparing = true |
| 75 | } |
| 76 | |
| 77 | if len(pointerFile) > 0 { |
| 78 | something = true |
| 79 | buildFile, err := os.Open(pointerFile) |
| 80 | if err != nil { |
| 81 | Error(err.Error()) |
| 82 | ExitWithCode(1) |
| 83 | } |
| 84 | |
| 85 | var ptr *lfs.Pointer |
| 86 | if pointerNoExtensions || !cfg.InRepo() { |
| 87 | oidHash := sha256.New() |
| 88 | size, err := io.Copy(oidHash, buildFile) |
| 89 | if err != nil { |
| 90 | Error(err.Error()) |
| 91 | buildFile.Close() |
| 92 | ExitWithCode(1) |
| 93 | } |
| 94 | |
| 95 | ptr = lfs.NewPointer(hex.EncodeToString(oidHash.Sum(nil)), size, nil) |
| 96 | } else { |
| 97 | gitfilter := lfs.NewGitFilter(cfg) |
| 98 | hasExtensionsConfig = len(cfg.Extensions()) > 0 |
| 99 | cleaned, err := gitfilter.Clean(buildFile, pointerFile, -1, nil) |
| 100 | if err != nil { |
| 101 | Error(err.Error()) |
| 102 | buildFile.Close() |
| 103 | ExitWithCode(1) |
| 104 | } |
| 105 | |
| 106 | ptr = cleaned.Pointer |
| 107 | } |
| 108 | buildFile.Close() |
| 109 | |
| 110 | fmt.Fprint(os.Stderr, tr.Tr.Get("Git LFS pointer for %s", pointerFile), "\n") |
| 111 | if !pointerNoExtensions && hasExtensionsConfig { |
| 112 | fmt.Fprint(os.Stderr, tr.Tr.Get("warning: Using LFS extensions, use --no-extensions for a plain pointer."), "\n") |
| 113 | } |
| 114 | fmt.Fprint(os.Stderr, "\n") |
| 115 | |
| 116 | buf := &bytes.Buffer{} |
| 117 | lfs.EncodePointer(io.MultiWriter(os.Stdout, buf), ptr) |
| 118 | |
| 119 | if comparing { |
| 120 | buildOid, err = git.HashObject(bytes.NewReader(buf.Bytes())) |
| 121 | if err != nil { |
| 122 | Error(err.Error()) |
| 123 | ExitWithCode(1) |
| 124 | } |
| 125 | fmt.Fprint(os.Stderr, "\n", tr.Tr.Get("Git blob OID: %s", buildOid), "\n\n") |
| 126 | } |
| 127 | } else { |
| 128 | comparing = false |
| 129 | } |
| 130 | |
| 131 | if len(pointerCompare) > 0 || pointerStdin { |
| 132 | something = true |
| 133 | compFile, err := pointerReader() |
| 134 | if err != nil { |
| 135 | Error(err.Error()) |
| 136 | ExitWithCode(1) |
| 137 | } |
| 138 | |
| 139 | buf := &bytes.Buffer{} |
| 140 | tee := io.TeeReader(compFile, buf) |
| 141 | comparePointer, err = lfs.DecodePointer(tee) |
| 142 | compFile.Close() |
| 143 | |
| 144 | pointerName := "STDIN" |
| 145 | if !pointerStdin { |
| 146 | pointerName = pointerCompare |
| 147 | } |
| 148 | fmt.Fprint(os.Stderr, tr.Tr.Get("Pointer from %s", pointerName), "\n\n") |
| 149 | |
| 150 | if err != nil { |
| 151 | Error(err.Error()) |
| 152 | ExitWithCode(1) |
| 153 | } |
| 154 | |
| 155 | fmt.Fprint(os.Stderr, buf.String()) |
| 156 | if comparing { |
| 157 | compareOid, err = git.HashObject(bytes.NewReader(buf.Bytes())) |
| 158 | if err != nil { |
| 159 | Error(err.Error()) |
| 160 | ExitWithCode(1) |
| 161 | } |
| 162 | fmt.Fprint(os.Stderr, "\n", tr.Tr.Get("Git blob OID: %s", compareOid), "\n") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if comparing && buildOid != compareOid { |
| 167 | fmt.Fprint(os.Stderr, "\n", tr.Tr.Get("Pointers do not match"), "\n") |
| 168 | if hasExtensionsConfig || len(comparePointer.Extensions) > 0 { |
| 169 | fmt.Fprint(os.Stderr, tr.Tr.Get("note: Mismatch may be due to differing LFS extensions."), "\n") |
| 170 | } |
| 171 | ExitWithCode(1) |
| 172 | } |
| 173 | |
| 174 | if !something { |
| 175 | Error(tr.Tr.Get("Nothing to do!")) |
| 176 | ExitWithCode(1) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func pointerReader() (io.ReadCloser, error) { |
| 181 | if len(pointerCompare) > 0 { |
| 182 | if pointerStdin { |
| 183 | return nil, errors.New(tr.Tr.Get("cannot read from STDIN and --pointer")) |
| 184 | } |
| 185 | |
| 186 | return os.Open(pointerCompare) |
| 187 | } |
| 188 | |
| 189 | requireStdin(tr.Tr.Get("The --stdin flag expects a pointer file from STDIN.")) |
| 190 | |
| 191 | return os.Stdin, nil |
| 192 | } |
| 193 | |
| 194 | func init() { |
| 195 | RegisterCommand("pointer", pointerCommand, func(cmd *cobra.Command) { |
| 196 | cmd.Flags().StringVarP(&pointerFile, "file", "f", "", "Path to a local file to generate the pointer from.") |
| 197 | cmd.Flags().StringVarP(&pointerCompare, "pointer", "p", "", "Path to a local file containing a pointer built by another Git LFS implementation.") |
| 198 | cmd.Flags().BoolVarP(&pointerStdin, "stdin", "", false, "Read a pointer built by another Git LFS implementation through STDIN.") |
| 199 | cmd.Flags().BoolVarP(&pointerCheck, "check", "", false, "Check whether the given file is a Git LFS pointer.") |
| 200 | cmd.Flags().BoolVarP(&pointerStrict, "strict", "", false, "Check whether the given Git LFS pointer is canonical.") |
| 201 | cmd.Flags().BoolVarP(&pointerNoStrict, "no-strict", "", false, "Don't check whether the given Git LFS pointer is canonical.") |
| 202 | cmd.Flags().BoolVarP(&pointerNoExtensions, "no-extensions", "", false, "Don't print the extensions of the pointer.") |
| 203 | }) |
| 204 | } |