forked from Maratyszcza/confu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fall back to git executable if pygit2 is not working
- Loading branch information
1 parent
6e4b714
commit c09a611
Showing
4 changed files
with
59 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,70 @@ | ||
import logging | ||
import pygit2 | ||
|
||
|
||
logger = logging.getLogger("confu") | ||
|
||
try: | ||
import pygit2 | ||
if not(pygit2.features & pygit2.GIT_FEATURE_HTTPS) or not(pygit2.features & pygit2.GIT_FEATURE_SSH): | ||
logger.warning("pygit2 is built without HTTPS or SSH support, fall back to using git executable") | ||
pygit2 = None | ||
except ImportError: | ||
pygit2 = None | ||
|
||
|
||
failed_certificate_hosts = set() | ||
|
||
|
||
class RemoteCallbacks(pygit2.RemoteCallbacks): | ||
def __init__(self, credentials=None, certificate=None): | ||
super(RemoteCallbacks, self).__init__(credentials, certificate) | ||
if pygit2 is not None: | ||
class RemoteCallbacks(pygit2.RemoteCallbacks): | ||
def __init__(self, credentials=None, certificate=None): | ||
super(RemoteCallbacks, self).__init__(credentials, certificate) | ||
|
||
def certificate_check(self, certificate, valid, host): | ||
if not valid: | ||
# Do not complain twice about the same host | ||
if host not in failed_certificate_hosts: | ||
logger.warning("could not validate certificate for {host}".format(host=host)) | ||
failed_certificate_hosts.add(host) | ||
|
||
return True | ||
else: | ||
class Repo: | ||
def __init__(self, root_dir): | ||
self.root_dir = root_dir | ||
|
||
@staticmethod | ||
def clone(url, path, checkout_branch=None): | ||
import subprocess | ||
args = ["git", "clone", "--quiet", url] | ||
if checkout_branch is not None: | ||
args += ["-b", checkout_branch] | ||
args.append(path) | ||
|
||
import os | ||
env = os.environ.copy() | ||
env["LC_ALL"] = "C" | ||
|
||
git = subprocess.Popen(args, env=env) | ||
git.communicate() | ||
assert git.returncode == 0 | ||
|
||
return Repo(path) | ||
|
||
def checkout(self, refname): | ||
import subprocess | ||
args = ["git", "checkout", "--quiet", refname] | ||
|
||
def certificate_check(self, certificate, valid, host): | ||
if not valid: | ||
# Do not complain twice about the same host | ||
if host not in failed_certificate_hosts: | ||
logger.warning("could not validate certificate for {host}".format(host=host)) | ||
failed_certificate_hosts.add(host) | ||
import os | ||
env = os.environ.copy() | ||
env["LC_ALL"] = "C" | ||
|
||
return True | ||
git = subprocess.Popen(args, cwd=self.root_dir, env=env) | ||
git.communicate() | ||
assert git.returncode == 0 | ||
|
||
|
||
def clone(url, path, checkout_branch=None): | ||
remote_callbacks = RemoteCallbacks() | ||
return pygit2.clone_repository(url, path, checkout_branch=checkout_branch, callbacks=remote_callbacks) | ||
if pygit2 is not None: | ||
remote_callbacks = RemoteCallbacks() | ||
return pygit2.clone_repository(url, path, checkout_branch=checkout_branch, callbacks=remote_callbacks) | ||
else: | ||
return Repo.clone(url, path, checkout_branch=checkout_branch) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters