From 757991253219f1ad4b395e2aebf6e336336b3260 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 3 Apr 2022 14:06:38 -0500 Subject: [PATCH] fix(git): Fix refetching of remotes Deprecates #352, Fixes #332 --- tests/test_sync.py | 66 +++++++++++++++++++-------------------------- vcspull/cli/sync.py | 40 +-------------------------- 2 files changed, 28 insertions(+), 78 deletions(-) diff --git a/tests/test_sync.py b/tests/test_sync.py index 4c65f407..ee044b2d 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -71,7 +71,7 @@ def write_config_remote( {CLONE_NAME}: repo: git+file://{repo_dir} remotes: - secondremote: git+file://{repo_dir} + secondremote: git+file://{repo_dir} """, ["secondremote"], ], @@ -109,8 +109,6 @@ def test_config_variations( assert set(remote_list).issubset(remote_names) or {"origin"}.issubset( remote_names ) - captured = capsys.readouterr() - assert f"Updating remote {list(remote_names)[0]}" in captured.out for remote_name, remote_info in remotes.items(): current_remote = repo.remote(remote_name) @@ -141,7 +139,7 @@ def test_config_variations( {CLONE_NAME}: repo: git+file://{repo_dir} remotes: - secondremote: git+file://{repo_dir} + mirror_repo: git+file://{repo_dir} """, True, ], @@ -155,8 +153,8 @@ def test_updating_remote( ): """Ensure additions/changes to yaml config are reflected""" - dummy_repo_name = "dummy_repo" - dummy_repo = create_git_dummy_repo(dummy_repo_name) + mirror_name = "mirror_repo" + mirror_repo = create_git_dummy_repo(mirror_name) repo_parent = tmp_path / "study" / "myrepo" repo_parent.mkdir(parents=True) @@ -165,54 +163,44 @@ def test_updating_remote( "name": "myclone", "repo_dir": f"{tmp_path}/study/myrepo/myclone", "parent_dir": f"{tmp_path}/study/myrepo", - "url": f"git+file://{dummy_repo}", + "url": f"git+file://{mirror_repo}", "remotes": { - "secondremote": GitRemote( - name="secondremote", - fetch_url=f"git+file://{dummy_repo}", - push_url=f"git+file://{dummy_repo}", - ) + mirror_name: { + "fetch_url": f"git+file://{mirror_repo}", + "push_url": f"git+file://{mirror_repo}", + } }, } - def merge_dict(_dict, extra): - _dict = _dict.copy() - _dict.update(**extra) - return _dict - - configs = [base_config] - for repo_dict in filter_repos( - configs, + [base_config], ): update_repo(repo_dict).remotes()["origin"] - expected_remote_url = f"git+file://{dummy_repo}/moo" - - config = merge_dict( - base_config, - extra={ - "remotes": { - "secondremote": GitRemote( - name="secondremote", - fetch_url=expected_remote_url, - push_url=expected_remote_url, - ) - } - }, - ) - configs = [config] + expected_remote_url = f"git+file://{mirror_repo}" + + config = base_config | { + "remotes": { + "mirror_repo": GitRemote( + name="mirror_repo", + fetch_url=expected_remote_url, + push_url=expected_remote_url, + ) + } + } - repo_dict = filter_repos(configs, name="myclone")[0] + repo_dict = filter_repos([config], name="myclone")[0] r = update_repo(repo_dict) for remote_name, remote_info in r.remotes().items(): current_remote_url = r.remote(remote_name).fetch_url.replace("git+", "") config_remote_url = ( next( ( - r.fetch_url - for rname, r in config["remotes"].items() - if rname == remote_name + test_git_remote.fetch_url + for test_git_remote_name, test_git_remote in config[ + "remotes" + ].items() + if test_git_remote_name == remote_name ), None, ) diff --git a/vcspull/cli/sync.py b/vcspull/cli/sync.py index 4469ad6c..b7268dd9 100644 --- a/vcspull/cli/sync.py +++ b/vcspull/cli/sync.py @@ -123,44 +123,6 @@ def update_repo(repo_dict): repo_dict["progress_callback"] = progress_cb r = create_repo_from_pip_url(**repo_dict) # Creates the repo object + r.update_repo(set_remotes=True) # Creates repo if not exists and fetches - remote_settings = repo_dict.get("remotes", {}) - if remote_settings.get("origin", {}) == {}: - from libvcs.git import GitRemote - - remote_settings["origin"] = GitRemote( - name="origin", - push_url=repo_dict["pip_url"], - fetch_url=repo_dict["pip_url"], - ) - - remotes_updated = False - r.update_repo() # Creates repo if not exists and fetches - - for remote_name, remote_setting in remote_settings.items(): - config_remote_name = remote_name # From config file - try: - current_remote = r.remote(config_remote_name) - except FileNotFoundError: # git repo doesn't exist yet, so cna't be outdated - break - - current_fetch_url = ( - current_remote.fetch_url if current_remote is not None else None - ) - - if current_remote is None or current_fetch_url != remote_setting.fetch_url: - print( - "Updating remote {name} ({current_url}) with {new_url}".format( - name=config_remote_name, - current_url=current_fetch_url, - new_url=remote_setting.fetch_url, - ) - ) - r.set_remote( - name=config_remote_name, url=remote_setting.fetch_url, overwrite=True - ) - remotes_updated = True - - if remotes_updated: # Fetch again since we added / changed remotes - r.update_repo() return r