diff --git a/guw/main.py b/guw/main.py index 94e1b4c..5a1c8dd 100644 --- a/guw/main.py +++ b/guw/main.py @@ -222,12 +222,11 @@ def _sync_at(self, tmpdir, backup, local, features, prev_feature, interactive): # Now remove every feature that must be removed self.config["features"] = [f for f in self.config["features"] if f["status"] != "_remove"] # Make target branch be the last feature - last_feature = self.config["features"][-1] - if last_feature: - if last_feature["status"] != "integrated": - self._copy(repo, last_feature, self._get_target_feature(), backup) - else: - logger.info("All features already integrated, nothing to do") + last_feature = next(reversed([f for f in self.config["features"] if f["status"] != "integrated"]), None) + if not last_feature: + last_feature = self._get_source_feature() + self._copy(repo, last_feature, self._get_target_feature(), backup) + # If we are syncing from upstream, make sure to update source too upstream_feature = self._get_upstream_feature() if ( @@ -383,13 +382,9 @@ def remove(self, backup, keep, local, folder, to_remove, interactive=False): if not feature: logger.critical(f"Feature {feature} not found") return - if not idx: - prev_feature = self._get_source_feature() - else: - prev_feature = self.config["features"][idx - 1] feature["status"] = "_remove" # Sync it again - self._sync(backup, keep, local, folder, self.config["features"][idx:], prev_feature, interactive=interactive) + self._sync(backup, keep, local, folder, self.config["features"], prev_feature=None, interactive=interactive) # Dump the new toml self.dump() diff --git a/tests/test_remove.py b/tests/test_remove.py index 15ca73e..8d4a4a8 100644 --- a/tests/test_remove.py +++ b/tests/test_remove.py @@ -32,7 +32,7 @@ def setUp(self): def cleanUp(self): shutil.rmtree(self.tmpdir) - def test_remove(self): + def test_remove_last(self): config = """ [[remotes]] name = "origin" @@ -65,3 +65,38 @@ def test_remove(self): # Check the proper order of the commits, like git log --pretty=%s commits = [x.summary for x in repo.iter_commits("example1-final")] self.assertEqual(commits, expected_commits) + + def test_remove_prev_feature_is_integrated(self): + config = """ + [[remotes]] + name = "origin" + url = "https://github.com/fluendo/git-upstream-workflow.git" + + [target] + remote = "origin" + branch = "example1-final" + + [source] + remote = "origin" + branch = "example1-main" + + [[features]] + remote = "origin" + name = "example1-feature1" + pr = "https://github/fluendo/git-upstream-workflow/pull-requests/10" + status = "integrated" + + [[features]] + remote = "origin" + name = "example1-feature1-fixup" + pr = "https://github/fluendo/git-upstream-workflow/pull-requests/10" + status = "pending" + """ + expected_commits = ["Initial commit"] + guw = GUW(tomli.loads(config)) + guw.remove(False, True, True, self.tmpdir, "example1-feature1-fixup") + repo = git.Repo(self.tmpdir) + # Check the proper order of the commits, like git log --pretty=%s + commits = [x.summary for x in repo.iter_commits("example1-final")] + + self.assertEqual(commits, expected_commits)