mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-11 15:28:27 +01:00
REfactoring: extract _remove_or_collapse()
This commit is contained in:
@@ -47,7 +47,6 @@ class CmdShow(Traverse):
|
||||
return 0
|
||||
|
||||
def run(self):
|
||||
|
||||
if self.parsed_args.workflow:
|
||||
# Workflow
|
||||
wf = self.git.get_all_commits('data/eval_auc.txt')
|
||||
|
||||
+21
-17
@@ -82,7 +82,7 @@ class Workflow(object):
|
||||
g = nx.DiGraph(name='DVC Workflow', directed=False)
|
||||
|
||||
if self._no_repro_commits:
|
||||
self.collaps_repro_commits()
|
||||
self.collapse_repro_commits()
|
||||
|
||||
for hash in set(self._edges.keys() + self._back_edges.keys()):
|
||||
commit = self._commits[hash]
|
||||
@@ -115,26 +115,12 @@ class Workflow(object):
|
||||
|
||||
self._build_graph(next, g)
|
||||
|
||||
def collaps_repro_commits(self):
|
||||
def collapse_repro_commits(self):
|
||||
hashes_to_remove = []
|
||||
for commit in self._commits.values():
|
||||
if commit.is_repro:
|
||||
parent_commit_hashes = self._back_edges[commit.hash]
|
||||
child_commit_hashes = self._edges.get(commit.hash)
|
||||
|
||||
# Remove if next commit is dvc-repro as well
|
||||
if child_commit_hashes and all(self._commits[hash].is_repro for hash in child_commit_hashes):
|
||||
for hash in child_commit_hashes:
|
||||
self._commits[hash].add_parents(commit.parent_hashes)
|
||||
self._commits[hash].remove_parent(commit.hash)
|
||||
pass
|
||||
|
||||
self._redirect_edges(self._edges, child_commit_hashes, commit.hash, parent_commit_hashes)
|
||||
self._redirect_edges(self._back_edges, parent_commit_hashes, commit.hash, child_commit_hashes)
|
||||
|
||||
if self._remove_or_collapse(commit):
|
||||
hashes_to_remove.append(commit.hash)
|
||||
else:
|
||||
commit.make_colapsed()
|
||||
|
||||
for hash in hashes_to_remove:
|
||||
del self._commits[hash]
|
||||
@@ -142,6 +128,24 @@ class Workflow(object):
|
||||
del self._back_edges[hash]
|
||||
pass
|
||||
|
||||
def _remove_or_collapse(self, commit):
|
||||
parent_commit_hashes = self._back_edges[commit.hash]
|
||||
child_commit_hashes = self._edges.get(commit.hash)
|
||||
|
||||
if child_commit_hashes and all(self._commits[hash].is_repro for hash in child_commit_hashes):
|
||||
for hash in child_commit_hashes:
|
||||
self._commits[hash].add_parents(commit.parent_hashes)
|
||||
self._commits[hash].remove_parent(commit.hash)
|
||||
|
||||
self._redirect_edges(self._edges, child_commit_hashes, commit.hash, parent_commit_hashes)
|
||||
self._redirect_edges(self._back_edges, parent_commit_hashes, commit.hash, child_commit_hashes)
|
||||
|
||||
return True
|
||||
else:
|
||||
commit.make_colapsed()
|
||||
return False
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def _redirect_edges(edges, child_commit_hashes, hash, commit_hashes):
|
||||
for h in commit_hashes:
|
||||
|
||||
@@ -39,7 +39,7 @@ class TestWorkflow(TestCase):
|
||||
wf.add_commit(self._commit3)
|
||||
wf.add_commit(self._commit4)
|
||||
|
||||
wf.collaps_repro_commits()
|
||||
wf.collapse_repro_commits()
|
||||
|
||||
self.assertEqual(len(wf._commits), 3)
|
||||
self.assertEqual(wf._commits[self._commit1.hash].text, self._commit1._comment + '\n' + self._commit1.hash)
|
||||
@@ -57,7 +57,7 @@ class TestWorkflow(TestCase):
|
||||
wf.add_commit(self._commit3) # Dead end which cannot be collapsed
|
||||
|
||||
self.assertEqual(len(wf._commits), 3)
|
||||
wf.collaps_repro_commits()
|
||||
wf.collapse_repro_commits()
|
||||
self.assertEqual(len(wf._commits), 2)
|
||||
|
||||
self.assertEqual(wf._commits[self._commit1.hash].text, self._commit1._comment + '\n' + self._commit1.hash)
|
||||
|
||||
Reference in New Issue
Block a user