Example #1
0
 def switch(git, branch):
     # git(f'switch -c "{escape(branch)}"'); return git
     if git.exists(branch):
         git(f'checkout "{escape(branch)}"')
     else:
         git(f'checkout -b "{escape(branch)}"')
     return git
Example #2
0
 def clone(cls, path: str, url: str, username: str, password: str,
           email: str, *args, **kwargs):
     url = cls._make_remote_url(url, username, password)
     git(f'clone', *args, *kwargs.values(), url, path)
     repo = Repo(path,
                 url=url,
                 username=username,
                 password=password,
                 email=email)
     return repo
Example #3
0
 def set_credential_helper(git, helper, *options):
     git.disable_credential_helper()
     try:
         current = git.get_credential_helper()
     except:
         current = ""
     if helper not in current:
         git(f"config --global credential.helper '{helper}{' ' + ' '.join(options) if options else ''}'"
             )
     return git
Example #4
0
 def url(git, value):
     if value:
         # git(f'config remote.{git.remote}.url "{escape(url)}"')
         try:
             git(f'remote add {git.remote} "{escape(value)}"')
         except:
             log.info("IGNORED fatal: remote origin already exists.")
             try_to(git, f'remote set-url {git.remote} "{escape(value)}"')
         git._url = value
     else:
         git._url = git.get_url()
     for protocol in git.remote_protocols:
         if git._url.startswith(protocol):
             git.remote_protocol = protocol
Example #5
0
 def can_merge(git, into):
     """we check if the branch we want to send commits to is up to date or not. if current branch is equal to into branch we check if we want to commit"""
     current_branch = git.get_current_branch()
     if current_branch == into: return git.has_commitable_changes()
     for line in git(f'branch --contains {current_branch}').splitlines():
         if line.strip().decode('utf-8') == into:
             return True
Example #6
0
 def __init__(git,
              url=None,
              username=None,
              password=None,
              email=None,
              root=None):
     git.version = git('--version').replace(b'git version ', b'')
     v = git.version.split(b'.')
     if int(v[0]) < 2 or (int(v[0]) == 2 and int(v[1]) < 16):
         raise OSError(
             f"git version {v[2:-1]} is not supported, please install a newer version:"
             "\nhttps://git-scm.com/book/en/v2/Getting-Started-Installing-Git"
         )
     # if int(v[0]) < 3 and int(v[1]) < 23:
     #     def switch(git, branch):
     #         if git.exists(branch):
     #             git(f'checkout "{escape(branch)}"')
     #         else: git(f'checkout -b "{escape(branch)}"')
     #         return git
     #     git.switch = switch
     git.set_credential_helper('cache', '--timeout=31536000')
     git.root = root
     git.url = url
     git.username = username
     git.email = email
     git.password = password
Example #7
0
 def get_local_branches(git) -> [bytes]:
     branches = git('branch').splitlines()
     for i, branch in enumerate(branches):
         branches[i] = branch.strip()
         if branch.startswith(b'* '):
             branch = branch[2:]
             branches[i] = branch
     return branches
Example #8
0
    def init(
        cls,
        url: str,
        username: str,
        password: str,
        email: str,
        fetch_args: tuple = None,
        pull_args: tuple = None,
    ):
        url = cls._make_remote_url(url, username, password)
        git('init')
        git('remote add origin', url)
        project = cls(url, username, password, email)

        if fetch_args is None: fetch_args = tuple()
        if pull_args is None: pull_args = tuple()
        project.fetch(*fetch_args)
        project.pull(*pull_args)
        return project
Example #9
0
 def checkout(git, to_branch):
     git(f'checkout "{escape(to_branch)}"')
     return git
Example #10
0
 def create_remote(git, branch=None):
     git(f'push -u {git.remote} "{escape(git.get_current_branch() if branch is None else branch)}"'
         )
     return git
Example #11
0
 def create_branch(git, name):
     git(f'checkout -b "{escape(name)}"')
     return git
Example #12
0
 def get_password(git) -> str:
     return git('config --get user.password').decode(
         'utf-8').splitlines()[0]
Example #13
0
def get_root():
    return bs(git('rev-parse --show-toplevel')).replace('/', os.sep)[:-2]
Example #14
0
 def has_remote(git):
     return git('checkout')
Example #15
0
 def tag(git, name, msg):
     git(f'tag -a {name} -m "{escape(msg)}"')
     return git
Example #16
0
 def stash(git):
     git('stash')
     return git
Example #17
0
 def get_credential_helper(git):
     return git('config --global --get credential.helper').decode(
         'utf-8').splitlines()[0]
Example #18
0
 def get_remote_branches(git) -> [bytes]:
     return [
         branch.strip().split(b' -> ', 1)[0]
         for branch in git('branch -r').splitlines()
     ]
Example #19
0
 def get_current_branch(git) -> str:
     return git('symbolic-ref --short HEAD').decode('utf-8').strip()
Example #20
0
 def get_config(git) -> [bytes]:
     return git('config --list').splitlines()
Example #21
0
 def get_email(git) -> str:
     return git('config --get user.email').decode('utf-8').splitlines()[0]
Example #22
0
 def delete_remote(git, branch):
     git(f'push {git.remote} --delete "{escape(branch)}"')
     return git
Example #23
0
 def delete_local(git, branch):
     git(f'branch -d "{escape(branch)}"')
     return git
Example #24
0
 def get_url(git) -> str:
     return git(f'config --get remote.{git.remote}.url').decode(
         'utf-8').splitlines()[0]
Example #25
0
 def add(git, arg='.', *args):
     git('add -f', arg, *args)
     return git
Example #26
0
 def pull(git, *args):
     git('pull --rebase', *args)
     return git
Example #27
0
 def push(git, *args):
     git('push', *args)
     return git
Example #28
0
 def has_commitable_changes(git):
     return git('diff')
Example #29
0
 def fetch(git, *args):
     git('fetch', *args)
     return git
Example #30
0
 def get_username(git) -> str:
     return git('config --get user.name').decode('utf-8').splitlines()[0]