Skip to content

Commit 58bd4e0

Browse files
Allow specification of remote tracking branches in merge and display them as branches in other commands
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 0357116 commit 58bd4e0

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

git_sim/git_sim_base_command.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,26 @@ def draw_head(self, commit, commitId):
297297

298298
def draw_branch(self, commit):
299299
x = 0
300-
branches = [branch.name for branch in self.repo.heads]
300+
301+
remote_tracking_branches = self.get_remote_tracking_branches()
302+
303+
branches = [branch.name for branch in self.repo.heads] + list(
304+
remote_tracking_branches.keys()
305+
)
306+
301307
for selected_branch in self.selected_branches:
302308
branches.insert(0, branches.pop(branches.index(selected_branch)))
303309

304310
for branch in branches:
305-
306-
if commit.hexsha == self.repo.heads[branch].commit.hexsha:
311+
# Use forward slash to check if branch is local or remote tracking
312+
# and draw the branch label if its hexsha matches the current commit
313+
if (
314+
"/" not in branch # local branch
315+
and commit.hexsha == self.repo.heads[branch].commit.hexsha
316+
) or (
317+
"/" in branch # remote tracking branch
318+
and commit.hexsha == remote_tracking_branches[branch]
319+
):
307320
branchText = m.Text(
308321
branch, font="Monospace", font_size=20, color=self.scene.fontColor
309322
)
@@ -924,6 +937,15 @@ def draw_dark_ref(self):
924937
def trim_path(self, path):
925938
return (path[:5] + "..." + path[-15:]) if len(path) > 20 else path
926939

940+
def get_remote_tracking_branches(self):
941+
remote_refs = [remote.refs for remote in self.repo.remotes]
942+
remote_tracking_branches = {}
943+
for reflist in remote_refs:
944+
for ref in reflist:
945+
if "HEAD" not in ref.name and ref.name not in remote_tracking_branches:
946+
remote_tracking_branches[ref.name] = ref.commit.hexsha
947+
return remote_tracking_branches
948+
927949

928950
class DottedLine(m.Line):
929951
def __init__(self, *args, dot_spacing=0.4, dot_kwargs={}, **kwargs):

git_sim/git_sim_merge.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ def execute(self):
5656
self.orig_commits = self.commits
5757
self.get_commits(start=self.scene.args.branch[0])
5858

59-
if self.scene.args.branch[0] in self.repo.git.branch(
60-
"--contains", self.orig_commits[0].hexsha
61-
):
62-
self.ff = True
59+
# Use forward slash to determine if supplied branch arg is local or remote tracking branch
60+
if "/" not in self.scene.args.branch[0]:
61+
if self.scene.args.branch[0] in self.repo.git.branch(
62+
"--contains", self.orig_commits[0].hexsha
63+
):
64+
self.ff = True
65+
else:
66+
if self.scene.args.branch[0] in self.repo.git.branch(
67+
"-r", "--contains", self.orig_commits[0].hexsha
68+
):
69+
self.ff = True
6370

6471
if self.ff:
6572
self.get_commits(start=self.scene.args.branch[0])
@@ -83,7 +90,7 @@ def execute(self):
8390
)
8491
self.draw_ref(
8592
self.commits[0],
86-
self.drawnRefs["HEAD"] if self.scene.args.no_ff else self.topref,
93+
self.drawnRefs["HEAD"],
8794
text=self.repo.active_branch.name,
8895
color=m.GREEN,
8996
)

0 commit comments

Comments
 (0)