Skip to content

Commit 1045626

Browse files
committed
cli/add(fix[duplicate-check]): Handle both config formats when checking duplicates
why: The duplicate repo check was showing the raw dict structure {'repo': 'url'} instead of just the URL when checking verbose format configs, causing test failures. what: - Extract URL from both string and dict config formats - Check for both 'repo' and 'url' keys in dict configs - Display clean URL in warning messages - Fix line length issues flagged by ruff - Fix mypy type error with proper type annotation This ensures consistent user experience regardless of whether existing configs use simple string format or verbose dict format.
1 parent 7803660 commit 1045626

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

src/vcspull/cli/add.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,27 @@ def add_repo(
142142

143143
# Check if repo already exists
144144
if name in raw_config[base_dir_key]:
145+
existing_config = raw_config[base_dir_key][name]
146+
# Handle both string and dict formats
147+
current_url: str
148+
if isinstance(existing_config, str):
149+
current_url = existing_config
150+
elif isinstance(existing_config, dict):
151+
repo_value = existing_config.get("repo")
152+
url_value = existing_config.get("url")
153+
current_url = repo_value or url_value or "unknown"
154+
else:
155+
current_url = str(existing_config)
156+
145157
log.warning(
146158
f"Repository '{name}' already exists under '{base_dir_key}'. "
147-
f"Current URL: {raw_config[base_dir_key][name]}. "
159+
f"Current URL: {current_url}. "
148160
f"To update, remove and re-add, or edit the YAML file manually.",
149161
)
150162
return
151163

152164
# Add the repository in verbose format
153-
raw_config[base_dir_key][name] = {
154-
"repo": url
155-
}
165+
raw_config[base_dir_key][name] = {"repo": url}
156166

157167
# Save config
158168
try:

src/vcspull/cli/add_from_fs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ def add_from_filesystem(
255255
)
256256
for name, url, key in existing_repos:
257257
log.info(
258-
f" {Fore.BLUE}{Style.RESET_ALL} {Fore.CYAN}{name}{Style.RESET_ALL} "
258+
f" {Fore.BLUE}{Style.RESET_ALL} "
259+
f"{Fore.CYAN}{name}{Style.RESET_ALL} "
259260
f"({Fore.YELLOW}{url}{Style.RESET_ALL}) at "
260261
f"{Fore.MAGENTA}{key}{name}{Style.RESET_ALL} "
261262
f"in {Fore.BLUE}{config_file_path}{Style.RESET_ALL}"
@@ -272,9 +273,10 @@ def add_from_filesystem(
272273
# Show what will be added
273274
log.info(
274275
f"\n{Fore.GREEN}Found {len(repos_to_add)} new "
275-
f"{'repository' if len(repos_to_add) == 1 else 'repositories'} to add:{Style.RESET_ALL}"
276+
f"{'repository' if len(repos_to_add) == 1 else 'repositories'} "
277+
f"to add:{Style.RESET_ALL}"
276278
)
277-
for repo_name, repo_url, determined_base_key in repos_to_add:
279+
for repo_name, repo_url, _determined_base_key in repos_to_add:
278280
log.info(
279281
f" {Fore.GREEN}+{Style.RESET_ALL} {Fore.CYAN}{repo_name}{Style.RESET_ALL} "
280282
f"({Fore.YELLOW}{repo_url}{Style.RESET_ALL})"
@@ -300,9 +302,7 @@ def add_from_filesystem(
300302
continue
301303

302304
if repo_name not in raw_config[determined_base_key]:
303-
raw_config[determined_base_key][repo_name] = {
304-
"repo": repo_url
305-
}
305+
raw_config[determined_base_key][repo_name] = {"repo": repo_url}
306306
log.info(
307307
f"{Fore.GREEN}+{Style.RESET_ALL} Adding "
308308
f"{Fore.CYAN}'{repo_name}'{Style.RESET_ALL} "

tests/cli/test_add.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pathlib
66
import typing as t
77

8-
import pytest
98
import yaml
109

1110
from vcspull.cli.add import add_repo
@@ -26,7 +25,7 @@ def test_add_simple_repo(
2625
caplog.set_level("INFO")
2726

2827
config_file = tmp_path / ".vcspull.yaml"
29-
28+
3029
# Add a repository
3130
add_repo(
3231
name="myproject",
@@ -60,7 +59,7 @@ def test_add_with_custom_base_dir(
6059
caplog.set_level("INFO")
6160

6261
config_file = tmp_path / ".vcspull.yaml"
63-
62+
6463
# Add a repository with custom base dir
6564
add_repo(
6665
name="mylib",
@@ -88,14 +87,10 @@ def test_add_duplicate_repo(
8887
caplog.set_level("WARNING")
8988

9089
config_file = tmp_path / ".vcspull.yaml"
91-
90+
9291
# Pre-create config with existing repo
9392
existing_config = {
94-
"~/code/": {
95-
"existing": {
96-
"repo": "git@github.com:user/existing.git"
97-
}
98-
}
93+
"~/code/": {"existing": {"repo": "git@github.com:user/existing.git"}}
9994
}
10095
with config_file.open("w") as f:
10196
yaml.dump(existing_config, f)
@@ -116,7 +111,10 @@ def test_add_duplicate_repo(
116111
# Config should not be changed
117112
with config_file.open() as f:
118113
config_data = yaml.safe_load(f)
119-
assert config_data["~/code/"]["existing"]["repo"] == "git@github.com:user/existing.git"
114+
assert (
115+
config_data["~/code/"]["existing"]["repo"]
116+
== "git@github.com:user/existing.git"
117+
)
120118

121119
def test_add_to_existing_config(
122120
self,
@@ -127,14 +125,10 @@ def test_add_to_existing_config(
127125
caplog.set_level("INFO")
128126

129127
config_file = tmp_path / ".vcspull.yaml"
130-
128+
131129
# Pre-create config with some repos
132130
existing_config = {
133-
"~/work/": {
134-
"project1": {
135-
"repo": "git@github.com:user/project1.git"
136-
}
137-
}
131+
"~/work/": {"project1": {"repo": "git@github.com:user/project1.git"}}
138132
}
139133
with config_file.open("w") as f:
140134
yaml.dump(existing_config, f)
@@ -156,4 +150,4 @@ def test_add_to_existing_config(
156150
assert "project2" in config_data["~/work/"]
157151
assert config_data["~/work/"]["project2"] == {
158152
"repo": "git@github.com:user/project2.git"
159-
}
153+
}

tests/cli/test_add_from_fs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def test_many_existing_repos_summary(
642642
# Should NOT list individual repos
643643
assert "• existing0" not in caplog.text
644644
assert "• existing7" not in caplog.text
645-
645+
646646
# Verify new repo is shown clearly
647647
assert "Found 1 new repository to add:" in caplog.text
648648
assert "+ new_repo" in caplog.text

0 commit comments

Comments
 (0)