Python git.Git() Examples

The following are 30 code examples of git.Git(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module git , or try the search function .
Example #1
Source File: params.py    From ParlAI with MIT License 7 votes vote down vote up
def print_git_commit():
    """
    Print the current git commit of ParlAI and parlai_internal.
    """
    root = os.path.dirname(os.path.dirname(parlai.__file__))
    internal_root = os.path.join(root, 'parlai_internal')
    try:
        git_ = git.Git(root)
        current_commit = git_.rev_parse('HEAD')
        logging.info(f'Current ParlAI commit: {current_commit}')
    except git.GitCommandNotFound:
        pass
    except git.GitCommandError:
        pass

    try:
        git_ = git.Git(internal_root)
        internal_commit = git_.rev_parse('HEAD')
        logging.info(f'Current internal commit: {internal_commit}')
    except git.GitCommandNotFound:
        pass
    except git.GitCommandError:
        pass 
Example #2
Source File: main.py    From BOJ-AutoCommit with MIT License 6 votes vote down vote up
def login_github():
    git = Git()
    
    for i in range(3):
        user_id = input('* GitHub id: ')
        user_password = getpass.getpass('* GitHub password: ')

        error, result = git.login_github(user_id, user_password)
        if error:
            print(PRINT_FORMAT % (result))
            continue

        print(PRINT_FORMAT % (result))
        repo_name = input('* Repository: ')
        git.set_repository(repo_name)
        return git
    
    sys.exit(ERROR_FORMAT % ('login_github', 'Login failed')) 
Example #3
Source File: git_repository.py    From pydriller with Apache License 2.0 6 votes vote down vote up
def __init__(self, path: str, conf=None):
        """
        Init the Git RepositoryMining.

        :param str path: path to the repository
        """
        self.path = Path(path)
        self.project_name = self.path.name
        self.lock = Lock()
        self._git = None
        self._repo = None

        # if no configuration is passed, then creates a new "emtpy" one
        # with just "path_to_repo" inside.
        if conf is None:
            conf = Conf({
                "path_to_repo": str(self.path),
                "git_repo": self
            })

        self._conf = conf
        self._conf.set_value("main_branch", None)  # init main_branch to None 
Example #4
Source File: location.py    From nautilus-git with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, git_uri, window):
        self._window = window
        self._git = Git(git_uri)
        self._watchdog = WatchDog(self._git.dir)
        self._watchdog.connect("refresh", self._refresh)

        self._builder = Gtk.Builder()

        self._builder.add_from_resource('/com/nautilus/git/ui/location.ui')
        self._builder.connect_signals({
            "open_remote_clicked": self._open_remote_browser,
            "compare_commits_clicked": self._compare_commits,
            "popover_clicked": self._trigger_popover,
            "branch_clicked": self._update_branch
        })
        self._build_widgets() 
Example #5
Source File: params.py    From neural_chat with MIT License 6 votes vote down vote up
def print_git_commit():
    """Print the current git commit of ParlAI and parlai_internal."""
    root = os.path.dirname(os.path.dirname(parlai.__file__))
    internal_root = os.path.join(root, 'parlai_internal')
    try:
        git_ = git.Git(root)
        current_commit = git_.rev_parse('HEAD')
        print(f'[ Current ParlAI commit: {current_commit} ]')
    except git.GitCommandNotFound:
        pass
    except git.GitCommandError:
        pass

    try:
        git_ = git.Git(internal_root)
        internal_commit = git_.rev_parse('HEAD')
        print(f'[ Current internal commit: {internal_commit} ]')
    except git.GitCommandNotFound:
        pass 
Example #6
Source File: upgrade.py    From mycroft-skills-kit with Apache License 2.0 6 votes vote down vote up
def create_pr_message(self, skill_git: Git, skill_repo: Repository) -> tuple:
        """Reads git commits from skill repo to create a list of changes as the PR content"""
        title = 'Upgrade ' + self.skill.name
        body = body_template.format(
            skill_name=self.skill.name,
            commits='\n'.join(
                ' - [{}]({})'.format(
                    skill_git.show('-s', sha, format='%s'),
                    skill_repo.get_commit(sha).html_url
                )
                for sha in skill_git.rev_list(
                    '--ancestry-path', '{}..{}'.format(self.skill.entry.sha, 'HEAD')
                ).split('\n')
            )
        )
        return title, body 
