git: support clone --git

Summary:
Previously `clone` has to use git URLs with `git+` prefix in scheme such as
`git+ssh://...`, `git+file://...`, `git+https://...`. That was the same
behavior as the `hg-git` extension but is inconvenient for scp-like URLs
like `git@github.com:git/git.git` (provided by GitHub). That URL needs to be
translated to ssh form `git+ssh://git@github.com/git/git.git` manually,
which can be annoying.

Let's use a `--git` flag so `clone --git` can use the scp-like URL directly.
The `--git` flag also makes `clone` more consistent with `init`.

Reviewed By: DurhamG

Differential Revision: D33837782

fbshipit-source-id: 7fe11be6cf63932bde1f400cdfc2fc8120f5067a
This commit is contained in:
Jun Wu 2022-02-01 16:12:25 -08:00 committed by Facebook GitHub Bot
parent f88bf230ca
commit 0c883afe41
8 changed files with 44 additions and 33 deletions

View File

@ -293,7 +293,10 @@ def wrappackers():
def cloneshallow(orig, ui, source, *args, **opts):
# skip for (full) git repos
giturl = git.maybegiturl(source)
if opts.get("git"):
giturl = source
else:
giturl = git.maybegiturl(source)
if opts.get("shallow") and giturl is None:
repos = []

View File

@ -1510,6 +1510,7 @@ def cat(ui, repo, file1, *pats, **opts):
True,
_("use remotefilelog (only turn it off in legacy tests) (ADVANCED)"),
),
("", "git", False, _("use git protocol (EXPERIMENTAL)")),
]
+ remoteopts,
_("[OPTION]... SOURCE [DEST]"),
@ -1618,7 +1619,10 @@ def clone(ui, source, dest=None, **opts):
"""
if opts.get("noupdate") and opts.get("updaterev"):
raise error.Abort(_("cannot specify both --noupdate and --updaterev"))
giturl = git.maybegiturl(source)
if opts.get("git"):
giturl = source
else:
giturl = git.maybegiturl(source)
if giturl is not None:
if opts.get("noupdate"):
update = False

View File

@ -2030,7 +2030,11 @@ class path(object):
This is its own function so that extensions can change the definition of
'valid' in this case (like when pulling from a git repo into a hg
one)."""
return os.path.isdir(os.path.join(path, ".hg"))
# objects/: potentially a bare git repo
return any(
os.path.isdir(os.path.join(path, name))
for name in (".hg", ".git", "objects")
)
@property
def suboptions(self):

View File

@ -398,7 +398,7 @@ Show all commands + options
branch: force, clean, new
bundle: force, rev, base, all, type, ssh, remotecmd
cat: output, rev, decode, include, exclude, template
clone: noupdate, updaterev, rev, pull, uncompressed, stream, shallow, ssh, remotecmd
clone: noupdate, updaterev, rev, pull, uncompressed, stream, shallow, git, ssh, remotecmd
commit: addremove, amend, edit, interactive, reuse-message, include, exclude, message, logfile, date, user
config: untrusted, edit, local, global, template
continue:

View File

@ -19,7 +19,7 @@ Server repo
Client repo
$ cd
$ hg clone -q git+file://$TESTTMP/server-repo/.hg/store/git client-repo
$ hg clone -q --git "$TESTTMP/server-repo/.hg/store/git" client-repo
$ cd client-repo
$ hg log -Gr: -T '{desc} {remotenames} {phase}'
@ B remote/main public

View File

