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

Method clone

repository.go:885–999  ·  view source on GitHub ↗

Clone clones a remote repository

(ctx context.Context, o *CloneOptions)

Source from the content-addressed store, hash-verified

883
884// Clone clones a remote repository
885func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
886 if err := o.Validate(); err != nil {
887 return err
888 }
889
890 c := &config.RemoteConfig{
891 Name: o.RemoteName,
892 URLs: []string{o.URL},
893 Fetch: r.cloneRefSpec(o),
894 Mirror: o.Mirror,
895 }
896
897 if _, err := r.CreateRemote(c); err != nil {
898 return err
899 }
900
901 // When the repository to clone is on the local machine,
902 // instead of using hard links, automatically setup .git/objects/info/alternates
903 // to share the objects with the source repository
904 if o.Shared {
905 if !url.IsLocalEndpoint(o.URL) {
906 return ErrAlternatePathNotSupported
907 }
908 altpath := o.URL
909 remoteRepo, err := PlainOpen(o.URL)
910 if err != nil {
911 return fmt.Errorf("failed to open remote repository: %w", err)
912 }
913 conf, err := remoteRepo.Config()
914 if err != nil {
915 return fmt.Errorf("failed to read remote repository configuration: %w", err)
916 }
917 if !conf.Core.IsBare {
918 altpath = path.Join(altpath, GitDirName)
919 }
920 if err := r.Storer.AddAlternate(altpath); err != nil {
921 return fmt.Errorf("failed to add alternate file to git objects dir: %w", err)
922 }
923 }
924
925 ref, err := r.fetchAndUpdateReferences(ctx, &FetchOptions{
926 RefSpecs: c.Fetch,
927 Depth: o.Depth,
928 Auth: o.Auth,
929 Progress: o.Progress,
930 Tags: o.Tags,
931 RemoteName: o.RemoteName,
932 InsecureSkipTLS: o.InsecureSkipTLS,
933 CABundle: o.CABundle,
934 ProxyOptions: o.ProxyOptions,
935 }, o.ReferenceName)
936 if err != nil {
937 return err
938 }
939
940 if r.wt != nil && !o.NoCheckout {
941 w, err := r.Worktree()
942 if err != nil {

Calls 15

cloneRefSpecMethod · 0.95
CreateRemoteMethod · 0.95
WorktreeMethod · 0.95
HeadMethod · 0.95
CreateBranchMethod · 0.95
IsLocalEndpointFunction · 0.92
PlainOpenFunction · 0.85
JoinMethod · 0.80
updateSubmodulesMethod · 0.80
IsBranchMethod · 0.80

Tested by 15

TestCloneDeepMethod · 0.64
TestCloneConfigMethod · 0.64
TestCloneSingleBranchMethod · 0.64
TestCloneSingleTagMethod · 0.64
TestCloneDetachedHEADMethod · 0.64
TestLogMethod · 0.64
TestLogAllMethod · 0.64