-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentry_with_update.py
More file actions
82 lines (63 loc) · 2.95 KB
/
Copy pathentry_with_update.py
File metadata and controls
82 lines (63 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import sys
root = os.path.dirname(os.path.abspath(__file__))
sys.path.append(root)
os.chdir(root)
for legacy_arg in ('--dev', '--main'):
while legacy_arg in sys.argv:
sys.argv.remove(legacy_arg)
print(f'Ignoring deprecated branch argument: {legacy_arg}')
try:
import pygit2
pygit2.option(pygit2.GIT_OPT_SET_OWNER_VALIDATION, 0)
repo = pygit2.Repository(os.path.abspath(os.path.dirname(__file__)))
remote_name = 'origin'
branch_name = 'main'
local_branch_ref = f'refs/heads/{branch_name}'
remote_reference = f'refs/remotes/{remote_name}/{branch_name}'
remote = repo.remotes[remote_name]
remote.fetch()
if remote_reference not in list(repo.references):
raise RuntimeError(f'Missing remote branch: {remote_reference}')
if local_branch_ref not in list(repo.references):
remote_branch = repo.references[remote_reference]
local_branch = repo.create_branch(branch_name, repo[remote_branch.target])
local_branch.upstream = remote_branch
else:
local_branch = repo.lookup_branch(branch_name)
if repo.head.shorthand != branch_name:
print(f'Ready to checkout {branch_name}')
repo.checkout(local_branch)
local_branch = repo.lookup_reference(local_branch_ref)
local_commit = repo.revparse_single(local_branch_ref)
remote_commit = repo.revparse_single(remote_reference)
merge_result, _ = repo.merge_analysis(remote_commit.id)
branch_name_display = branch_name
local_commit_short = str(local_commit.id)[:7]
remote_commit_short = str(remote_commit.id)[:7]
if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
print(f'{branch_name_display}: Already up-to-date, {local_commit_short}')
elif remote_commit.id != local_commit.id:
try:
repo.reset(remote_commit.id, pygit2.GIT_RESET_HARD)
try:
local_branch.set_target(remote_commit.id)
print(f'{branch_name_display}: Force Sync Remote Updates, {local_commit_short} -> {remote_commit_short}')
except Exception as ref_error:
try:
ref_name = f'refs/heads/{branch_name}'
repo.references.create(ref_name, remote_commit.id, True)
print(f'{branch_name_display}: Force Sync Remote Updates, {local_commit_short} -> {remote_commit_short}')
except Exception as alt_error:
print(f'Warning: Failed to update the reference, but the file has been updated. - {str(alt_error)}')
except Exception as reset_error:
print(f'{branch_name_display}: Failed to Force Update - {str(reset_error)}')
else:
print(f'{branch_name_display}: Update failed when {local_commit_short} <- {remote_commit_short}')
except Exception as e:
print(f'Repo Update failed.')
print(str(e))
if __name__ == '__main__':
import multiprocessing as mp
mp.set_start_method('spawn')
from launch import *