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"
|
2017-08-12 04:18:19 +00:00
|
|
|
"github.com/git-lfs/git-lfs/git"
|
2016-11-15 17:01:18 +00:00
|
|
|
"github.com/git-lfs/git-lfs/lfs"
|
2017-02-11 13:36:28 +00:00
|
|
|
"github.com/git-lfs/git-lfs/tools"
|
2017-07-31 16:37:44 +00:00
|
|
|
"github.com/git-lfs/git-lfs/tools/humanize"
|
2017-08-12 04:18:19 +00:00
|
|
|
"github.com/git-lfs/git-lfs/tq"
|
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
|
|
|
|
2017-08-12 04:18:19 +00:00
|
|
|
// delayedSmudge performs a 'delayed' smudge, adding the LFS pointer to the
|
2017-08-16 17:03:58 +00:00
|
|
|
// `*tq.TransferQueue` "q" if the file is not present locally, passes the given
|
2017-08-12 04:18:19 +00:00
|
|
|
// filepathfilter, and is not skipped. If the pointer is malformed, or already
|
|
|
|
// exists, it streams the contents to be written into the working copy to "to".
|
|
|
|
//
|
|
|
|
// delayedSmudge returns the number of bytes written, whether the checkout was
|
|
|
|
// delayed, the *lfs.Pointer that was smudged, and an error, if one occurred.
|
2017-10-24 18:07:53 +00:00
|
|
|
func delayedSmudge(gf *lfs.GitFilter, s *git.FilterProcessScanner, to io.Writer, from io.Reader, q *tq.TransferQueue, filename string, skip bool, filter *filepathfilter.Filter) (int64, bool, *lfs.Pointer, error) {
|
2017-08-12 04:18:19 +00:00
|
|
|
ptr, pbuf, perr := lfs.DecodeFrom(from)
|
|
|
|
if perr != nil {
|
|
|
|
// Write 'statusFromErr(nil)', even though 'perr != nil', since
|
|
|
|
// we are about to write non-delayed smudged contents to "to".
|
|
|
|
if err := s.WriteStatus(statusFromErr(nil)); err != nil {
|
|
|
|
return 0, false, nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-25 00:59:36 +00:00
|
|
|
n, err := tools.Spool(to, pbuf, cfg.TempDir())
|
2017-08-12 04:18:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return n, false, nil, errors.Wrap(err, perr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != 0 {
|
|
|
|
return 0, false, nil, errors.NewNotAPointerError(errors.Errorf(
|
|
|
|
"Unable to parse pointer at: %q", filename,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return 0, false, nil, nil
|
|
|
|
}
|
|
|
|
|
2017-10-24 21:35:14 +00:00
|
|
|
lfs.LinkOrCopyFromReference(cfg, ptr.Oid, ptr.Size)
|
2017-08-12 04:18:19 +00:00
|
|
|
|
2017-10-25 17:31:15 +00:00
|
|
|
path, err := cfg.Filesystem().ObjectPath(ptr.Oid)
|
2017-08-12 04:18:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !skip && filter.Allows(filename) {
|
2017-08-20 16:23:08 +00:00
|
|
|
if _, statErr := os.Stat(path); statErr != nil {
|
Don't fail if we lack objects the server has
A Git LFS client may not have the entire history of the objects for the
repository. However, in some situations, we traverse the entire history
of a branch when pushing it, meaning that we need to process every
LFS object in the history of that branch. If the objects for the entire
history are not present, we currently fail to push.
Instead, let's mark objects we don't have on disk as missing and only
fail when we would need to upload those objects. We'll know the server
has the objects if the batch response provides no actions to take for
them when we request an upload. Pass the missing flag down through the
code, and always set it to false for non-uploads.
If for some reason we fail to properly flag a missing object, we will
still fail later on when we cannot open the file, just in a messier and
more poorly controlled way. The technique used here will attempt to
abort the batch as soon as we notice a problem, which means that in the
common case (less than 100 objects) we won't have transferred any
objects, so the user can notice the failure as soon as possible.
Update the tests to look for a string which will occur in the error
message, since we no longer produce the system error message for ENOENT.
2019-04-30 19:18:18 +00:00
|
|
|
q.Add(filename, path, ptr.Oid, ptr.Size, false)
|
2017-08-12 04:18:19 +00:00
|
|
|
return 0, true, ptr, nil
|
|
|
|
}
|
2017-08-20 16:31:01 +00:00
|
|
|
|
2017-08-21 20:44:47 +00:00
|
|
|
// Write 'statusFromErr(nil)', since the object is already
|
|
|
|
// present in the local cache, we will write the object's
|
|
|
|
// contents without delaying.
|
2017-08-20 16:31:01 +00:00
|
|
|
if err := s.WriteStatus(statusFromErr(nil)); err != nil {
|
|
|
|
return 0, false, nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-24 18:07:53 +00:00
|
|
|
n, err := gf.Smudge(to, ptr, filename, false, nil, nil)
|
2017-08-20 16:31:01 +00:00
|
|
|
return n, false, ptr, err
|
2017-08-12 04:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.WriteStatus(statusFromErr(nil)); err != nil {
|
|
|
|
return 0, false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := ptr.Encode(to)
|
|
|
|
return int64(n), false, ptr, err
|
|
|
|
}
|
|
|
|
|
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".
|
|
|
|
//
|
2017-02-11 13:36:28 +00:00
|
|
|
// 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
|
2017-02-17 00:38:15 +00:00
|
|
|
// copied out back to Git. If the pointer file is empty, an empty file will be
|
|
|
|
// written with no error.
|
2017-02-11 13:36:28 +00:00
|
|
|
//
|
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.
|
2017-10-24 18:07:53 +00:00
|
|
|
func smudge(gf *lfs.GitFilter, to io.Writer, from io.Reader, filename string, skip bool, filter *filepathfilter.Filter) (int64, 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 {
|
2017-10-25 00:59:36 +00:00
|
|
|
n, err := tools.Spool(to, pbuf, cfg.TempDir())
|
2017-02-17 00:38:15 +00:00
|
|
|
if err != nil {
|
2017-07-31 16:37:28 +00:00
|
|
|
return 0, errors.Wrap(err, perr.Error())
|
2016-12-22 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 00:38:15 +00:00
|
|
|
if n != 0 {
|
2017-07-31 16:37:28 +00:00
|
|
|
return 0, errors.NewNotAPointerError(errors.Errorf(
|
2017-02-17 00:38:15 +00:00
|
|
|
"Unable to parse pointer at: %q", filename,
|
|
|
|
))
|
|
|
|
}
|
2017-07-31 16:37:28 +00:00
|
|
|
return 0, nil
|
2016-12-22 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 21:35:14 +00:00
|
|
|
lfs.LinkOrCopyFromReference(cfg, ptr.Oid, ptr.Size)
|
2017-10-24 19:46:32 +00:00
|
|
|
cb, file, err := gf.CopyCallbackFile("download", filename, 1, 1)
|
2016-11-09 00:38:15 +00:00
|
|
|
if err != nil {
|
2017-07-31 16:37:28 +00:00
|
|
|
return 0, err
|
2016-11-09 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 23:34:57 +00:00
|
|
|
download := !skip
|
|
|
|
if download {
|
|
|
|
download = filter.Allows(filename)
|
2016-11-09 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 17:11:45 +00:00
|
|
|
n, err := gf.Smudge(to, ptr, filename, download, getTransferManifestOperationRemote("download", cfg.Remote()), cb)
|
2016-11-09 00:38:15 +00:00
|
|
|
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) {
|
2017-04-26 15:12:31 +00:00
|
|
|
var oid string = ptr.Oid
|
|
|
|
if len(oid) >= 7 {
|
|
|
|
oid = oid[:7]
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:12:52 +00:00
|
|
|
LoggedError(err, "Error downloading object: %s (%s): %s", filename, oid, err)
|
2016-11-09 00:38:15 +00:00
|
|
|
if !cfg.SkipDownloadErrors() {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 16:37:28 +00:00
|
|
|
return n, nil
|
2016-11-09 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2017-10-18 21:42:00 +00:00
|
|
|
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())
|
2017-10-24 18:07:53 +00:00
|
|
|
gitfilter := lfs.NewGitFilter(cfg)
|
2016-12-22 21:49:08 +00:00
|
|
|
|
2017-10-24 18:07:53 +00:00
|
|
|
if n, err := smudge(gitfilter, 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())
|
|
|
|
}
|
2017-08-15 15:47:00 +00:00
|
|
|
} else if possiblyMalformedObjectSize(n) {
|
2017-07-31 16:37:44 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Possibly malformed smudge on Windows: see `git lfs help smudge` for more info.")
|
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]
|
|
|
|
}
|
2017-02-13 16:08:14 +00:00
|
|
|
return "<unknown file>"
|
2014-08-05 16:35:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 15:47:00 +00:00
|
|
|
func possiblyMalformedObjectSize(n int64) bool {
|
2017-08-14 16:44:24 +00:00
|
|
|
return n > 4*humanize.Gigabyte
|
2017-07-31 16:37:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|