-
Notifications
You must be signed in to change notification settings - Fork 618
/
git.py
221 lines (192 loc) 路 6.95 KB
/
git.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
219
220
221
import configparser
import logging
import os
from typing import Optional
from urllib.parse import urlparse, urlunparse
import wandb
logger = logging.getLogger(__name__)
class GitRepo:
def __init__(
self,
root: Optional[str] = None,
remote: str = "origin",
lazy: bool = True,
remote_url: Optional[str] = None,
commit: Optional[str] = None,
) -> None:
self.remote_name = remote if remote_url is None else None
self._root = root
self._remote_url = remote_url
self._commit = commit
self._repo = None
if not lazy:
self.repo
@property
def repo(self):
if self._repo is None:
if self.remote_name is None:
self._repo = False
else:
try:
self._repo = Repo(
self._root or os.getcwd(), search_parent_directories=True
)
except exc.InvalidGitRepositoryError:
logger.debug("git repository is invalid")
self._repo = False
except exc.NoSuchPathError:
wandb.termwarn(f"git root {self._root} does not exist")
logger.warn(f"git root {self._root} does not exist")
self._repo = False
return self._repo
@property
def auto(self):
return self._remote_url is None
def is_untracked(self, file_name: str) -> bool:
if not self.repo:
return True
return file_name in self.repo.untracked_files
@property
def enabled(self) -> bool:
return bool(self.repo)
@property
def root(self) -> Optional[str]:
if not self.repo:
return None
try:
return self.repo.git.rev_parse("--show-toplevel")
except exc.GitCommandError as e:
# todo: collect telemetry on this
logger.error(f"git root error: {e}")
return None
@property
def dirty(self) -> bool:
if not self.repo:
return False
return self.repo.is_dirty()
@property
def email(self) -> Optional[str]:
if not self.repo:
return None
try:
return self.repo.config_reader().get_value("user", "email")
except configparser.Error:
return None
@property
def last_commit(self):
if self._commit:
return self._commit
if not self.repo:
return None
if not self.repo.head or not self.repo.head.is_valid():
return None
# TODO: Saw a user getting a Unicode decode error when parsing refs,
# more details on implementing a real fix in [WB-4064]
try:
if len(self.repo.refs) > 0:
return self.repo.head.commit.hexsha
else:
return self.repo.git.show_ref("--head").split(" ")[0]
except Exception:
logger.exception("Unable to find most recent commit in git")
return None
@property
def branch(self) -> Optional[str]:
if not self.repo:
return None
return self.repo.head.ref.name
@property
def remote(self):
if not self.repo:
return None
try:
return self.repo.remotes[self.remote_name]
except IndexError:
return None
# the --submodule=diff option doesn't exist in pre-2.11 versions of git (november 2016)
# https://stackoverflow.com/questions/10757091/git-list-of-all-changed-files-including-those-in-submodules
@property
def has_submodule_diff(self) -> bool:
if not self.repo:
return False
return self.repo.git.version_info >= (2, 11, 0)
@property
def remote_url(self):
if self._remote_url:
return self._remote_url
if not self.remote:
return None
parsed = urlparse(self.remote.url)
hostname = parsed.hostname
if parsed.port is not None:
hostname += ":" + str(parsed.port)
if parsed.password is not None:
return urlunparse(parsed._replace(netloc=f"{parsed.username}:@{hostname}"))
return urlunparse(parsed._replace(netloc=hostname))
@property
def root_dir(self):
if not self.repo:
return None
return self.repo.git.rev_parse("--show-toplevel")
def get_upstream_fork_point(self):
"""Get the most recent ancestor of HEAD that occurs on an upstream
branch.
First looks at the current branch's tracking branch, if applicable. If
that doesn't work, looks at every other branch to find the most recent
ancestor of HEAD that occurs on a tracking branch.
Returns:
git.Commit object or None
"""
possible_relatives = []
try:
if not self.repo:
return None
try:
active_branch = self.repo.active_branch
except (TypeError, ValueError):
logger.debug("git is in a detached head state")
return None # detached head
else:
tracking_branch = active_branch.tracking_branch()
if tracking_branch:
possible_relatives.append(tracking_branch.commit)
if not possible_relatives:
for branch in self.repo.branches:
tracking_branch = branch.tracking_branch()
if tracking_branch is not None:
possible_relatives.append(tracking_branch.commit)
head = self.repo.head
most_recent_ancestor = None
for possible_relative in possible_relatives:
# at most one:
for ancestor in self.repo.merge_base(head, possible_relative):
if most_recent_ancestor is None:
most_recent_ancestor = ancestor
elif self.repo.is_ancestor(most_recent_ancestor, ancestor):
most_recent_ancestor = ancestor
return most_recent_ancestor
except exc.GitCommandError as e:
logger.debug("git remote upstream fork point could not be found")
logger.debug(str(e))
return None
def tag(self, name, message):
try:
return self.repo.create_tag("wandb/" + name, message=message, force=True)
except exc.GitCommandError:
print("Failed to tag repository.")
return None
def push(self, name):
if self.remote:
try:
return self.remote.push("wandb/" + name, force=True)
except exc.GitCommandError:
logger.debug("failed to push git")
return None
class FakeGitRepo(GitRepo):
@property
def repo(self):
return None
try:
from git import Repo, exc # type: ignore
except ImportError: # import fails if user doesn't have git
GitRepo = FakeGitRepo # type: ignore