Skip to content

Commit

Permalink
Tidy up existing filters - now called "key filters" - ready for spati…
Browse files Browse the repository at this point in the history
…al filters
  • Loading branch information
olsen232 committed Jul 7, 2021
1 parent c0f742e commit 887d604
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 188 deletions.
14 changes: 7 additions & 7 deletions kart/base_diff_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .diff_structs import RepoDiff, DatasetDiff
from .exceptions import InvalidOperation, NotFound, NotYetImplemented, NO_WORKING_COPY
from .filter_util import build_feature_filter, UNFILTERED
from .key_filters import RepoKeyFilter


L = logging.getLogger("kart.diff_writer")
Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(
self,
repo,
commit_spec,
filters,
user_key_filters,
output_path="-",
*,
json_style="pretty",
Expand All @@ -71,15 +71,15 @@ def __init__(
repo, commit_spec
)

self.filters = filters
self.diff_filter = build_feature_filter(filters)
self.user_key_filters = user_key_filters
self.repo_key_filter = RepoKeyFilter.build_from_user_patterns(user_key_filters)

base_ds_paths = {ds.path for ds in self.base_rs.datasets}
target_ds_paths = {ds.path for ds in self.target_rs.datasets}
all_ds_paths = base_ds_paths | target_ds_paths

if self.diff_filter is not UNFILTERED:
all_ds_paths = all_ds_paths & self.diff_filter.keys()
if not self.repo_key_filter.match_all:
all_ds_paths = all_ds_paths & self.repo_key_filter.keys()

self.all_ds_paths = sorted(list(all_ds_paths))

Expand Down Expand Up @@ -188,7 +188,7 @@ def get_dataset_diff(self, dataset_path, prune=True):
"""Returns the DatasetDiff object for the dataset at path dataset_path."""

diff = DatasetDiff()
ds_filter = self.diff_filter[dataset_path]
ds_filter = self.repo_key_filter[dataset_path]

if self.base_rs != self.target_rs:
# diff += base_rs<>target_rs
Expand Down
9 changes: 6 additions & 3 deletions kart/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)

from .exceptions import DbConnectionError
from .key_filters import RepoKeyFilter
from .structs import CommitWithReference
from .working_copy import WorkingCopyStatus
from .output_util import InputMode, get_input_mode
Expand Down Expand Up @@ -289,8 +290,8 @@ def _find_remote_branch_by_name(repo, name):
),
default="HEAD",
)
@click.argument("pathspec", nargs=-1)
def restore(ctx, source, pathspec):
@click.argument("filters", nargs=-1)
def restore(ctx, source, filters):
"""
Restore specified paths in the working copy with some contents from the given restore source.
By default, restores the entire working copy to the commit at HEAD (so, discards all uncommitted changes).
Expand All @@ -307,11 +308,13 @@ def restore(ctx, source, pathspec):
except (KeyError, pygit2.InvalidSpecError):
raise NotFound(f"{source} is not a commit or tree", exit_code=NO_COMMIT)

repo_key_filter = RepoKeyFilter.build_from_user_patterns(filters)

working_copy.reset(
commit_or_tree,
force=True,
track_changes_as_dirty=True,
paths=pathspec,
repo_key_filter=repo_key_filter,
)


Expand Down
8 changes: 4 additions & 4 deletions kart/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
NO_DATA,
NO_WORKING_COPY,
)
from .filter_util import build_feature_filter
from .key_filters import RepoKeyFilter
from .output_util import dump_json_output
from .status import (
get_branch_status_message,
Expand Down Expand Up @@ -89,8 +89,8 @@ def commit(ctx, message, allow_empty, output_format, filters):

working_copy.assert_db_tree_match(tree)

commit_filter = build_feature_filter(filters)
wc_diff = working_copy.diff_to_tree(commit_filter)
repo_key_filter = RepoKeyFilter.build_from_user_patterns(filters)
wc_diff = working_copy.diff_to_tree(repo_key_filter)

if not wc_diff and not allow_empty:
raise NotFound("No changes to commit", exit_code=NO_CHANGES)
Expand All @@ -108,7 +108,7 @@ def commit(ctx, message, allow_empty, output_format, filters):
wc_diff, commit_msg, allow_empty=allow_empty
)

working_copy.reset_tracking_table(commit_filter)
working_copy.reset_tracking_table(repo_key_filter)
working_copy.update_state_table_tree(new_commit.peel(pygit2.Tree).id.hex)

jdict = commit_obj_to_json(new_commit, repo, wc_diff)
Expand Down
9 changes: 4 additions & 5 deletions kart/conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .crs_util import CoordinateReferenceString
from .exceptions import SUCCESS, SUCCESS_WITH_FLAG
from .filter_util import build_feature_filter, UNFILTERED
from .key_filters import RepoKeyFilter
from .merge_util import MergeIndex, MergeContext, rich_conflicts
from .output_util import dump_json_output
from .repo import KartRepoState
Expand All @@ -23,7 +23,7 @@ def list_conflicts(
merge_index,
merge_context,
output_format="text",
conflict_filter=UNFILTERED,
conflict_filter=RepoKeyFilter.MATCH_ALL,
summarise=0,
flat=False,
target_crs=None,
Expand Down Expand Up @@ -54,14 +54,13 @@ def list_conflicts(
"""
output_dict = {}
conflict_output = _CONFLICT_PLACEHOLDER
conflict_filter = conflict_filter or UNFILTERED

