Skip to content

Commit

Permalink
query HEAD name, instead of assuming main/master (#386)
Browse files Browse the repository at this point in the history
Summary: query HEAD name, instead of assuming main/master

Test Plan:
 ---
Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/sapling/pull/386).
* __->__ #386

Pull Request resolved: #386

Fixes #375

Ran a number of affected integration tests:

```
./run-tests.py test-git.t test-git-submodule.t test-git-submodule-rebase.t test-rust-clone.t test-doctest.py
```

Using the example from #375,
cloned the repo and verified that it cloned the `trunk` branch:

```
$ lhg clone --git https://github.com/cli/cli
$ cd cli
$ lhg
@  179e9c256  Yesterday at 14:13  2159081+qoega  remote/trunk
│  Add projectsV2 support to issue create, issue edit, pr create, and pr edit (#6735)
│
o    06ae07f97  Yesterday at 07:25  mislav
├─╮  Merge pull request #6880 from cli/setdefault-bare-repo
│ │
o │    351226d34  Yesterday at 07:17  mislav
├───╮  Merge pull request #6881 from cli/reviewers-json-fix
│ │ │
o │ │    241f9197e  Wednesday at 21:15  rmw
├─────╮  Merge pull request #6815 from cli/intake-doc
...
```

Reviewed By: quark-zju

Differential Revision: D42597162

Pulled By: bolinfest

fbshipit-source-id: 2f4b3869f452b57290f2a7a69721ecdd4e444b2e
  • Loading branch information
discentem authored and facebook-github-bot committed Jan 21, 2023
1 parent cea7748 commit c8b66c6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
71 changes: 71 additions & 0 deletions eden/scm/edenscm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import errno
import hashlib
import os
import re
import shutil
import subprocess
import textwrap
Expand Down Expand Up @@ -128,6 +129,26 @@ def clone(ui, url, destpath=None, update=True, pullnames=None):
initgit(repo, "git", url)
if url:
if pullnames is None:
ls_remote_args = ["ls-remote", "--symref", url, "HEAD"]
symref_head_output = callgit(repo, ls_remote_args).decode("utf-8")
default_branch = parse_symref_head(symref_head_output)
if default_branch:
pullnames = [default_branch]
elif not symref_head_output:
# Empty string: may be empty repo?
pass
else:
ui.status_err(
_("could not parse output of '%s': %s")
% (
" ".join(ls_remote_args),
symref_head_output,
)
)

if pullnames is None:
# If `git ls-remote --symref <url> HEAD` failed to yield a name,
# fall back to the using the names in the config.
pullnames = bookmod.selectivepullbookmarknames(repo)

# Make sure we pull "update". If it looks like a hash, add to
Expand All @@ -143,6 +164,7 @@ def clone(ui, url, destpath=None, update=True, pullnames=None):
if not nodes:
pullnames.append(update)

pullnames = util.dedup(pullnames)
pull(repo, "default", names=pullnames, nodes=nodes)
except (Exception, KeyboardInterrupt):
repo = None
Expand All @@ -159,6 +181,55 @@ def clone(ui, url, destpath=None, update=True, pullnames=None):
return repo


def parse_symref_head(symref_head_output: str) -> Optional[str]:
r"""
Args:
symref_head_output - output of `ls-remote --symref <url> HEAD`
>>> sapling = (
... "ref: refs/heads/main\tHEAD\n"
... "f58888310501872447b1b2fa4a8789210a6c6252\tHEAD\n"
... )
>>> parse_symref_head(sapling)
'main'
>>> pytorch = (
... "ref: refs/heads/master\tHEAD\n"
... "8b3e35ea4aa210f48a92966e3347b78dfc6e9360\tHEAD\n"
... )
>>> parse_symref_head(pytorch)
'master'
>>> foobar = (
... "ref: refs/heads/foo/bar\tHEAD\n"
... "8b3e35ea4aa210f48a92966e3347b78dfc6e9360\tHEAD\n"
... )
>>> parse_symref_head(foobar)
'foo/bar'
>>> tag = (
... "ref: refs/tags/foo\tHEAD\n"
... "8b3e35ea4aa210f48a92966e3347b78dfc6e9360\tHEAD\n"
... )
>>> parse_symref_head(tag) is None
True
>>> gerrit_refs_for = (
... "ref: refs/for/master\tHEAD\n"
... "8b3e35ea4aa210f48a92966e3347b78dfc6e9360\tHEAD\n"
... )
>>> parse_symref_head(gerrit_refs_for) is None
True
>>> parse_symref_head('') is None
True
"""
pat = re.compile(r"^ref: ([^\t]*)\t")
match = pat.match(symref_head_output)
if match:
ref = match.group(1)
prefix = "refs/heads/"
if ref.startswith(prefix):
return ref[len(prefix) :]

return None


def initgit(repo, gitdir, giturl=None):
"""Change a repo to be backed by a bare git repo in `gitdir`.
This should only be called for newly created repos.
Expand Down
1 change: 1 addition & 0 deletions eden/scm/tests/test-doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def testmod(name, optionflags=0, testtarget=None):
testmod("edenscm.drawdag")
testmod("edenscm.encoding")
testmod("edenscm.formatter")
testmod("edenscm.git")
testmod("edenscm.gituser")
testmod("edenscm.hg")
testmod("edenscm.match")
Expand Down
4 changes: 2 additions & 2 deletions eden/scm/tests/test-git.t
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ Test clone with flags (--noupdate, --updaterev):
remote/master 3f5848713286
$ cd ..

$ hg clone --git "$TESTTMP/gitrepo" cloned1 --config remotenames.selectivepulldefault=foo,master
$ hg clone --git "$TESTTMP/gitrepo" -u foo cloned1
From $TESTTMP/gitrepo
* [new ref] 57eda5013e068ac543a52ad073cec3d7750113b5 -> remote/foo
* [new ref] 3f5848713286c67b8a71a450e98c7fa66787bde2 -> remote/master
* [new ref] 57eda5013e068ac543a52ad073cec3d7750113b5 -> remote/foo
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg --cwd cloned1 log -r . -T '{node|short} {remotenames} {desc}\n'
57eda5013e06 remote/foo alpha3
Expand Down

0 comments on commit c8b66c6

Please sign in to comment.