commit --amend initial version
This commit is contained in:
@@ -89,6 +89,7 @@ class Window:
|
||||
def __init__(self):
|
||||
self.panel = None
|
||||
self.input = None
|
||||
self.input_view = None
|
||||
self.commands = []
|
||||
|
||||
def focus_view(self, view):
|
||||
@@ -102,7 +103,10 @@ class Window:
|
||||
|
||||
def show_input_panel(self, caption, initial_text, on_done, on_change, on_cancel):
|
||||
self.input = (caption, initial_text, on_done, on_change, on_cancel)
|
||||
return View()
|
||||
self.input_view = View(initial_text)
|
||||
# the real panel hands its prefill back selected, which is why callers clear the selection
|
||||
self.input_view.sel().add(Region(0, len(initial_text)))
|
||||
return self.input_view
|
||||
|
||||
|
||||
class View:
|
||||
|
||||
@@ -206,6 +206,71 @@ def test_commit_prompts_then_commits(monkeypatch):
|
||||
assert refreshed == ["committed 2 files"]
|
||||
|
||||
|
||||
def prompt_amend(monkeypatch, st, head="last subject"):
|
||||
"""Press m; git reports `head` as HEAD's message. Returns (window, amended, refreshed)"""
|
||||
amended, refreshed = [], []
|
||||
monkeypatch.setattr(commands.git, "head_message", lambda repo: head)
|
||||
monkeypatch.setattr(
|
||||
commands.git, "amend", lambda repo, message, allow_empty=False: amended.append((repo, message, allow_empty))
|
||||
)
|
||||
monkeypatch.setattr(commands, "refresh_listing", lambda view, st, message=None: refreshed.append(message))
|
||||
window = sublime.Window()
|
||||
commands.ACTIONS["amend"](sublime.View(), window, st)
|
||||
return window, amended, refreshed
|
||||
|
||||
|
||||
def test_amend_refuses_commit_mode(monkeypatch):
|
||||
messages = []
|
||||
monkeypatch.setattr(commands.sublime, "status_message", lambda m: messages.append(m))
|
||||
st = staged_state(True)
|
||||
st.enter("abc", None)
|
||||
|
||||
window, amended, _ = prompt_amend(monkeypatch, st)
|
||||
assert messages[-1] == "vDiff: amend works on uncommitted changes only"
|
||||
assert window.input is None and amended == []
|
||||
|
||||
|
||||
def test_amend_prefills_the_subject_with_the_caret_at_its_end(monkeypatch):
|
||||
window, amended, _ = prompt_amend(monkeypatch, staged_state(True), head="fix the parser")
|
||||
|
||||
caption, initial, _, _, on_cancel = window.input
|
||||
assert caption == "Amend message" and initial == "fix the parser" and amended == []
|
||||
# the panel selects its prefill; the caret must sit after it, ready to type
|
||||
assert list(window.input_view.sel()) == [sublime.Region(len("fix the parser"))]
|
||||
# Esc is a no-op: there is no cancel callback to run
|
||||
assert on_cancel is None
|
||||
|
||||
|
||||
def test_amend_offers_the_subject_and_keeps_the_rest_verbatim(monkeypatch):
|
||||
head = "typo hree\n\nCloses #12.\n\n- a\n- b"
|
||||
window, amended, refreshed = prompt_amend(monkeypatch, staged_state(True, False, True), head=head)
|
||||
|
||||
assert window.input[1] == "typo hree" # only the first line is offered for editing
|
||||
window.input[2]("typo here")
|
||||
# everything below the first line comes back untouched, and staged files keep git's guard
|
||||
assert amended == [("/repo", "typo here" + head[len("typo hree") :], False)]
|
||||
assert refreshed == ["amended the last commit with 2 files"]
|
||||
|
||||
|
||||
def test_amend_rewords_with_nothing_staged(monkeypatch):
|
||||
window, amended, refreshed = prompt_amend(monkeypatch, staged_state(False, False))
|
||||
|
||||
window.input[2]("just a reword")
|
||||
# a reword alone opts out of git's refusal, so an empty commit can still be renamed
|
||||
assert amended == [("/repo", "just a reword", True)]
|
||||
assert refreshed == ["amended the last commit"]
|
||||
|
||||
|
||||
def test_amend_with_an_empty_message_does_nothing(monkeypatch):
|
||||
messages = []
|
||||
monkeypatch.setattr(commands.sublime, "status_message", lambda m: messages.append(m))
|
||||
window, amended, refreshed = prompt_amend(monkeypatch, staged_state(True))
|
||||
|
||||
window.input[2](" ")
|
||||
assert amended == [] and refreshed == []
|
||||
assert messages[-1] == "vDiff: amend cancelled: empty message"
|
||||
|
||||
|
||||
def test_commit_message_counts_one_file(monkeypatch):
|
||||
refreshed = []
|
||||
monkeypatch.setattr(commands.git, "commit", lambda repo, message: None)
|
||||
|
||||
@@ -200,3 +200,72 @@ def test_commit_without_staged_files_raises(repo):
|
||||
with pytest.raises(git.GitError) as info:
|
||||
git.commit(repo, "nothing to record")
|
||||
assert info.value.code != 0
|
||||
|
||||
|
||||
def message(repo):
|
||||
"""HEAD's raw message, as git stores it"""
|
||||
return git.run(repo, ["log", "-1", "--format=%B"]).strip("\n")
|
||||
|
||||
|
||||
def test_head_message_returns_the_whole_message(repo):
|
||||
assert git.head_message(repo) == "initial"
|
||||
|
||||
sh(repo, "commit", "-q", "--allow-empty", "-m", "second", "-m", "body text\nsecond line")
|
||||
assert git.head_message(repo) == "second\n\nbody text\nsecond line"
|
||||
|
||||
|
||||
def test_amend_keeps_everything_below_the_first_line_byte_for_byte(repo):
|
||||
body = "Closes #12.\n\n- a\n- b"
|
||||
sh(repo, "commit", "-q", "--allow-empty", "-m", "typo hree", "-m", body)
|
||||
before = git.run(repo, ["rev-list", "--count", "HEAD"])
|
||||
_, newline, rest = git.head_message(repo).partition("\n")
|
||||
|
||||
git.amend(repo, "typo here" + newline + rest, allow_empty=True)
|
||||
|
||||
# the paragraph the user never touched comes back exactly as git stored it
|
||||
assert message(repo) == "typo here\n\n" + body
|
||||
# an amend rewrites HEAD, it does not add a commit
|
||||
assert git.run(repo, ["rev-list", "--count", "HEAD"]) == before
|
||||
|
||||
|
||||
def test_amend_without_a_body_leaves_a_one_line_message(repo):
|
||||
git.amend(repo, "reworded")
|
||||
assert message(repo) == "reworded"
|
||||
|
||||
|
||||
def test_amend_rewords_an_empty_commit(repo):
|
||||
# git refuses an amend that leaves nothing behind unless --allow-empty says otherwise
|
||||
sh(repo, "commit", "-q", "--allow-empty", "-m", "marker")
|
||||
git.amend(repo, "marker, reworded", allow_empty=True)
|
||||
assert message(repo) == "marker, reworded"
|
||||
|
||||
|
||||
def test_amend_that_would_empty_the_commit_is_refused(repo):
|
||||
# folding staged files in keeps git's guard: only a reword opts out of it
|
||||
write(repo, "new.py", "temporary\n")
|
||||
sh(repo, "add", "new.py")
|
||||
sh(repo, "commit", "-q", "-m", "add new.py")
|
||||
sh(repo, "rm", "-q", "new.py")
|
||||
|
||||
with pytest.raises(git.GitError):
|
||||
git.amend(repo, "add new.py")
|
||||
|
||||
|
||||
def test_amend_folds_staged_files_into_head(repo):
|
||||
write(repo, "a.py", "amended\n")
|
||||
changes = {c.path: c for c in git.changed_files(repo)}
|
||||
git.stage(repo, changes["a.py"])
|
||||
before = git.run(repo, ["rev-list", "--count", "HEAD"])
|
||||
|
||||
git.amend(repo, "initial, with a.py")
|
||||
|
||||
assert git.run(repo, ["rev-list", "--count", "HEAD"]) == before
|
||||
assert not git.changed_files(repo)
|
||||
assert message(repo) == "initial, with a.py"
|
||||
|
||||
|
||||
def test_head_message_without_a_commit_raises(tmp_path):
|
||||
fresh = str(tmp_path)
|
||||
sh(fresh, "init", "-q", "-b", "main")
|
||||
with pytest.raises(git.GitError):
|
||||
git.head_message(fresh)
|
||||
|
||||
@@ -134,7 +134,7 @@ def test_help_popup_lists_every_key():
|
||||
assert view.is_popup_visible()
|
||||
for group in ("Navigate", "Panels", "Actions", "View"):
|
||||
assert ">{}<".format(group) in html
|
||||
for keys in ("] [", "n p", "o ⏎", "j k", "f", "c", "s", "u", "d", "C m", "r", "?", "esc"):
|
||||
for keys in ("] [", "n p", "o ⏎", "j k", "f", "c", "s", "u", "d", "C", "m", "r", "?", "esc"):
|
||||
assert ">{}<".format(keys) in html
|
||||
assert "next / previous change" in html and "open the real file at this line" in html
|
||||
# working-tree mode: nothing dimmed, every single-action row is a link
|
||||
@@ -152,9 +152,9 @@ def test_help_popup_dims_file_actions_in_commit_mode():
|
||||
_, html = help_popup(st)
|
||||
|
||||
assert "uncommitted only" in html
|
||||
assert html.count('class="dim"') == 4
|
||||
assert html.count('class="dim"') == 5
|
||||
assert 'href="stage"' not in html and 'href="unstage"' not in html and 'href="discard"' not in html
|
||||
assert 'href="commit"' not in html
|
||||
assert 'href="commit"' not in html and 'href="amend"' not in html
|
||||
# everything that still applies stays clickable
|
||||
assert 'href="files"' in html and 'href="open"' in html and 'href="refresh"' in html
|
||||
|
||||
|
||||
Reference in New Issue
Block a user