2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2014-05-27 16:27:54 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-13 19:43:41 +00:00
|
|
|
"os"
|
|
|
|
|
2015-03-19 19:30:55 +00:00
|
|
|
"github.com/github/git-lfs/lfs"
|
2015-05-25 18:20:50 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
|
2014-05-27 16:27:54 +00:00
|
|
|
)
|
|
|
|
|
2014-06-26 20:12:25 +00:00
|
|
|
var (
|
|
|
|
cleanCmd = &cobra.Command{
|
2015-09-17 22:08:28 +00:00
|
|
|
Use: "clean",
|
|
|
|
Run: cleanCommand,
|
2014-06-26 20:12:25 +00:00
|
|
|
}
|
|
|
|
)
|
2014-05-27 16:27:54 +00:00
|
|
|
|
2014-06-26 20:12:25 +00:00
|
|
|
func cleanCommand(cmd *cobra.Command, args []string) {
|
2015-04-24 20:24:32 +00:00
|
|
|
requireStdin("This command should be run by the Git 'clean' filter")
|
2015-03-19 19:30:55 +00:00
|
|
|
lfs.InstallHooks(false)
|
2014-06-04 19:03:47 +00:00
|
|
|
|
2015-07-21 23:53:31 +00:00
|
|
|
var fileName string
|
2015-03-19 19:30:55 +00:00
|
|
|
var cb lfs.CopyCallback
|
2014-08-07 17:01:06 +00:00
|
|
|
var file *os.File
|
|
|
|
var fileSize int64
|
2014-06-26 20:12:25 +00:00
|
|
|
if len(args) > 0 {
|
2015-07-21 23:53:31 +00:00
|
|
|
fileName = args[0]
|
2014-08-07 17:01:06 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 23:53:31 +00:00
|
|
|
cleaned, err := lfs.PointerClean(os.Stdin, fileName, fileSize, cb)
|
2014-08-07 17:01:06 +00:00
|
|
|
if file != nil {
|
|
|
|
file.Close()
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 17:41:32 +00:00
|
|
|
if cleaned != nil {
|
|
|
|
defer cleaned.Teardown()
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:08:57 +00:00
|
|
|
if lfs.IsCleanPointerError(err) {
|
|
|
|
os.Stdout.Write(lfs.ErrorGetContext(err, "bytes").([]byte))
|
2015-04-28 21:37:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-27 16:27:54 +00:00
|
|
|
if err != nil {
|
2014-08-07 21:32:45 +00:00
|
|
|
Panic(err, "Error cleaning asset.")
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 15:36:10 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2014-05-27 16:27:54 +00:00
|
|
|
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 {
|
2014-06-05 18:19:30 +00:00
|
|
|
Exit("Files don't match:\n%s\n%s", mediafile, tmpfile)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
2014-06-05 18:19:30 +00:00
|
|
|
Debug("%s exists", mediafile)
|
2014-05-27 16:27:54 +00:00
|
|
|
} 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)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 18:19:30 +00:00
|
|
|
Debug("Writing %s", mediafile)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 16:20:36 +00:00
|
|
|
lfs.EncodePointer(os.Stdout, cleaned.Pointer)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-06-26 20:12:25 +00:00
|
|
|
RootCmd.AddCommand(cleanCmd)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|