Skip to content

Commit 44f0c36

Browse files
committed
✨ feat(version): detect and display version files before updating
1 parent e34a78c commit 44f0c36

File tree

3 files changed

+87
-49
lines changed

3 files changed

+87
-49
lines changed

tests/unit/test_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
_prompt_for_version_choice,
1515
bump_version,
1616
get_default_bump_by_commits_dict,
17+
get_detected_files,
1718
get_next_version,
1819
get_version_from_cargo_toml,
1920
get_version_from_files,

tgit/version.py

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,57 @@ def get_default_bump_by_commits_dict(commits_by_type: dict[str, list[git.Commit]
284284
return "patch"
285285

286286

287+
def get_detected_files(path: str) -> list[Path]:
288+
"""获取递归模式下检测到的所有版本文件。"""
289+
current_path = Path(path).resolve()
290+
filenames = ["package.json", "pyproject.toml", "setup.py", "Cargo.toml", "VERSION", "VERSION.txt", "build.gradle.kts"]
291+
detected_files: list[Path] = []
292+
293+
# 需要忽略 node_modules 目录
294+
for root, dirs, files in os.walk(current_path):
295+
if "node_modules" in dirs:
296+
dirs.remove("node_modules")
297+
for file in files:
298+
if file in filenames:
299+
file_path = Path(root) / file
300+
detected_files.append(file_path)
301+
302+
return detected_files
303+
304+
305+
def get_root_detected_files(path: str) -> list[Path]:
306+
"""获取根目录下检测到的所有版本文件。"""
307+
current_path = Path(path).resolve()
308+
filenames = ["package.json", "pyproject.toml", "setup.py", "Cargo.toml", "VERSION", "VERSION.txt", "build.gradle.kts"]
309+
detected_files: list[Path] = []
310+
311+
for filename in filenames:
312+
file_path = current_path / filename
313+
if file_path.exists():
314+
detected_files.append(file_path)
315+
316+
return detected_files
317+
318+
287319
def handle_version(args: VersionArgs) -> None:
288320
verbose = args.verbose
289321
path = args.path
290322
prev_version = get_current_version(path, verbose)
291323
reclusive = args.recursive
292324

325+
# 在版本选择前显示检测到的文件
326+
detected_files = get_detected_files(path) if reclusive else get_root_detected_files(path)
327+
328+
if detected_files:
329+
console.print(f"Detected [cyan bold]{len(detected_files)}[/cyan bold] files to update:")
330+
current_path = Path(path).resolve()
331+
for file_path in detected_files:
332+
relative_path = file_path.relative_to(current_path)
333+
console.print(f" - {relative_path}")
334+
else:
335+
console.print("No version files detected for update.")
336+
return
337+
293338
if next_version := get_next_version(args, prev_version, verbose):
294339
# 获取目标 tag 名
295340
target_tag = f"v{next_version}"
@@ -490,37 +535,29 @@ def update_version_files(
490535
if verbose > 0:
491536
console.print(f"Current path: [cyan bold]{current_path}")
492537

493-
if reclusive:
494-
# 获取当前目录及其子目录下,所有名称在上述列表中的文件
495-
# 使用os.walk()函数,可以遍历指定目录下的所有子目录和文件
496-
filenames = ["package.json", "pyproject.toml", "setup.py", "Cargo.toml", "VERSION", "VERSION.txt", "build.gradle.kts"]
497-
# 需要忽略 node_modules 目录
498-
for root, dirs, files in os.walk(current_path):
499-
if "node_modules" in dirs:
500-
dirs.remove("node_modules")
501-
for file in files:
502-
if file in filenames:
503-
# file_path = os.path.join(root, file)
504-
file_path = Path(root) / file
505-
update_version_in_file(verbose, next_version_str, file, file_path)
506-
else:
538+
# 获取检测到的文件列表
539+
detected_files = get_detected_files(args.path) if reclusive else get_root_detected_files(args.path)
540+
541+
# 更新文件
542+
for file_path in detected_files:
507543
# Check if we're in a test environment to avoid interactive prompts
508544
is_test_env = "pytest" in sys.modules or "unittest" in sys.modules
509-
update_file_in_root(next_version_str, verbose, current_path, show_diff=not is_test_env)
545+
show_diff = not is_test_env and not reclusive
546+
update_version_in_file(verbose, next_version_str, file_path.name, file_path, show_diff=show_diff)
510547

511548

512-
def update_version_in_file(verbose: int, next_version_str: str, file: str, file_path: Path) -> None:
549+
def update_version_in_file(verbose: int, next_version_str: str, file: str, file_path: Path, *, show_diff: bool = False) -> None:
513550
# sourcery skip: collection-into-set, merge-duplicate-blocks, remove-redundant-if
514551
if file == "package.json":
515-
update_file(str(file_path), r'"version":\s*".*?"', f'"version": "{next_version_str}"', verbose, show_diff=False)
552+
update_file(str(file_path), r'"version":\s*".*?"', f'"version": "{next_version_str}"', verbose, show_diff=show_diff)
516553
elif file in ("pyproject.toml", "build.gradle.kts"):
517-
update_file(str(file_path), r'version\s*=\s*".*?"', f'version = "{next_version_str}"', verbose, show_diff=False)
554+
update_file(str(file_path), r'version\s*=\s*".*?"', f'version = "{next_version_str}"', verbose, show_diff=show_diff)
518555
elif file == "setup.py":
519-
update_file(str(file_path), r"version=['\"].*?['\"]", f"version='{next_version_str}'", verbose, show_diff=False)
556+
update_file(str(file_path), r"version=['\"].*?['\"]", f"version='{next_version_str}'", verbose, show_diff=show_diff)
520557
elif file == "Cargo.toml":
521-
update_file(str(file_path), r'version\s*=\s*".*?"', f'version = "{next_version_str}"', verbose, show_diff=False)
558+
update_file(str(file_path), r'version\s*=\s*".*?"', f'version = "{next_version_str}"', verbose, show_diff=show_diff)
522559
elif file in ("VERSION", "VERSION.txt"):
523-
update_file(str(file_path), None, next_version_str, verbose, show_diff=False)
560+
update_file(str(file_path), None, next_version_str, verbose, show_diff=show_diff)
524561

525562

526563
def update_file_in_root(next_version_str: str, verbose: int, root_path: Path, *, show_diff: bool = True) -> None:

uv.lock

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)