2018-06-13 21:10:32 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-09-01 19:41:10 +00:00
|
|
|
"github.com/git-lfs/git-lfs/v3/errors"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/filepathfilter"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/git"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/git/githistory"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/lfs"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/tasklog"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/tools"
|
2020-06-30 16:21:08 +00:00
|
|
|
"github.com/git-lfs/gitobj/v2"
|
2018-06-13 21:10:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func migrateExportCommand(cmd *cobra.Command, args []string) {
|
commands: warn if working copy is dirty
In 'git lfs migrate import' and 'git lfs migrate export', Git LFS makes
destructive changes to a caller's repository and therefore invokes 'git
checkout --force', which throws away local changes.
To prevent this, let's introduce a check that notifies users when they
are going to throw away local changes, and allows for the user to abort
if they do not wish to discard their local changes.
With this, a user can safely migrate over dirty repositories (i.e., in
the case that they wanted to fix a file that should have been in Git
LFS, but wasn't) without having to finagle their repository to get it
into a migrate-able state.
For users with lots of pending migrations, also teach --yes, which
allows a user to avoid the check, and instead simply prints the warning
message to STDERR.
2018-07-10 15:18:46 +00:00
|
|
|
ensureWorkingCopyClean(os.Stdin, os.Stderr)
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := tasklog.NewLogger(os.Stderr,
|
|
|
|
tasklog.ForceProgress(cfg.ForceProgress()),
|
|
|
|
)
|
2018-06-13 21:10:32 +00:00
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
db, err := getObjectDatabase()
|
|
|
|
if err != nil {
|
|
|
|
ExitWithError(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
rewriter := getHistoryRewriter(cmd, db, l)
|
|
|
|
|
|
|
|
filter := rewriter.Filter()
|
|
|
|
if len(filter.Include()) <= 0 {
|
|
|
|
ExitWithError(errors.Errorf("fatal: one or more files must be specified with --include"))
|
|
|
|
}
|
|
|
|
|
|
|
|
tracked := trackedFromExportFilter(filter)
|
|
|
|
gitfilter := lfs.NewGitFilter(cfg)
|
|
|
|
|
2018-06-21 21:03:54 +00:00
|
|
|
opts := &githistory.RewriteOptions{
|
2018-06-13 21:10:32 +00:00
|
|
|
Verbose: migrateVerbose,
|
|
|
|
ObjectMapFilePath: objectMapFilePath,
|
2018-07-05 16:49:10 +00:00
|
|
|
BlobFn: func(path string, b *gitobj.Blob) (*gitobj.Blob, error) {
|
2018-06-13 21:10:32 +00:00
|
|
|
if filepath.Base(path) == ".gitattributes" {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2018-06-25 21:36:57 +00:00
|
|
|
ptr, err := lfs.DecodePointer(b.Contents)
|
|
|
|
if err != nil {
|
2018-06-28 21:58:41 +00:00
|
|
|
if errors.IsNotAPointerError(err) {
|
|
|
|
return b, nil
|
|
|
|
}
|
2018-06-25 21:36:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-13 21:10:32 +00:00
|
|
|
|
2018-06-25 21:36:57 +00:00
|
|
|
downloadPath, err := gitfilter.ObjectPath(ptr.Oid)
|
|
|
|
if err != nil {
|
2018-06-13 21:10:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:49:10 +00:00
|
|
|
return gitobj.NewBlobFromFile(downloadPath)
|
2018-06-13 21:10:32 +00:00
|
|
|
},
|
|
|
|
|
2018-07-05 16:49:10 +00:00
|
|
|
TreeCallbackFn: func(path string, t *gitobj.Tree) (*gitobj.Tree, error) {
|
2018-06-13 21:10:32 +00:00
|
|
|
if path != "/" {
|
|
|
|
// Ignore non-root trees.
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ours := tracked
|
|
|
|
theirs, err := trackedFromAttrs(db, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a blob of the attributes that are optionally
|
|
|
|
// present in the "t" tree's .gitattributes blob, and
|
|
|
|
// union in the patterns that we've tracked.
|
|
|
|
//
|
|
|
|
// Perform this Union() operation each time we visit a
|
|
|
|
// root tree such that if the underlying .gitattributes
|
|
|
|
// is present and has a diff between commits in the
|
|
|
|
// range of commits to migrate, those changes are
|
|
|
|
// preserved.
|
|
|
|
blob, err := trackedToBlob(db, theirs.Clone().Union(ours))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, return a copy of the tree "t" that has the
|
|
|
|
// new .gitattributes file included/replaced.
|
2018-07-05 16:49:10 +00:00
|
|
|
return t.Merge(&gitobj.TreeEntry{
|
2018-06-13 21:10:32 +00:00
|
|
|
Name: ".gitattributes",
|
|
|
|
Filemode: 0100644,
|
|
|
|
Oid: blob,
|
|
|
|
}), nil
|
|
|
|
},
|
|
|
|
|
|
|
|
UpdateRefs: true,
|
2018-06-21 21:03:54 +00:00
|
|
|
}
|
|
|
|
|
commands: make sure we're in the working tree
In the normal case, Git commands perform repository autodiscovery based
on the current working directory. However, in some cases, it's possible
to specify a Git working tree unrelated to the current working directory
by using GIT_WORK_TREE. In such a case, we want to make sure that we
change into the working tree such that our working directory is always
within the working tree, if one exists. This is what Git does, and it
means that when we write files into the repository, such as a
.gitattributes file, we write them into the proper place.
Note also that we adjust the code to require that the working directory
be non-empty when we require a working copy instead of that the
repository be non-bare. That's because we don't want people to be
working inside of the Git directory in such situations, where the
repository would be non-bare but would not have a working tree.
We add tests for this case for track and untrack, which require a
working tree, and for checkout, which requires only a repository. This
means that we can verify the behavior of the functions we've added
without needing to add tests for this case to each of the subcommands.
2020-10-02 19:03:55 +00:00
|
|
|
setupRepository()
|
2018-06-21 21:03:54 +00:00
|
|
|
|
|
|
|
opts, err = rewriteOptions(args, opts, l)
|
|
|
|
if err != nil {
|
|
|
|
ExitWithError(err)
|
|
|
|
}
|
|
|
|
|
2018-06-27 18:03:23 +00:00
|
|
|
remote := cfg.Remote()
|
|
|
|
if cmd.Flag("remote").Changed {
|
|
|
|
remote = exportRemote
|
|
|
|
}
|
|
|
|
remoteURL := getAPIClient().Endpoints.RemoteEndpoint("download", remote).Url
|
|
|
|
if remoteURL == "" && cmd.Flag("remote").Changed {
|
|
|
|
ExitWithError(errors.Errorf("fatal: invalid remote %s provided", remote))
|
|
|
|
}
|
|
|
|
|
2018-06-21 21:03:54 +00:00
|
|
|
// If we have a valid remote, pre-download all objects using the Transfer Queue
|
2018-06-27 18:03:23 +00:00
|
|
|
if remoteURL != "" {
|
|
|
|
q := newDownloadQueue(getTransferManifestOperationRemote("Download", remote), remote)
|
2019-08-09 15:25:23 +00:00
|
|
|
gs := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
|
2018-06-25 23:21:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-26 00:08:58 +00:00
|
|
|
if !filter.Allows(p.Name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-25 23:21:55 +00:00
|
|
|
downloadPath, err := gitfilter.ObjectPath(p.Oid)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(downloadPath); os.IsNotExist(err) {
|
2020-02-10 15:52:15 +00:00
|
|
|
q.Add(p.Name, downloadPath, p.Oid, p.Size, false, nil)
|
2018-06-25 23:21:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
gs.ScanRefs(opts.Include, opts.Exclude, nil)
|
2018-06-21 21:03:54 +00:00
|
|
|
|
|
|
|
q.Wait()
|
|
|
|
|
|
|
|
for _, err := range q.Errors() {
|
|
|
|
if err != nil {
|
|
|
|
ExitWithError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the rewrite
|
|
|
|
if _, err := rewriter.Rewrite(opts); err != nil {
|
|
|
|
ExitWithError(err)
|
|
|
|
}
|
2018-06-13 21:10:32 +00:00
|
|
|
|
2018-06-25 21:10:31 +00:00
|
|
|
// Only perform `git-checkout(1) -f` if the repository is non-bare.
|
2018-06-13 21:10:32 +00:00
|
|
|
if bare, _ := git.IsBare(); !bare {
|
|
|
|
t := l.Waiter("migrate: checkout")
|
|
|
|
err := git.Checkout("", nil, true)
|
|
|
|
t.Complete()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ExitWithError(err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 23:13:29 +00:00
|
|
|
|
|
|
|
fetchPruneCfg := lfs.NewFetchPruneConfig(cfg.Git)
|
|
|
|
|
|
|
|
// Set our preservation time-window for objects existing on the remote to
|
|
|
|
// 0. Because the newly rewritten commits have not yet been pushed, some
|
|
|
|
// exported objects can still exist on the remote within the time window
|
|
|
|
// and thus will not be pruned from the cache.
|
|
|
|
fetchPruneCfg.FetchRecentRefsDays = 0
|
|
|
|
|
|
|
|
// Prune our cache
|
|
|
|
prune(fetchPruneCfg, false, false, true)
|
2018-06-13 21:10:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:10:31 +00:00
|
|
|
// trackedFromExportFilter returns an ordered set of strings where each entry
|
|
|
|
// is a line we intend to place in the .gitattributes file. It adds/removes the
|
|
|
|
// filter/diff/merge=lfs attributes based on patterns included/excluded in the
|
|
|
|
// given filter. Since `migrate export` removes files from Git LFS, it will
|
|
|
|
// remove attributes for included files, and add attributes for excluded files
|
2018-06-13 21:10:32 +00:00
|
|
|
func trackedFromExportFilter(filter *filepathfilter.Filter) *tools.OrderedSet {
|
|
|
|
tracked := tools.NewOrderedSet()
|
|
|
|
|
|
|
|
for _, include := range filter.Include() {
|
2019-11-12 15:18:21 +00:00
|
|
|
tracked.Add(fmt.Sprintf("%s !text !filter !merge !diff", escapeAttrPattern(include)))
|
2018-06-13 21:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, exclude := range filter.Exclude() {
|
|
|
|
tracked.Add(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text", escapeAttrPattern(exclude)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracked
|
|
|
|
}
|