Skip to content

Commit

Permalink
Prune local branches that have been deleted on the remote to fix remo…
Browse files Browse the repository at this point in the history
…te branch parsing (#42)

* debug, finally got the error after rebooting

* debug

* pruning should fix it

* remove duplicate check, it's on line 198 as well

* change comment

* don't prune

* debug

* debug

* debug

* debug

* simpler

* add a second check just in case

* add a second check just in case

* add color

* add color

* add support for more colors

* try cyan

* use info

* i mean prompt :D

* add newline

* switch

* better

* debug

* support stale branches

* remove

* debug

* remove debugging

* remove debugging

* change info print

* add color to username

* use error function

* Bump version
  • Loading branch information
sshane authored Aug 13, 2020
1 parent 8875b9e commit 5a64ab8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Release 0.1.9 (2020-08-12)
=====

* Fix "branch you specified does not exist" error due to deleted remote branches
* Also add a prompt to prune the local branches that have deleted on the remote

Release 0.1.8 (2020-07-29)
=====

Expand Down
40 changes: 29 additions & 11 deletions commands/fork/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

REMOTE_ALREADY_EXISTS = 'already exists'
DEFAULT_BRANCH_START = 'HEAD branch: '
REMOTE_BRANCHES_START = 'Remote branches:'
REMOTE_BRANCHES_START = 'Remote branches:\n'
REMOTE_BRANCH_START = 'Remote branch:'


Expand Down Expand Up @@ -148,7 +148,7 @@ def _switch(self):
if flags.username is None: # branch is specified, so use current checked out fork/username
_current_fork = self.fork_params.get('current_fork')
if _current_fork is not None: # ...if available
info('No username specified, using current fork: {}'.format(COLORS.SUCCESS + _current_fork + COLORS.ENDC))
info('Assuming current fork for username: {}'.format(COLORS.SUCCESS + _current_fork + COLORS.ENDC))
flags.username = _current_fork
else:
error('Current fork is unknown, please switch to a fork first before switching between branches!')
Expand Down Expand Up @@ -182,16 +182,17 @@ def _switch(self):

# fork has been added as a remote, switch to it
if fork_in_params:
info('Fetching {}\'s latest changes...'.format(flags.username))
info('Fetching {}\'s latest changes...'.format(COLORS.SUCCESS + flags.username + COLORS.WARNING))
else:
info('Fetching {}\'s fork, this may take a sec...'.format(flags.username))
info('Fetching {}\'s fork, this may take a sec...'.format(COLORS.SUCCESS + flags.username + COLORS.WARNING))

r = run(['git', '-C', OPENPILOT_PATH, 'fetch', username])
if not r:
error('Error while fetching remote, please try again')
return
self.__add_fork(username)

self.__prune_remote_branches(username)
r = check_output(['git', '-C', OPENPILOT_PATH, 'remote', 'show', username])
remote_branches, default_remote_branch = self.__get_remote_branches(r)
if remote_branches is None:
Expand All @@ -208,17 +209,13 @@ def _switch(self):
else:
fork_branch = '{}_{}'.format(username, default_remote_branch)
branch = default_remote_branch # for command to checkout correct branch from remote, branch is previously None since user didn't specify

elif len(flags.branch) > 0:
fork_branch = f'{username}_{flags.branch}'
branch = flags.branch
if remote_branches is None:
return
if branch not in remote_branches:
error('The branch you specified does not exist!')
self.__show_similar_branches(branch, remote_branches) # if possible
return

else:
error('Error with branch!')
return
Expand Down Expand Up @@ -269,6 +266,25 @@ def __show_similar_branches(self, branch, branches):
cb = COLORS.CYAN + cb
print(' - {}{}'.format(cb, COLORS.ENDC))

def __prune_remote_branches(self, username): # remove deleted remote branches locally
r = check_output(['git', '-C', OPENPILOT_PATH, 'remote', 'prune', username, '--dry-run'])
if r.output == '': # nothing to prune
return
branches_to_prune = [b.strip() for b in r.output.split('\n') if 'would prune' in b]
branches_to_prune = [b[b.index(username):] for b in branches_to_prune]

error('\nDeleted remote branches detected:')
for b in branches_to_prune:
print(COLORS.CYAN + ' - {}'.format(b) + COLORS.ENDC)
warning('\nWould you like to delete them locally?')
if is_affirmative():
r = check_output(['git', '-C', OPENPILOT_PATH, 'remote', 'prune', username])
if r.success:
success('Pruned local branches successfully!')
else:
error('Please try again, something went wrong:')
print(r.output)

def __get_remote_branches(self, r):
# get remote's branches to verify from output of command in parent function
if not r.success:
Expand All @@ -278,12 +294,14 @@ def __get_remote_branches(self, r):
start_remote_branches = r.output.index(REMOTE_BRANCHES_START)
remote_branches_txt = r.output[start_remote_branches + len(REMOTE_BRANCHES_START):].split('\n')
remote_branches = []
for b in remote_branches_txt[1:]: # remove first useless line
for b in remote_branches_txt:
b = b.replace('tracked', '').strip()
if ' ' in b: # end of branches
if 'stale' in b: # support stale/to-be-pruned branches
b = b.split(' ')[0].split('/')[-1]
if ' ' in b or b == '': # end of branches
break
remote_branches.append(b)
elif REMOTE_BRANCH_START in r.output: # remote has single branch
elif REMOTE_BRANCH_START in r.output: # remote has single branch, shouldn't need to handle stale here
start_remote_branch = r.output.index(REMOTE_BRANCH_START)
remote_branches = r.output[start_remote_branch + len(REMOTE_BRANCH_START):].split('\n')
remote_branches = [b.replace('tracked', '').strip() for b in remote_branches if b.strip() != '' and 'tracked' in b]
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ COMMUNITY_BASHRC_PATH=/data/community/.bashrc
OH_MY_COMMA_PATH=/data/community/.oh-my-comma
GIT_BRANCH_NAME=master
GIT_REMOTE_URL=https://github.com/emu-sh/.oh-my-comma.git
OMC_VERSION=0.1.8
OMC_VERSION=0.1.9

update=false
if [ $# -ge 1 ] && [ $1 = "update" ]; then
Expand Down
49 changes: 32 additions & 17 deletions py_utils/colors.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
class COLORS:
# HEADER = '\033[95m'
OKBLUE = '\033[94m'
CBLUE = '\33[44m'
# BOLD = '\033[1m'
OKGREEN = '\033[92m'
CWHITE = '\33[37m'
ENDC = '\033[0m' + CWHITE
UNDERLINE = '\033[4m'
def __init__(self):
self.HEADER = '\033[95m'
self.OKBLUE = '\033[94m'
self.CBLUE = '\33[44m'
self.BOLD = '\033[1m'
self.CITALIC = '\33[3m'
self.OKGREEN = '\033[92m'
self.CWHITE = '\33[37m'
self.ENDC = '\033[0m' + self.CWHITE
self.UNDERLINE = '\033[4m'
self.PINK = '\33[38;5;207m'
self.PRETTY_YELLOW = self.BASE(220)

RED = '\033[91m'
PURPLE_BG = '\33[45m'
YELLOW = '\033[93m'
self.RED = '\033[91m'
self.PURPLE_BG = '\33[45m'
self.YELLOW = '\033[93m'
self.BLUE_GREEN = self.BASE(85)

FAIL = RED
INFO = PURPLE_BG
SUCCESS = OKGREEN
PROMPT = YELLOW
CYAN = '\033[36m'
WARNING = '\033[33m'
self.FAIL = self.RED
self.INFO = self.PURPLE_BG
self.SUCCESS = self.OKGREEN
self.PROMPT = self.YELLOW
self.DBLUE = '\033[36m'
self.CYAN = self.BASE(39)
self.WARNING = '\033[33m'

def BASE(self, col): # seems to support more colors
return '\33[38;5;{}m'.format(col)

def BASEBG(self, col): # seems to support more colors
return '\33[48;5;{}m'.format(col)


COLORS = COLORS()
2 changes: 1 addition & 1 deletion py_utils/emu_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def kill(procname):

def is_affirmative():
i = None
print(COLORS.WARNING, end='')
print(COLORS.PROMPT, end='')
while i not in ['y', 'n', 'yes', 'no', 'sure', '']:
i = input('[Y/n]: ').lower().strip()
print(COLORS.ENDC)
Expand Down

0 comments on commit 5a64ab8

Please sign in to comment.