| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/errors" |
| 8 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 9 | "github.com/git-lfs/git-lfs/v3/tr" |
| 10 | "github.com/rubyist/tracerx" |
| 11 | "github.com/spf13/cobra" |
| 12 | ) |
| 13 | |
| 14 | // clean cleans an object read from the given `io.Reader`, "from", and writes |
| 15 | // out a corresponding pointer to the `io.Writer`, "to". If there were any |
| 16 | // errors encountered along the way, they will be returned immediately if the |
| 17 | // error is non-fatal, otherwise they will halt using the built in |
| 18 | // `commands.Panic`. |
| 19 | // |
| 20 | // If the total size of the object is unknown until it is read, fileSize |
| 21 | // should be a negative integer. |
| 22 | // |
| 23 | // If the object read from "from" is _already_ a clean pointer, then it will be |
| 24 | // written out verbatim to "to", without trying to make it a pointer again. |
| 25 | func clean(gf *lfs.GitFilter, to io.Writer, from io.Reader, fileName string, fileSize int64) (*lfs.Pointer, error) { |
| 26 | cb, file, err := gf.CopyCallbackFile("clean", fileName, 1, 1) |
| 27 | if file != nil { |
| 28 | defer file.Close() |
| 29 | } |
| 30 | if err != nil { |
| 31 | Error(err.Error()) |
| 32 | } |
| 33 | |
| 34 | cleaned, err := gf.Clean(from, fileName, fileSize, cb) |
| 35 | |
| 36 | if cleaned != nil { |
| 37 | defer cleaned.Teardown() |
| 38 | } |
| 39 | |
| 40 | if errors.IsCleanPointerError(err) { |
| 41 | // If the contents read from the working directory was _already_ |
| 42 | // a pointer, we'll get a `CleanPointerError`, with the context |
| 43 | // containing the bytes that we should write back out to Git. |
| 44 | |
| 45 | _, err = to.Write(errors.GetContext(err, "bytes").([]byte)) |
| 46 | return nil, err |
| 47 | } |
| 48 | |
| 49 | if err != nil { |
| 50 | ExitWithError(errors.Wrap(err, tr.Tr.Get("Error cleaning Git LFS object"))) |
| 51 | } |
| 52 | |
| 53 | tmpfile := cleaned.Filename |
| 54 | mediafile, err := gf.ObjectPath(cleaned.Oid) |
| 55 | if err != nil { |
| 56 | Panic(err, tr.Tr.Get("Unable to get local media path.")) |
| 57 | } |
| 58 | |
| 59 | if stat, _ := os.Stat(mediafile); stat != nil { |
| 60 | if stat.Size() != cleaned.Size && len(cleaned.Pointer.Extensions) == 0 { |
| 61 | Exit("%s\n%s\n%s", tr.Tr.Get("Files don't match:"), mediafile, tmpfile) |
| 62 | } |
| 63 | tracerx.Printf("%s exists", mediafile) |
| 64 | } else { |
| 65 | if err := os.Rename(tmpfile, mediafile); err != nil { |
| 66 | Panic(err, tr.Tr.Get("Unable to move %s to %s", tmpfile, mediafile)) |
| 67 | } |
| 68 | |
| 69 | tracerx.Printf("Writing %s", mediafile) |
| 70 | } |
| 71 | |
| 72 | _, err = lfs.EncodePointer(to, cleaned.Pointer) |
| 73 | return cleaned.Pointer, err |
| 74 | } |
| 75 | |
| 76 | func cleanCommand(cmd *cobra.Command, args []string) { |
| 77 | requireStdin(tr.Tr.Get("This command should be run by the Git 'clean' filter")) |
| 78 | setupRepository() |
| 79 | installHooks(false) |
| 80 | |
| 81 | var fileName string |
| 82 | if len(args) > 0 { |
| 83 | fileName = args[0] |
| 84 | } |
| 85 | |
| 86 | gitfilter := lfs.NewGitFilter(cfg) |
| 87 | ptr, err := clean(gitfilter, os.Stdout, os.Stdin, fileName, -1) |
| 88 | if err != nil { |
| 89 | Error(err.Error()) |
| 90 | } |
| 91 | |
| 92 | if ptr != nil && possiblyMalformedObjectSize(ptr.Size) { |
| 93 | Error(tr.Tr.Get("Possibly malformed conversion on Windows, see `git lfs help smudge` for more details.")) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func init() { |
| 98 | RegisterCommand("clean", cleanCommand, nil) |
| 99 | } |