forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_checkout.go
175 lines (147 loc) · 4.7 KB
/
command_checkout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package commands
import (
"fmt"
"os"
"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/lfs"
"github.com/git-lfs/git-lfs/v3/tasklog"
"github.com/git-lfs/git-lfs/v3/tq"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/spf13/cobra"
)
var (
checkoutTo string
checkoutBase bool
checkoutOurs bool
checkoutTheirs bool
)
func checkoutCommand(cmd *cobra.Command, args []string) {
setupRepository()
stage, err := whichCheckout()
if err != nil {
Exit(tr.Tr.Get("Error parsing args: %v", err))
}
if checkoutTo != "" && stage != git.IndexStageDefault {
if len(args) != 1 {
Exit(tr.Tr.Get("--to requires exactly one Git LFS object file path"))
}
checkoutConflict(rootedPaths(args)[0], stage)
return
} else if checkoutTo != "" || stage != git.IndexStageDefault {
Exit(tr.Tr.Get("--to and exactly one of --theirs, --ours, and --base must be used together"))
}
ref, err := git.CurrentRef()
if err != nil {
Panic(err, tr.Tr.Get("Could not checkout"))
}
singleCheckout := newSingleCheckout(cfg.Git, "")
if singleCheckout.Skip() {
fmt.Println(tr.Tr.Get("Cannot checkout LFS objects, Git LFS is not installed."))
return
}
var totalBytes int64
var pointers []*lfs.WrappedPointer
logger := tasklog.NewLogger(os.Stdout,
tasklog.ForceProgress(cfg.ForceProgress()),
)
meter := tq.NewMeter(cfg)
meter.Direction = tq.Checkout
meter.Logger = meter.LoggerFromEnv(cfg.Os)
logger.Enqueue(meter)
chgitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
if err != nil {
LoggedError(err, tr.Tr.Get("Scanner error: %s", err))
return
}
totalBytes += p.Size
meter.Add(p.Size)
meter.StartTransfer(p.Name)
pointers = append(pointers, p)
})
chgitscanner.Filter = filepathfilter.New(rootedPaths(args), nil, filepathfilter.GitIgnore)
if err := chgitscanner.ScanTree(ref.Sha, nil); err != nil {
ExitWithError(err)
}
chgitscanner.Close()
meter.Start()
for _, p := range pointers {
singleCheckout.Run(p)
// not strictly correct (parallel) but we don't have a callback & it's just local
// plus only 1 slot in channel so it'll block & be close
meter.TransferBytes("checkout", p.Name, p.Size, totalBytes, int(p.Size))
meter.FinishTransfer(p.Name)
}
meter.Finish()
singleCheckout.Close()
}
func checkoutConflict(file string, stage git.IndexStage) {
singleCheckout := newSingleCheckout(cfg.Git, "")
if singleCheckout.Skip() {
fmt.Println(tr.Tr.Get("Cannot checkout LFS objects, Git LFS is not installed."))
return
}
ref, err := git.ResolveRef(fmt.Sprintf(":%d:%s", stage, file))
if err != nil {
Exit(tr.Tr.Get("Could not checkout (are you not in the middle of a merge?): %v", err))
}
scanner, err := git.NewObjectScanner(cfg.GitEnv(), cfg.OSEnv())
if err != nil {
Exit(tr.Tr.Get("Could not create object scanner: %v", err))
}
if !scanner.Scan(ref.Sha) {
Exit(tr.Tr.Get("Could not find object %q", ref.Sha))
}
ptr, err := lfs.DecodePointer(scanner.Contents())
if err != nil {
Exit(tr.Tr.Get("Could not find decoder pointer for object %q: %v", ref.Sha, err))
}
p := &lfs.WrappedPointer{Name: file, Pointer: ptr}
if err := singleCheckout.RunToPath(p, checkoutTo); err != nil {
Exit(tr.Tr.Get("Error checking out %v to %q: %v", ref.Sha, checkoutTo, err))
}
singleCheckout.Close()
}
func whichCheckout() (stage git.IndexStage, err error) {
seen := 0
stage = git.IndexStageDefault
if checkoutBase {
seen++
stage = git.IndexStageBase
}
if checkoutOurs {
seen++
stage = git.IndexStageOurs
}
if checkoutTheirs {
seen++
stage = git.IndexStageTheirs
}
if seen > 1 {
return 0, errors.New(tr.Tr.Get("at most one of --base, --theirs, and --ours is allowed"))
}
return stage, nil
}
// Parameters are filters
// firstly convert any pathspecs to patterns relative to the root of the repo,
// in case this is being executed in a sub-folder
func rootedPaths(args []string) []string {
pathConverter, err := lfs.NewCurrentToRepoPatternConverter(cfg)
if err != nil {
Panic(err, tr.Tr.Get("Could not checkout"))
}
rootedpaths := make([]string, 0, len(args))
for _, arg := range args {
rootedpaths = append(rootedpaths, pathConverter.Convert(arg))
}
return rootedpaths
}
func init() {
RegisterCommand("checkout", checkoutCommand, func(cmd *cobra.Command) {
cmd.Flags().StringVar(&checkoutTo, "to", "", "Checkout a conflicted file to this path")
cmd.Flags().BoolVar(&checkoutOurs, "ours", false, "Checkout our version of a conflicted file")
cmd.Flags().BoolVar(&checkoutTheirs, "theirs", false, "Checkout their version of a conflicted file")
cmd.Flags().BoolVar(&checkoutBase, "base", false, "Checkout the base version of a conflicted file")
})
}