github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/lambda/GitPullS3/pygit2/__init__.py (about)

     1  # -*- coding: utf-8 -*-
     2  #
     3  # Copyright 2010-2015 The pygit2 contributors
     4  #
     5  # This file is free software; you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License, version 2,
     7  # as published by the Free Software Foundation.
     8  #
     9  # In addition to the permissions in the GNU General Public License,
    10  # the authors give you unlimited permission to link the compiled
    11  # version of this file into combinations with other programs,
    12  # and to distribute those combinations without any restriction
    13  # coming from the use of this file.  (The General Public License
    14  # restrictions do apply in other respects; for example, they cover
    15  # modification of the file, and distribution when not linked into
    16  # a combined executable.)
    17  #
    18  # This file is distributed in the hope that it will be useful, but
    19  # WITHOUT ANY WARRANTY; without even the implied warranty of
    20  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    21  # General Public License for more details.
    22  #
    23  # You should have received a copy of the GNU General Public License
    24  # along with this program; see the file COPYING.  If not, write to
    25  # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
    26  # Boston, MA 02110-1301, USA.
    27  
    28  # Import from the future
    29  from __future__ import absolute_import
    30  
    31  # Low level API
    32  from _pygit2 import *
    33  
    34  # High level API
    35  from .blame import Blame, BlameHunk
    36  from .config import Config
    37  from .credentials import *
    38  from .errors import check_error, Passthrough
    39  from .ffi import ffi, C
    40  from .index import Index, IndexEntry
    41  from .remote import Remote, RemoteCallbacks, get_credentials
    42  from .repository import Repository
    43  from .settings import Settings
    44  from .submodule import Submodule
    45  from .utils import to_bytes, to_str
    46  from ._build import __version__
    47  
    48  
    49  # Features
    50  features = C.git_libgit2_features()
    51  GIT_FEATURE_THREADS = C.GIT_FEATURE_THREADS
    52  GIT_FEATURE_HTTPS = C.GIT_FEATURE_HTTPS
    53  GIT_FEATURE_SSH = C.GIT_FEATURE_SSH
    54  
    55  # GIT_REPOSITORY_INIT_*
    56  GIT_REPOSITORY_INIT_OPTIONS_VERSION = C.GIT_REPOSITORY_INIT_OPTIONS_VERSION
    57  GIT_REPOSITORY_INIT_BARE = C.GIT_REPOSITORY_INIT_BARE
    58  GIT_REPOSITORY_INIT_NO_REINIT = C.GIT_REPOSITORY_INIT_NO_REINIT
    59  GIT_REPOSITORY_INIT_NO_DOTGIT_DIR = C.GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
    60  GIT_REPOSITORY_INIT_MKDIR = C.GIT_REPOSITORY_INIT_MKDIR
    61  GIT_REPOSITORY_INIT_MKPATH = C.GIT_REPOSITORY_INIT_MKPATH
    62  GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE = C.GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
    63  GIT_REPOSITORY_INIT_RELATIVE_GITLINK = C.GIT_REPOSITORY_INIT_RELATIVE_GITLINK
    64  GIT_REPOSITORY_INIT_SHARED_UMASK = C.GIT_REPOSITORY_INIT_SHARED_UMASK
    65  GIT_REPOSITORY_INIT_SHARED_GROUP = C.GIT_REPOSITORY_INIT_SHARED_GROUP
    66  GIT_REPOSITORY_INIT_SHARED_ALL = C.GIT_REPOSITORY_INIT_SHARED_ALL
    67  
    68  # GIT_ATTR_CHECK_*
    69  GIT_ATTR_CHECK_FILE_THEN_INDEX = C.GIT_ATTR_CHECK_FILE_THEN_INDEX
    70  GIT_ATTR_CHECK_INDEX_THEN_FILE = C.GIT_ATTR_CHECK_INDEX_THEN_FILE
    71  GIT_ATTR_CHECK_INDEX_ONLY      = C.GIT_ATTR_CHECK_INDEX_ONLY
    72  GIT_ATTR_CHECK_NO_SYSTEM       = C.GIT_ATTR_CHECK_NO_SYSTEM
    73  
    74  
    75  def init_repository(path, bare=False,
    76                      flags=GIT_REPOSITORY_INIT_MKPATH,
    77                      mode=0,
    78                      workdir_path=None,
    79                      description=None,
    80                      template_path=None,
    81                      initial_head=None,
    82                      origin_url=None):
    83      """
    84      Creates a new Git repository in the given *path*.
    85  
    86      If *bare* is True the repository will be bare, i.e. it will not have a
    87      working copy.
    88  
    89      The *flags* may be a combination of:
    90  
    91      - GIT_REPOSITORY_INIT_BARE (overriden by the *bare* parameter)
    92      - GIT_REPOSITORY_INIT_NO_REINIT
    93      - GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
    94      - GIT_REPOSITORY_INIT_MKDIR
    95      - GIT_REPOSITORY_INIT_MKPATH (set by default)
    96      - GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
    97  
    98      The *mode* parameter may be any of GIT_REPOSITORY_SHARED_UMASK (default),
    99      GIT_REPOSITORY_SHARED_GROUP or GIT_REPOSITORY_INIT_SHARED_ALL, or a custom
   100      value.
   101  
   102      The *workdir_path*, *description*, *template_path*, *initial_head* and
   103      *origin_url* are all strings.
   104  
   105      See libgit2's documentation on git_repository_init_ext for further details.
   106      """
   107      # Pre-process input parameters
   108      if bare:
   109          flags |= GIT_REPOSITORY_INIT_BARE
   110  
   111      # Options
   112      options = ffi.new('git_repository_init_options *')
   113      C.git_repository_init_init_options(options,
   114                                         GIT_REPOSITORY_INIT_OPTIONS_VERSION)
   115      options.flags = flags
   116      options.mode = mode
   117  
   118      if workdir_path:
   119          workdir_path_ref = ffi.new('char []', to_bytes(workdir_path))
   120          options.workdir_path = workdir_path_ref
   121  
   122      if description:
   123          description_ref = ffi.new('char []', to_bytes(description))
   124          options.description = description_ref
   125  
   126      if template_path:
   127          template_path_ref = ffi.new('char []', to_bytes(template_path))
   128          options.template_path = template_path_ref
   129  
   130      if initial_head:
   131          initial_head_ref = ffi.new('char []', to_bytes(initial_head))
   132          options.initial_head = initial_head_ref
   133  
   134      if origin_url:
   135          origin_url_ref = ffi.new('char []', to_bytes(origin_url))
   136          options.origin_url = origin_url_ref
   137  
   138      # Call
   139      crepository = ffi.new('git_repository **')
   140      err = C.git_repository_init_ext(crepository, to_bytes(path), options)
   141      check_error(err)
   142  
   143      # Ok
   144      return Repository(to_str(path))
   145  
   146  @ffi.callback('int (*git_repository_create_cb)(git_repository **out,'
   147                'const char *path, int bare, void *payload)')
   148  def _repository_create_cb(repo_out, path, bare, data):
   149      d = ffi.from_handle(data)
   150      try:
   151          repository = d['repository_cb'](ffi.string(path), bare != 0)
   152          # we no longer own the C object
   153          repository._disown()
   154          repo_out[0] = repository._repo
   155      except Exception as e:
   156          d['exception'] = e
   157          return C.GIT_EUSER
   158  
   159      return 0
   160  
   161  @ffi.callback('int (*git_remote_create_cb)(git_remote **out, git_repository *repo,'
   162                'const char *name, const char *url, void *payload)')
   163  def _remote_create_cb(remote_out, repo, name, url, data):
   164      d = ffi.from_handle(data)
   165      try:
   166          remote = d['remote_cb'](Repository._from_c(repo, False), ffi.string(name), ffi.string(url))
   167          remote_out[0] = remote._remote
   168          # we no longer own the C object
   169          remote._remote = ffi.NULL
   170      except Exception as e:
   171          d['exception'] = e
   172          return C.GIT_EUSER
   173  
   174      return 0
   175  
   176  def clone_repository(
   177          url, path, bare=False, repository=None, remote=None,
   178          checkout_branch=None, callbacks=None):
   179      """Clones a new Git repository from *url* in the given *path*.
   180  
   181      Returns a Repository class pointing to the newly cloned repository.
   182  
   183      :param str url: URL of the repository to clone
   184  
   185      :param str path: Local path to clone into
   186  
   187      :param bool bare: Whether the local repository should be bare
   188  
   189      :param callable remote: Callback for the remote to use.
   190  
   191      :param callable repository: Callback for the repository to use.
   192  
   193      :param str checkout_branch: Branch to checkout after the
   194       clone. The default is to use the remote's default branch.
   195  
   196      :param RemoteCallbacks callbacks: object which implements the
   197       callbacks as methods.
   198  
   199      :rtype: Repository
   200  
   201      The repository callback has `(path, bare) -> Repository` as a
   202      signature. The Repository it returns will be used instead of
   203      creating a new one.
   204  
   205      The remote callback has `(Repository, name, url) -> Remote` as a
   206      signature. The Remote it returns will be used instead of the default
   207      one.
   208  
   209      The callbacks should be an object which inherits from
   210      `pyclass:RemoteCallbacks`.
   211  
   212      """
   213  
   214      opts = ffi.new('git_clone_options *')
   215      crepo = ffi.new('git_repository **')
   216  
   217      branch = checkout_branch or None
   218  
   219      # Data, let's use a dict as we don't really want much more
   220      d = {}
   221      d['repository_cb'] = repository
   222      d['remote_cb'] = remote
   223      d_handle = ffi.new_handle(d)
   224  
   225      # Perform the initialization with the version we compiled
   226      C.git_clone_init_options(opts, C.GIT_CLONE_OPTIONS_VERSION)
   227  
   228      # We need to keep the ref alive ourselves
   229      checkout_branch_ref = None
   230      if branch:
   231          checkout_branch_ref = ffi.new('char []', to_bytes(branch))
   232          opts.checkout_branch = checkout_branch_ref
   233  
   234      if repository:
   235          opts.repository_cb = _repository_create_cb
   236          opts.repository_cb_payload = d_handle
   237  
   238      if remote:
   239          opts.remote_cb = _remote_create_cb
   240          opts.remote_cb_payload = d_handle
   241  
   242  
   243      opts.bare = bare
   244  
   245      if callbacks is None:
   246          callbacks = RemoteCallbacks()
   247  
   248      callbacks._fill_fetch_options(opts.fetch_opts)
   249  
   250      err = C.git_clone(crepo, to_bytes(url), to_bytes(path), opts)
   251  
   252      if 'exception' in d:
   253          raise d['exception']
   254  
   255      check_error(err)
   256  
   257      return Repository._from_c(crepo[0], owned=True)
   258  
   259  settings = Settings()