MCPcopy
hub / github.com/go-git/go-git / Checkout

Method Checkout

worktree.go:164–202  ·  view source on GitHub ↗

Checkout switch branches or restore working tree files.

(opts *CheckoutOptions)

Source from the content-addressed store, hash-verified

162
163// Checkout switch branches or restore working tree files.
164func (w *Worktree) Checkout(opts *CheckoutOptions) error {
165 if err := opts.Validate(); err != nil {
166 return err
167 }
168
169 if opts.Create {
170 if err := w.createBranch(opts); err != nil {
171 return err
172 }
173 }
174
175 c, err := w.getCommitFromCheckoutOptions(opts)
176 if err != nil {
177 return err
178 }
179
180 ro := &ResetOptions{Commit: c, Mode: MergeReset}
181 if opts.Force {
182 ro.Mode = HardReset
183 } else if opts.Keep {
184 ro.Mode = SoftReset
185 }
186
187 if !opts.Hash.IsZero() && !opts.Create {
188 err = w.setHEADToCommit(opts.Hash)
189 } else {
190 err = w.setHEADToBranch(opts.Branch, c)
191 }
192
193 if err != nil {
194 return err
195 }
196
197 if len(opts.SparseCheckoutDirectories) > 0 {
198 return w.ResetSparsely(ro, opts.SparseCheckoutDirectories)
199 }
200
201 return w.Reset(ro)
202}
203
204func (w *Worktree) createBranch(opts *CheckoutOptions) error {
205 if err := opts.Branch.Validate(); err != nil {

Callers 15

TestCheckoutMethod · 0.95
TestCheckoutForceMethod · 0.95
TestCheckoutKeepMethod · 0.95
TestCheckoutIndexMemMethod · 0.95
TestCheckoutIndexOSMethod · 0.95
TestCheckoutBranchMethod · 0.95
TestCheckoutCreateMethod · 0.95

Calls 8

createBranchMethod · 0.95
setHEADToCommitMethod · 0.95
setHEADToBranchMethod · 0.95
ResetSparselyMethod · 0.95
ResetMethod · 0.95
IsZeroMethod · 0.65
ValidateMethod · 0.45

Tested by 15

TestCheckoutMethod · 0.76
TestCheckoutForceMethod · 0.76
TestCheckoutKeepMethod · 0.76
TestCheckoutIndexMemMethod · 0.76
TestCheckoutIndexOSMethod · 0.76
TestCheckoutBranchMethod · 0.76
TestCheckoutCreateMethod · 0.76