github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/lambda/GitPullS3/pygit2/refspec.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  # Import from pygit2
    32  from .errors import check_error
    33  from .ffi import ffi, C
    34  from .utils import to_bytes
    35  
    36  
    37  class Refspec(object):
    38      """The constructor is for internal use only"""
    39      def __init__(self, owner, ptr):
    40          self._owner = owner
    41          self._refspec = ptr
    42  
    43      @property
    44      def src(self):
    45          """Source or lhs of the refspec"""
    46          return ffi.string(C.git_refspec_src(self._refspec)).decode()
    47  
    48      @property
    49      def dst(self):
    50          """Destinaton or rhs of the refspec"""
    51          return ffi.string(C.git_refspec_dst(self._refspec)).decode()
    52  
    53      @property
    54      def force(self):
    55          """Whether this refspeca llows non-fast-forward updates"""
    56          return bool(C.git_refspec_force(self._refspec))
    57  
    58      @property
    59      def string(self):
    60          """String which was used to create this refspec"""
    61          return ffi.string(C.git_refspec_string(self._refspec)).decode()
    62  
    63      @property
    64      def direction(self):
    65          """Direction of this refspec (fetch or push)"""
    66          return C.git_refspec_direction(self._refspec)
    67  
    68      def src_matches(self, ref):
    69          """Return True if the given string matches the source of this refspec,
    70          False otherwise.
    71          """
    72          return bool(C.git_refspec_src_matches(self._refspec, to_bytes(ref)))
    73  
    74      def dst_matches(self, ref):
    75          """Return True if the given string matches the destination of this
    76          refspec, False otherwise."""
    77          return bool(C.git_refspec_dst_matches(self._refspec, to_bytes(ref)))
    78  
    79      def _transform(self, ref, fn):
    80          buf = ffi.new('git_buf *', (ffi.NULL, 0))
    81          err = fn(buf, self._refspec, to_bytes(ref))
    82          check_error(err)
    83  
    84          try:
    85              return ffi.string(buf.ptr).decode()
    86          finally:
    87              C.git_buf_free(buf)
    88  
    89      def transform(self, ref):
    90          """Transform a reference name according to this refspec from the lhs to
    91          the rhs. Return an string.
    92          """
    93          return self._transform(ref, C.git_refspec_transform)
    94  
    95      def rtransform(self, ref):
    96          """Transform a reference name according to this refspec from the lhs to
    97          the rhs. Return an string.
    98          """
    99          return self._transform(ref, C.git_refspec_rtransform)