Skip to content

Commit d7f09e8

Browse files
authored
show current branch in print, make sync take you back to original branch (#6)
1 parent c34e6f7 commit d7f09e8

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

gitstack.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def git_get_current_branch() -> str:
6363
return p.stdout.decode().strip()
6464

6565

66+
def print_branch_level(branch: str, current_branch: str, depth: int) -> None:
67+
"""Prints a branch line as part of a tree"""
68+
branch_line = f"\u001b[32m{branch}\u001b[0m" if branch == current_branch else branch
69+
level_indicator = " " * (2 * (depth - 1)) + "↳ "
70+
print(f"{level_indicator}{branch_line}" if depth > 0 else branch_line)
71+
72+
6673
class NoValidTrunkError(Exception):
6774
"""Error for when none of the trunk candidates match"""
6875

@@ -117,10 +124,9 @@ def wrapup(self) -> None:
117124

118125
def print_stack(self):
119126
"""Pretty print the entire stack"""
127+
current_branch = git_get_current_branch()
120128
self._traverse_stack(
121-
lambda branch, depth: print(" " * (2 * (depth - 1)) + "↳ " + branch)
122-
if depth > 0
123-
else print(branch)
129+
lambda branch, depth: print_branch_level(branch, current_branch, depth)
124130
)
125131

126132
def create_branch(self, branch: str, parent) -> None:
@@ -211,7 +217,15 @@ def switch_to_child(self) -> None:
211217

212218
def sync(self):
213219
"""Rebase all branches on top of current trunk"""
220+
current_branch = git_get_current_branch()
214221
self._traverse_stack(lambda branch, depth: self._check_and_rebase(branch))
222+
# switch back to original branch once done
223+
subprocess.run(
224+
["git", "switch", current_branch],
225+
check=True,
226+
stdout=sys.stdout.buffer,
227+
stderr=sys.stderr.buffer,
228+
)
215229

216230
def _traverse_stack(self, fn: Callable[[str, int], None]):
217231
"""DFS through the gitstack from trunk, calling a function on each branch"""

0 commit comments

Comments
 (0)