| 918 | } |
| 919 | |
| 920 | func (c *Client) Clone(ctx context.Context, cloneURL string, args []string, mods ...CommandModifier) (string, error) { |
| 921 | // Note that even if this is an SSH clone URL, we are setting the pattern anyway. |
| 922 | // We could write some code to prevent this, but it also doesn't seem harmful. |
| 923 | pattern, err := CredentialPatternFromGitURL(cloneURL) |
| 924 | if err != nil { |
| 925 | return "", err |
| 926 | } |
| 927 | |
| 928 | cloneArgs, target := parseCloneArgs(args) |
| 929 | cloneArgs = append(cloneArgs, cloneURL) |
| 930 | // If the args contain an explicit target, pass it to clone otherwise, |
| 931 | // parse the URL to determine where git cloned it to so we can return it. |
| 932 | if target != "" { |
| 933 | cloneArgs = append(cloneArgs, target) |
| 934 | } else { |
| 935 | target = path.Base(strings.TrimSuffix(cloneURL, ".git")) |
| 936 | |
| 937 | if slices.Contains(cloneArgs, "--bare") { |
| 938 | target += ".git" |
| 939 | } |
| 940 | } |
| 941 | cloneArgs = append([]string{"clone"}, cloneArgs...) |
| 942 | cmd, err := c.AuthenticatedCommand(ctx, pattern, cloneArgs...) |
| 943 | if err != nil { |
| 944 | return "", err |
| 945 | } |
| 946 | for _, mod := range mods { |
| 947 | mod(cmd) |
| 948 | } |
| 949 | err = cmd.Run() |
| 950 | if err != nil { |
| 951 | return "", err |
| 952 | } |
| 953 | return target, nil |
| 954 | } |
| 955 | |
| 956 | func resolveGitPath() (string, error) { |
| 957 | path, err := safeexec.LookPath("git") |