Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399package git
import ( "bytes" "context" "errors" "fmt" "path"
"github.com/go-git/go-billy/v5" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/index" "github.com/go-git/go-git/v5/plumbing/transport")
var ( ErrSubmoduleAlreadyInitialized = errors.New("submodule already initialized") ErrSubmoduleNotInitialized = errors.New("submodule not initialized"))
// Submodule a submodule allows you to keep another Git repository in a// subdirectory of your repository.type Submodule struct { // initialized defines if a submodule was already initialized. initialized bool
c *config.Submodule w *Worktree}
// Config returns the submodule configfunc (s *Submodule) Config() *config.Submodule { return s.c}
// Init initialize the submodule reading the recorded Entry in the index for// the given submodulefunc (s *Submodule) Init() error { cfg, err := s.w.r.Config() if err != nil { return err }
_, ok := cfg.Submodules[s.c.Name] if ok { return ErrSubmoduleAlreadyInitialized }
s.initialized = true
cfg.Submodules[s.c.Name] = s.c return s.w.r.Storer.SetConfig(cfg)}
// Status returns the status of the submodule.func (s *Submodule) Status() (*SubmoduleStatus, error) { idx, err := s.w.r.Storer.Index() if err != nil { return nil, err }
return s.status(idx)}
func (s *Submodule) status(idx *index.Index) (*SubmoduleStatus, error) { status := &SubmoduleStatus{ Path: s.c.Path, }
e, err := idx.Entry(s.c.Path) if err != nil && err != index.ErrEntryNotFound { return nil, err }
if e != nil { status.Expected = e.Hash }
if !s.initialized { return status, nil }
r, err := s.Repository() if err != nil { return nil, err }
head, err := r.Head() if err == nil { status.Current = head.Hash() }
if err != nil && err == plumbing.ErrReferenceNotFound { err = nil }
return status, err}
// Repository returns the Repository represented by this submodulefunc (s *Submodule) Repository() (*Repository, error) { if !s.initialized { return nil, ErrSubmoduleNotInitialized }
storer, err := s.w.r.Storer.Module(s.c.Name) if err != nil { return nil, err }
_, err = storer.Reference(plumbing.HEAD) if err != nil && err != plumbing.ErrReferenceNotFound { return nil, err }
var exists bool if err == nil { exists = true }
var worktree billy.Filesystem if worktree, err = s.w.Filesystem.Chroot(s.c.Path); err != nil { return nil, err }
if exists { return Open(storer, worktree) }
r, err := Init(storer, worktree) if err != nil { return nil, err }
moduleEndpoint, err := transport.NewEndpoint(s.c.URL) if err != nil { return nil, err }
if !path.IsAbs(moduleEndpoint.Path) && moduleEndpoint.Protocol == "file" { remotes, err := s.w.r.Remotes() if err != nil { return nil, err }
rootEndpoint, err := transport.NewEndpoint(remotes[0].c.URLs[0]) if err != nil { return nil, err }
rootEndpoint.Path = path.Join(rootEndpoint.Path, moduleEndpoint.Path) *moduleEndpoint = *rootEndpoint }
_, err = r.CreateRemote(&config.RemoteConfig{ Name: DefaultRemoteName, URLs: []string{moduleEndpoint.String()}, })
return r, err}
// Update the registered submodule to match what the superproject expects, the// submodule should be initialized first calling the Init method or setting in// the options SubmoduleUpdateOptions.Init equals truefunc (s *Submodule) Update(o *SubmoduleUpdateOptions) error { return s.UpdateContext(context.Background(), o)}
// UpdateContext the registered submodule to match what the superproject// expects, the submodule should be initialized first calling the Init method or// setting in the options SubmoduleUpdateOptions.Init equals true.//// The provided Context must be non-nil. If the context expires before the// operation is complete, an error is returned. The context only affects the// transport operations.func (s *Submodule) UpdateContext(ctx context.Context, o *SubmoduleUpdateOptions) error { return s.update(ctx, o, plumbing.ZeroHash)}
func (s *Submodule) update(ctx context.Context, o *SubmoduleUpdateOptions, forceHash plumbing.Hash) error { if !s.initialized && !o.Init { return ErrSubmoduleNotInitialized }
if !s.initialized && o.Init { if err := s.Init(); err != nil { return err } }
idx, err := s.w.r.Storer.Index() if err != nil { return err }
hash := forceHash if hash.IsZero() { e, err := idx.Entry(s.c.Path) if err != nil { return err }
hash = e.Hash }
r, err := s.Repository() if err != nil { return err }
if err := s.fetchAndCheckout(ctx, r, o, hash); err != nil { return err }
return s.doRecursiveUpdate(ctx, r, o)}
func (s *Submodule) doRecursiveUpdate(ctx context.Context, r *Repository, o *SubmoduleUpdateOptions) error { if o.RecurseSubmodules == NoRecurseSubmodules { return nil }
w, err := r.Worktree() if err != nil { return err }
l, err := w.Submodules() if err != nil { return err }
new := &SubmoduleUpdateOptions{} *new = *o
new.RecurseSubmodules-- return l.UpdateContext(ctx, new)}
func (s *Submodule) fetchAndCheckout( ctx context.Context, r *Repository, o *SubmoduleUpdateOptions, hash plumbing.Hash,) error { if !o.NoFetch { err := r.FetchContext(ctx, &FetchOptions{Auth: o.Auth, Depth: o.Depth}) if err != nil && err != NoErrAlreadyUpToDate { return err } }
w, err := r.Worktree() if err != nil { return err }
// Handle a case when submodule refers to an orphaned commit that's still reachable // through Git server using a special protocol capability[1]. // // [1]: https://git-scm.com/docs/protocol-capabilities#_allow_reachable_sha1_in_want if !o.NoFetch { if _, err := w.r.Object(plumbing.AnyObject, hash); err != nil { refSpec := config.RefSpec("+" + hash.String() + ":" + hash.String())
err := r.FetchContext(ctx, &FetchOptions{ Auth: o.Auth, RefSpecs: []config.RefSpec{refSpec}, Depth: o.Depth, }) if err != nil && err != NoErrAlreadyUpToDate && err != ErrExactSHA1NotSupported { return err } } }
if err := w.Checkout(&CheckoutOptions{Hash: hash}); err != nil { return err }
head := plumbing.NewHashReference(plumbing.HEAD, hash) return r.Storer.SetReference(head)}
// Submodules list of several submodules from the same repository.type Submodules []*Submodule
// Init initializes the submodules in this list.func (s Submodules) Init() error { for _, sub := range s { if err := sub.Init(); err != nil { return err } }
return nil}
// Update updates all the submodules in this list.func (s Submodules) Update(o *SubmoduleUpdateOptions) error { return s.UpdateContext(context.Background(), o)}
// UpdateContext updates all the submodules in this list.//// The provided Context must be non-nil. If the context expires before the// operation is complete, an error is returned. The context only affects the// transport operations.func (s Submodules) UpdateContext(ctx context.Context, o *SubmoduleUpdateOptions) error { for _, sub := range s { if err := sub.UpdateContext(ctx, o); err != nil { return err } }
return nil}
// Status returns the status of the submodules.func (s Submodules) Status() (SubmodulesStatus, error) { var list SubmodulesStatus
var r *Repository for _, sub := range s { if r == nil { r = sub.w.r }
idx, err := r.Storer.Index() if err != nil { return nil, err }
status, err := sub.status(idx) if err != nil { return nil, err }
list = append(list, status) }
return list, nil}
// SubmodulesStatus contains the status for all submodiles in the worktreetype SubmodulesStatus []*SubmoduleStatus
// String is equivalent to `git submodule status`func (s SubmodulesStatus) String() string { buf := bytes.NewBuffer(nil) for _, sub := range s { fmt.Fprintln(buf, sub) }
return buf.String()}
// SubmoduleStatus contains the status for a submodule in the worktreetype SubmoduleStatus struct { Path string Current plumbing.Hash Expected plumbing.Hash Branch plumbing.ReferenceName}
// IsClean is the HEAD of the submodule is equals to the expected commitfunc (s *SubmoduleStatus) IsClean() bool { return s.Current == s.Expected}
// String is equivalent to `git submodule status <submodule>`//// This will print the SHA-1 of the currently checked out commit for a// submodule, along with the submodule path and the output of git describe fo// the SHA-1. Each SHA-1 will be prefixed with - if the submodule is not// initialized, + if the currently checked out submodule commit does not match// the SHA-1 found in the index of the containing repository.func (s *SubmoduleStatus) String() string { var extra string var status = ' '
if s.Current.IsZero() { status = '-' } else if !s.IsClean() { status = '+' }
if len(s.Branch) != 0 { extra = string(s.Branch[5:]) } else if !s.Current.IsZero() { extra = s.Current.String()[:7] }
if extra != "" { extra = fmt.Sprintf(" (%s)", extra) }
return fmt.Sprintf("%c%s %s%s", status, s.Expected, s.Path, extra)}