Skip to content

Commit dff546d

Browse files
committed
Don't allow submit in the middle of a rebase
1 parent b569b3d commit dff546d

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/stack_pr/cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
get_current_branch_name,
7070
get_gh_username,
7171
get_uncommitted_changes,
72+
is_rebase_in_progress,
7273
)
7374
from stack_pr.shell_commands import (
7475
get_command_output,
@@ -170,6 +171,10 @@
170171
171172
Please commit or stash them before working with stacks.
172173
"""
174+
ERROR_REBASE_IN_PROGRESS = """Cannot submit while in the middle of a rebase.
175+
176+
Please complete or abort the current rebase first.
177+
"""
173178
UPDATE_STACK_TIP = """
174179
If you'd like to push your local changes first, you can use the following command to update the stack:
175180
$ stack-pr export -B {top_commit}~{stack_size} -H {top_commit}"""
@@ -933,6 +938,11 @@ def command_submit(
933938
a draft.
934939
"""
935940
log(h("SUBMIT"), level=1)
941+
942+
if is_rebase_in_progress():
943+
error(ERROR_REBASE_IN_PROGRESS)
944+
sys.exit(1)
945+
936946
current_branch = get_current_branch_name()
937947

938948
if should_update_local_base(

src/stack_pr/git.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,20 @@ def get_changed_dirs(
213213
files changed.
214214
"""
215215
return {Path(file.parts[0]) for file in get_changed_files(base, repo_dir)}
216+
217+
218+
def is_rebase_in_progress(repo_dir: Path | None = None) -> bool:
219+
"""Check if a rebase operation is currently in progress.
220+
221+
Args:
222+
repo_dir: path to the repo. Defaults to the current working directory.
223+
224+
Returns:
225+
True if a rebase is in progress, False otherwise.
226+
"""
227+
if repo_dir is None:
228+
git_dir = Path(".git")
229+
else:
230+
git_dir = repo_dir / ".git"
231+
232+
return (git_dir / "rebase-merge").exists() or (git_dir / "rebase-apply").exists()

tests/test_misc.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import tempfile
23
from pathlib import Path
34

45
sys.path.append(str(Path(__file__).parent.parent / "src"))
@@ -12,7 +13,7 @@
1213
get_gh_username,
1314
get_taken_branch_ids,
1415
)
15-
from stack_pr.git import git_config
16+
from stack_pr.git import git_config, is_rebase_in_progress
1617

1718

1819
@pytest.fixture(scope="module")
@@ -90,3 +91,35 @@ def test_generate_available_branch_name() -> None:
9091
"refs/remotes/origin/TestBot-stack-134",
9192
]
9293
assert generate_available_branch_name(refs, template) == "TestBot-stack-135"
94+
95+
96+
def test_is_rebase_in_progress() -> None:
97+
"""Test the is_rebase_in_progress function with different git states."""
98+
with tempfile.TemporaryDirectory() as temp_dir:
99+
repo_dir = Path(temp_dir)
100+
git_dir = repo_dir / ".git"
101+
git_dir.mkdir()
102+
103+
# Test no rebase in progress
104+
assert not is_rebase_in_progress(repo_dir)
105+
106+
# Test rebase-merge directory exists
107+
rebase_merge_dir = git_dir / "rebase-merge"
108+
rebase_merge_dir.mkdir()
109+
assert is_rebase_in_progress(repo_dir)
110+
111+
# Clean up and test rebase-apply directory
112+
rebase_merge_dir.rmdir()
113+
assert not is_rebase_in_progress(repo_dir)
114+
115+
rebase_apply_dir = git_dir / "rebase-apply"
116+
rebase_apply_dir.mkdir()
117+
assert is_rebase_in_progress(repo_dir)
118+
119+
# Test both directories exist
120+
rebase_merge_dir.mkdir()
121+
assert is_rebase_in_progress(repo_dir)
122+
123+
# Test with None repo_dir (current directory)
124+
# This should not raise an error even if .git doesn't exist in cwd
125+
assert not is_rebase_in_progress(None)

0 commit comments

Comments
 (0)