Skip to content

Commit

Permalink
fix(git): Fix refetching of remotes
Browse files Browse the repository at this point in the history
Deprecates #352, Fixes #332
  • Loading branch information
tony committed Apr 3, 2022
1 parent ee7aa8f commit 7579912
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 78 deletions.
66 changes: 27 additions & 39 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
],
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
],
Expand All @@ -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)
Expand All @@ -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,
)
Expand Down
40 changes: 1 addition & 39 deletions vcspull/cli/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7579912

Please sign in to comment.