MCPcopy Create free account
hub / github.com/go-git/go-git / Checkout

Method Checkout

worktree.go:161–199  ·  view source on GitHub ↗

Checkout switch branches or restore working tree files.

(opts *CheckoutOptions)

Source from the content-addressed store, hash-verified

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