-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathgitutils.py
218 lines (186 loc) · 8.35 KB
/
gitutils.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""gitutils.py
Utils for getting git-related information.
"""
import os
import re
import subprocess
import time
from datetime import datetime
from typing import List, Optional
import git
# Assume the nightly branch commit message is in the following format
# Hash in the parentheses links to the commit on the master branch
NIGHTLY_COMMIT_MSG = "nightly release \((.*)\)"
def get_torch_main_commit(pytorch_repo: str, nightly_commit: str):
try:
repo = git.Repo(pytorch_repo)
msg = repo.commit(nightly_commit).message
# There are two possibilities of the hash `nightly_commit`:
# 1. The hash belongs to the nightly branch
# If so, the git commit message should match `NIGHTLY_COMMIT_MSG`
# 2. The hash belongs to the master/main branch
# We can directly use this hash in this case
nightly_commit_regex = re.compile(NIGHTLY_COMMIT_MSG)
search_result = nightly_commit_regex.search(msg)
if search_result:
return search_result.group(1)
# We now believe the commit now belongs to the master/main branch
# Unfortunately, there is no way to map a commit back to a branch with gitpython
except Exception as e:
return nightly_commit
return nightly_commit
def clean_git_repo(repo: str) -> bool:
try:
command = f"git clean -xdf"
subprocess.check_call(command, cwd=repo, shell=True)
return True
except subprocess.CalledProcessError:
print(f"Failed to cleanup git repo {repo}")
return None
def update_git_repo_branch(repo: str, branch: str) -> bool:
try:
command = f"git pull origin {branch}"
out = subprocess.check_output(command, cwd=repo, shell=True).decode().strip()
return out
except subprocess.CalledProcessError:
print(f"Failed to update git repo {repo}, branch {branch}")
return None
def get_git_commit_on_date(repo: str, date: datetime) -> Optional[str]:
try:
# Get the first commit since date
formatted_date = date.strftime("%Y-%m-%d")
command = f"git log --until={formatted_date} -1 --oneline | cut -d ' ' -f 1"
out = subprocess.check_output(command, cwd=repo, shell=True).decode().strip()
return out
except subprocess.CalledProcessError:
print(f"Failed to get the last commit on date {formatted_date} in repo {repo}")
return None
def check_git_exist_local_branch(repo: str, branch: str) -> bool:
command = f"git rev-parse --verify {branch} &> /dev/null "
retcode = subprocess.call(command, cwd=repo, shell=True)
return retcode == 0
def get_git_commit_date(repo: str, commit: str) -> str:
try:
command = f"git show -s --format=%ci {commit}"
out = subprocess.check_output(command, cwd=repo, shell=True).decode().strip()
return out
except subprocess.CalledProcessError:
print(f"Failed to get date of commit {commit} in repo {repo}")
return None
def checkout_git_branch(repo: str, branch: str) -> bool:
try:
if check_git_exist_local_branch(repo, branch):
command = f"git checkout {branch} &> /dev/null "
else:
command = f"git checkout --track origin/{branch} &> /dev/null"
retcode = subprocess.call(command, cwd=repo, shell=True)
return retcode == 0
except subprocess.CalledProcessError:
print(f"Failed to checkout git repo {repo}, branch {branch}")
return None
def get_current_branch(repo: str) -> Optional[str]:
try:
command = "git branch --show-current"
out = subprocess.check_output(command, cwd=repo, shell=True).decode().strip()
return out
except subprocess.CalledProcessError:
print(f"Failed to get current branch name for repo {repo}")
return None
def get_git_origin(repo: str) -> Optional[str]:
try:
command = "git remote get-url origin"
out = subprocess.check_output(command, cwd=repo, shell=True).decode().strip()
return out
except:
print(f"git command {command} returns non-zero status in repo {repo}")
return None
def get_git_commits(repo: str, start: str, end: str) -> Optional[List[str]]:
try:
command = f'git log --reverse --oneline --ancestry-path {start}^..{end} | cut -d " " -f 1'
out = (
subprocess.check_output(command, cwd=repo, shell=True)
.decode()
.strip()
.split("\n")
)
if out == [""]:
out = None
return out
except subprocess.CalledProcessError:
print(f"git command {command} returns non-zero status in repo {repo}")
return None
def get_current_commit(repo: str) -> Optional[str]:
try:
command = f'git log --reverse --oneline -1 | cut -d " " -f 1'
out = subprocess.check_output(command, cwd=repo, shell=True).decode().strip()
return out
except subprocess.CalledProcessError:
print(f"Failed to get the current commit in repo {repo}")
return None
def cleanup_local_changes(repo: str):
print("Resetting git repository to HEAD...", end="", flush=True)
command = ["git", "reset", "--hard", "HEAD"]
subprocess.check_call(command, cwd=repo, shell=False)
print("Done", flush=True)
def checkout_git_commit(repo: str, commit: str) -> bool:
try:
assert len(commit) != 0
cleanup_local_changes(repo)
command = ["git", "checkout", "--recurse-submodules", commit]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "submodule", "update", "--init", "--recursive"]
subprocess.check_call(command, cwd=repo, shell=False)
return True
except subprocess.CalledProcessError:
# Sleep 5 seconds for concurrent git process, remove the index.lock file if exists, and try again
try:
time.sleep(5)
index_lock = os.path.join(repo, ".git", "index.lock")
if os.path.exists(index_lock):
os.remove(index_lock)
cleanup_local_changes(repo)
command = ["git", "checkout", "--recurse-submodules", commit]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "submodule", "update", "--init", "--recursive"]
subprocess.check_call(command, cwd=repo, shell=False)
return True
except subprocess.CalledProcessError:
print(f"Failed to checkout commit {commit} in repo {repo}")
return False
def update_git_repo(repo: str, branch: str = "main") -> bool:
try:
print(
f"======================= [TORCHBENCH] Updating repository {repo} branch {branch} ======================="
)
assert len(branch) != 0
command = ["git", "checkout", "--recurse-submodules", branch]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "pull"]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "checkout", "--recurse-submodules", branch]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "submodule", "update", "--init", "--recursive"]
subprocess.check_call(command, cwd=repo, shell=False)
return True
except subprocess.CalledProcessError:
# Sleep 5 seconds for concurrent git process, remove the index.lock file if exists, and try again
try:
time.sleep(5)
print(
f"======================= [TORCHBENCH] Updating repository {repo} branch {branch} (2nd try) ======================="
)
index_lock = os.path.join(repo, ".git", "index.lock")
if os.path.exists(index_lock):
os.remove(index_lock)
command = ["git", "checkout", "--recurse-submodules", branch]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "pull"]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "checkout", "--recurse-submodules", branch]
subprocess.check_call(command, cwd=repo, shell=False)
command = ["git", "submodule", "update", "--init", "--recursive"]
subprocess.check_call(command, cwd=repo, shell=False)
return True
except subprocess.CalledProcessError:
print(f"Failed to update to branch {branch} in repo {repo}")
return False