Example #7
Source File: create.py    From mycroft-skills-kit with Apache License 2.0 6 votes vote down vote up
def link_github_repo(self, get_repo_name: Callable = None) -> Optional[Repository]:
        if 'origin' not in Git(self.path).remote().split('\n'):
            if ask_yes_no(
                    'Would you like to link an existing GitHub repo to it? (Y/n)',
                    True):
                repo_name = (get_repo_name and get_repo_name()) or (
                        self.name + '-skill')
                repo = self.user.get_repo(repo_name)
                self.git.remote('add', 'origin', repo.html_url)
                self.git.fetch()
                try:
                    self.git.pull('origin', 'master')
                except GitCommandError as e:
                    if e.status == 128:
                        raise UnrelatedGithubHistory(repo_name) from e
                    raise
                self.git.push('origin', 'master', set_upstream=True)
                print('Linked and pushed to GitHub repo:', repo.html_url)
                return repo 
Example #8
Source File: gotoroot.py    From squadron with MIT License 6 votes vote down vote up
def go_to_root(fn):
    """
    Decorator which will execute the decorated function at the root
    of the git hierarchy. It returns to the old directory after
    executing the function
    """

    def wrapped(squadron_dir, *args, **kwargs):
        old_cwd = os.getcwd()
        try:
            if squadron_dir == os.getcwd():
                # We might not be at the root
                root_dir = Git(squadron_dir).rev_parse('--show-toplevel')

                os.chdir(root_dir)
                squadron_dir = root_dir
            return fn(squadron_dir, *args, **kwargs)
        finally:
            os.chdir(old_cwd)
    return wrapped 
Example #9
Source File: amdvlk_build_deb_from_tag.py    From AMDVLK with MIT License 6 votes vote down vote up
def DownloadAMDVLKComponents(self):
        os.chdir(self.srcDir);

        for i in self.components:
            if not os.path.exists(self.srcDir + i):
                print("Downloading " + i + ".....");
                git.Git().clone(self.targetRepo + i);

            repo = git.Repo(i);
            repo.git.clean('-xdf');
            # Clean the submodule
            repo.git.clean('-xdff');
            if (i == 'llvm-project'):
                repo.git.checkout('remotes/origin/amd-gfx-gpuopen-' + self.branch, B='amd-gfx-gpuopen-' + self.branch);
            elif (i == 'MetroHash' or i == 'CWPack'):
                repo.git.checkout('remotes/origin/amd-master', B='amd-master');
            else:
                repo.git.checkout('remotes/origin/' + self.branch, B=self.branch);
            repo.git.pull(); 
Example #10
Source File: gitutil.py    From cc-utils with Apache License 2.0 5 votes vote down vote up
def index_to_commit(self, message, parent_commits=None):
        '''moves all diffs from worktree to a new commit without modifying branches.
        The worktree remains unchanged after the method returns.

        @param parent_commits: optional iterable of parent commits; head is used if absent
        @return the git.Commit object representing the newly created commit
        '''
        if not parent_commits:
            parent_commits = [self.repo.head.commit]
        # add all changes
        git.cmd.Git(self.repo.working_tree_dir).add('.')
        tree = self.repo.index.write_tree()

        if self.github_cfg:
            credentials = self.github_cfg.credentials()
            author = git.objects.util.Actor(credentials.username(), credentials.email_address())
            committer = git.objects.util.Actor(credentials.username(), credentials.email_address())

            create_commit = functools.partial(
                git.Commit.create_from_tree,
                author=author,
                committer=committer,
            )
        else:
            create_commit = git.Commit.create_from_tree

        commit = create_commit(
            repo=self.repo,
            tree=tree,
            parent_commits=parent_commits,
            message=message
        )
        self.repo.index.reset()
        return commit 
