Skip to content

Commit 9e1f725

Browse files
Handle push subcommand failure scenario 'non-fast-forward'
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent a157af1 commit 9e1f725

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

git_sim/git_sim_base_command.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,10 @@ def draw_branch(self, commit, i, make_branches_remote=False):
382382
self.is_remote_tracking_branch(branch) # remote tracking branch
383383
and commit.hexsha == remote_tracking_branches[branch]
384384
):
385+
text = (make_branches_remote + "/" + branch) if (make_branches_remote and not self.is_remote_tracking_branch(branch)) else branch
386+
385387
branchText = m.Text(
386-
branch if not make_branches_remote else make_branches_remote + "/" + branch, font="Monospace", font_size=20, color=self.fontColor
388+
text, font="Monospace", font_size=20, color=self.fontColor
387389
)
388390
branchRec = m.Rectangle(
389391
color=m.GREEN,
@@ -1151,13 +1153,18 @@ def color_by(self, offset=0):
11511153
elif settings.color_by == "branch":
11521154
pass
11531155

1154-
elif settings.color_by == "notlocal":
1156+
elif settings.color_by == "notlocal1":
11551157
for commit_id in self.drawnCommits:
11561158
try:
11571159
self.orig_repo.commit(commit_id)
11581160
except ValueError:
11591161
self.drawnCommits[commit_id].set_color(m.GOLD)
11601162

1163+
elif settings.color_by == "notlocal2":
1164+
for commit_id in self.drawnCommits:
1165+
if not self.orig_repo.is_ancestor(commit_id, "HEAD"):
1166+
self.drawnCommits[commit_id].set_color(m.GOLD)
1167+
11611168
def add_group_to_author_groups(self, author, group):
11621169
if author not in self.author_groups:
11631170
self.author_groups[author] = [group]

git_sim/push.py

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def __init__(self, remote: str = None, branch: str = None):
2222
self.remote = remote
2323
self.branch = branch
2424
settings.max_branches_per_commit = 2
25-
settings.color_by = "notlocal"
2625

2726
if self.remote and self.remote not in self.repo.remotes:
2827
print("git-sim error: no remote with name '" + self.remote + "'")
@@ -68,19 +67,30 @@ def construct(self):
6867

6968
# Push the local clone into the local clone of the remote repo
7069
push_result = 0
70+
self.orig_repo = None
7171
try:
7272
self.repo.git.push(self.remote, self.branch)
7373
# If push fails...
7474
except git.GitCommandError as e:
75-
if "rejected" in e.stderr and "fetch first" in e.stderr:
75+
if "rejected" in e.stderr and ("fetch first" in e.stderr):
7676
push_result = 1
7777
self.orig_repo = self.repo
7878
self.repo = self.remote_repo
79+
settings.color_by = "notlocal1"
80+
elif "rejected" in e.stderr and ("non-fast-forward" in e.stderr):
81+
push_result = 2
82+
self.orig_repo = self.repo
83+
self.repo = self.remote_repo
84+
settings.color_by = "notlocal2"
7985
else:
8086
print(f"git-sim error: git push failed: {e.stderr}")
87+
return
8188

8289
head_commit = self.get_commit()
83-
self.parse_commits(head_commit, make_branches_remote=(self.remote if self.remote else self.repo.remotes[0].name))
90+
if push_result > 0:
91+
self.parse_commits(head_commit, make_branches_remote=(self.remote if self.remote else self.repo.remotes[0].name))
92+
else:
93+
self.parse_commits(head_commit)
8494
self.recenter_frame()
8595
self.scale_frame()
8696
self.failed_push(push_result)
@@ -134,17 +144,53 @@ def failed_push(self, push_result):
134144
weight=m.BOLD,
135145
)
136146
text4.move_to(text3.get_center()).shift(m.DOWN / 2)
147+
texts = [text1, text2, text3, text4]
137148

138-
self.toFadeOut.add(text1)
139-
self.toFadeOut.add(text2)
140-
self.toFadeOut.add(text3)
141-
self.toFadeOut.add(text4)
142-
self.recenter_frame()
143-
self.scale_frame()
144-
if settings.animate:
145-
self.play(m.AddTextLetterByLetter(text2), m.AddTextLetterByLetter(text2), m.AddTextLetterByLetter(text3), m.AddTextLetterByLetter(text4))
146-
else:
147-
self.add(text1, text2, text3, text4)
149+
elif push_result == 2:
150+
text1 = m.Text(
151+
f"'git push' failed since the tip of your current branch is behind the remote.",
152+
font="Monospace",
153+
font_size=20,
154+
color=self.fontColor,
155+
weight=m.BOLD,
156+
)
157+
text1.move_to([self.camera.frame.get_center()[0], 5, 0])
158+
159+
text2 = m.Text(
160+
f"Run 'git pull' (or 'git-sim pull' to simulate first) and then try again.",
161+
font="Monospace",
162+
font_size=20,
163+
color=self.fontColor,
164+
weight=m.BOLD,
165+
)
166+
text2.move_to(text1.get_center()).shift(m.DOWN / 2)
167+
168+
text3 = m.Text(
169+
f"Gold commits exist are ahead of your current branch tip (need to be pulled).",
170+
font="Monospace",
171+
font_size=20,
172+
color=m.GOLD,
173+
weight=m.BOLD,
174+
)
175+
text3.move_to(text2.get_center()).shift(m.DOWN / 2)
176+
177+
text4 = m.Text(
178+
f"Red commits are up to date in both local and remote branches.",
179+
font="Monospace",
180+
font_size=20,
181+
color=m.RED,
182+
weight=m.BOLD,
183+
)
184+
text4.move_to(text3.get_center()).shift(m.DOWN / 2)
185+
texts = [text1, text2, text3, text4]
186+
187+
self.toFadeOut.add(*texts)
188+
self.recenter_frame()
189+
self.scale_frame()
190+
if settings.animate:
191+
self.play(*[m.AddTextLetterByLetter(t) for t in texts])
192+
else:
193+
self.add(*texts)
148194

149195

150196

0 commit comments

Comments
 (0)