Skip to content

merge-driver: support removes and changes #8360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dvc/commands/git_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _run(self):
our = Dvcfile(dvc, self.args.our, verify=False)
their = Dvcfile(dvc, self.args.their, verify=False)

our.merge(ancestor, their)
our.merge(ancestor, their, allowed=["add", "remove", "change"])

return 0
finally:
Expand Down
10 changes: 5 additions & 5 deletions dvc/dvcfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def remove(self, force=False): # pylint: disable=unused-argument
def dump(self, stage, **kwargs):
raise NotImplementedError

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
raise NotImplementedError


Expand Down Expand Up @@ -204,12 +204,12 @@ def dump(self, stage, **kwargs):
def remove_stage(self, stage): # pylint: disable=unused-argument
self.remove()

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
assert isinstance(ancestor, SingleStageFile)
assert isinstance(other, SingleStageFile)

stage = self.stage
stage.merge(ancestor.stage, other.stage)
stage.merge(ancestor.stage, other.stage, allowed=allowed)
self.dump(stage)


Expand Down Expand Up @@ -310,7 +310,7 @@ def remove_stage(self, stage):
else:
super().remove()

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
raise NotImplementedError


Expand Down Expand Up @@ -411,7 +411,7 @@ def remove_stage(self, stage):
else:
self.remove()

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
raise NotImplementedError


Expand Down
8 changes: 6 additions & 2 deletions dvc/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def _check_can_merge(self, out):
"unable to auto-merge outputs that are not directories"
)

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
from dvc_data.hashfile.tree import MergeError as TreeMergeError
from dvc_data.hashfile.tree import du, merge

Expand All @@ -1144,7 +1144,11 @@ def merge(self, ancestor, other):

try:
merged = merge(
self.odb, ancestor_info, self.hash_info, other.hash_info
self.odb,
ancestor_info,
self.hash_info,
other.hash_info,
allowed=allowed,
)
except TreeMergeError as exc:
raise MergeError(str(exc)) from exc
Expand Down
6 changes: 3 additions & 3 deletions dvc/stage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def _check_can_merge(stage, ancestor_out=None):
"unable to auto-merge DVC files with deleted outputs"
)

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
assert other

if not other.outs:
Expand All @@ -728,7 +728,7 @@ def merge(self, ancestor, other):
self._check_can_merge(self, ancestor_out)
self._check_can_merge(other, ancestor_out)

self.outs[0].merge(ancestor_out, other.outs[0])
self.outs[0].merge(ancestor_out, other.outs[0], allowed=allowed)

def dump(self, **kwargs):
self.dvcfile.dump(self, **kwargs)
Expand Down Expand Up @@ -770,5 +770,5 @@ def changed_stage(self):
def _changed_stage_entry(self):
return f"'cmd' of {self} has changed."

def merge(self, ancestor, other):
def merge(self, ancestor, other, allowed=None):
raise NotImplementedError
25 changes: 19 additions & 6 deletions tests/func/test_merge_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ def _gen(tmp_dir, struct, name):
(None, {"foo": "foo"}, {"bar": "bar"}, {"foo": "foo", "bar": "bar"}),
(None, None, {"bar": "bar"}, {"bar": "bar"}),
(None, {"foo": "foo"}, None, {"foo": "foo"}),
(
{"foo": "foo"},
{"foo": "bar"},
{"foo": "foo", "baz": "baz"},
{"foo": "bar", "baz": "baz"},
),
({"foo": "foo"}, {}, {"foo": "foo", "bar": "bar"}, {"bar": "bar"}),
(
{"common": "common", "subdir": {"foo": "foo", "bar": "bar"}},
{"common": "common", "subdir": {"foo": "foo", "bar": "baz"}},
{"common": "common", "subdir": {"bar": "bar", "bizz": "bizz"}},
{
"common": "common",
"subdir": {"bar": "baz", "bizz": "bizz"},
},
),
],
)
def test_merge(tmp_dir, dvc, ancestor, our, their, merged):
Expand Down Expand Up @@ -74,18 +90,15 @@ def test_merge(tmp_dir, dvc, ancestor, our, their, merged):
{"foo": "foo"},
{"foo": "bar"},
{"foo": "baz"},
(
"unable to auto-merge directories with "
"diff that contains 'change'ed files"
),
("unable to auto-merge the following paths:\nfoo"),
),
(
{"common": "common", "foo": "foo"},
{"common": "common", "bar": "bar"},
{"baz": "baz"},
(
"unable to auto-merge directories with "
"diff that contains 'remove'ed files"
"unable to auto-merge the following paths:\n"
"both deleted: ('foo',)"
),
),
],
Expand Down