Skip to content

Commit 0f945fe

Browse files
Fix stash pop and stash apply subcommands
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 165793e commit 0f945fe

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/git_sim/commands.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,15 @@ def stash(
366366
default=None,
367367
help="The name of the file to stash changes for",
368368
),
369+
stash_index: str = typer.Argument(
370+
default="0",
371+
help="Stash index",
372+
),
369373
):
370374
from git_sim.stash import Stash
371375

372376
settings.hide_first_tag = True
373-
scene = Stash(files=files, command=command)
377+
scene = Stash(files=files, command=command, stash_index=stash_index)
374378
handle_animations(scene=scene)
375379

376380

src/git_sim/stash.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import sys
23
from enum import Enum
34
import manim as m
@@ -10,14 +11,21 @@
1011

1112

1213
class Stash(GitSimBaseCommand):
13-
def __init__(self, files: List[str], command: StashSubCommand):
14+
def __init__(self, files: List[str], command: StashSubCommand, stash_index: int):
1415
super().__init__()
1516
self.files = files
1617
self.no_files = True if not self.files else False
1718
self.command = command
1819
settings.hide_merged_branches = True
1920
self.n = self.n_default
2021

22+
self.stash_index = self.parse_stash_format(stash_index)
23+
if self.stash_index is None:
24+
print(
25+
"git-sim error: specify stash index as either integer or stash@{i}"
26+
)
27+
sys.exit()
28+
2129
try:
2230
self.selected_branches.append(self.repo.active_branch.name)
2331
except TypeError:
@@ -44,7 +52,7 @@ def __init__(self, files: List[str], command: StashSubCommand):
4452
and not settings.quiet
4553
):
4654
print(
47-
"Files are not required in apply/pop subcommand. Ignoring the file list....."
55+
"Files are not required in apply/pop subcommand. Ignoring the file list..."
4856
)
4957

5058
self.cmd += f"{type(self).__name__.lower()} {self.command.value if self.command else ''} {' '.join(self.files) if not self.no_files else ''}"
@@ -147,17 +155,21 @@ def populate_zones(
147155
thirdColumnArrowMap={},
148156
):
149157
if self.command in [StashSubCommand.POP, StashSubCommand.APPLY]:
150-
for x in self.repo.index.diff(None):
151-
thirdColumnFileNames.add(x.a_path)
152-
firstColumnFileNames.add(x.a_path)
153-
thirdColumnArrowMap[x.a_path] = m.Arrow(
158+
try:
159+
stashedFileNames = self.repo.git.stash("show", "--name-only", self.stash_index)
160+
stashedFileNames = stashedFileNames.split("\n")
161+
except:
162+
print(f"git-sim error: No stash entry with index {self.stashIndex} exists in stash")
163+
sys.exit()
164+
for s in stashedFileNames:
165+
thirdColumnFileNames.add(s)
166+
firstColumnFileNames.add(s)
167+
thirdColumnArrowMap[s] = m.Arrow(
154168
stroke_width=3, color=self.fontColor
155169
)
156-
157-
for y in self.repo.index.diff("HEAD"):
158-
firstColumnFileNames.add(y.a_path)
159-
thirdColumnFileNames.add(y.a_path)
160-
thirdColumnArrowMap[y.a_path] = m.Arrow(
170+
firstColumnFileNames.add(s)
171+
thirdColumnFileNames.add(s)
172+
thirdColumnArrowMap[s] = m.Arrow(
161173
stroke_width=3, color=self.fontColor
162174
)
163175

@@ -179,3 +191,14 @@ def populate_zones(
179191
secondColumnArrowMap[y.a_path] = m.Arrow(
180192
stroke_width=3, color=self.fontColor
181193
)
194+
195+
def parse_stash_format(self, s):
196+
# Regular expression to match either a plain integer or stash@{integer}
197+
match = re.match(r"^(?:stash@\{(\d+)\}|\b(\d+)\b)$", s)
198+
if match:
199+
# match.group(1) is the integer in the stash@{integer} format
200+
# match.group(2) is the integer if it's just a plain number
201+
# One of these groups will be None, the other will have our number as a string
202+
number_str = match.group(1) or match.group(2)
203+
return int(number_str)
204+
return None

0 commit comments

Comments
 (0)