From 0ce7010b7f10872bba2982f744d5abce894db51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20=F0=9F=91=A8=F0=9F=8F=BD=E2=80=8D=F0=9F=92=BB=20Copl?= =?UTF-8?q?an?= Date: Tue, 25 Apr 2023 12:10:49 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(clone):=20set=20publicheads=20to=20re?= =?UTF-8?q?mote=20HEAD=20when=20cloning=20(#607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ✨(clone): set publicheads to remote HEAD when cloning c8b66c68991066b3e added support for cloning the remote git repo's HEAD (in this case `develop`), instead of main/master by default. However, this doesn't mark `remote/develop` and its ancestors as public commits. This commit will also set remote git repo's HEAD as a public commit Closes https://github.com/facebook/sapling/issues/600 Pull Request resolved: https://github.com/facebook/sapling/issues/607 Pull Request resolved: https://github.com/facebook/sapling/pull/607 Test Plan: - Added test `test-git-clone-sets-publicheads.t` Reviewed By: sggutier Differential Revision: D45249747 Pulled By: zzl0 fbshipit-source-id: 8c3a36bd21a25a856d13cc3b2c78f9cbdd9a94c5 --- eden/scm/edenscm/git.py | 21 +++++++++++++- .../tests/test-git-clone-sets-publicheads.t | 29 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 eden/scm/tests/test-git-clone-sets-publicheads.t diff --git a/eden/scm/edenscm/git.py b/eden/scm/edenscm/git.py index 917513fb01cea..0a995c98e894f 100644 --- a/eden/scm/edenscm/git.py +++ b/eden/scm/edenscm/git.py @@ -21,7 +21,8 @@ import bindings from edenscm import tracing -from . import bookmarks as bookmod, error, identity, progress, util +from . import bookmarks as bookmod, error, identity, progress, rcutil, util + from .i18n import _ from .node import bin, hex, nullid @@ -154,6 +155,8 @@ def clone(ui, url, destpath=None, update=True, pullnames=None): # fall back to the using the names in the config. pullnames = bookmod.selectivepullbookmarknames(repo) + update_publicheads(repo, pullnames) + # Make sure we pull "update". If it looks like a hash, add to # "nodes", otherwise to "names". nodes = [] @@ -183,6 +186,15 @@ def clone(ui, url, destpath=None, update=True, pullnames=None): return repo +def update_publicheads(repo, pullnames): + default_publicheads = repo.ui.configlist( + "remotenames", "publicheads" + ) # ['remote/master', 'remote/main'] + remote_publicheads = ["remote/" + path for path in pullnames] + all_publicheads = ",".join(sorted(set(default_publicheads + remote_publicheads))) + update_and_persist_config(repo, "remotenames", "publicheads", all_publicheads) + + def parse_symref_head(symref_head_output: str) -> Optional[str]: r""" Args: @@ -342,6 +354,13 @@ def readconfig(repo): return config +def update_and_persist_config(repo, section, name, value): + """edit config and save it to the repo's config file""" + configfilename = repo.ui.identity.configrepofile() + configfilepath = repo.localvfs.join(configfilename) + rcutil.editconfig(repo.ui, configfilepath, section, name, value) + + @dataclass class RefName: """simple reference name handling for git diff --git a/eden/scm/tests/test-git-clone-sets-publicheads.t b/eden/scm/tests/test-git-clone-sets-publicheads.t new file mode 100644 index 0000000000000..375d41a3dfff6 --- /dev/null +++ b/eden/scm/tests/test-git-clone-sets-publicheads.t @@ -0,0 +1,29 @@ +#debugruntest-compatible +#require git no-windows + + $ . $TESTDIR/git.sh + +Prepare a git repo: + + $ git init -q gitrepo + $ cd gitrepo + $ git config core.autocrlf false + $ echo 1 > alpha + $ git add alpha + $ git commit -q -malpha + + $ git switch -c foo + Switched to a new branch 'foo' + + $ echo 2 > beta + $ git add beta + $ git commit -q -mbeta + +Test git clone sets publicheads + $ hg clone --git "$TESTTMP/gitrepo" cloned + From $TESTTMP/gitrepo + * [new ref] 3f5848713286c67b8a71a450e98c7fa66787bde2 -> remote/foo + 2 files updated, 0 files merged, 0 files removed, 0 files unresolved + $ cd cloned + $ hg config remotenames.publicheads + remote/foo,remote/main,remote/master