Clone clones a remote repository
(ctx context.Context, o *CloneOptions)
| 883 | |
| 884 | // Clone clones a remote repository |
| 885 | func (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 { |