git-lfs/commands/command_smudge.go

113 lines
3.0 KiB
Go
Raw Normal View History

package commands
import (
"fmt"
"io"
"os"
2015-05-13 19:43:41 +00:00
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/errors"
2016-11-21 23:34:57 +00:00
"github.com/git-lfs/git-lfs/filepathfilter"
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/lfs"
"github.com/git-lfs/git-lfs/localstorage"
"github.com/git-lfs/git-lfs/tools"
"github.com/spf13/cobra"
)
2014-06-26 20:55:53 +00:00
var (
2016-11-09 15:38:25 +00:00
// smudgeSkip is a command-line flag belonging to the "git-lfs smudge"
2016-11-09 01:04:45 +00:00
// command specifying whether to skip the smudge process.
smudgeSkip = false
2014-06-26 20:55:53 +00:00
)
2016-11-09 01:04:45 +00:00
// smudge smudges the given `*lfs.Pointer`, "ptr", and writes its objects
// contents to the `io.Writer`, "to".
//
// If the encoded LFS pointer is not parse-able as a pointer, the contents of
// that file will instead be spooled to a temporary location on disk and then
// copied out back to Git. If the pointer file is empty, an empty file will be
// written with no error.
//
2016-11-09 01:04:45 +00:00
// If the smudged object did not "pass" the include and exclude filterset, it
// will not be downloaded, and the object will remain a pointer on disk, as if
// the smudge filter had not been applied at all.
//
// Any errors encountered along the way will be returned immediately if they
// were non-fatal, otherwise execution will halt and the process will be
// terminated by using the `commands.Panic()` func.
func smudge(to io.Writer, from io.Reader, filename string, skip bool, filter *filepathfilter.Filter) error {
ptr, pbuf, perr := lfs.DecodeFrom(from)
if perr != nil {
n, err := tools.Spool(to, pbuf, localstorage.Objects().TempDir)
if err != nil {
return errors.Wrap(err, perr.Error())
}
if n != 0 {
return errors.NewNotAPointerError(errors.Errorf(
"Unable to parse pointer at: %q", filename,
))
}
return nil
}
2016-11-21 23:34:57 +00:00
lfs.LinkOrCopyFromReference(ptr.Oid, ptr.Size)
cb, file, err := lfs.CopyCallbackFile("smudge", filename, 1, 1)
if err != nil {
return err
}
2016-11-21 23:34:57 +00:00
download := !skip
if download {
download = filter.Allows(filename)
}
err = ptr.Smudge(to, filename, download, getTransferManifest(), cb)
if file != nil {
file.Close()
}
if err != nil {
ptr.Encode(to)
// Download declined error is ok to skip if we weren't requesting download
if !(errors.IsDownloadDeclinedError(err) && !download) {
LoggedError(err, "Error downloading object: %s (%s)", filename, ptr.Oid)
if !cfg.SkipDownloadErrors() {
os.Exit(2)
}
}
}
return nil
}
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
2016-11-21 23:34:57 +00:00
if !smudgeSkip && cfg.Os.Bool("GIT_LFS_SKIP_SMUDGE", false) {
smudgeSkip = true
}
filter := filepathfilter.New(cfg.FetchIncludePaths(), cfg.FetchExcludePaths())
if err := smudge(os.Stdout, os.Stdin, smudgeFilename(args), smudgeSkip, filter); err != nil {
if errors.IsNotAPointerError(err) {
fmt.Fprintln(os.Stderr, err.Error())
} else {
Error(err.Error())
}
2014-08-07 14:53:13 +00:00
}
}
func smudgeFilename(args []string) string {
if len(args) > 0 {
return args[0]
}
return "<unknown file>"
}
func init() {
RegisterCommand("smudge", smudgeCommand, func(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&smudgeSkip, "skip", "s", false, "")
})
}