clone.go 3.5 KB
Newer Older
J
Jingwen Owen Ou 已提交
1 2 3
package commands

import (
4
	"fmt"
J
Jingwen Owen Ou 已提交
5 6
	"regexp"
	"strings"
7

8 9
	"github.com/github/hub/v2/github"
	"github.com/github/hub/v2/utils"
J
Jingwen Owen Ou 已提交
10 11 12 13 14
)

var cmdClone = &Command{
	Run:          clone,
	GitExtension: true,
15 16 17 18 19 20 21 22 23 24 25 26 27
	Usage:        "clone [-p] [<OPTIONS>] [<USER>/]<REPOSITORY> [<DESTINATION>]",
	Long: `Clone a repository from GitHub.

## Options:
	-p
		(Deprecated) Clone private repositories over SSH.

	[<USER>/]<REPOSITORY>
		<USER> defaults to your own GitHub username.

	<DESTINATION>
		Directory name to clone into (default: <REPOSITORY>).

28 29
## Protocol used for cloning

30
The ''git:'' protocol will be used for cloning public repositories, while the SSH
31
protocol will be used for private repositories and those that you have push
N
nikolas 已提交
32
access to. Alternatively, hub can be configured to use HTTPS protocol for
33 34
everything. See "HTTPS instead of git protocol" and "HUB_PROTOCOL" of hub(1).

35 36 37
## Examples:
		$ hub clone rtomayko/ronn
		> git clone git://github.com/rtomayko/ronn.git
M
Mislav Marohnić 已提交
38 39 40 41

## See also:

hub-fork(1), hub(1), git-clone(1)
42
`,
J
Jingwen Owen Ou 已提交
43 44
}

45 46 47 48
func init() {
	CmdRunner.Use(cmdClone)
}

J
Jingwen Owen Ou 已提交
49
func clone(command *Command, args *Args) {
J
Jingwen Owen Ou 已提交
50
	if !args.IsParamsEmpty() {
J
Jingwen Owen Ou 已提交
51 52 53 54 55 56
		transformCloneArgs(args)
	}
}

func transformCloneArgs(args *Args) {
	isSSH := parseClonePrivateFlag(args)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

	// git help clone | grep -e '^ \+-.\+<'
	p := utils.NewArgsParser()
	p.RegisterValue("--branch", "-b")
	p.RegisterValue("--depth")
	p.RegisterValue("--reference")
	if args.Command == "submodule" {
		p.RegisterValue("--name")
	} else {
		p.RegisterValue("--config", "-c")
		p.RegisterValue("--jobs", "-j")
		p.RegisterValue("--origin", "-o")
		p.RegisterValue("--reference-if-able")
		p.RegisterValue("--separate-git-dir")
		p.RegisterValue("--shallow-exclude")
		p.RegisterValue("--shallow-since")
		p.RegisterValue("--template")
		p.RegisterValue("--upload-pack", "-u")
	}
	p.Parse(args.Params)

J
Jingwen Owen Ou 已提交
78
	nameWithOwnerRegexp := regexp.MustCompile(NameWithOwnerRe)
79
	for _, i := range p.PositionalIndices {
J
Jingwen Owen Ou 已提交
80
		a := args.Params[i]
81
		if nameWithOwnerRegexp.MatchString(a) && !isCloneable(a) {
K
kfcampbell 已提交
82
			url := getCloneURL(a, isSSH, args.Command != "submodule")
83
			args.ReplaceParam(i, url)
J
Jingwen Owen Ou 已提交
84
		}
85
		break
J
Jingwen Owen Ou 已提交
86 87 88 89
	}
}

func parseClonePrivateFlag(args *Args) bool {
J
Jingwen Owen Ou 已提交
90 91
	if i := args.IndexOfParam("-p"); i != -1 {
		args.RemoveParam(i)
J
Jingwen Owen Ou 已提交
92 93 94 95 96
		return true
	}

	return false
}
97

K
kfcampbell 已提交
98
func getCloneURL(nameWithOwner string, isSSH, allowSSH bool) string {
99 100 101 102
	name := nameWithOwner
	owner := ""
	if strings.Contains(name, "/") {
		split := strings.SplitN(name, "/", 2)
103
		owner = split[0]
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		name = split[1]
	}

	var host *github.Host
	if owner == "" {
		config := github.CurrentConfig()
		h, err := config.DefaultHost()
		if err != nil {
			utils.Check(github.FormatError("cloning repository", err))
		}

		host = h
		owner = host.User
	}

	var hostStr string
	if host != nil {
		hostStr = host.Host
	}

	expectWiki := strings.HasSuffix(name, ".wiki")
	if expectWiki {
		name = strings.TrimSuffix(name, ".wiki")
	}

	project := github.NewProject(owner, name, hostStr)
	gh := github.NewClient(project.Host)
	repo, err := gh.Repository(project)
	if err != nil {
		if strings.Contains(err.Error(), "HTTP 404") {
			err = fmt.Errorf("Error: repository %s/%s doesn't exist", project.Owner, project.Name)
		}
		utils.Check(err)
	}

	owner = repo.Owner.Login
	name = repo.Name
	if expectWiki {
		if !repo.HasWiki {
			utils.Check(fmt.Errorf("Error: %s/%s doesn't have a wiki", owner, name))
		} else {
			name = name + ".wiki"
		}
	}

	if !isSSH &&
		allowSSH &&
151
		!github.IsHTTPSProtocol() {
152
		isSSH = repo.Private || repo.Permissions.Push
153 154
	}

155
	return project.GitURL(name, owner, isSSH)
156
}