Example #11
Source File: bitbucket.py    From badwolf with MIT License 5 votes vote down vote up
def clone_repository(self, full_name, path, **kwargs):
        clone_url = self.get_git_url(full_name)
        return git.Git().clone(clone_url, path, **kwargs) 
Example #12
Source File: git_processor.py    From everware with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def git_client(self):
        """single global git client instance"""
        cls = self.__class__
        if cls._git_client is None:
            cls._git_client = git.Git()
        return cls._git_client 
Example #13
Source File: iagitup.py    From iagitup with GNU General Public License v3.0 5 votes vote down vote up
def repo_download(github_repo_url):
    """Downloads a GitHub repo locally.

       arguments:
            github_repo_url -- the GitHub repo home url

       returns:
            gh_repo_data, repo_folder - the repo details and the local repo folder
    """
    download_dir = os.path.expanduser('~/.iagitup/downloads')
    mkdirs(os.path.expanduser('~/.iagitup'))
    mkdirs(download_dir)

    # parsing url to initialize the github api rul and get the repo_data
    gh_user, gh_repo = github_repo_url.split('/')[3:]
    gh_api_url = "https://api.github.com/repos/{}/{}".format(gh_user,gh_repo)

    # delete the temp directory if exists
    repo_folder = download_dir+'/'+gh_repo
    if os.path.exists(repo_folder):
        shutil.rmtree(repo_folder)

    # get the data from GitHub api
    req = requests.get(gh_api_url)
    if req.status_code == 200:
        gh_repo_data = json.loads(req.text)
        # download the repo from github
        repo_folder = '{}/{}'.format(download_dir,gh_repo)
        try:
            git.Git().clone(gh_repo_data['clone_url'],repo_folder)
        except Exception as e:
            print 'Error occurred while downloading: {}'.format(github_repo_url)
            print str(e)
            exit(1)
    else:
        raise ValueError('Error occurred while downloading: {}'.format(github_repo_url))

    return gh_repo_data, repo_folder 
Example #14
Source File: _git.py    From sumatra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def check_version():
    if not hasattr(git, "Repo"):
        raise VersionControlError(
            "GitPython not installed. There is a 'git' package, but it is not "
            "GitPython (https://pypi.python.org/pypi/GitPython/)")
    minimum_version = '0.3.5'
    if LooseVersion(git.__version__) < LooseVersion(minimum_version):
        raise VersionControlError(
            "Your Git Python binding is too old. You require at least "
            "version {0}. You can install the latest version e.g. via "
            "'pip install -U gitpython'".format(minimum_version)) 
Example #15
Source File: _git.py    From sumatra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def use_version(self, version):
        logger.debug("Using git version: %s" % version)
        if version is not 'master':
            assert not self.has_changed()
        g = git.Git(self.path)
        g.checkout(version) 
Example #16
Source File: _git.py    From sumatra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def diff(self):
        """Difference between working copy and repository."""
        g = git.Git(self.path)
        return g.diff('HEAD', color='never') 
Example #17
Source File: _git.py    From sumatra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def reset(self):
        """Resets all uncommitted changes since the commit. Destructive, be
        careful with use"""
        g = git.Git(self.path)
        g.reset('HEAD', '--hard') 
Example #18
Source File: _git.py    From sumatra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def patch(self, diff):
        """Resets all uncommitted changes since the commit. Destructive, be
        careful with use"""
        assert not self.has_changed(), "Cannot patch dirty working copy"
        # Create temp patch file
        if diff[-1] != '\n':
            diff = diff + '\n'
        with tempfile.NamedTemporaryFile(mode='w+', delete=False) as temp_file:
            temp_file.write(diff)
            temp_file_name = temp_file.name
        try:
            g = git.Git(self.path)
            g.apply(temp_file_name)
        finally:
            os.remove(temp_file_name) 