if output_format == "geojson":
flat = True # geojson must be flat or it is not valid geojson
summarise = 0

conflicts = rich_conflicts(merge_index.unresolved_conflicts.values(), merge_context)
if conflict_filter != UNFILTERED:
if not conflict_filter.match_all:
conflicts = (c for c in conflicts if c.matches_filter(conflict_filter))

for conflict in conflicts:
Expand Down Expand Up @@ -284,7 +283,7 @@ def conflicts(
ctx.exit(SUCCESS_WITH_FLAG if merge_index.conflicts else SUCCESS)

merge_context = MergeContext.read_from_repo(repo)
conflict_filter = build_feature_filter(filters)
conflict_filter = RepoKeyFilter.build_from_user_patterns(filters)
result = list_conflicts(
merge_index,
merge_context,
Expand Down
12 changes: 6 additions & 6 deletions kart/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .crs_util import CoordinateReferenceString
from .diff_structs import RepoDiff, DatasetDiff
from .filter_util import UNFILTERED
from .key_filters import RepoKeyFilter, DatasetKeyFilter
from .output_util import dump_json_output
from .repo import KartRepoState
from . import diff_estimation
Expand All @@ -15,7 +15,7 @@


def get_dataset_diff(
base_rs, target_rs, working_copy, dataset_path, ds_filter=UNFILTERED
base_rs, target_rs, working_copy, dataset_path, ds_filter=DatasetKeyFilter.MATCH_ALL
):
diff = DatasetDiff()

Expand Down Expand Up @@ -48,19 +48,19 @@ def get_dataset_diff(
return diff


def get_repo_diff(base_rs, target_rs, feature_filter=UNFILTERED):
def get_repo_diff(base_rs, target_rs, repo_key_filter=RepoKeyFilter.MATCH_ALL):
"""Generates a Diff for every dataset in both RepoStructures."""
base_ds_paths = {ds.path for ds in base_rs.datasets}
target_ds_paths = {ds.path for ds in target_rs.datasets}
all_ds_paths = base_ds_paths | target_ds_paths

if feature_filter is not UNFILTERED:
all_ds_paths = all_ds_paths.intersection(feature_filter.keys())
if not repo_key_filter.match_all:
all_ds_paths = all_ds_paths & repo_key_filter.keys()

result = RepoDiff()
for ds_path in sorted(all_ds_paths):
ds_diff = get_dataset_diff(
base_rs, target_rs, None, ds_path, feature_filter[ds_path]
base_rs, target_rs, None, ds_path, repo_key_filter[ds_path]
)
result[ds_path] = ds_diff

Expand Down
66 changes: 0 additions & 66 deletions kart/filter_util.py

This file was deleted.

Loading

0 comments on commit 887d604

Please sign in to comment.