Skip to content

Commit

Permalink
Force update when local branch is ahead (#849)
Browse files Browse the repository at this point in the history
* Force update when local branch is ahead

* Remove whitespace

* Hide branches selection if not developer

* Fixing initial configuration not defaulting to 'master,develop'. Fix update restart loop when local branch is ahead but reset is not allowed. Allow to define an empty 'reset branch list' which means 'any branch' could be reset

* Adding 'Branches to reset' description

* Fix hidden for non devs

* Fix initial value of GIT_RESET_BRANCHES.

Like 'ALLOWED_EXTENSIONS' and other settings
  • Loading branch information
fernandog committed Aug 7, 2016
1 parent f90f3d3 commit a33244e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
21 changes: 18 additions & 3 deletions gui/slick/views/config_general.mako
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sickbeard import metadata
from sickbeard.metadata.generic import GenericMetadata
from sickbeard.helpers import anon_url
gh_branch = sickbeard.versionCheckScheduler.action.list_remote_branches()
%>
<%block name="content">
% if not header is UNDEFINED:
Expand Down Expand Up @@ -223,7 +224,7 @@
</span>
</label>
</div>
<div class="field-pair">
<label for="fanart_background">
<span class="component-title">Show fanart in the background</span>
Expand Down Expand Up @@ -667,7 +668,6 @@
<span class="component-title">Branch version:</span>
<span class="component-desc">
<select id="branchVersion" class="form-control form-control-inline input-sm pull-left">
<% gh_branch = sickbeard.versionCheckScheduler.action.list_remote_branches() %>
% if gh_branch:
% for cur_branch in gh_branch:
% if sickbeard.GIT_USERNAME and sickbeard.GIT_PASSWORD and sickbeard.DEVELOPER == 1:
Expand Down Expand Up @@ -730,7 +730,7 @@
</span>
</label>
</div>
<div class="field-pair" hidden>
<div class="field-pair"${' hidden' if not sickbeard.DEVELOPER else ''}>
<label for="git_reset">
<span class="component-title">Git reset</span>
<span class="component-desc">
Expand All @@ -739,6 +739,21 @@
</span>
</label>
</div>
<div class="field-pair"${' hidden' if not sickbeard.DEVELOPER else ''}>
<label for="git_reset_branches">
<span class="component-title">Branches to reset</span>
<span class="component-desc">
<select id="git_reset_branches" name="git_reset_branches" multiple="multiple" style="min-width:200px;height:99px;">
% for branch in gh_branch:
<option value="${branch}" ${'selected="selected"' if branch in sickbeard.GIT_RESET_BRANCHES else ''}>${branch}</option>
% endfor
</select>
</span>
<div class="clear-left">
<span class="component-desc"><b>NOTE:</b> Empty selection means that any branch could be reset.</span>
</div>
</label>
</div>
<input type="submit" class="btn config_submitter" value="Save Changes" />
</fieldset>
</div>
Expand Down
7 changes: 6 additions & 1 deletion sickbeard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
BRANCH = ''

GIT_RESET = True
GIT_RESET_BRANCHES = 'develop,master'
GIT_REMOTE = ''
GIT_REMOTE_URL = ''
CUR_COMMIT_BRANCH = ''
Expand Down Expand Up @@ -618,7 +619,7 @@ def get_backlog_cycle_time():
def initialize(consoleLogging=True): # pylint: disable=too-many-locals, too-many-branches, too-many-statements
with INIT_LOCK:
# pylint: disable=global-statement
global BRANCH, GIT_RESET, GIT_REMOTE, GIT_REMOTE_URL, CUR_COMMIT_HASH, CUR_COMMIT_BRANCH, ACTUAL_LOG_DIR, LOG_DIR, LOG_NR, LOG_SIZE, WEB_PORT, WEB_LOG, ENCRYPTION_VERSION, ENCRYPTION_SECRET, WEB_ROOT, WEB_USERNAME, WEB_PASSWORD, WEB_HOST, WEB_IPV6, WEB_COOKIE_SECRET, WEB_USE_GZIP, API_KEY, ENABLE_HTTPS, HTTPS_CERT, HTTPS_KEY, \
global BRANCH, GIT_RESET, GIT_RESET_BRANCHES, GIT_REMOTE, GIT_REMOTE_URL, CUR_COMMIT_HASH, CUR_COMMIT_BRANCH, ACTUAL_LOG_DIR, LOG_DIR, LOG_NR, LOG_SIZE, WEB_PORT, WEB_LOG, ENCRYPTION_VERSION, ENCRYPTION_SECRET, WEB_ROOT, WEB_USERNAME, WEB_PASSWORD, WEB_HOST, WEB_IPV6, WEB_COOKIE_SECRET, WEB_USE_GZIP, API_KEY, ENABLE_HTTPS, HTTPS_CERT, HTTPS_KEY, \
HANDLE_REVERSE_PROXY, USE_NZBS, USE_TORRENTS, NZB_METHOD, NZB_DIR, DOWNLOAD_PROPERS, RANDOMIZE_PROVIDERS, CHECK_PROPERS_INTERVAL, ALLOW_HIGH_PRIORITY, SAB_FORCED, TORRENT_METHOD, NOTIFY_ON_LOGIN, SUBLIMINAL_LOG, PRIVACY_LEVEL, \
SAB_USERNAME, SAB_PASSWORD, SAB_APIKEY, SAB_CATEGORY, SAB_CATEGORY_BACKLOG, SAB_CATEGORY_ANIME, SAB_CATEGORY_ANIME_BACKLOG, SAB_HOST, \
NZBGET_USERNAME, NZBGET_PASSWORD, NZBGET_CATEGORY, NZBGET_CATEGORY_BACKLOG, NZBGET_CATEGORY_ANIME, NZBGET_CATEGORY_ANIME_BACKLOG, NZBGET_PRIORITY, NZBGET_HOST, NZBGET_USE_HTTPS, backlogSearchScheduler, \
Expand Down Expand Up @@ -728,6 +729,9 @@ def initialize(consoleLogging=True): # pylint: disable=too-many-locals, too-man

# git reset on update
GIT_RESET = bool(check_setting_int(CFG, 'General', 'git_reset', 1))
GIT_RESET_BRANCHES = check_setting_str(CFG, 'General', 'git_reset_branches', GIT_RESET_BRANCHES).split(',')
if GIT_RESET_BRANCHES[0] == '':
GIT_RESET_BRANCHES = []

# current git branch
BRANCH = check_setting_str(CFG, 'General', 'branch', '')
Expand Down Expand Up @@ -1615,6 +1619,7 @@ def save_config(): # pylint: disable=too-many-statements, too-many-branches
new_config['General']['git_username'] = GIT_USERNAME
new_config['General']['git_password'] = helpers.encrypt(GIT_PASSWORD, ENCRYPTION_VERSION)
new_config['General']['git_reset'] = int(GIT_RESET)
new_config['General']['git_reset_branches'] = ','.join(GIT_RESET_BRANCHES)
new_config['General']['branch'] = BRANCH
new_config['General']['git_remote'] = GIT_REMOTE
new_config['General']['git_remote_url'] = GIT_REMOTE_URL
Expand Down
7 changes: 3 additions & 4 deletions sickbeard/server/web/config/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def saveGeneral(self, log_dir=None, log_nr=5, log_size=1, web_port=None, notify_
calendar_unprotected=None, calendar_icons=None, debug=None, ssl_verify=None, no_restart=None, coming_eps_missed_range=None,
fuzzy_dating=None, trim_zero=None, date_preset=None, date_preset_na=None, time_preset=None,
indexer_timeout=None, download_url=None, rootDir=None, theme_name=None, default_page=None,
git_reset=None, git_username=None, git_password=None, display_all_seasons=None, subliminal_log=None,
git_reset=None, git_reset_branches=None, git_username=None, git_password=None, display_all_seasons=None, subliminal_log=None,
privacy_level='normal', use_legacy_name_parser=None, fanart_background=None, fanart_background_opacity=None):

results = []
Expand Down Expand Up @@ -99,9 +99,8 @@ def saveGeneral(self, log_dir=None, log_nr=5, log_size=1, web_port=None, notify_
sickbeard.PROXY_INDEXERS = config.checkbox_to_value(proxy_indexers)
sickbeard.GIT_USERNAME = git_username
sickbeard.GIT_PASSWORD = git_password
# sickbeard.GIT_RESET = config.checkbox_to_value(git_reset)
# Force GIT_RESET
sickbeard.GIT_RESET = 1
sickbeard.GIT_RESET = config.checkbox_to_value(git_reset)
sickbeard.GIT_RESET_BRANCHES = git_reset_branches.split(',') if git_reset_branches else []
sickbeard.GIT_PATH = git_path
sickbeard.GIT_REMOTE = git_remote
sickbeard.CALENDAR_UNPROTECTED = config.checkbox_to_value(calendar_unprotected)
Expand Down
51 changes: 41 additions & 10 deletions sickbeard/versionChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def check_for_new_version(self, force=False):

# found updates
self.updater.set_newest_text()
return True
return self.updater.can_update()

def check_for_new_news(self, force=False):
"""
Expand Down Expand Up @@ -583,11 +583,7 @@ def set_newest_text(self):
# if we're up to date then don't set this
sickbeard.NEWEST_VERSION_STRING = None

if self._num_commits_ahead:
logger.log(u"Local branch is ahead of " + self.branch + ". Automatic update not possible.", logger.WARNING)
newest_text = "Local branch is ahead of " + self.branch + ". Automatic update not possible."

elif self._num_commits_behind > 0:
if self._num_commits_behind > 0 or self._is_hard_reset_allowed():

base_url = 'http://github.com/' + self.github_org + '/' + self.github_repo
if self._newest_commit_hash:
Expand All @@ -599,7 +595,15 @@ def set_newest_text(self):
newest_text += " (you're " + str(self._num_commits_behind) + " commit"
if self._num_commits_behind > 1:
newest_text += 's'
newest_text += ' behind)' + "&mdash; <a href=\"" + self.get_update_url() + "\">Update Now</a>"
newest_text += ' behind'
if self._num_commits_ahead > 0:
newest_text += ' and {ahead} commit{s} ahead'.format(ahead=self._num_commits_ahead,
s='s' if self._num_commits_ahead > 1 else '')
newest_text += ')' + "&mdash; <a href=\"" + self.get_update_url() + "\">Update Now</a>"

elif self._num_commits_ahead > 0:
logger.log(u"Local branch is ahead of " + self.branch + ". Automatic update not possible.", logger.WARNING)
newest_text = "Local branch is ahead of " + self.branch + ". Automatic update not possible."

else:
return
Expand All @@ -622,11 +626,19 @@ def need_update(self):
logger.log(u"Unable to contact github, can't check for update: " + repr(e), logger.WARNING)
return False

if self._num_commits_behind > 0:
if self._num_commits_behind > 0 or self._num_commits_ahead > 0:
return True

return False

def can_update(self):
"""Return whether update can be executed.
:return:
:rtype: bool
"""
return self._num_commits_ahead <= 0 or self._is_hard_reset_allowed()

def update(self):
"""
Calls git pull origin <branch> in order to update SickRage. Returns a bool depending
Expand All @@ -637,7 +649,7 @@ def update(self):
self.update_remote_origin()

# remove untracked files and performs a hard reset on git branch to avoid update issues
if sickbeard.GIT_RESET:
if self._is_hard_reset_allowed():
# self.clean() # This is removing user data and backups
self.reset()

Expand All @@ -659,6 +671,16 @@ def update(self):
else:
return False

@staticmethod
def _is_hard_reset_allowed():
"""Return whether git hard reset is allowed or not.
:return:
:rtype: bool
"""
return sickbeard.GIT_RESET and (not sickbeard.GIT_RESET_BRANCHES or
sickbeard.BRANCH in sickbeard.GIT_RESET_BRANCHES)

def clean(self):
"""
Calls git clean to remove all untracked files. Returns a bool depending
Expand All @@ -673,7 +695,8 @@ def reset(self):
Calls git reset --hard to perform a hard reset. Returns a bool depending
on the call's success.
"""
_, _, exit_status = self._run_git(self._git_path, 'reset --hard') # @UnusedVariable
_, _, exit_status = self._run_git(self._git_path, 'reset --hard {0}/{1}'.format
(sickbeard.GIT_REMOTE, sickbeard.BRANCH)) # @UnusedVariable
if exit_status == 0:
return True

Expand Down Expand Up @@ -750,6 +773,14 @@ def need_update(self):

return False

def can_update(self):
"""Whether or not the update can be performed.
:return:
:rtype: bool
"""
return True

def _check_github_for_update(self):
"""
Uses pygithub to ask github if there is a newer version that the provided
Expand Down

1 comment on commit a33244e

@NicoLeOca
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fernandog
thank you; that one was a pita

Please sign in to comment.