Checkout switch branches or restore working tree files.
(opts *CheckoutOptions)
| 162 | |
| 163 | // Checkout switch branches or restore working tree files. |
| 164 | func (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 | |
| 204 | func (w *Worktree) createBranch(opts *CheckoutOptions) error { |
| 205 | if err := opts.Branch.Validate(); err != nil { |