bazarr/check_update.py

50 lines
2.3 KiB
Python
Raw Normal View History

2017-10-28 10:18:16 +08:00
from get_general_settings import *
2017-11-07 12:53:31 +08:00
import os
2017-11-11 13:54:19 +08:00
import pygit2
2017-11-07 12:53:31 +08:00
2017-11-14 22:38:34 +08:00
current_working_directory = os.path.dirname(__file__)
2017-11-11 13:54:19 +08:00
repository_path = pygit2.discover_repository(current_working_directory)
local_repo = pygit2.Repository(repository_path)
2017-11-07 13:11:42 +08:00
2017-11-11 13:54:19 +08:00
def check_and_apply_update(repo=local_repo, remote_name='origin'):
for remote in repo.remotes:
if remote.name == remote_name:
remote.fetch()
2017-11-27 08:54:59 +08:00
remote_id = repo.lookup_reference('refs/remotes/origin/' + str(branch)).target
2017-11-11 13:54:19 +08:00
merge_result, _ = repo.merge_analysis(remote_id)
# Up to date, do nothing
if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
2017-11-15 09:20:44 +08:00
result = 'No new version of Bazarr available.'
pass
2017-11-11 13:54:19 +08:00
# We can just fastforward
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
repo.checkout_tree(repo.get(remote_id))
2017-11-16 10:25:14 +08:00
master_ref = repo.lookup_reference('refs/heads/' + branch)
2017-11-11 13:54:19 +08:00
master_ref.set_target(remote_id)
repo.head.set_target(remote_id)
result = 'Bazarr updated to latest version and restarting.'
2017-11-15 09:20:44 +08:00
os.execlp('python', 'python', os.path.join(os.path.dirname(__file__), 'bazarr.py'))
2017-11-22 19:37:17 +08:00
# We can just do it normally
2017-11-22 05:09:33 +08:00
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL:
repo.merge(remote_id)
print repo.index.conflicts
assert repo.index.conflicts is None, 'Conflicts, ahhhh!'
user = repo.default_signature
tree = repo.index.write_tree()
commit = repo.create_commit('HEAD',
user,
user,
'Merge!',
tree,
2017-11-22 05:10:20 +08:00
[repo.head.target, remote_id])
2017-11-22 05:09:33 +08:00
repo.state_cleanup()
2017-11-22 08:58:52 +08:00
result = 'Conflict detected when trying to update.'
2017-11-22 19:37:17 +08:00
os.execlp('python', 'python', os.path.join(os.path.dirname(__file__), 'bazarr.py'))
# We can't do it
2017-11-11 13:54:19 +08:00
else:
2017-11-22 19:37:17 +08:00
result = 'Bazarr cannot be updated: Unknown merge analysis result'
2017-11-22 05:10:20 +08:00
2017-11-15 09:20:44 +08:00
return result