Example #19
Source File: _git.py    From sumatra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def checkout(self, path="."):
        """Clone a repository."""
        path = os.path.abspath(path)
        g = git.Git(path)
        if self.url == path:
            # already have a repository in the working directory
            pass
        else:
            tmpdir = os.path.join(path, "tmp_git")
            g.clone(self.url, tmpdir)
            move_contents(tmpdir, path)
        self.__repository = git.Repo(path) 
Example #20
Source File: page.py    From nautilus-git with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, git_uri):
        self._git = Git(git_uri)
        self._watchdog = WatchDog(self._git.dir)
        self._watchdog.connect("refresh", self._refresh)

        self._builder = Gtk.Builder()
        self._builder.add_from_resource('/com/nautilus/git/ui/page.ui')
        self._build_widgets() 
Example #21
Source File: git_repository.py    From pydriller with Apache License 2.0 5 votes vote down vote up
def git(self):
        """
        GitPython object Git.

        :return: Git
        """
        if self._git is None:
            self._open_git()
        return self._git 
Example #22
Source File: git_repository.py    From pydriller with Apache License 2.0 5 votes vote down vote up
def _open_git(self):
        self._git = Git(str(self.path)) 
Example #23
Source File: commit.py    From pydriller with Apache License 2.0 5 votes vote down vote up
def _get_modifications(self):
        options = {}
        if self._conf.get('histogram'):
            options['histogram'] = True

        if self._conf.get('skip_whitespaces'):
            options['w'] = True

        if len(self.parents) == 1:
            # the commit has a parent
            diff_index = self._c_object.parents[0].diff(self._c_object,
                                                        create_patch=True,
                                                        **options)
        elif len(self.parents) > 1:
            # if it's a merge commit, the modified files of the commit are the
            # conflicts. This because if the file is not in conflict,
            # pydriller will visit the modification in one of the previous
            # commits. However, parsing the output of a combined diff (that
            # returns the list of conflicts) is challenging: so, right now,
            # I will return an empty array, in the meanwhile I will try to
            # find a way to parse the output.
            # c_git = Git(str(self.project_path))
            # d = c_git.diff_tree("--cc", commit.hexsha, '-r', '--abbrev=40',
            #                     '--full-index', '-M', '-p', '--no-color')
            diff_index = []
        else:
            # this is the first commit of the repo. Comparing it with git
            # NULL TREE
            diff_index = self._c_object.diff(NULL_TREE,
                                             create_patch=True,
                                             **options)

        return self._parse_diff(diff_index) 
Example #24
Source File: commit.py    From pydriller with Apache License 2.0 5 votes vote down vote up
def _get_branches(self):
        c_git = Git(str(self._conf.get('path_to_repo')))
        branches = set()
        for branch in set(c_git.branch('--contains', self.hash).split('\n')):
            branches.add(branch.strip().replace('* ', ''))
        return branches 
Example #25
Source File: create.py    From mycroft-skills-kit with Apache License 2.0 5 votes vote down vote up
def create_github_repo(self, get_repo_name: Callable = None) -> Optional[Repository]:
        if 'origin' not in Git(self.path).remote().split('\n'):
            if ask_yes_no('Would you like to create a GitHub repo for it? (Y/n)', True):
                repo_name = (get_repo_name and get_repo_name()) or (self.name + '-skill')
                try:
                    repo = self.user.create_repo(repo_name, self.short_description)
                except GithubException as e:
                    if e.status == 422:
                        raise GithubRepoExists(repo_name) from e
                    raise
                self.git.remote('add', 'origin', repo.html_url)
                call(['git', 'push', '-u', 'origin', 'master'], cwd=self.git.working_dir)
                print('Created GitHub repo:', repo.html_url)
                return repo
        return None 
Example #26
Source File: utils.py    From osspolice with GNU General Public License v3.0 5 votes vote down vote up
def checkout_git_tag(repo_dir, tag):
    import git
    exe = which("git")
    if not exe:
        raise Exception("git executable not found")
    repo = git.Git(repo_dir)
    try:
        repo.checkout(tag)
    except Exception as e:
        print ("failed to checkout tag %s of repo %s, error is %s" % (tag, repo_dir, str(e)))
        return False
    return True 
