Skip to content

Commit

Permalink
Support --amend in kart apply and kart meta set
Browse files Browse the repository at this point in the history
  • Loading branch information
olsen232 committed Sep 19, 2022
1 parent 8ee77a2 commit a61998f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _When adding new entries to the changelog, please include issue/PR numbers where
- Bugfix: don't allow `kart merge` to fast-forward if the user specifies a merge message. [#705](https://github.com/koordinates/kart/issues/705)
- `diff`, `show`, `create-patch` and `apply` now handle attached files (as could already be created using `kart commit-files`) [#583](https://github.com/koordinates/kart/issues/583)
- `metadata.xml` is now consistently treated as an attached file, rather than as a hidden "meta item". [#583](https://github.com/koordinates/kart/issues/583)
- `apply` and `meta set` now support `--amend` to amend an existing commit

## 0.11.5

Expand Down
15 changes: 13 additions & 2 deletions kart/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ def apply_patch(
repo,
do_commit,
patch_file,
allow_empty,
ref="HEAD",
allow_empty=False,
amend=False,
**kwargs,
):
try:
Expand Down Expand Up @@ -293,6 +294,9 @@ def apply_patch(
except KeyError:
raise NotFound(f"No such ref {ref}")

if amend and not do_commit:
raise click.UsageError("--no-commit and --amend are incompatible")

allow_minimal_updates = bool(resolve_missing_values_from_rs)

rs = repo.structure(ref)
Expand Down Expand Up @@ -335,9 +339,10 @@ def apply_patch(
if do_commit:
commit = rs.commit_diff(
repo_diff,
metadata["message"],
metadata.get("message"),
author=_build_signature(metadata, "author", repo),
allow_empty=allow_empty,
amend=amend,
resolve_missing_values_from_rs=resolve_missing_values_from_rs,
)
click.echo(f"Commit {commit.hex}")
Expand Down Expand Up @@ -381,6 +386,12 @@ def apply_patch(
help="Which ref to apply the patch onto.",
shell_complete=ref_completer,
)
@click.option(
"--amend",
default=False,
is_flag=True,
help="Amend the previous commit instead of adding a new commit",
)
@click.argument("patch_file", type=click.File("r", encoding="utf-8"))
def apply(ctx, **kwargs):
"""
Expand Down
11 changes: 9 additions & 2 deletions kart/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ def convert(self, value, param, ctx):
help="Use the given message as the commit message",
type=StringFromFile(encoding="utf-8"),
)
@click.option(
"--amend",
default=False,
is_flag=True,
help="Amend the previous commit instead of adding a new commit",
)
@click.argument("dataset")
@click.argument(
"items",
Expand All @@ -166,7 +172,7 @@ def convert(self, value, param, ctx):
metavar="KEY=VALUE [KEY=VALUE...]",
)
@click.pass_context
def meta_set(ctx, message, dataset, items):
def meta_set(ctx, message, amend, dataset, items):
"""
Sets multiple meta items for a dataset, and creates a commit.
"""
Expand All @@ -177,7 +183,7 @@ def meta_set(ctx, message, dataset, items):
"This repo doesn't support meta changes, use `kart upgrade`"
)

if message is None:
if message is None and not amend:
message = f"Update metadata for {dataset}"

def _parse(key, value):
Expand Down Expand Up @@ -208,6 +214,7 @@ def _parse(key, value):
do_commit=True,
patch_file=patch_file,
allow_empty=False,
amend=amend,
)


Expand Down
36 changes: 31 additions & 5 deletions kart/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def commit_diff(
author=None,
committer=None,
allow_empty=False,
amend=False,
resolve_missing_values_from_rs: Optional["RepoStructure"] = None,
):
"""
Expand All @@ -391,23 +392,48 @@ def commit_diff(
if (not allow_empty) and new_tree == self.tree:
raise NotFound("No changes to commit", exit_code=NO_CHANGES)

L.info("Committing...")

if self.ref == "HEAD":
parent_commit = self.repo.head_commit
else:
parent_commit = self.repo.references[self.ref].peel(pygit2.Commit)
parents = [parent_commit.oid] if parent_commit is not None else []

# This will also update the ref (branch) to point to the new commit
if amend:
if not parent_commit:
raise click.UsageError(
"Cannot --amend - there is no previous commit to amend"
)
parents = [gp.id for gp in parent_commit.parents]
if not message:
message = parent_commit.message
commit_to_ref = None
else:
parents = [parent_commit.oid] if parent_commit is not None else []
commit_to_ref = self.ref

if not message:
raise click.UsageError("Aborting commit due to empty commit message.")

L.info("Committing...")

# This will also update commit_to_ref to point to the new commit, if it is not None.
new_commit = object_builder.commit(
self.ref,
commit_to_ref,
author or self.repo.author_signature(),
committer or self.repo.committer_signature(),
message,
parents,
)

if amend:
if self.ref == "HEAD" and self.repo.head_branch is None:
self.repo.head.set_target(new_commit.id)
elif self.ref == "HEAD" and self.repo.head_branch is not None:
self.repo.references[self.repo.head_branch].set_target(
new_commit.id
)
else:
self.repo.references[self.ref].set_target(new_commit.id)

L.info(f"Commit: {new_commit.id.hex}")
return new_commit

Expand Down
25 changes: 25 additions & 0 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ def test_meta_set(data_archive, cli_runner):
assert meta["description"]["+"] == "newdescription"


def test_meta_set_amend(data_archive, cli_runner):
with data_archive("points"):
r = cli_runner.invoke(
[
"meta",
"set",
"nz_pa_points_topo_150k",
"title=newtitle",
"description=newdescription",
"--amend",
]
)
assert r.exit_code == 0, r.stderr
r = cli_runner.invoke(["show", "-o", "json"])
assert r.exit_code == 0, r.stderr
output = json.loads(r.stdout)
patch_info = output.pop("kart.show/v1")
assert patch_info["message"] == "Improve naming on Coromandel East coast"
feature = output["kart.diff/v1+hexwkb"]["nz_pa_points_topo_150k"]["feature"]
assert len(feature) == 5
meta = output["kart.diff/v1+hexwkb"]["nz_pa_points_topo_150k"]["meta"]
assert meta["title"] == {"-": "NZ Pa Points (Topo, 1:50k)", "+": "newtitle"}
assert meta["description"]["+"] == "newdescription"


def test_meta_set_custom_fields(data_archive, cli_runner):
with data_archive("points"):
# Make sure this works even when we have a working copy
Expand Down

0 comments on commit a61998f

Please sign in to comment.