Skip to content

Commit

Permalink
Add an option to skip non-existent files in diff-related dl-git commands
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantAnxiety committed Oct 24, 2023
1 parent 0501d44 commit d3995f8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
10 changes: 7 additions & 3 deletions terrarium/dl_gitmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ List files that have changed between two given revisions including the changes i
dl-git range-diff-paths --base <base> --head <head>
dl-git range-diff-paths --base <base> --head <head> --absolute
dl-git range-diff-paths --base <base> --head <head> --only-added-commits
dl-git range-diff-paths --base <base> --head <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
```
Expand All @@ -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 <commit-id> | dl-git list-diff-paths
echo <commit-id>> | dl-git list-diff-paths --absolute
echo <commit-id> | dl-git list-diff-paths --absolute
echo <commit-id> | 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`.
31 changes: 22 additions & 9 deletions terrarium/dl_gitmanager/dl_gitmanager/git_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import (
Collection,
Expand Down Expand Up @@ -114,23 +115,35 @@ 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:
result.add(str(base_path / diff_item.a_path))
if diff_item.b_path:
result.add(str(base_path / diff_item.b_path))
for diff_path in (diff_item.a_path, diff_item.b_path):
if not diff_path:
continue
absolute_path = base_path / diff_path
if not without_deleted or absolute_path.exists():
result.add(str(absolute_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}
Expand Down
29 changes: 22 additions & 7 deletions terrarium/dl_gitmanager/dl_gitmanager/scripts/gitmanager_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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}")

Expand Down

0 comments on commit d3995f8

Please sign in to comment.