diff --git a/terrarium/dl_gitmanager/README.md b/terrarium/dl_gitmanager/README.md index fdeada893..fd10683e6 100644 --- a/terrarium/dl_gitmanager/README.md +++ b/terrarium/dl_gitmanager/README.md @@ -47,12 +47,13 @@ List files that have changed between two given revisions including the changes i dl-git range-diff-paths --base --head dl-git range-diff-paths --base --head --absolute dl-git range-diff-paths --base --head --only-added-commits +dl-git range-diff-paths --base --head --without-deleted-files ``` Here `base` and `head` can be a commit ID, branch name, `HEAD~3` or any similar notation that is usually accepted by git. These arguments are optional. By default `head` is `HEAD` and `base` is `HEAD~1`. -Thgis means you can use the following command to see the diff of the last commit in the current branch: +This means you can use the following command to see the diff of the last commit in the current branch: ``` dl-git diff-paths ``` @@ -63,13 +64,16 @@ By default they are printed relative to the repository root. The `--only-added-commits` option makes the tool inspect only commits that have been added in the head version. +The `--without-deleted-files` option makes the tool omit deleted (non-existent) files in the resulting list. + ### list-diff-paths List files that have changed in commits passed on as input ``` echo | dl-git list-diff-paths -echo > | dl-git list-diff-paths --absolute +echo | dl-git list-diff-paths --absolute +echo | dl-git list-diff-paths --without-deleted-files ``` -Option `--absolute` has the same meaning as in `range-diff-paths`. +Options `--absolute` and `--without-deleted-files` have the same meaning as in `range-diff-paths`. diff --git a/terrarium/dl_gitmanager/dl_gitmanager/git_manager.py b/terrarium/dl_gitmanager/dl_gitmanager/git_manager.py index 2199b92db..6c4f58538 100644 --- a/terrarium/dl_gitmanager/dl_gitmanager/git_manager.py +++ b/terrarium/dl_gitmanager/dl_gitmanager/git_manager.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os from pathlib import Path from typing import ( Collection, @@ -114,7 +115,9 @@ def _iter_list_diffs( for _, diff_item in submodule_manager._iter_list_diffs(commits=sm_commits_for_all_commits): yield submodule_base_path, diff_item - def _collect_paths_from_diffs(self, diff_iterable: Iterable[tuple[Path, Diff]]) -> list[str]: + def _collect_paths_from_diffs( + self, diff_iterable: Iterable[tuple[Path, Diff]], without_deleted: bool = False + ) -> list[str]: result: set[str] = set() for base_path, diff_item in diff_iterable: if diff_item.a_path: @@ -122,15 +125,26 @@ def _collect_paths_from_diffs(self, diff_iterable: Iterable[tuple[Path, Diff]]) if diff_item.b_path: result.add(str(base_path / diff_item.b_path)) + if without_deleted: + result = {path for path in result if os.path.exists(path)} + return sorted(result) - def get_range_diff_paths(self, base: str, head: str, absolute: bool = False) -> list[str]: + def get_range_diff_paths( + self, base: str, head: str, absolute: bool = False, without_deleted: bool = False + ) -> list[str]: return self._collect_paths_from_diffs( - diff_iterable=self._iter_range_diffs(base=base, head=head, absolute=absolute) + diff_iterable=self._iter_range_diffs(base=base, head=head, absolute=absolute), + without_deleted=without_deleted, ) - def get_list_diff_paths(self, commits: Collection[str], absolute: bool = False) -> list[str]: - return self._collect_paths_from_diffs(diff_iterable=self._iter_list_diffs(commits=commits, absolute=absolute)) + def get_list_diff_paths( + self, commits: Collection[str], absolute: bool = False, without_deleted: bool = False + ) -> list[str]: + return self._collect_paths_from_diffs( + diff_iterable=self._iter_list_diffs(commits=commits, absolute=absolute), + without_deleted=without_deleted, + ) def get_all_ancestor_commits(self, commit: str) -> set[str]: result: set[str] = {commit} diff --git a/terrarium/dl_gitmanager/dl_gitmanager/scripts/gitmanager_cli.py b/terrarium/dl_gitmanager/dl_gitmanager/scripts/gitmanager_cli.py index f9e635b7e..8f16b6baa 100644 --- a/terrarium/dl_gitmanager/dl_gitmanager/scripts/gitmanager_cli.py +++ b/terrarium/dl_gitmanager/dl_gitmanager/scripts/gitmanager_cli.py @@ -49,6 +49,9 @@ def get_parser(cls) -> argparse.ArgumentParser: range_diff_paths_parser.add_argument( "--only-added-commits", action="store_true", help="Inspect only commits that are added in head" ) + range_diff_paths_parser.add_argument( + "--without-deleted-files", action="store_true", help="Omit deleted files in the resulting list" + ) subparsers.add_parser( "list-diff-paths", @@ -58,18 +61,26 @@ def get_parser(cls) -> argparse.ArgumentParser: return parser - def range_diff_paths(self, base: str, head: Optional[str], absolute: bool, only_added_commits: bool) -> None: + def range_diff_paths( + self, base: str, head: Optional[str], absolute: bool, only_added_commits: bool, without_deleted: bool = False + ) -> None: diff_name_list: list[str] if only_added_commits: commits = self.git_manager.get_missing_commits(base=base, head=head) - diff_name_list = self.git_manager.get_list_diff_paths(commits=commits, absolute=absolute) + diff_name_list = self.git_manager.get_list_diff_paths( + commits=commits, absolute=absolute, without_deleted=without_deleted + ) else: - diff_name_list = self.git_manager.get_range_diff_paths(base=base, head=head, absolute=absolute) + diff_name_list = self.git_manager.get_range_diff_paths( + base=base, head=head, absolute=absolute, without_deleted=without_deleted + ) print("\n".join(diff_name_list)) - def list_diff_paths(self, absolute: bool) -> None: + def list_diff_paths(self, absolute: bool, without_deleted: bool = False) -> None: commits = [line.strip() for line in self.input_text_io if line.strip()] - diff_name_list = self.git_manager.get_list_diff_paths(commits=commits, absolute=absolute) + diff_name_list = self.git_manager.get_list_diff_paths( + commits=commits, absolute=absolute, without_deleted=without_deleted + ) print("\n".join(diff_name_list)) @classmethod @@ -86,10 +97,14 @@ def run_parsed_args(cls, args: argparse.Namespace) -> None: match args.command: case "range-diff-paths": tool.range_diff_paths( - base=args.base, head=args.head, absolute=args.absolute, only_added_commits=args.only_added_commits + base=args.base, + head=args.head, + absolute=args.absolute, + only_added_commits=args.only_added_commits, + without_deleted=args.without_deleted_files, ) case "list-diff-paths": - tool.list_diff_paths(absolute=args.absolute) + tool.list_diff_paths(absolute=args.absolute, without_deleted=args.without_deleted_files) case _: raise RuntimeError(f"Got unknown command: {args.command}")