Example #27
Source File: github.py    From osspolice with GNU General Public License v3.0 5 votes vote down vote up
def clone_repo(self, repo_name, repo_url, repo_path, branch=None):
        attempts = self.__retries
        while attempts:
            try:
                import git
                logger.info("cloning repo %s branch %s into %s (attempt %s timeout %s)",
                            repo_name, branch, repo_path, self.__retries - attempts, self.__timeout)

                # register timeout handler
                with utils.time_limit(self.__timeout):
                    if branch:
                        git.Git().clone(repo_url, repo_path, depth=1, branch=branch)
                    else:
                        git.Git().clone(repo_url, repo_path, depth=1)

                    return repo_name

            # timed out
            except utils.TimeoutException as te:
                logger.error("Repo clone for %s %s", repo_name, str(te))
                attempts -= 1
                if os.path.isdir(repo_path):
                    shutil.rmtree(repo_path)
                # try again
                continue

            except Exception as e:
                logger.error("Failed to clone repo %s: %s", repo_name, str(e))
                return None

        logger.error("Giving up on cloning github repo %s!", repo_name)
        return None 
Example #28
Source File: init.py    From renku-python with Apache License 2.0 5 votes vote down vote up
def read_template_manifest(folder, checkout=False):
    """Extract template metadata from the manifest file.

    :param folder: path where to find the template manifest file
    :param checkout: checkout the template folder from local repo
    """
    manifest_path = folder / TEMPLATE_MANIFEST
    try:
        manifest = yaml.safe_load(manifest_path.read_text())
    except FileNotFoundError as e:
        raise errors.InvalidTemplateError(
            'There is no manifest file "{0}"'.format(TEMPLATE_MANIFEST)
        ) from e
    validate_template_manifest(manifest)

    if checkout:
        git_repo = git.Git(str(folder))
        template_folders = [template['folder'] for template in manifest]
        if len(template_folders) < 1:
            raise errors.InvalidTemplateError(
                'Cannot find any valid template in manifest file'
            )
        for template_folder in template_folders:
            template_path = folder / template_folder
            try:
                git_repo.checkout(template_folder)
            except git.exc.GitCommandError as e:
                raise errors.InvalidTemplateError(
                    'Cannot checkout the folder "{0}"'.format(template_folder)
                ) from e
            validate_template(template_path)

    return manifest 
Example #29
Source File: conf.py    From ParlAI with MIT License 5 votes vote down vote up
def linkcode_resolve(domain, info):
    # Resolve function for the linkcode extension.
    # Stolen shamelessly from Lasagne! Thanks Lasagne!
    # https://github.com/Lasagne/Lasagne/blob/5d3c63cb315c50b1cbd27a6bc8664b406f34dd99/docs/conf.py#L114-L135
    def find_source():
        # try to find the file and line number, based on code from numpy:
        # https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
        obj = sys.modules[info['module']]
        for part in info['fullname'].split('.'):
            obj = getattr(obj, part)
        import inspect
        import os

        fn = inspect.getsourcefile(obj)
        fn = os.path.relpath(fn, start=os.path.dirname(parlai.__file__))
        source, lineno = inspect.getsourcelines(obj)
        return fn, lineno, lineno + len(source) - 1

    if domain != 'py' or not info['module']:
        return None
    try:
        filename = 'parlai/%s#L%d-L%d' % find_source()
        tag = git.Git().rev_parse('HEAD')
        return "https://github.com/facebookresearch/ParlAI/blob/%s/%s" % (tag, filename)
    except Exception:
        return None


# At the bottom of conf.py 
Example #30
Source File: check_updates.py    From SSMA with GNU General Public License v3.0 5 votes vote down vote up
def download_yara_rules_git():
    git.Git().clone("https://github.com/Yara-Rules/rules")