Debian Codegraph @

commands/command_migrate_export.go

raw symbols SCIP
1 package commands
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7
8 "github.com/git-lfs/git-lfs/v3/errors"
9 "github.com/git-lfs/git-lfs/v3/filepathfilter"
10 "github.com/git-lfs/git-lfs/v3/git"
11 "github.com/git-lfs/git-lfs/v3/git/githistory"
12 "github.com/git-lfs/git-lfs/v3/lfs"
13 "github.com/git-lfs/git-lfs/v3/tasklog"
14 "github.com/git-lfs/git-lfs/v3/tools"
15 "github.com/git-lfs/git-lfs/v3/tr"
16 "github.com/git-lfs/gitobj/v2"
17 "github.com/spf13/cobra"
18 )
19
20 func migrateExportCommand(cmd *cobra.Command, args []string) {
21 ensureWorkingCopyClean(os.Stdin, os.Stderr)
22
23 l := tasklog.NewLogger(os.Stderr,
24 tasklog.ForceProgress(cfg.ForceProgress()),
25 )
26 defer l.Close()
27
28 db, err := getObjectDatabase()
29 if err != nil {
30 ExitWithError(err)
31 }
32 defer db.Close()
33
34 rewriter := getHistoryRewriter(cmd, db, l)
35
36 filter := rewriter.Filter()
37 if len(filter.Include()) <= 0 {
38 ExitWithError(errors.New(tr.Tr.Get("One or more files must be specified with --include")))
39 }
40
41 tracked := trackedFromExportFilter(filter)
42 gitfilter := lfs.NewGitFilter(cfg)
43
44 opts := &githistory.RewriteOptions{
45 Verbose: migrateVerbose,
46 ObjectMapFilePath: objectMapFilePath,
47 BlobFn: func(path string, b *gitobj.Blob) (*gitobj.Blob, error) {
48 if filepath.Base(path) == ".gitattributes" {
49 return b, nil
50 }
51
52 ptr, err := lfs.DecodePointer(b.Contents)
53 if err != nil {
54 if errors.IsNotAPointerError(err) {
55 return b, nil
56 }
57 return nil, err
58 }
59
60 downloadPath, err := gitfilter.ObjectPath(ptr.Oid)
61 if err != nil {
62 return nil, err
63 }
64
65 return gitobj.NewBlobFromFile(downloadPath)
66 },
67
68 TreeCallbackFn: func(path string, t *gitobj.Tree) (*gitobj.Tree, error) {
69 if path != "/" {
70 // Ignore non-root trees.
71 return t, nil
72 }
73
74 ours := tracked
75 theirs, err := trackedFromAttrs(db, t)
76 if err != nil {
77 return nil, err
78 }
79
80 // Create a blob of the attributes that are optionally
81 // present in the "t" tree's .gitattributes blob, and
82 // union in the patterns that we've tracked.
83 //
84 // Perform this Union() operation each time we visit a
85 // root tree such that if the underlying .gitattributes
86 // is present and has a diff between commits in the
87 // range of commits to migrate, those changes are
88 // preserved.
89 blob, err := trackedToBlob(db, theirs.Clone().Union(ours))
90 if err != nil {
91 return nil, err
92 }
93
94 // Finally, return a copy of the tree "t" that has the
95 // new .gitattributes file included/replaced.
96 return t.Merge(&gitobj.TreeEntry{
97 Name: ".gitattributes",
98 Filemode: 0100644,
99 Oid: blob,
100 }), nil
101 },
102
103 UpdateRefs: true,
104 }
105
106 setupRepository()
107
108 opts, err = rewriteOptions(args, opts, l)
109 if err != nil {
110 ExitWithError(err)
111 }
112
113 remote := cfg.Remote()
114 if cmd.Flag("remote").Changed {
115 remote = exportRemote
116 }
117 remoteURL := getAPIClient().Endpoints.RemoteEndpoint("download", remote).Url
118 if remoteURL == "" && cmd.Flag("remote").Changed {
119 ExitWithError(errors.New(tr.Tr.Get("Invalid remote %s provided", remote)))
120 }
121
122 // If we have a valid remote, pre-download all objects using the Transfer Queue
123 if remoteURL != "" {
124 q := newDownloadQueue(getTransferManifestOperationRemote("download", remote), remote)
125 gs := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
126 if err != nil {
127 return
128 }
129
130 if !filter.Allows(p.Name) {
131 return
132 }
133
134 downloadPath, err := gitfilter.ObjectPath(p.Oid)
135 if err != nil {
136 return
137 }
138
139 if _, err := os.Stat(downloadPath); os.IsNotExist(err) {
140 q.Add(p.Name, downloadPath, p.Oid, p.Size, false, nil)
141 }
142 })
143 gs.ScanRefs(opts.Include, opts.Exclude, nil)
144
145 q.Wait()
146
147 for _, err := range q.Errors() {
148 if err != nil {
149 ExitWithError(err)
150 }
151 }
152 }
153
154 // Perform the rewrite
155 if _, err := rewriter.Rewrite(opts); err != nil {
156 ExitWithError(err)
157 }
158
159 // Only perform `git-checkout(1) -f` if the repository is non-bare.
160 if bare, _ := git.IsBare(); !bare {
161 if err := performForceCheckout(l); err != nil {
162 ExitWithError(err)
163 }
164 }
165
166 fetchPruneCfg := lfs.NewFetchPruneConfig(cfg.Git)
167
168 // Set our preservation time-window for objects existing on the remote to
169 // 0. Because the newly rewritten commits have not yet been pushed, some
170 // exported objects can still exist on the remote within the time window
171 // and thus will not be pruned from the cache.
172 fetchPruneCfg.FetchRecentRefsDays = 0
173
174 // Prune our cache
175 prune(fetchPruneCfg, false, false, false, false, true)
176 }
177
178 func performForceCheckout(l *tasklog.Logger) error {
179 t := l.Waiter(tr.Tr.Get("Checkout"))
180 defer t.Complete()
181
182 return git.Checkout("", nil, true)
183 }
184
185 // trackedFromExportFilter returns an ordered set of strings where each entry
186 // is a line we intend to place in the .gitattributes file. It adds/removes the
187 // filter/diff/merge=lfs attributes based on patterns included/excluded in the
188 // given filter. Since `migrate export` removes files from Git LFS, it will
189 // remove attributes for included files, and add attributes for excluded files
190 func trackedFromExportFilter(filter *filepathfilter.Filter) *tools.OrderedSet {
191 tracked := tools.NewOrderedSet()
192
193 for _, include := range filter.Include() {
194 tracked.Add(fmt.Sprintf("%s !text !filter !merge !diff", escapeAttrPattern(include)))
195 }
196
197 for _, exclude := range filter.Exclude() {
198 tracked.Add(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text", escapeAttrPattern(exclude)))
199 }
200
201 return tracked
202 }