MCPcopy Index your code
hub / github.com/devspace-sh/devspace / Checkout

Method Checkout

pkg/util/git/go_git.go:90–142  ·  view source on GitHub ↗

Checkout certain tag, branch or hash

(tag, branch, revision string)

Source from the content-addressed store, hash-verified

88
89// Checkout certain tag, branch or hash
90func (gr *GoGitRepository) Checkout(tag, branch, revision string) error {
91 r, err := git.PlainOpen(gr.LocalPath)
92 if err != nil {
93 return err
94 }
95
96 // Resolve to the correct hash
97 var hash *plumbing.Hash
98 if tag != "" {
99 hash, err = r.ResolveRevision(plumbing.Revision(fmt.Sprintf("refs/tags/%s", tag)))
100 if err != nil {
101 return errors.Errorf("Error resolving tag revision: %v", err)
102 }
103 } else if branch != "" {
104 remoteRef, err := r.Reference(plumbing.ReferenceName(fmt.Sprintf("refs/remotes/origin/%s", branch)), true)
105 if err != nil {
106 return errors.Errorf("Error resolving branch revision: %v", err)
107 }
108
109 newRef := plumbing.NewHashReference(plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)), remoteRef.Hash())
110 err = r.Storer.SetReference(newRef)
111 if err != nil {
112 return err
113 }
114
115 // Checkout the branch
116 w, err := r.Worktree()
117 if err != nil {
118 return err
119 }
120
121 return w.Checkout(&git.CheckoutOptions{
122 Branch: newRef.Name(),
123 Create: false,
124 })
125 } else if revision != "" {
126 h := plumbing.NewHash(revision)
127 hash = &h
128 } else {
129 return errors.New("Tag, branch or hash has to be defined")
130 }
131
132 // Checkout the hash
133 w, err := r.Worktree()
134 if err != nil {
135 return err
136 }
137
138 return w.Checkout(&git.CheckoutOptions{
139 Hash: *hash,
140 Force: true,
141 })
142}

Callers 2

TestGoGitFunction · 0.95
UpdateMethod · 0.80

Calls 2

NameMethod · 0.65
ErrorfMethod · 0.45

Tested by 1

TestGoGitFunction · 0.76