Skip to content

Commit c4b86f7

Browse files
committed
Suggest sliding out non-existent branches in status and other contexts
1 parent 70d0087 commit c4b86f7

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release notes
22

3+
## New in git-machete 2.15.4
4+
5+
- improved: slide-out is suggested in certain contexts (like `status`) in case a non-existent branch is found in .git/machete
6+
37
## New in git-machete 2.15.3
48

59
- changed: skip verification of managed branches for `anno` and `show`

git_machete/cmd.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def safe_input(msg):
116116

117117

118118
def ask_if(msg, opt_yes_msg, apply_fmt=True):
119-
if opt_yes:
119+
if opt_yes and opt_yes_msg:
120120
print(fmt(opt_yes_msg) if apply_fmt else opt_yes_msg)
121121
return 'y'
122122
return safe_input(fmt(msg) if apply_fmt else msg).lower()
@@ -322,6 +322,7 @@ def read_definition_file(verify_branches=True):
322322
last_depth = -1
323323
hint = "Edit the definition file manually with `git machete edit`"
324324

325+
invalid_branches = []
325326
for idx, l in enumerate(lines):
326327
pfx = "".join(itertools.takewhile(str.isspace, l))
327328
if pfx and not indent:
@@ -335,8 +336,7 @@ def read_definition_file(verify_branches=True):
335336
raise MacheteException("%s, line %i: branch `%s` re-appears in the tree definition. %s" %
336337
(definition_file_path, idx + 1, b, hint))
337338
if verify_branches and b not in local_branches():
338-
raise MacheteException("%s, line %i: `%s` is not a local branch. %s" %
339-
(definition_file_path, idx + 1, b, hint))
339+
invalid_branches += [b]
340340
managed_branches += [b]
341341

342342
if pfx:
@@ -366,6 +366,47 @@ def read_definition_file(verify_branches=True):
366366
else:
367367
roots += [b]
368368

369+
if not invalid_branches:
370+
return
371+
372+
if len(invalid_branches) == 1:
373+
ans = ask_if("Skipping `" + invalid_branches[0] +
374+
"` which is not a local branch (perhaps it has been deleted?).\n" +
375+
"Slide it out from the definition file?" +
376+
pretty_choices("y", "e[dit]", "N"), opt_yes_msg=None)
377+
else:
378+
ans = ask_if("Skipping " + ", ".join("`" + b + "`" for b in invalid_branches) +
379+
" which are not local branches (perhaps they have been deleted?).\n" +
380+
"Slide them out from the definition file?" +
381+
pretty_choices("y", "e[dit]", "N"), opt_yes_msg=None)
382+
383+
def recursive_slide_out_invalid_branches(b):
384+
new_down_branches = flat_map(recursive_slide_out_invalid_branches, down_branches.get(b) or [])
385+
if b in invalid_branches:
386+
if b in down_branches:
387+
del down_branches[b]
388+
if b in annotations:
389+
del annotations[b]
390+
if b in up_branch:
391+
for d in new_down_branches:
392+
up_branch[d] = up_branch[b]
393+
del up_branch[b]
394+
else:
395+
for d in new_down_branches:
396+
del up_branch[d]
397+
return new_down_branches
398+
else:
399+
down_branches[b] = new_down_branches
400+
return [b]
401+
402+
roots = flat_map(recursive_slide_out_invalid_branches, roots)
403+
managed_branches = excluding(managed_branches, invalid_branches)
404+
if ans in ('y', 'yes'):
405+
save_definition_file()
406+
elif ans in ('e', 'edit'):
407+
edit()
408+
read_definition_file(verify_branches)
409+
369410

370411
def render_tree():
371412
global roots, down_branches, indent, annotations

0 commit comments

Comments
 (0)