MCPcopy
hub / github.com/go-git/go-git / fetchAndCheckout

Method fetchAndCheckout

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

Source from the content-addressed store, hash-verified

306}
307
308func (s *Submodule) fetchAndCheckout(
309 ctx context.Context, r *Repository, o *SubmoduleUpdateOptions, hash plumbing.Hash,
310) error {
311 if !o.NoFetch {
312 err := r.FetchContext(ctx, &FetchOptions{Auth: o.Auth, Depth: o.Depth})
313 if err != nil && err != NoErrAlreadyUpToDate {
314 return err
315 }
316 }
317
318 w, err := r.Worktree()
319 if err != nil {
320 return err
321 }
322
323 // Handle a case when submodule refers to an orphaned commit that's still reachable
324 // through Git server using a special protocol capability[1].
325 //
326 // [1]: https://git-scm.com/docs/protocol-capabilities#_allow_reachable_sha1_in_want
327 if !o.NoFetch {
328 if _, err := w.r.Object(plumbing.AnyObject, hash); err != nil {
329 refSpec := config.RefSpec("+" + hash.String() + ":" + hash.String())
330
331 err := r.FetchContext(ctx, &FetchOptions{
332 Auth: o.Auth,
333 RefSpecs: []config.RefSpec{refSpec},
334 Depth: o.Depth,
335 })
336 if err != nil && err != NoErrAlreadyUpToDate && err != ErrExactSHA1NotSupported {
337 return err
338 }
339 }
340 }
341
342 if err := w.Checkout(&CheckoutOptions{Hash: hash}); err != nil {
343 return err
344 }
345
346 head := plumbing.NewHashReference(plumbing.HEAD, hash)
347 return r.Storer.SetReference(head)
348}
349
350// Submodules list of several submodules from the same repository.
351type 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