2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2014-05-27 16:27:54 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-22 22:02:25 +00:00
|
|
|
"fmt"
|
2014-09-05 16:38:14 +00:00
|
|
|
"io"
|
2014-05-27 16:27:54 +00:00
|
|
|
"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"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2014-05-27 16:27:54 +00:00
|
|
|
)
|
|
|
|
|
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.
|
2015-09-23 19:43:04 +00:00
|
|
|
smudgeSkip = false
|
2014-06-26 20:55:53 +00:00
|
|
|
)
|
2014-05-27 16:27:54 +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 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.
|
2016-12-22 21:49:08 +00:00
|
|
|
func smudge(to io.Writer, from io.Reader, filename string, skip bool, filter *filepathfilter.Filter) error {
|
2016-12-23 19:16:57 +00:00
|
|
|
ptr, pbuf, perr := lfs.DecodeFrom(from)
|
2016-12-22 21:49:08 +00:00
|
|
|
if perr != nil {
|
2016-12-23 19:16:57 +00:00
|
|
|
if _, err := io.Copy(to, pbuf); err != nil {
|
2016-12-22 21:49:08 +00:00
|
|
|
return errors.Wrap(err, perr.Error())
|
|
|
|
}
|
|
|
|
|
2016-12-22 22:02:25 +00:00
|
|
|
return errors.NewNotAPointerError(errors.Errorf(
|
|
|
|
"Unable to parse pointer at: %q", filename,
|
|
|
|
))
|
2016-12-22 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 23:34:57 +00:00
|
|
|
lfs.LinkOrCopyFromReference(ptr.Oid, ptr.Size)
|
2016-11-09 00:38:15 +00:00
|
|
|
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)
|
2016-11-09 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ptr.Smudge(to, filename, download, TransferManifest(), 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) {
|
2015-04-24 20:24:32 +00:00
|
|
|
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())
|
2016-12-22 21:49:08 +00:00
|
|
|
|
|
|
|
if err := smudge(os.Stdout, os.Stdin, smudgeFilename(args), smudgeSkip, filter); err != nil {
|
2016-12-22 22:02:25 +00:00
|
|
|
if errors.IsNotAPointerError(err) {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
} else {
|
|
|
|
Error(err.Error())
|
|
|
|
}
|
2014-08-07 14:53:13 +00:00
|
|
|
}
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 21:49:08 +00:00
|
|
|
func smudgeFilename(args []string) string {
|
2014-08-05 16:35:09 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
return args[0]
|
|
|
|
}
|
|
|
|
return "<unknown file>"
|
|
|
|
}
|
|
|
|
|
2014-05-27 16:27:54 +00:00
|
|
|
func init() {
|
2016-09-01 16:09:38 +00:00
|
|
|
RegisterCommand("smudge", smudgeCommand, func(cmd *cobra.Command) {
|
2016-08-10 15:33:25 +00:00
|
|
|
cmd.Flags().BoolVarP(&smudgeSkip, "skip", "s", false, "")
|
|
|
|
})
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|