| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | |
| 8 | "github.com/git-lfs/git-lfs/v3/errors" |
| 9 | "github.com/git-lfs/git-lfs/v3/filepathfilter" |
| 10 | "github.com/git-lfs/git-lfs/v3/git" |
| 11 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 12 | "github.com/git-lfs/git-lfs/v3/tools" |
| 13 | "github.com/git-lfs/git-lfs/v3/tools/humanize" |
| 14 | "github.com/git-lfs/git-lfs/v3/tq" |
| 15 | "github.com/git-lfs/git-lfs/v3/tr" |
| 16 | "github.com/spf13/cobra" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | // smudgeSkip is a command-line flag belonging to the "git-lfs smudge" |
| 21 | // command specifying whether to skip the smudge process. |
| 22 | smudgeSkip = false |
| 23 | ) |
| 24 | |
| 25 | // delayedSmudge performs a 'delayed' smudge, adding the LFS pointer to the |
| 26 | // `*tq.TransferQueue` "q" if the file is not present locally, passes the given |
| 27 | // filepathfilter, and is not skipped. If the pointer is malformed, or already |
| 28 | // exists, it streams the contents to be written into the working copy to "to". |
| 29 | // |
| 30 | // delayedSmudge returns the number of bytes written, whether the checkout was |
| 31 | // delayed, the *lfs.Pointer that was smudged, and an error, if one occurred. |
| 32 | func delayedSmudge(gf *lfs.GitFilter, s *git.FilterProcessScanner, to io.Writer, from io.Reader, q *tq.TransferQueue, filename string, skip bool, filter *filepathfilter.Filter) (int64, bool, *lfs.Pointer, error) { |
| 33 | ptr, pbuf, perr := lfs.DecodeFrom(from) |
| 34 | if perr != nil { |
| 35 | // Write 'statusFromErr(nil)', even though 'perr != nil', since |
| 36 | // we are about to write non-delayed smudged contents to "to". |
| 37 | if err := s.WriteStatus(statusFromErr(nil)); err != nil { |
| 38 | return 0, false, nil, err |
| 39 | } |
| 40 | |
| 41 | n, err := tools.Spool(to, pbuf, cfg.TempDir()) |
| 42 | if err != nil { |
| 43 | return n, false, nil, errors.Wrap(err, perr.Error()) |
| 44 | } |
| 45 | |
| 46 | if n != 0 { |
| 47 | return 0, false, nil, errors.NewNotAPointerError(errors.New( |
| 48 | tr.Tr.Get("Unable to parse pointer at: %q", filename), |
| 49 | )) |
| 50 | } |
| 51 | return 0, false, nil, nil |
| 52 | } |
| 53 | |
| 54 | lfs.LinkOrCopyFromReference(cfg, ptr.Oid, ptr.Size) |
| 55 | |
| 56 | path, err := cfg.Filesystem().ObjectPath(ptr.Oid) |
| 57 | if err != nil { |
| 58 | return 0, false, nil, err |
| 59 | } |
| 60 | |
| 61 | if !skip && filter.Allows(filename) { |
| 62 | if _, statErr := os.Stat(path); statErr != nil && ptr.Size != 0 { |
| 63 | q.Add(filename, path, ptr.Oid, ptr.Size, false, nil) |
| 64 | return 0, true, ptr, nil |
| 65 | } |
| 66 | |
| 67 | // Write 'statusFromErr(nil)', since the object is already |
| 68 | // present in the local cache, we will write the object's |
| 69 | // contents without delaying. |
| 70 | if err := s.WriteStatus(statusFromErr(nil)); err != nil { |
| 71 | return 0, false, nil, err |
| 72 | } |
| 73 | |
| 74 | n, err := gf.Smudge(to, ptr, filename, false, nil, nil) |
| 75 | return n, false, ptr, err |
| 76 | } |
| 77 | |
| 78 | if err := s.WriteStatus(statusFromErr(nil)); err != nil { |
| 79 | return 0, false, nil, err |
| 80 | } |
| 81 | |
| 82 | n, err := ptr.Encode(to) |
| 83 | return int64(n), false, ptr, err |
| 84 | } |
| 85 | |
| 86 | // smudge smudges the given `*lfs.Pointer`, "ptr", and writes its objects |
| 87 | // contents to the `io.Writer`, "to". |
| 88 | // |
| 89 | // If the encoded LFS pointer is not parse-able as a pointer, the contents of |
| 90 | // that file will instead be spooled to a temporary location on disk and then |
| 91 | // copied out back to Git. If the pointer file is empty, an empty file will be |
| 92 | // written with no error. |
| 93 | // |
| 94 | // If the smudged object did not "pass" the include and exclude filterset, it |
| 95 | // will not be downloaded, and the object will remain a pointer on disk, as if |
| 96 | // the smudge filter had not been applied at all. |
| 97 | // |
| 98 | // Any errors encountered along the way will be returned immediately if they |
| 99 | // were non-fatal, otherwise execution will halt and the process will be |
| 100 | // terminated by using the `commands.Panic()` func. |
| 101 | func smudge(gf *lfs.GitFilter, to io.Writer, from io.Reader, filename string, skip bool, filter *filepathfilter.Filter) (int64, error) { |
| 102 | ptr, pbuf, perr := lfs.DecodeFrom(from) |
| 103 | if perr != nil { |
| 104 | n, err := tools.Spool(to, pbuf, cfg.TempDir()) |
| 105 | if err != nil { |
| 106 | return 0, errors.Wrap(err, perr.Error()) |
| 107 | } |
| 108 | |
| 109 | if n != 0 { |
| 110 | return 0, errors.NewNotAPointerError(errors.New( |
| 111 | tr.Tr.Get("Unable to parse pointer at: %q", filename), |
| 112 | )) |
| 113 | } |
| 114 | return 0, nil |
| 115 | } |
| 116 | |
| 117 | lfs.LinkOrCopyFromReference(cfg, ptr.Oid, ptr.Size) |
| 118 | cb, file, err := gf.CopyCallbackFile("download", filename, 1, 1) |
| 119 | if err != nil { |
| 120 | return 0, err |
| 121 | } |
| 122 | |
| 123 | if skip || !filter.Allows(filename) { |
| 124 | n, err := ptr.Encode(to) |
| 125 | return int64(n), err |
| 126 | } |
| 127 | |
| 128 | n, err := gf.Smudge(to, ptr, filename, true, getTransferManifestOperationRemote("download", cfg.Remote()), cb) |
| 129 | if file != nil { |
| 130 | file.Close() |
| 131 | } |
| 132 | |
| 133 | if err != nil { |
| 134 | ptr.Encode(to) |
| 135 | var oid string = ptr.Oid |
| 136 | if len(oid) >= 7 { |
| 137 | oid = oid[:7] |
| 138 | } |
| 139 | |
| 140 | LoggedError(err, tr.Tr.Get("Error downloading object: %s (%s): %s", filename, oid, err)) |
| 141 | if !cfg.SkipDownloadErrors() { |
| 142 | ExitWithCode(2) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return n, nil |
| 147 | } |
| 148 | |
| 149 | func smudgeCommand(cmd *cobra.Command, args []string) { |
| 150 | requireStdin(tr.Tr.Get("This command should be run by the Git 'smudge' filter")) |
| 151 | setupRepository() |
| 152 | installHooks(false) |
| 153 | |
| 154 | if !smudgeSkip && cfg.Os.Bool("GIT_LFS_SKIP_SMUDGE", false) { |
| 155 | smudgeSkip = true |
| 156 | } |
| 157 | filter := filepathfilter.New(cfg.FetchIncludePaths(), cfg.FetchExcludePaths(), filepathfilter.GitIgnore, cfg.Git) |
| 158 | gitfilter := lfs.NewGitFilter(cfg) |
| 159 | |
| 160 | if n, err := smudge(gitfilter, os.Stdout, os.Stdin, smudgeFilename(args), smudgeSkip, filter); err != nil { |
| 161 | if errors.IsNotAPointerError(err) { |
| 162 | fmt.Fprintln(os.Stderr, err.Error()) |
| 163 | } else { |
| 164 | Error(err.Error()) |
| 165 | } |
| 166 | } else if possiblyMalformedObjectSize(n) { |
| 167 | fmt.Fprintln(os.Stderr, tr.Tr.Get("Possibly malformed smudge on Windows: see `git lfs help smudge` for more info.")) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func smudgeFilename(args []string) string { |
| 172 | if len(args) > 0 { |
| 173 | return args[0] |
| 174 | } |
| 175 | return fmt.Sprintf("<%s>", tr.Tr.Get("unknown file")) |
| 176 | } |
| 177 | |
| 178 | func possiblyMalformedObjectSize(n int64) bool { |
| 179 | return n >= 4*humanize.Gibibyte |
| 180 | } |
| 181 | |
| 182 | func init() { |
| 183 | RegisterCommand("smudge", smudgeCommand, func(cmd *cobra.Command) { |
| 184 | cmd.Flags().BoolVarP(&smudgeSkip, "skip", "s", false, "") |
| 185 | }) |
| 186 | } |