Skip to content

Commit bb50f89

Browse files
committed
formatted with black
1 parent 3119d39 commit bb50f89

File tree

1 file changed

+100
-79
lines changed
  • pyrevitlib/pyrevit/coreutils

1 file changed

+100
-79
lines changed

pyrevitlib/pyrevit/coreutils/git.py

Lines changed: 100 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,36 @@
1616
from pyrevit.framework import DateTime, DateTimeOffset
1717
from pyrevit.coreutils.logger import get_logger
1818

19-
#pylint: disable=W0703,C0302
20-
mlogger = get_logger(__name__) #pylint: disable=C0103
19+
# pylint: disable=W0703,C0302
20+
mlogger = get_logger(__name__) # pylint: disable=C0103
2121

2222

23-
GIT_LIB = 'LibGit2Sharp'
23+
GIT_LIB = "LibGit2Sharp"
2424

2525
LIBGIT_DLL = framework.get_dll_file(GIT_LIB)
26-
mlogger.debug('Loading dll: %s', LIBGIT_DLL)
26+
mlogger.debug("Loading dll: %s", LIBGIT_DLL)
2727

2828
try:
2929
if PY3:
3030
clr.AddReference(LIBGIT_DLL)
3131
else:
3232
clr.AddReferenceToFileAndPath(LIBGIT_DLL)
3333

34-
import LibGit2Sharp as libgit #pylint: disable=import-error
34+
import LibGit2Sharp as libgit # pylint: disable=import-error
3535

3636
except Exception as load_err:
37-
mlogger.error('Can not load %s module. '
38-
'This module is necessary for getting pyRevit version '
39-
'and staying updated. | %s', GIT_LIB, load_err)
37+
mlogger.error(
38+
"Can not load %s module. "
39+
"This module is necessary for getting pyRevit version "
40+
"and staying updated. | %s",
41+
GIT_LIB,
42+
load_err,
43+
)
4044

4145

4246
class PyRevitGitAuthenticationError(PyRevitException):
4347
"""Git authentication error."""
48+
4449
pass
4550

4651

