-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.go
68 lines (57 loc) · 2.03 KB
/
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
package git
import (
"strings"
)
// CheckoutOption provides a way for setting specific options while attempting
// to checkout a branch. Each supported option can customize how a branch is checked
// out from the remote and integrated into the current repository (working directory)
type CheckoutOption func(*checkoutOptions)
type checkoutOptions struct {
Config []string
}
// WithCheckoutConfig allows temporary git config to be set while checking
// out a branch from the remote. Config set using this approach will override
// any config defined within existing git config files. Config must be
// provided as key value pairs, mismatched config will result in an
// [ErrMissingConfigValue] error. Any invalid paths will result in an
// [ErrInvalidConfigPath] error
func WithCheckoutConfig(kv ...string) CheckoutOption {
return func(opts *checkoutOptions) {
opts.Config = trim(kv...)
}
}
// Checkout will attempt to checkout a branch with the given name. If the branch
// does not exist, it is created at the current working tree reference (or commit),
// and then switched to. If the branch does exist, then switching to it restores
// all working tree files
func (c *Client) Checkout(branch string, opts ...CheckoutOption) (string, error) {
options := &checkoutOptions{}
for _, opt := range opts {
opt(options)
}
cfg, err := ToInlineConfig(options.Config...)
if err != nil {
return "", err
}
// Query the repository for all existing branches, both local and remote.
// If a pull hasn't been done, there is a chance that an expected
// remote branch will not be tracked
out, err := c.Exec("git branch --all --format='%(refname:short)'")
if err != nil {
return out, err
}
var buf strings.Builder
buf.WriteString("git")
if len(cfg) > 0 {
buf.WriteString(" ")
buf.WriteString(strings.Join(cfg, " "))
}
buf.WriteString(" checkout ")
for _, ref := range strings.Split(out, "\n") {
if strings.HasSuffix(ref, branch) {
return c.Exec(buf.String() + branch)
}
}
buf.WriteString(" -b ")
return c.Exec(buf.String() + branch)
}