-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworktree.go
More file actions
70 lines (61 loc) · 1.42 KB
/
Copy pathworktree.go
File metadata and controls
70 lines (61 loc) · 1.42 KB
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
package git
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/util"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/format/index"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/utils/ioutil"
"github.com/go-git/go-git/v5/utils/merkletrie"
"github.com/go-git/go-git/v5/utils/merkletrie/noder"
)
// Checkout switch branches or restore working tree files.
func (w *Worktree) Checkout(opts *CheckoutOptions) error {
if err := opts.Validate(); err != nil {
return err
}
if opts.Create {
if err := w.createBranch(opts); err != nil {
return err
}
}
c, err := w.getCommitFromCheckoutOptions(opts)
if err != nil {
return err
}
oldHead, err := w.r.Head()
if err != nil && err != plumbing.ErrReferenceNotFound {
return err
}
var headHasChanged bool
if !opts.Keep {
headHasChanged, err = w.setHEAD(opts, c)
if err != nil {
return err
}
}
if headHasChanged || opts.Force {
err = w.checkout(c, opts.Force)
if err != nil {
if !opts.Keep && headHasChanged {
if oldHead != nil {
_ = w.r.Storer.SetReference(oldHead)
} else {
_ = w.r.Storer.RemoveReference(plumbing.HEAD)
}
}
return err
}
}
return nil
}