Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_file(branch, filename, universal_newlines=False):
cmd = ['git', 'show', '{branch}:{filename}'.format(
branch=branch, filename=git_path(filename)
)]
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE,
universal_newlines=universal_newlines)
stdout, stderr = p.communicate()
if p.wait() != 0:
raise GitError('unable to read file {!r}'.format(filename),
str(stderr))
return stdout
def get_config(key):
cmd = ['git', 'config', key]
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
if p.wait() != 0:
raise GitError('error getting config {!r}'.format(key), stderr)
return stdout.strip()
def push_branch(remote, branch, force=False):
cmd = (['git', 'push'] + (['--force'] if force else []) +
['--', remote, branch])
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
if p.wait() != 0:
raise GitError('failed to push branch {} to {}'.format(branch, remote),
stderr)
def update_ref(branch, new_ref):
cmd = ['git', 'update-ref', 'refs/heads/{}'.format(branch), new_ref]
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
stderr = p.communicate()[1]
if p.wait() != 0:
raise GitError('error updating ref for {}'.format(branch), stderr)
dest = urlparse.urlunsplit(
url[:2] + (url[2] + '/',) + url[3:]
)
self.send_header('Location', dest)
self.end_headers()
return
path = posixpath.join(path, 'index.html')
git_utils.file_mode(self.branch, path)
self.send_response(200)
self.send_header('Content-Type', self.guess_type(path))
self.end_headers()
return path
except git_utils.GitError:
self.send_error(404, 'File not found')
except Exception: # pragma: no cover
self.send_error(500, 'Internal server error')
def file_mode(branch, filename):
filename = filename.rstrip('/')
# The root directory of the repo is, well... a directory.
if not filename:
return 0o040000
cmd = ['git', 'ls-tree', '--full-tree', '--', branch, git_path(filename)]
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
if p.wait() != 0:
raise GitError('unable to read file {!r}'.format(filename), stderr)
if not stdout:
raise GitError('file not found')
return int(stdout.split(' ', 1)[0], 8)
def get_latest_commit(rev, short=False):
cmd = ['git', 'rev-parse'] + (['--short'] if short else []) + [rev]
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
if p.wait() != 0:
raise GitError('error getting latest commit', stderr)
return stdout.strip()
def list_versions(branch='gh-pages'):
try:
return Versions.loads(git_utils.read_file(
branch, versions_file, universal_newlines=True
))
except git_utils.GitError:
return Versions()
import time
import unicodedata
from enum import Enum
BranchStatus = Enum('BranchState', ['even', 'ahead', 'behind', 'diverged'])
class GitError(Exception):
def __init__(self, message, stderr=None):
if stderr:
message += ': "{}"'.format(stderr.strip())
super().__init__(message)
class GitBranchDiverged(GitError):
def __init__(self, branch1, branch2):
super().__init__('{} has diverged from {}'.format(branch1, branch2))
class GitRevUnrelated(GitError):
def __init__(self, branch1, branch2):
super().__init__('{} is unrelated to {}'.format(branch1, branch2))
def git_path(path):
path = os.path.normpath(path)
# Fix unicode pathnames on macOS; see
# .
if sys.platform == 'darwin': # pragma: no cover
if isinstance(path, bytes):
path = path.decode('utf-8')