diff --git a/src/ghstack/cherry_pick.py b/src/ghstack/cherry_pick.py index 85d27dd..e8077a2 100644 --- a/src/ghstack/cherry_pick.py +++ b/src/ghstack/cherry_pick.py @@ -14,6 +14,7 @@ def main( sh: ghstack.shell.Shell, remote_name: str, stack: bool = False, + keep_redundant_commits: bool = False, ) -> None: params = ghstack.github_utils.parse_pull_request( @@ -56,10 +57,18 @@ def main( logging.info(f"Cherry-picking {len(commit_list)} commits from stack") for commit in commit_list: - sh.git("cherry-pick", commit) + args = ["cherry-pick"] + if keep_redundant_commits: + args.append("--keep-redundant-commits") + args.append(commit) + sh.git(*args) logging.info(f"Cherry-picked {commit}") else: # Cherry-pick just the single commit remote_orig_ref = remote_name + "/" + orig_ref - sh.git("cherry-pick", remote_orig_ref) + args = ["cherry-pick"] + if keep_redundant_commits: + args.append("--keep-redundant-commits") + args.append(remote_orig_ref) + sh.git(*args) logging.info(f"Cherry-picked {orig_ref}") diff --git a/src/ghstack/cli.py b/src/ghstack/cli.py index 5a37868..34bbb89 100644 --- a/src/ghstack/cli.py +++ b/src/ghstack/cli.py @@ -134,8 +134,13 @@ def checkout(pull_request: str) -> None: is_flag=True, help="Cherry-pick all commits from the commit to the merge-base with main branch", ) +@click.option( + "--keep-redundant-commits", + is_flag=True, + help="Keep redundant commits when cherry-picking (passed to git cherry-pick)", +) @click.argument("pull_request", metavar="PR") -def cherry_pick(stack: bool, pull_request: str) -> None: +def cherry_pick(stack: bool, keep_redundant_commits: bool, pull_request: str) -> None: """ Cherry-pick a PR """ @@ -146,6 +151,7 @@ def cherry_pick(stack: bool, pull_request: str) -> None: sh=shell, remote_name=config.remote_name, stack=stack, + keep_redundant_commits=keep_redundant_commits, ) diff --git a/src/ghstack/test_prelude.py b/src/ghstack/test_prelude.py index 78461ba..a21ebcd 100644 --- a/src/ghstack/test_prelude.py +++ b/src/ghstack/test_prelude.py @@ -240,7 +240,7 @@ def gh_unlink() -> None: ) -def gh_cherry_pick(pull_request: str, stack: bool = False) -> None: +def gh_cherry_pick(pull_request: str, stack: bool = False, keep_redundant_commits: bool = False) -> None: self = CTX return ghstack.cherry_pick.main( pull_request=pull_request, @@ -248,6 +248,7 @@ def gh_cherry_pick(pull_request: str, stack: bool = False) -> None: sh=self.sh, remote_name="origin", stack=stack, + keep_redundant_commits=keep_redundant_commits, )