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

Method fetchAndCheckout

submodule.go:242–282  ·  view source on GitHub ↗
(
	ctx context.Context, r *Repository, o *SubmoduleUpdateOptions, hash plumbing.Hash,
)

Source from the content-addressed store, hash-verified

240}
241
242func (s *Submodule) fetchAndCheckout(
243 ctx context.Context, r *Repository, o *SubmoduleUpdateOptions, hash plumbing.Hash,
244) error {
245 if !o.NoFetch {
246 err := r.FetchContext(ctx, &FetchOptions{Auth: o.Auth, Depth: o.Depth})
247 if err != nil && err != NoErrAlreadyUpToDate {
248 return err
249 }
250 }
251
252 w, err := r.Worktree()
253 if err != nil {
254 return err
255 }
256
257 // Handle a case when submodule refers to an orphaned commit that's still reachable
258 // through Git server using a special protocol capability[1].
259 //
260 // [1]: https://git-scm.com/docs/protocol-capabilities#_allow_reachable_sha1_in_want
261 if !o.NoFetch {
262 if _, err := w.r.Object(plumbing.AnyObject, hash); err != nil {
263 refSpec := config.RefSpec("+" + hash.String() + ":" + hash.String())
264
265 err := r.FetchContext(ctx, &FetchOptions{
266 Auth: o.Auth,
267 RefSpecs: []config.RefSpec{refSpec},
268 Depth: o.Depth,
269 })
270 if err != nil && err != NoErrAlreadyUpToDate && err != ErrExactSHA1NotSupported {
271 return err
272 }
273 }
274 }
275
276 if err := w.Checkout(&CheckoutOptions{Hash: hash}); err != nil {
277 return err
278 }
279
280 head := plumbing.NewHashReference(plumbing.HEAD, hash)
281 return r.Storer.SetReference(head)
282}
283
284// Submodules list of several submodules from the same repository.
285type Submodules []*Submodule

Callers 1

updateMethod · 0.95

Calls 8

RefSpecTypeAlias · 0.92
NewHashReferenceFunction · 0.92
WorktreeMethod · 0.80
CheckoutMethod · 0.80
StringMethod · 0.65
SetReferenceMethod · 0.65
FetchContextMethod · 0.45
ObjectMethod · 0.45

Tested by

no test coverage detected