git-lfs/commands/command_clean.go

103 lines
2.5 KiB
Go
Raw Normal View History

package commands
import (
2016-11-09 00:50:52 +00:00
"io"
2015-05-13 19:43:41 +00:00
"os"
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/lfs"
"github.com/git-lfs/git-lfs/progress"
"github.com/spf13/cobra"
)
2016-11-09 00:57:11 +00:00
// clean cleans an object read from the given `io.Reader`, "from", and writes
// out a corresponding pointer to the `io.Writer`, "to". If there were any
// errors encountered along the way, they will be returned immediately if the
// error is non-fatal, otherwise they will halt using the built in
// `commands.Panic`.
//
// If the object read from "from" is _already_ a clean pointer, then it will be
// written out verbatim to "to", without trying to make it a pointer again.
func clean(to io.Writer, from io.Reader, fileName string) error {
var cb progress.CopyCallback
2014-08-07 17:01:06 +00:00
var file *os.File
var fileSize int64
2016-11-09 00:50:52 +00:00
if len(fileName) > 0 {
2015-07-21 23:53:31 +00:00
stat, err := os.Stat(fileName)
2014-08-07 17:01:06 +00:00
if err == nil && stat != nil {
fileSize = stat.Size()
2015-07-21 23:53:31 +00:00
localCb, localFile, err := lfs.CopyCallbackFile("clean", fileName, 1, 1)
2014-08-07 17:01:06 +00:00
if err != nil {
Error(err.Error())
} else {
cb = localCb
file = localFile
}
}
}
2016-11-09 00:50:52 +00:00
cleaned, err := lfs.PointerClean(from, fileName, fileSize, cb)
2014-08-07 17:01:06 +00:00
if file != nil {
file.Close()
}
if cleaned != nil {
defer cleaned.Teardown()
}
2016-08-18 20:20:33 +00:00
if errors.IsCleanPointerError(err) {
2016-11-09 00:50:52 +00:00
// If the contents read from the working directory was _already_
// a pointer, we'll get a `CleanPointerError`, with the context
// containing the bytes that we should write back out to Git.
_, err = to.Write(errors.GetContext(err, "bytes").([]byte))
return err
}
if err != nil {
2014-08-07 21:32:45 +00:00
Panic(err, "Error cleaning asset.")
}
tmpfile := cleaned.Filename
2015-03-19 19:30:55 +00:00
mediafile, err := lfs.LocalMediaPath(cleaned.Oid)
2014-06-05 19:05:02 +00:00
if err != nil {
Panic(err, "Unable to get local media path.")
}
if stat, _ := os.Stat(mediafile); stat != nil {
2015-07-21 23:53:31 +00:00
if stat.Size() != cleaned.Size && len(cleaned.Pointer.Extensions) == 0 {
Exit("Files don't match:\n%s\n%s", mediafile, tmpfile)
}
Debug("%s exists", mediafile)
} else {
if err := os.Rename(tmpfile, mediafile); err != nil {
2014-06-05 18:48:23 +00:00
Panic(err, "Unable to move %s to %s\n", tmpfile, mediafile)
}
Debug("Writing %s", mediafile)
}
2016-11-09 00:50:52 +00:00
_, err = lfs.EncodePointer(to, cleaned.Pointer)
return err
}
func cleanCommand(cmd *cobra.Command, args []string) {
requireStdin("This command should be run by the Git 'clean' filter")
lfs.InstallHooks(false)
var fileName string
if len(args) > 0 {
fileName = args[0]
}
if err := clean(os.Stdout, os.Stdin, fileName); err != nil {
2016-11-09 00:50:52 +00:00
Error(err.Error())
}
}
func init() {
RegisterCommand("clean", cleanCommand, nil)
}