git-lfs/commands/command_smudge.go

99 lines
2.2 KiB
Go
Raw Normal View History

package commands
import (
"bytes"
"io"
"os"
"path/filepath"
2015-05-13 19:43:41 +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-06-26 20:55:53 +00:00
var (
2014-07-25 17:16:57 +00:00
smudgeInfo = false
smudgeSkip = false
2014-07-25 17:16:57 +00:00
smudgeCmd = &cobra.Command{
2015-09-17 22:08:28 +00:00
Use: "smudge",
Run: smudgeCommand,
2014-06-26 20:55:53 +00:00
}
)
2014-06-26 20:55:53 +00:00
func smudgeCommand(cmd *cobra.Command, args []string) {
requireStdin("This command should be run by the Git 'smudge' filter")
2015-03-19 19:30:55 +00:00
lfs.InstallHooks(false)
2014-06-04 19:03:47 +00:00
if smudgeSkip || lfs.Config.GetenvBool("GIT_LFS_SKIP_SMUDGE", false) {
_, err := io.Copy(os.Stdout, os.Stdin)
if err != nil {
Panic(err, "Error writing data to stdout:")
}
return
}
// keeps the initial buffer from lfs.DecodePointer
b := &bytes.Buffer{}
r := io.TeeReader(os.Stdin, b)
ptr, err := lfs.DecodePointer(r)
if err != nil {
mr := io.MultiReader(b, os.Stdin)
2014-09-05 16:55:13 +00:00
_, err := io.Copy(os.Stdout, mr)
if err != nil {
Panic(err, "Error writing data to stdout:")
}
return
}
2014-07-25 17:16:57 +00:00
if smudgeInfo {
2015-03-19 19:30:55 +00:00
localPath, err := lfs.LocalMediaPath(ptr.Oid)
2014-07-25 17:16:57 +00:00
if err != nil {
Exit(err.Error())
}
2014-07-25 17:26:01 +00:00
stat, err := os.Stat(localPath)
2014-07-25 17:16:57 +00:00
if err != nil {
Print("%d --", ptr.Size)
2014-07-25 19:16:25 +00:00
} else {
Print("%d %s", stat.Size(), localPath)
2014-07-25 17:16:57 +00:00
}
return
}
2014-08-07 14:53:13 +00:00
filename := smudgeFilename(args, err)
2015-03-19 19:30:55 +00:00
cb, file, err := lfs.CopyCallbackFile("smudge", filename, 1, 1)
if err != nil {
Error(err.Error())
2014-08-07 14:53:13 +00:00
}
cfg := lfs.Config
download := lfs.FilenamePassesIncludeExcludeFilter(filename, cfg.FetchIncludePaths(), cfg.FetchExcludePaths())
err = ptr.Smudge(os.Stdout, filename, download, cb)
2014-08-07 14:53:13 +00:00
if file != nil {
file.Close()
}
if err != nil {
ptr.Encode(os.Stdout)
2014-08-07 21:33:29 +00:00
LoggedError(err, "Error accessing media: %s (%s)", filename, ptr.Oid)
}
}
func smudgeFilename(args []string, err error) string {
if len(args) > 0 {
return args[0]
}
if lfs.IsSmudgeError(err) {
return filepath.Base(lfs.ErrorGetContext(err, "FileName").(string))
}
return "<unknown file>"
}
func init() {
2015-04-22 17:41:41 +00:00
smudgeCmd.Flags().BoolVarP(&smudgeInfo, "info", "i", false, "Display the local path and size of the smudged file.")
smudgeCmd.Flags().BoolVarP(&smudgeSkip, "skip", "s", false, "Skip automatic downloading of objects on clone or pull.")
2014-06-26 20:55:53 +00:00
RootCmd.AddCommand(smudgeCmd)
}