Skip to content

Commit

Permalink
Merge pull request #669 from koordinates/diff-commit-spec
Browse files Browse the repository at this point in the history
Support `kart diff COMMIT1 COMMIT2` [#666]
  • Loading branch information
olsen232 authored Jul 7, 2022
2 parents c9df7bd + e6164b2 commit 535fd94
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _When adding new entries to the changelog, please include issue/PR numbers where
- Changed format of feature IDs in GeoJSON output to be more informative and consistent. [#135](https://github.com/koordinates/kart/issues/135)
- Fixed primary key issues for shapefile import - now generates an `auto_pk` by default, but uses an existing field if specified (doesn't use the FID). [#646](https://github.com/koordinates/kart/pull/646)
- Add `--with-dataset-types` option to `kart meta get` which is informative now that there is more than one type of dataset. [#649](https://github.com/koordinates/kart/pull/649)
- Support `kart diff COMMIT1 COMMIT2` as an alternative to typing `kart diff COMMIT1...COMMIT2` [#666](https://github.com/koordinates/kart/issues/666)

## 0.11.3

Expand Down
19 changes: 17 additions & 2 deletions kart/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from . import diff_estimation
from .cli_util import OutputFormatType, parse_output_format
from .crs_util import CoordinateReferenceString
from .exceptions import NotFound
from .output_util import dump_json_output
from .repo import KartRepoState
from .structs import CommitWithReference


def feature_count_diff(
Expand Down Expand Up @@ -140,10 +142,12 @@ def diff(
- if supplied with the form: commit-A...commit-B - diffs between commit-A and commit-B.
- supplying two seperate refs: commit-A commit-B - also diffs between commit-A and commit-B
- if supplied with the form: commit-A..commit-B - diffs between (the common ancestor of
commit-A and commit-B) and (commit-B).
To list only particular conflicts, supply one or more FILTERS of the form [DATASET[:PRIMARY_KEY]]
To list only particular changes, supply one or more FILTERS of the form [DATASET[:PRIMARY_KEY]]
"""
output_type, fmt = parse_output_format(output_format, json_style)

Expand All @@ -158,9 +162,20 @@ def diff(
only_feature_count,
)

repo = ctx.obj.get_repo(allowed_states=KartRepoState.ALL_STATES)

# Handle the `commit-A commit-B` format:
if commit_spec and ".." not in commit_spec and filters:
try:
if CommitWithReference.resolve(repo, filters[0]):
filters = list(filters)
extra_commit_spec = filters.pop(0)
commit_spec = f"{commit_spec}...{extra_commit_spec}"
except NotFound:
pass

from .base_diff_writer import BaseDiffWriter

repo = ctx.obj.get_repo(allowed_states=KartRepoState.ALL_STATES)
diff_writer_class = BaseDiffWriter.get_diff_writer_class(output_type)
diff_writer = diff_writer_class(
repo,
Expand Down
10 changes: 8 additions & 2 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,14 @@ def test_diff_rev_rev(head_sha, head1_sha, data_archive_readonly, cli_runner):
f"{head1_sha}...{head_sha}",
f"{head1_sha}...",
"HEAD^1...HEAD",
["HEAD^1", "HEAD"],
)

R_SPECS = (
f"{head_sha}...{head1_sha}",
f"...{head1_sha}",
"HEAD...HEAD^1",
["HEAD", "HEAD^1"],
)

CHANGE_IDS = {
Expand All @@ -1111,7 +1113,9 @@ def test_diff_rev_rev(head_sha, head1_sha, data_archive_readonly, cli_runner):
with data_archive_readonly("points"):
for spec in F_SPECS:
print(f"fwd: {spec}")
r = cli_runner.invoke(["diff", "--exit-code", "-o", "json", spec])
if isinstance(spec, str):
spec = [spec]
r = cli_runner.invoke(["diff", "--exit-code", "-o", "json", *spec])
assert r.exit_code == 1, r
odata = json.loads(r.stdout)["kart.diff/v1+hexwkb"]
assert len(odata[H.POINTS.LAYER]["feature"]) == 5
Expand All @@ -1134,7 +1138,9 @@ def test_diff_rev_rev(head_sha, head1_sha, data_archive_readonly, cli_runner):

for spec in R_SPECS:
print(f"rev: {spec}")
r = cli_runner.invoke(["diff", "--exit-code", "-o", "json", spec])
if isinstance(spec, str):
spec = [spec]
r = cli_runner.invoke(["diff", "--exit-code", "-o", "json", *spec])
assert r.exit_code == 1, r
odata = json.loads(r.stdout)["kart.diff/v1+hexwkb"]
assert len(odata[H.POINTS.LAYER]["feature"]) == 5
Expand Down

0 comments on commit 535fd94

Please sign in to comment.