|
| 1 | +# |
| 2 | +# Copyright © 2021-2026 Mergify SAS |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import os |
| 19 | +import re |
| 20 | +import subprocess |
| 21 | +from typing import TYPE_CHECKING |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from mergify_cli.stack.edit import stack_edit |
| 26 | + |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + import pathlib |
| 30 | + |
| 31 | + |
| 32 | +def _run_git(*args: str, cwd: pathlib.Path | None = None) -> str: |
| 33 | + return subprocess.check_output( |
| 34 | + ["git", *args], |
| 35 | + text=True, |
| 36 | + cwd=cwd, |
| 37 | + ).strip() |
| 38 | + |
| 39 | + |
| 40 | +def _create_commit( |
| 41 | + repo: pathlib.Path, |
| 42 | + filename: str, |
| 43 | + content: str, |
| 44 | + message: str, |
| 45 | +) -> tuple[str, str | None]: |
| 46 | + """Create a commit and return (sha, change_id).""" |
| 47 | + (repo / filename).write_text(content) |
| 48 | + _run_git("add", filename, cwd=repo) |
| 49 | + _run_git("commit", "-m", message, cwd=repo) |
| 50 | + sha = _run_git("rev-parse", "HEAD", cwd=repo) |
| 51 | + body = _run_git("log", "-1", "--format=%b", "HEAD", cwd=repo) |
| 52 | + change_id_match = re.search(r"Change-Id: (I[0-9a-z]{40})", body) |
| 53 | + return sha, change_id_match.group(1) if change_id_match else None |
| 54 | + |
| 55 | + |
| 56 | +def _setup_tracking(repo: pathlib.Path) -> None: |
| 57 | + """Create a bare origin and set up tracking for the current branch.""" |
| 58 | + origin_path = repo.parent / f"{repo.name}_origin.git" |
| 59 | + _run_git("init", "--bare", str(origin_path)) |
| 60 | + _run_git("remote", "add", "origin", str(origin_path), cwd=repo) |
| 61 | + _run_git("push", "origin", "main", cwd=repo) |
| 62 | + _run_git("branch", "--set-upstream-to=origin/main", cwd=repo) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture |
| 66 | +def stack_repo( |
| 67 | + git_repo_with_hooks: pathlib.Path, |
| 68 | +) -> tuple[pathlib.Path, list[tuple[str, str | None]]]: |
| 69 | + """Create a repo with 3 commits (A, B, C) on a feature branch.""" |
| 70 | + repo = git_repo_with_hooks |
| 71 | + |
| 72 | + # Create an initial commit on main |
| 73 | + (repo / "init.txt").write_text("init") |
| 74 | + _run_git("add", "init.txt", cwd=repo) |
| 75 | + _run_git("commit", "-m", "Initial commit", cwd=repo) |
| 76 | + |
| 77 | + _setup_tracking(repo) |
| 78 | + |
| 79 | + # Create a feature branch |
| 80 | + _run_git("checkout", "-b", "feature", "main", cwd=repo) |
| 81 | + _run_git("branch", "--set-upstream-to=origin/main", cwd=repo) |
| 82 | + |
| 83 | + # Create 3 commits |
| 84 | + commits = [] |
| 85 | + for label, filename in [("A", "a.txt"), ("B", "b.txt"), ("C", "c.txt")]: |
| 86 | + sha, cid = _create_commit(repo, filename, f"content {label}", f"Commit {label}") |
| 87 | + commits.append((sha, cid)) |
| 88 | + |
| 89 | + return repo, commits |
| 90 | + |
| 91 | + |
| 92 | +class TestStackEdit: |
| 93 | + async def test_edit_stops_at_target_commit( |
| 94 | + self, |
| 95 | + stack_repo: tuple[pathlib.Path, list[tuple[str, str | None]]], |
| 96 | + ) -> None: |
| 97 | + """Editing a mid-stack commit stops the rebase at that commit.""" |
| 98 | + repo, commits = stack_repo |
| 99 | + os.chdir(repo) |
| 100 | + |
| 101 | + sha_b = commits[1][0][:12] |
| 102 | + await stack_edit(commit_prefix=sha_b) |
| 103 | + |
| 104 | + # Rebase should have stopped — HEAD is now the target commit |
| 105 | + head_subject = _run_git("log", "-1", "--format=%s", cwd=repo) |
| 106 | + assert head_subject == "Commit B" |
| 107 | + |
| 108 | + # Verify we're mid-rebase |
| 109 | + rebase_dir = repo / ".git" / "rebase-merge" |
| 110 | + assert rebase_dir.exists() |
| 111 | + |
| 112 | + # Clean up the rebase |
| 113 | + _run_git("rebase", "--abort", cwd=repo) |
| 114 | + |
| 115 | + async def test_edit_stops_at_first_commit( |
| 116 | + self, |
| 117 | + stack_repo: tuple[pathlib.Path, list[tuple[str, str | None]]], |
| 118 | + ) -> None: |
| 119 | + """Editing the first commit in the stack works.""" |
| 120 | + repo, commits = stack_repo |
| 121 | + os.chdir(repo) |
| 122 | + |
| 123 | + sha_a = commits[0][0][:12] |
| 124 | + await stack_edit(commit_prefix=sha_a) |
| 125 | + |
| 126 | + head_subject = _run_git("log", "-1", "--format=%s", cwd=repo) |
| 127 | + assert head_subject == "Commit A" |
| 128 | + |
| 129 | + rebase_dir = repo / ".git" / "rebase-merge" |
| 130 | + assert rebase_dir.exists() |
| 131 | + |
| 132 | + _run_git("rebase", "--abort", cwd=repo) |
| 133 | + |
| 134 | + async def test_edit_stops_at_last_commit( |
| 135 | + self, |
| 136 | + stack_repo: tuple[pathlib.Path, list[tuple[str, str | None]]], |
| 137 | + ) -> None: |
| 138 | + """Editing the last (HEAD) commit in the stack works.""" |
| 139 | + repo, commits = stack_repo |
| 140 | + os.chdir(repo) |
| 141 | + |
| 142 | + sha_c = commits[2][0][:12] |
| 143 | + await stack_edit(commit_prefix=sha_c) |
| 144 | + |
| 145 | + head_subject = _run_git("log", "-1", "--format=%s", cwd=repo) |
| 146 | + assert head_subject == "Commit C" |
| 147 | + |
| 148 | + rebase_dir = repo / ".git" / "rebase-merge" |
| 149 | + assert rebase_dir.exists() |
| 150 | + |
| 151 | + _run_git("rebase", "--abort", cwd=repo) |
| 152 | + |
| 153 | + async def test_edit_by_change_id( |
| 154 | + self, |
| 155 | + stack_repo: tuple[pathlib.Path, list[tuple[str, str | None]]], |
| 156 | + ) -> None: |
| 157 | + """Editing by Change-Id prefix works.""" |
| 158 | + repo, commits = stack_repo |
| 159 | + os.chdir(repo) |
| 160 | + |
| 161 | + cid_b = commits[1][1] |
| 162 | + assert cid_b is not None |
| 163 | + |
| 164 | + await stack_edit(commit_prefix=cid_b[:8]) |
| 165 | + |
| 166 | + head_subject = _run_git("log", "-1", "--format=%s", cwd=repo) |
| 167 | + assert head_subject == "Commit B" |
| 168 | + |
| 169 | + _run_git("rebase", "--abort", cwd=repo) |
| 170 | + |
| 171 | + async def test_edit_unknown_prefix_exits( |
| 172 | + self, |
| 173 | + stack_repo: tuple[pathlib.Path, list[tuple[str, str | None]]], |
| 174 | + ) -> None: |
| 175 | + """Unknown commit prefix causes exit.""" |
| 176 | + repo, _commits = stack_repo |
| 177 | + os.chdir(repo) |
| 178 | + |
| 179 | + with pytest.raises(SystemExit) as exc_info: |
| 180 | + await stack_edit(commit_prefix="deadbeef1234") |
| 181 | + assert exc_info.value.code == 1 |
| 182 | + |
| 183 | + async def test_edit_empty_stack( |
| 184 | + self, |
| 185 | + git_repo_with_hooks: pathlib.Path, |
| 186 | + ) -> None: |
| 187 | + """Empty stack prints message and returns.""" |
| 188 | + repo = git_repo_with_hooks |
| 189 | + |
| 190 | + (repo / "init.txt").write_text("init") |
| 191 | + _run_git("add", "init.txt", cwd=repo) |
| 192 | + _run_git("commit", "-m", "Initial commit", cwd=repo) |
| 193 | + |
| 194 | + _setup_tracking(repo) |
| 195 | + |
| 196 | + _run_git("checkout", "-b", "feature", "main", cwd=repo) |
| 197 | + _run_git("branch", "--set-upstream-to=origin/main", cwd=repo) |
| 198 | + |
| 199 | + os.chdir(repo) |
| 200 | + |
| 201 | + # Should return without error — no commits to edit |
| 202 | + await stack_edit(commit_prefix="abc") |
0 commit comments