@ -44,8 +44,8 @@ Prepare git repo with submodules
Clone the git repo with submodules
$ cd
$ hg clone git+file://$TESTTMP/parent-repo-git parent-repo-hg
From file:/*/$TESTTMP/parent-repo-git (glob)
$ hg clone --git "$TESTTMP/parent-repo-git" parent-repo-hg
From $TESTTMP/parent-repo-git
* [new ref] * -> remote/main (glob)
pulling submodule mod/1
pulling submodule mod/2
@ -102,7 +102,7 @@ Make changes to submodules via working copy
M mod/1
R mod/2
$ hg clone -q git+file://$TESTTMP/sub1/.hg/store/git mod/3
$ hg clone -q --git "$TESTTMP/sub1/.hg/store/git" mod/3
$ hg status
M .gitmodules
M mod/1
@ -181,11 +181,11 @@ Diff committed changes
Try checking out the submodule change made by hg
$ cd
$ hg clone -qU git+file://$TESTTMP/parent-repo-git parent-repo-hg2
$ hg clone -qU --git "$TESTTMP/parent-repo-git" parent-repo-hg2
$ cd parent-repo-hg2
$ hg pull -B foo --update
pulling from file:/*/$TESTTMP/parent-repo-git (glob)
From file:/*/$TESTTMP/parent-repo-git (glob)
pulling from $TESTTMP/parent-repo-git
From $TESTTMP/parent-repo-git
* [new ref] * -> remote/foo (glob)
pulling submodule mod/1
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
@ -207,8 +207,8 @@ Nested submodules can share submodules with same URLs
$ git commit -qm 'add .gitmodules'
$ cd
$ hg clone git+file://$TESTTMP/grandparent-repo-git grandparent-repo-hg
From file:/*/$TESTTMP/grandparent-repo-git (glob)
$ hg clone --git "$TESTTMP/grandparent-repo-git" grandparent-repo-hg
From $TESTTMP/grandparent-repo-git
* [new ref] * -> remote/main (glob)
pulling submodule mod/1
pulling submodule mod/p
@ -233,7 +233,7 @@ Rebase submodule change
$ git commit -qm A
$ cd
$ hg clone -q git+file://$TESTTMP/rebase-git rebase-hg
$ hg clone -q --git "$TESTTMP/rebase-git" rebase-hg
$ cd rebase-hg
$ touch B
$ hg commit -Aqm B B

View File