@@ -57,6 +62,7 @@ class RepoInfo(object):
5762
username (str): credentials - username
5863
password (str): credentials - password
5964
"""
65+
6066
def __init__(self, repo):
6167
self.directory = repo.Info.WorkingDirectory
6268
self.name = op.basename(op.normpath(self.directory))
@@ -67,8 +73,9 @@ def __init__(self, repo):
6773
self.username = self.password = None
6874

6975
def __repr__(self):
70-
return '<type \'RepoInfo\' head \'{}\' @ {}>'\
71-
.format(self.last_commit_hash, self.directory)
76+
return "<type 'RepoInfo' head '{}' @ {}>".format(
77+
self.last_commit_hash, self.directory
78+
)
7279

7380

7481
def _credentials_hndlr(username, password):
@@ -79,69 +86,79 @@ def _credentials_hndlr(username, password):
7986

8087

8188
def _get_credentials_hndlr(username, password):
82-
return libgit.Handlers. \
83-
CredentialsHandler(lambda url, uname, types:
84-
_credentials_hndlr(username, password))
89+
return libgit.Handlers.CredentialsHandler(
90+
lambda url, uname, types: _credentials_hndlr(username, password)
91+
)
8592

8693

8794
def _make_pull_options(repo_info):
88-
mlogger.debug('Making pull options: %s', repo_info)
95+
mlogger.debug("Making pull options: %s", repo_info)
8996
pull_ops = libgit.PullOptions()
9097
pull_ops.FetchOptions = libgit.FetchOptions()
9198
if repo_info.username and repo_info.password:
92-
mlogger.debug('Making Credentials handler. '
93-
'(Username and password are available but'
94-
'will not be logged for privacy purposes.)')
99+
mlogger.debug(
100+
"Making Credentials handler. "
101+
"(Username and password are available but"
102+
"will not be logged for privacy purposes.)"
103+
)
95104

96-
pull_ops.FetchOptions.CredentialsProvider = \
97-
_get_credentials_hndlr(repo_info.username, repo_info.password)
105+
pull_ops.FetchOptions.CredentialsProvider = _get_credentials_hndlr(
106+
repo_info.username, repo_info.password
107+
)
98108

99109
return pull_ops
100110

101111

102112
def _make_fetch_options(repo_info):
103-
mlogger.debug('Making fetch options: %s', repo_info)
113+
mlogger.debug("Making fetch options: %s", repo_info)
104114
fetch_ops = libgit.FetchOptions()
105115
if repo_info.username and repo_info.password:
106-
mlogger.debug('Making Credentials handler. '
107-
'(Username and password are available but'
108-
'will not be logged for privacy purposes.)')
116+
mlogger.debug(
117+
"Making Credentials handler. "
118+
"(Username and password are available but"
119+
"will not be logged for privacy purposes.)"
120+
)
109121

110-
fetch_ops.CredentialsProvider = \
111-
_get_credentials_hndlr(repo_info.username, repo_info.password)
122+
fetch_ops.CredentialsProvider = _get_credentials_hndlr(
123+
repo_info.username, repo_info.password
124+
)
112125

113126
return fetch_ops
114127

115128

116129
def _make_clone_options(username=None, password=None):
117-
mlogger.debug('Making clone options.')
130+
mlogger.debug("Making clone options.")
118131
clone_ops = libgit.CloneOptions()
119132

120133
if username and password:
121-
mlogger.debug('Making Credentials handler.')
134+
mlogger.debug("Making Credentials handler.")
122135
creds_handler = _get_credentials_hndlr(username, password)
123136

124137
# Only set the CredentialsProvider if it's a valid property
125-
if hasattr(clone_ops, 'CredentialsProvider'):
138+
if hasattr(clone_ops, "CredentialsProvider"):
126139
clone_ops.CredentialsProvider = creds_handler
127-
elif hasattr(clone_ops, 'FetchOptions') and hasattr(clone_ops.FetchOptions, 'CredentialsProvider'):
140+
elif hasattr(clone_ops, "FetchOptions") and hasattr(
141+
clone_ops.FetchOptions, "CredentialsProvider"
142+
):
128143
clone_ops.FetchOptions.CredentialsProvider = creds_handler
129144
else:
130-
mlogger.warning('CloneOptions does not support CredentialsProvider. Skipping credentials.')
145+
mlogger.warning(
146+
"CloneOptions does not support CredentialsProvider. Skipping credentials."
147+
)
131148

132149
return clone_ops
133150

134151

135152
def _make_pull_signature():
136-
mlogger.debug('Creating pull signature for username: %s', HOST_APP.username)
137-
return libgit.Signature(HOST_APP.username,
138-
HOST_APP.username,
139-
DateTimeOffset(DateTime.Now))
153+
mlogger.debug("Creating pull signature for username: %s", HOST_APP.username)
154+
return libgit.Signature(
155+
HOST_APP.username, HOST_APP.username, DateTimeOffset(DateTime.Now)
156+
)
140157

141158

142159
def _process_git_error(exception_err):
143160
exception_msg = safe_strtype(exception_err)
144-
if '401' in exception_msg:
161+
if "401" in exception_msg:
145162
raise PyRevitGitAuthenticationError(exception_msg)
146163
else:
147164
raise PyRevitException(exception_msg)
@@ -171,18 +188,18 @@ def git_pull(repo_info):
171188
"""
172189
repo = repo_info.repo
173190
try:
174-
libgit.Commands.Pull(repo,
175-
_make_pull_signature(),
176-
_make_pull_options(repo_info))
191+
libgit.Commands.Pull(
192+
repo, _make_pull_signature(), _make_pull_options(repo_info)
193+
)
177194

178-
mlogger.debug('Successfully pulled repo: %s', repo_info.directory)
179-
head_msg = safe_strtype(repo.Head.Tip.Message).replace('\n', '')
195+
mlogger.debug("Successfully pulled repo: %s", repo_info.directory)
196+
head_msg = safe_strtype(repo.Head.Tip.Message).replace("\n", "")
180197

181-
mlogger.debug('New head is: %s > %s', repo.Head.Tip.Id.Sha, head_msg)
198+
mlogger.debug("New head is: %s > %s", repo.Head.Tip.Id.Sha, head_msg)
182199
return RepoInfo(repo)
183200

184201
except Exception as pull_err:
185-
mlogger.debug('Failed git pull: %s | %s', repo_info.directory, pull_err)
202+
mlogger.debug("Failed git pull: %s | %s", repo_info.directory, pull_err)
186203
_process_git_error(pull_err)
187204

188205

