Skip to content

Commit 94832dd

Browse files
Add --amend option for commit subcommand
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 58bd4e0 commit 94832dd

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

git_sim/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def main():
133133
type=str,
134134
default="New commit",
135135
)
136+
commit.add_argument(
137+
"--amend",
138+
help="Amend the last commit message, must be used with the -m flag",
139+
action="store_true",
140+
)
136141

137142
stash = subparsers.add_parser("stash", help="stash -h")
138143
stash.add_argument(

git_sim/git_sim_base_command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self, scene):
1313

1414
self.drawnCommits = {}
1515
self.drawnRefs = {}
16+
self.drawnCommitIds = {}
1617
self.commits = []
1718
self.zoomOuts = 0
1819
self.toFadeOut = m.Group()
@@ -219,6 +220,9 @@ def draw_commit(
219220
)
220221
commitId.next_to(circle, m.UP)
221222

223+
if commit != "dark":
224+
self.drawnCommitIds[commit.hexsha] = commitId
225+
222226
message = m.Text(
223227
"\n".join(
224228
commitMessage[j : j + 20] for j in range(0, len(commitMessage), 20)

git_sim/git_sim_commit.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,69 @@ class GitSimCommit(GitSimBaseCommand):
1111
def __init__(self, scene):
1212
super().__init__(scene)
1313
self.maxrefs = 2
14-
self.defaultNumCommits = 4
15-
self.numCommits = 4
14+
self.defaultNumCommits = 4 if not self.scene.args.amend else 5
15+
self.numCommits = 4 if not self.scene.args.amend else 5
1616
self.hide_first_tag = True
1717

1818
try:
1919
self.selected_branches.append(self.repo.active_branch.name)
2020
except TypeError:
2121
pass
2222

23+
if self.scene.args.amend and self.scene.args.message == "New commit":
24+
print(
25+
"git-sim error: The --amend flag must be used with the -m flag to specify the amended commit message."
26+
)
27+
sys.exit(1)
28+
2329
def execute(self):
2430
print(
2531
"Simulating: git "
2632
+ self.scene.args.subcommand
33+
+ (" --amend" if self.scene.args.amend else "")
2734
+ ' -m "'
2835
+ self.scene.args.message
2936
+ '"'
3037
)
3138

3239
self.show_intro()
3340
self.get_commits()
41+
42+
if self.scene.args.amend:
43+
tree = self.repo.tree()
44+
amended = git.Commit.create_from_tree(
45+
self.repo,
46+
tree,
47+
self.scene.args.message,
48+
)
49+
self.commits[0] = amended
50+
3451
self.parse_commits(self.commits[self.i])
3552
self.center_frame_on_commit(self.commits[0])
36-
self.setup_and_draw_parent(self.commits[0], self.scene.args.message)
53+
54+
if not self.scene.args.amend:
55+
self.setup_and_draw_parent(self.commits[0], self.scene.args.message)
56+
else:
57+
self.draw_ref(self.commits[0], self.drawnCommitIds[amended.hexsha])
58+
self.draw_ref(
59+
self.commits[0],
60+
self.drawnRefs["HEAD"],
61+
text=self.repo.active_branch.name,
62+
color=m.GREEN,
63+
)
64+
3765
self.recenter_frame()
3866
self.scale_frame()
39-
self.reset_head_branch("abcdef")
40-
self.vsplit_frame()
41-
self.setup_and_draw_zones(
42-
first_column_name="Working directory",
43-
second_column_name="Staging area",
44-
third_column_name="New commit",
45-
)
67+
68+
if not self.scene.args.amend:
69+
self.reset_head_branch("abcdef")
70+
self.vsplit_frame()
71+
self.setup_and_draw_zones(
72+
first_column_name="Working directory",
73+
second_column_name="Staging area",
74+
third_column_name="New commit",
75+
)
76+
4677
self.fadeout()
4778
self.show_outro()
4879

0 commit comments

Comments
 (0)