@ -22,7 +22,7 @@ Prepare git repo with tags
Clone it
$ cd
$ hg clone -q git+file://$TESTTMP/git-repo hg-repo
$ hg clone -q --git $TESTTMP/git-repo hg-repo
$ cd hg-repo
No remotenames about tags initially
@ -33,14 +33,14 @@ No remotenames about tags initially
Pull tags explicitly
$ hg pull -B tags/v1
pulling from file:/*/$TESTTMP/git-repo (glob)
From file:/*/$TESTTMP/git-repo (glob)
pulling from $TESTTMP/git-repo
From $TESTTMP/git-repo
* [new ref] bfff4215bb0ba84b76577621c9974de957610ecb -> remote/tags/v1
Pull implicitly via autopull
$ hg update tags/v2
pulling 'tags/v2' from 'file:/*/$TESTTMP/git-repo' (glob)
pulling 'tags/v2' from '$TESTTMP/git-repo'
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
Verify the pulled tags can be seen
@ -62,7 +62,7 @@ Push tags
$ echo 2 > a
$ hg commit -m C
$ hg push --to tags/v3
To file:/*/$TESTTMP/git-repo (glob)
To $TESTTMP/git-repo
* [new tag] 42d0e8258ed6249380f83aaf4564a0c0865ae5f7 -> v3
$ hg log -Gr: -T '{remotenames} {desc}'
@ remote/tags/v3 C

View File

@ -211,7 +211,7 @@ Test clone with flags (--noupdate, --updaterev):
$ mkdir $TESTTMP/clonetest
$ cd $TESTTMP/clonetest
$ hg clone -q --noupdate "git+file://$TESTTMP/gitrepo"
$ hg clone -q --noupdate --git "$TESTTMP/gitrepo"
$ cd gitrepo
$ hg log -r . -T '{node|short}\n'
000000000000
@ -219,8 +219,8 @@ Test clone with flags (--noupdate, --updaterev):
remote/master 3f5848713286
$ cd ..
$ hg clone "git+file://$TESTTMP/gitrepo" cloned1 --config remotenames.selectivepulldefault=foo,master
From file:/*/$TESTTMP/gitrepo (glob)
$ hg clone --git "$TESTTMP/gitrepo" cloned1 --config remotenames.selectivepulldefault=foo,master
From $TESTTMP/gitrepo
* [new ref] 4899b7b71a9c241a7c43171f525cc9d6fcabfd4f -> remote/foo
* [new ref] 3f5848713286c67b8a71a450e98c7fa66787bde2 -> remote/master
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
@ -228,8 +228,8 @@ Test clone with flags (--noupdate, --updaterev):
4899b7b71a9c remote/foo alpha3
$ cd ..
$ hg clone --updaterev remote/foo "git+file://$TESTTMP/gitrepo" cloned2 --config remotenames.selectivepulldefault=foo
From file:/*/$TESTTMP/gitrepo (glob)
$ hg clone --updaterev remote/foo --git "$TESTTMP/gitrepo" cloned2 --config remotenames.selectivepulldefault=foo
From $TESTTMP/gitrepo
* [new ref] 4899b7b71a9c241a7c43171f525cc9d6fcabfd4f -> remote/foo
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg --cwd cloned2 log -r . -T '{node|short} {remotenames} {desc}\n'
@ -247,7 +247,7 @@ Test push:
- --to with -r
$ hg push -r '.^' --to parent_change_beta
To file:/*/$TESTTMP/gitrepo (glob)
To $TESTTMP/gitrepo
* [new branch] 4899b7b71a9c241a7c43171f525cc9d6fcabfd4f -> parent_change_beta
$ hg log -r '.^+.' -T '{desc} {remotenames}\n'
@ -256,7 +256,7 @@ Test push:
- delete bookmark
$ hg push --delete book_change_beta
To file:/*/$TESTTMP/gitrepo (glob)
To $TESTTMP/gitrepo
- [deleted] book_change_beta
$ hg log -r '.^+.' -T '{desc} {remotenames}\n'
@ -270,7 +270,7 @@ Test push:
$ cd "$TESTTMP"
$ git init -qb main --bare "pushforce.git"
$ hg clone "git+file://$TESTTMP/pushforce.git"
$ hg clone --git "$TESTTMP/pushforce.git"
$ cd pushforce
$ git --git-dir=.hg/store/git config advice.pushUpdateRejected false
@ -282,9 +282,9 @@ Test push:
$ hg push -qr $B --to foo
$ hg push -qr $C --to foo
To file:/*/$TESTTMP/pushforce.git (glob)
To $TESTTMP/pushforce.git
! [rejected] 5d38a953d58b0c80a4416ba62e62d3f2985a3726 -> foo (non-fast-forward)
error: failed to push some refs to 'file:/*/$TESTTMP/pushforce.git' (glob)
error: failed to push some refs to '$TESTTMP/pushforce.git'
[1]
$ hg push -qr $C --to foo --force
@ -292,7 +292,7 @@ Test push:
$ cd "$TESTTMP"
$ git init -qb main --bare "pushto.git"
$ hg clone "git+file://$TESTTMP/pushto.git"
$ hg clone --git "$TESTTMP/pushto.git"
$ cd pushto
$ drawdag << 'EOS'
@ -308,7 +308,7 @@ Test push:
(pick "main" automatically)
$ hg push
To file:/*/$TESTTMP/pushto.git (glob)
To $TESTTMP/pushto.git
0de3093..a9d5bd6 a9d5bd6ac8bcf89de9cd99fd215cca243e8aeed9 -> main
$ hg push -q --to stable
@ -340,8 +340,8 @@ Submodule does not cause a crash:
$ cd
$ setconfig git.submodules=false
$ hg clone "git+file://$TESTTMP/submod" cloned-submod
From file:/*/$TESTTMP/submod (glob)
$ hg clone --git "$TESTTMP/submod" cloned-submod
From $TESTTMP/submod
* [new ref] a4c97159e197fb3aaab3f24fc3b39d7942b311ff -> remote/master
3 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ cd cloned-submod