@@ -197,21 +214,22 @@ def git_fetch(repo_info):
197214
"""
198215
repo = repo_info.repo
199216
try:
200-
libgit.Commands.Fetch(repo,
201-
repo.Head.TrackedBranch.RemoteName,
202-
[],
203-
_make_fetch_options(repo_info),
204-
'fetching pyrevit updates')
205-
206-
mlogger.debug('Successfully pulled repo: %s', repo_info.directory)
207-
head_msg = safe_strtype(repo.Head.Tip.Message).replace('\n', '')
208-
209-
mlogger.debug('New head is: %s > %s', repo.Head.Tip.Id.Sha, head_msg)
217+
libgit.Commands.Fetch(
218+
repo,
219+
repo.Head.TrackedBranch.RemoteName,
220+
[],
221+
_make_fetch_options(repo_info),
222+
"fetching pyrevit updates",
223+
)
224+
225+
mlogger.debug("Successfully pulled repo: %s", repo_info.directory)
226+
head_msg = safe_strtype(repo.Head.Tip.Message).replace("\n", "")
227+
228+
mlogger.debug("New head is: %s > %s", repo.Head.Tip.Id.Sha, head_msg)
210229
return RepoInfo(repo)
211230

212231
except Exception as fetch_err:
213-
mlogger.debug('Failed git fetch: %s | %s',
214-
repo_info.directory, fetch_err)
232+
mlogger.debug("Failed git fetch: %s | %s", repo_info.directory, fetch_err)
215233
_process_git_error(fetch_err)
216234

217235

@@ -225,16 +243,18 @@ def git_clone(repo_url, clone_dir, username=None, password=None):
225243
password (str): credentials - password
226244
"""
227245
try:
228-
libgit.Repository.Clone(repo_url,
229-
clone_dir,
230-
_make_clone_options(username=username,
231-
password=password))
246+
libgit.Repository.Clone(
247+
repo_url,
248+
clone_dir,
249+
_make_clone_options(username=username, password=password),
250+
)
232251

233-
mlogger.debug('Completed git clone: %s @ %s', repo_url, clone_dir)
252+
mlogger.debug("Completed git clone: %s @ %s", repo_url, clone_dir)
234253

235254
except Exception as clone_err:
236-
mlogger.debug('Error cloning repo: %s to %s | %s',
237-
repo_url, clone_dir, clone_err)
255+
mlogger.debug(
256+
"Error cloning repo: %s to %s | %s", repo_url, clone_dir, clone_err
257+
)
238258
_process_git_error(clone_err)
239259

240260

@@ -248,28 +268,31 @@ def compare_branch_heads(repo_info):
248268
repo = repo_info.repo
249269
repo_branches = repo.Branches
250270

251-
mlogger.debug('Repo branches: %s', [b.FriendlyName for b in repo_branches])
271+
mlogger.debug("Repo branches: %s", [b.FriendlyName for b in repo_branches])
252272

253273
for branch in repo_branches:
254274
if branch.FriendlyName == repo_info.branch and not branch.IsRemote:
255275
try:
256276
if branch.TrackedBranch:
257-
mlogger.debug('Comparing heads: %s of %s',
258-
branch.CanonicalName,
259-
branch.TrackedBranch.CanonicalName)
260-
261-
hist_div = repo.ObjectDatabase. \
262-
CalculateHistoryDivergence(branch.Tip,
263-
branch.TrackedBranch.Tip)
277+
mlogger.debug(
278+
"Comparing heads: %s of %s",
279+
branch.CanonicalName,
280+
branch.TrackedBranch.CanonicalName,
281+
)
282+
283+
hist_div = repo.ObjectDatabase.CalculateHistoryDivergence(
284+
branch.Tip, branch.TrackedBranch.Tip
285+
)
264286
return hist_div
265287
except Exception as compare_err:
266-
mlogger.error('Can not compare branch %s in repo: %s | %s',
267-
branch,
268-
repo,
269-
safe_strtype(compare_err).replace('\n', ''))
288+
mlogger.error(
289+
"Can not compare branch %s in repo: %s | %s",
290+
branch,
291+
repo,
292+
safe_strtype(compare_err).replace("\n", ""),
293+
)
270294
else:
271-
mlogger.debug('Skipping remote branch: %s', branch.CanonicalName)
272-
295+
mlogger.debug("Skipping remote branch: %s", branch.CanonicalName)
273296

274297

275298
def get_all_new_commits(repo_info):
@@ -284,8 +307,7 @@ def get_all_new_commits(repo_info):
284307
repo = repo_info.repo
285308
current_commit = repo_info.last_commit_hash
286309

287-
ref_commit = repo.Lookup(libgit.ObjectId(current_commit),
288-
libgit.ObjectType.Commit)
310+
ref_commit = repo.Lookup(libgit.ObjectId(current_commit), libgit.ObjectType.Commit)
289311

290312
# Let's only consider the refs that lead to this commit...
291313
refs = repo.Refs.ReachableFrom([ref_commit])
@@ -299,8 +321,7 @@ def get_all_new_commits(repo_info):
299321
commits = repo.Commits.QueryBy(commit_filter)
300322
commitsdict = OrderedDict()
301323
for commit in commits:
302-
if commit in repo.Head.Commits \
303-
or commit in repo.Head.TrackedBranch.Commits:
324+
if commit in repo.Head.Commits or commit in repo.Head.TrackedBranch.Commits:
304325
commitsdict[commit.Id.ToString()] = commit.MessageShort
305326

306327
return commitsdict

0 commit comments

Comments
 (0)