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