Skip to content

Commit

Permalink
fix(git): Fix refetching of remotes (#365)
Browse files Browse the repository at this point in the history
Closes #352, Fixes #332
  • Loading branch information
tony authored Apr 3, 2022
2 parents ee7aa8f + 49c1b52 commit 7b0cfd8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 96 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ vcspull = 'vcspull:cli.cli'
python = "^3.9"
click = ">=7<8.1"
kaptan = "*"
libvcs = "~0.12.0b10"
libvcs = "~0.12.0b11"
colorama = ">=0.3.9"

[tool.poetry.dev-dependencies]
Expand Down
90 changes: 38 additions & 52 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 @@ -158,65 +156,53 @@ def test_updating_remote(
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)

base_config = {
initial_config = {
"name": "myclone",
"repo_dir": f"{tmp_path}/study/myrepo/myclone",
"parent_dir": f"{tmp_path}/study/myrepo",
"url": f"git+file://{dummy_repo}",
"remotes": {
"secondremote": GitRemote(
name="secondremote",
fetch_url=f"git+file://{dummy_repo}",
push_url=f"git+file://{dummy_repo}",
)
mirror_name: {
"name": mirror_name,
"fetch_url": f"git+file://{dummy_repo}",
"push_url": f"git+file://{dummy_repo}",
}
},
}

def merge_dict(_dict, extra):
_dict = _dict.copy()
_dict.update(**extra)
return _dict

configs = [base_config]

for repo_dict in filter_repos(
configs,
[initial_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]

repo_dict = filter_repos(configs, 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
),
None,
local_git_remotes = update_repo(repo_dict).remotes()
assert "origin" in local_git_remotes

expected_remote_url = f"git+file://{mirror_repo}"

config = initial_config | {
"remotes": {
mirror_name: GitRemote(
name=mirror_name,
fetch_url=expected_remote_url,
push_url=expected_remote_url,
)
if remote_name != "origin"
else config["url"]
).replace("git+", "")
assert config_remote_url == current_remote_url
}
}

repo_dict = filter_repos([config], name="myclone")[0]
repo = update_repo(repo_dict)
for remote_name, remote_info in repo.remotes().items():
current_remote_url = repo.remote(remote_name).fetch_url.replace("git+", "")
if remote_name in config["remotes"]:
assert (
config["remotes"][remote_name].fetch_url.replace("git+", "")
== current_remote_url
)

elif remote_name == "origin":
assert config["url"].replace("git+", "") == current_remote_url
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 7b0cfd8

Please sign in to comment.