forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_submodules.py
102 lines (74 loc) · 2.72 KB
/
update_submodules.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import re
import subprocess
import time
import requests
manual_mode = True
with open('.gitmodules') as f:
local_data = f.read()
def parse_submodules(submodules_content):
regex = re.compile(r'\[submodule "(.*)"\]\n\s+path = (.*)\n\s+url = (.*)\n', re.MULTILINE)
submodules = []
for match in regex.finditer(submodules_content):
submodules.append({
'path': match.group(2),
'url': match.group(3),
})
return submodules
def parse_remote_commit(path):
if path == 'cmake':
r = requests.get('https://github.com/telegramdesktop/tdesktop/tree/dev')
regex = re.compile(r'cmake @ (.*?)<')
try:
return regex.search(r.text).group(1)[:7]
except:
return None
parent = os.path.dirname(path)
r = requests.get('https://github.com/telegramdesktop/tdesktop/tree/dev/' + parent)
try:
data = r.json()
except:
return None
for item in data['payload']['tree']['items']:
if item['path'] == path:
return item['submoduleDisplayName'].split(' @ ')[1]
mismatched = {}
local_submodules = parse_submodules(local_data)
for submodule in local_submodules:
path = submodule['path']
current_commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=path).decode('utf-8').strip()[:7]
remote_commit = None
custom_commit = None
while remote_commit is None:
if manual_mode:
custom_commit = input('Enter commit for ' + path + ': ')
if custom_commit == '':
custom_commit = None
else:
remote_commit = custom_commit
else:
remote_commit = parse_remote_commit(path)
if remote_commit is None:
time.sleep(3)
eq = '==' if remote_commit == current_commit else '!='
print(f'{path:<50} {current_commit} {eq} {remote_commit}')
if remote_commit != current_commit:
mismatched[path] = (current_commit, remote_commit)
if mismatched:
print('\n\n\nMismatched submodules:')
for path, (current_commit, remote_commit) in mismatched.items():
print(f'{path:<50} {current_commit} != {remote_commit}')
s = input('\nSubmodules to update: ')
for submodule in s.split(','):
path = submodule.strip()
if path not in mismatched:
print(f'Unknown submodule: {path}')
continue
print(f'Updating {path}...')
before = os.getcwd()
os.chdir(path)
subprocess.check_call(['git', 'fetch'])
subprocess.check_call(['git', 'checkout', mismatched[path][1]])
os.chdir(before)
subprocess.check_call(['git', 'add', path])
print(f'Updated {path}')