Python git.GitCommandError() Examples

The following are 30 code examples of git.GitCommandError(). 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: decorators.py    From renku-python with Apache License 2.0 6 votes vote down vote up
def handle_git_except(f):
    """Wrapper which handles `RenkuException`."""
    # noqa
    @wraps(f)
    def decorated_function(*args, **kwargs):
        """Represents decorated function."""
        try:
            return f(*args, **kwargs)
        except GitCommandError as e:

            error_code = GIT_ACCESS_DENIED_ERROR_CODE \
                if 'Access denied' in e.stderr else GIT_UNKNOWN_ERROR_CODE

            return jsonify(
                error={
                    'code': error_code,
                    'reason':
                        'git error: {0}'.
                        format(' '.join(e.stderr.strip().split('\n'))),
                }
            )

    return decorated_function 
Example #3
Source File: repository.py    From git-pandas with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def file_owner(self, rev, filename, committer=True):
        """
        Returns the owner (by majority blame) of a given file in a given rev. Returns the committers' name.

        :param rev:
        :param filename:
        :param committer:
        """
        try:
            if committer:
                cm = 'committer'
            else:
                cm = 'author'

            blame = self.repo.blame(rev, os.path.join(self.git_dir, filename))
            blame = DataFrame([[x[0].committer.name, len(x[1])] for x in blame], columns=[cm, 'loc']).groupby(cm).agg(
                {'loc': np.sum})
            if blame.shape[0] > 0:
                return blame['loc'].idxmax()
            else:
                return None
        except (GitCommandError, KeyError):
            if self.verbose:
                print('Couldn\'t Calcualte File Owner for %s' % (rev,))
            return None 
Example #4
Source File: updater.py    From ethoscope with GNU General Public License v3.0 6 votes vote down vote up
def update_all_visible_branches(self):
        """
        Update all visible branches (for normal users, this will be master and dev).
        For developers, new branches can be discovered (see ``discover_branches``).
        :return:
        """
        branches = self._working_repo.branches
        out = {}

        one_success = False
        for b in branches:

            try:
                key = str(b)
                out[key]=False
                self.update_branch(b)
                out[key]=True
                one_success = True
            except GitCommandError as e:
                logging.error(traceback.format_exc())

        if not one_success:
            raise Exception("Could not update any branch. Are you connected to internet?")
        return out 
Example #5
Source File: githandler.py    From bioconda-utils with MIT License 6 votes vote down vote up
def __init__(self, folder: str=".",
                 dry_run=False,
                 home='bioconda/bioconda-recipes',
                 fork=None,
                 allow_dirty=True,
                 depth=1) -> None:
        if os.path.exists(folder):
            repo = git.Repo(folder, search_parent_directories=True)
        else:
            try:
                os.mkdir(folder)
                logger.error("cloning %s into %s", home, folder)
                repo = git.Repo.clone_from(home, folder, depth=depth)
            except git.GitCommandError:
                os.rmdir(folder)
                raise
        super().__init__(repo, dry_run, home, fork, allow_dirty)

        #: Branch to restore after running
        self.prev_active_branch = self.repo.active_branch 
Example #6
Source File: git.py    From renku-python with Apache License 2.0 6 votes vote down vote up
def find_attr(self, *paths):
        """Return map with path and its attributes."""
        from git.exc import GitCommandError

        attrs = defaultdict(dict)
        try:
            data = self.repo.git.check_attr('-z', '-a', '--', *paths)
            for file, name, value in zip_longest(
                *[iter(data.strip('\0').split('\0'))] * 3
            ):
                if file:
                    attrs[file][name] = value
        except GitCommandError:
            pass

        return attrs 
Example #7
Source File: sync.py    From FireHOL-IP-Aggregator with Apache License 2.0 6 votes vote down vote up
def fetch_diff(self):
        SyncGit.logger.info("Fetching diff from remote origin")

        try:
            firehol_repo = git.cmd.Git(self.repo_path)
            firehol_repo.checkout("master")
            firehol_repo.fetch("origin")
            diff_stdout = firehol_repo.execute(["git", "diff", "master", "origin/master"], True).split("\n")

            try:
                udiff = unidiff.PatchSet(diff_stdout)
                firehol_repo.execute(["git", "reset", "--hard", "origin/master"])
                firehol_repo.merge()
                self.logger.info("Successfully fetched diff from remote origin")

                return udiff

            except unidiff.UnidiffParseError:
                self.logger.exception("UnidiffParseError occurred")

        except git.GitCommandError:
            self.logger.exception("GitCommandError occurred") 
Example #8
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 #9
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 #10
Source File: GitStorage.py    From overleaf-backup-tool with GNU General Public License v3.0 6 votes vote down vote up
def create_or_update(self, git_url, repo_dir):
        for i in range(1, RETRY + 1):
            try:
                if os.path.isdir(repo_dir):
                     # pull
                     g = git.cmd.Git(repo_dir)
                     g.pull()
                else:
                     # clone
                     Repo.clone_from(git_url, repo_dir)
            except git.GitCommandError as ex:
                logging.info("error:{0}: retry:{1}/{2}".format(ex, i, RETRY))
                time.sleep(10 * i)
                logging.info("retrying")
            else:
                return True
        logging.exception("max retry count reached")
        raise 
Example #11
Source File: apply_patch2.py    From llvm-premerge-checks with Apache License 2.0 6 votes vote down vote up
def _refresh_master(self):
        """Update local git repo and origin.

        As origin is disjoint from upstream, it needs to be updated by this script.
        """
        if not self.push_branch:
            return

        print('Syncing local, origin and upstream...')
        self.repo.git.fetch('--all')
        self.repo.git.checkout('master')
        self.repo.git.reset('--hard')
        self.repo.git.clean('-fdx')
        if 'upstream' not in self.repo.remotes:
            self.repo.create_remote('upstream', url=LLVM_GITHUB_URL)
            self.repo.remotes.upstream.fetch()
        self.repo.git.pull('origin', 'master')
        self.repo.git.pull('upstream', 'master')
        try:
            self.repo.git.push('origin', 'master')
            print('refresh of master branch completed')
        except GitCommandError as e:
            print('Info: Could not push to origin master.') 
Example #12
Source File: make_release.py    From gimp-plugin-export-layers with GNU General Public License v3.0 6 votes vote down vote up
def _rollback(release_metadata):
  if release_metadata.dry_run:
    return
  
  try:
    release_metadata.repo.git.tag("-d", release_metadata.new_version)
  except git.GitCommandError:
    pass
  
  if os.path.isdir(INSTALLERS_OUTPUT_DIRPATH):
    shutil.rmtree(INSTALLERS_OUTPUT_DIRPATH)
  
  release_metadata.repo.git.reset(
    "--hard", release_metadata.last_commit_id_before_release)
  release_metadata.gh_pages_repo.git.reset(
    "--hard", release_metadata.last_gh_pages_commit_id_before_release) 
Example #13
Source File: git_repository.py    From pydriller with Apache License 2.0 6 votes vote down vote up
def get_commits_modified_file(self, filepath: str) -> List[str]:
        """
        Given a filepath, returns all the commits that modified this file
        (following renames).

        :param str filepath: path to the file
        :return: the list of commits' hash
        """
        path = str(Path(filepath))

        commits = []
        try:
            commits = self.git.log("--follow", "--format=%H", path).split('\n')
        except GitCommandError:
            logger.debug("Could not find information of file %s", path)

        return commits 
Example #14
Source File: git_repository.py    From pydriller with Apache License 2.0 5 votes vote down vote up
def _calculate_last_commits(self, commit: Commit,
                                modifications: List[Modification],
                                hashes_to_ignore_path: str = None) \
            -> Dict[str, Set[str]]:

        commits = {}  # type: Dict[str, Set[str]]

        for mod in modifications:
            path = mod.new_path
            if mod.change_type == ModificationType.RENAME or mod.change_type == ModificationType.DELETE:
                path = mod.old_path
            deleted_lines = mod.diff_parsed['deleted']

            try:
                blame = self._get_blame(commit.hash, path, hashes_to_ignore_path)
                for num_line, line in deleted_lines:
                    if not self._useless_line(line.strip()):
                        buggy_commit = blame[num_line - 1].split(' ')[0].replace('^', '')

                        # Skip unblamable lines.
                        if buggy_commit.startswith("*"):
                            continue

                        if mod.change_type == ModificationType.RENAME:
                            path = mod.new_path

                        commits.setdefault(path, set()).add(self.get_commit(buggy_commit).hash)
            except GitCommandError:
                logger.debug(
                    "Could not found file %s in commit %s. Probably a double "
                    "rename!", mod.filename, commit.hash)

        return commits 
Example #15
Source File: default.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def git_pull():
    """ Git Pull handler """
    app = get_app()
    if not have_git:
        session.flash = GIT_MISSING
        redirect(URL('site'))
    dialog = FORM.confirm(T('Pull'),
                          {T('Cancel'): URL('site')})
    if dialog.accepted:
        try:
            repo = git.Repo(os.path.join(apath(r=request), app))
            origin = repo.remotes.origin
            origin.fetch()
            origin.pull()
            session.flash = T("Application updated via git pull")
            redirect(URL('site'))

        except git.CheckoutError:
            session.flash = T("Pull failed, certain files could not be checked out. Check logs for details.")
            redirect(URL('site'))
        except git.UnmergedEntriesError:
            session.flash = T("Pull is not possible because you have unmerged files. Fix them up in the work tree, and then try again.")
            redirect(URL('site'))
        except git.GitCommandError:
            session.flash = T(
                "Pull failed, git exited abnormally. See logs for details.")
            redirect(URL('site'))
        except AssertionError:
            session.flash = T("Pull is not possible because you have unmerged files. Fix them up in the work tree, and then try again.")
            redirect(URL('site'))
    elif 'cancel' in request.vars:
        redirect(URL('site'))
    return dict(app=app, dialog=dialog) 
Example #16
Source File: models.py    From crane with MIT License 5 votes vote down vote up
def is_disconnected(self):
        """True if no path can be found from old commit to new commit."""
        try:
            return not (
                self.repo.is_ancestor(self.old_version, self.new_version)
                or self.repo.is_ancestor(self.new_version, self.old_version)
            )
        except git.GitCommandError:  # old commit was probably removed by force push or other black magic
            return True 
Example #17
Source File: feedstocks.py    From conda-smithy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fetch_feedstock(repo_dir):
    """Git fetch --all a single git repository."""
    repo = Repo(repo_dir)
    for remote in repo.remotes:
        try:
            remote.fetch()
        except GitCommandError:
            print(
                "Failed to fetch {} from {}.".format(remote.name, remote.url)
            ) 
Example #18
Source File: git_repository.py    From pydriller with Apache License 2.0 5 votes vote down vote up
def _delete_tmp_branch(self) -> None:
        try:
            # we are already in _PD, so checkout the master branch before
            # deleting it
            if self.repo.active_branch.name == '_PD':
                self.git.checkout('-f', self._conf.get("main_branch"))
            self.repo.delete_head('_PD', force=True)
        except GitCommandError:
            logger.debug("Branch _PD not found") 
Example #19
Source File: git_repo.py    From allura with Apache License 2.0 5 votes vote down vote up
def symbolics_for_commit(self, commit):
        try:
            branches = [
                b.name for b in self.branches if b.object_id == commit._id]
            tags = [t.name for t in self.tags if t.object_id == commit._id]
            return branches, tags
        except git.GitCommandError:
            return [], [] 
Example #20
Source File: gitter.py    From astrocats with MIT License 5 votes vote down vote up
def git_clone_all_repos(cat):
    """Perform a 'git clone' for each data repository that doesnt exist.
    """
    log = cat.log
    log.debug("gitter.git_clone_all_repos()")

    all_repos = cat.PATHS.get_all_repo_folders()
    out_repos = cat.PATHS.get_repo_output_folders()
    for repo in all_repos:
        log.info("Repo in: '{}'".format(repo))

        if os.path.isdir(repo):
            log.info("Directory exists.")
        else:
            log.debug("Cloning directory...")
            clone(repo, cat.log, depth=max(cat.args.clone_depth, 1))

        if cat.args.purge_outputs and repo in out_repos:
            for fil in glob(os.path.join(repo, '*.json')):
                os.remove(fil)

        grepo = git.cmd.Git(repo)
        try:
            grepo.status()
        except git.GitCommandError:
            log.error("Repository does not exist!")
            raise

        # Get the initial git SHA
        sha_beg = get_sha(repo)
        log.debug("Current SHA: '{}'".format(sha_beg))

    return 
Example #21
Source File: util.py    From mtgjson with MIT License 5 votes vote down vote up
def set_gist_json_file(
    username: str, api_token: str, repo_key: str, file_name: str, content: Any
) -> None:
    """
    Update a gist file and push it live
    :param username: GH api username
    :param api_token: GH api token
    :param repo_key: GH repo key
    :param file_name: File name
    :param content: New file content
    """
    with lzma.open(temp_working_dir.joinpath(file_name), "w") as file:
        file.write(json.dumps(content).encode("utf-8"))

    try:
        repo = git.Repo(temp_working_dir)

        # Update remote to allow pushing
        repo.git.remote(
            "set-url",
            "origin",
            f"https://{username}:{api_token}@gist.github.com/{repo_key}.git",
        )

        repo.git.commit("-am", "auto-push")
        origin = repo.remote()
        origin.push()
        LOGGER.info("Committing changes to CH database")
    except git.GitCommandError:
        LOGGER.warning(f"No changes to CH database detected")

    LOGGER.info("Removing local CH database")
    shutil.rmtree(temp_working_dir) 
Example #22
Source File: test_pipeline.py    From badwolf with MIT License 5 votes vote down vote up
def test_clone_repo_failed(app, pipeline):
    with mock.patch.object(pipeline, 'clone') as mock_clone, \
            mock.patch.object(pipeline, '_report_git_error') as report_git_error, \
            mock.patch.object(pipeline, 'parse_spec') as mock_spec, \
            mock.patch.object(PullRequest, 'comment') as pr_comment, \
            mock.patch.object(Changesets, 'comment') as cs_comment:
        mock_clone.side_effect = git.GitCommandError('git clone', 1)
        report_git_error.return_value = None
        pr_comment.return_value = None
        cs_comment.return_value = None
        pipeline.start()
        assert report_git_error.called
        mock_spec.assert_not_called() 
Example #23
Source File: pipeline.py    From badwolf with MIT License 5 votes vote down vote up
def start(self):
        '''Start Pipeline'''
        logger.info('Pipeline started for repository %s', self.context.repository)
        try:
            self.clone()
            self.parse_spec()
            exit_code = self.build()
            build_success = exit_code == 0
            self.save_artifacts(build_success)
            if exit_code != 137:
                # 137 means build cancelled
                self.lint()
            if build_success:
                self.deploy()
        except git.GitCommandError as git_err:
            logger.exception('Git command error')
            self._report_git_error(git_err)
        except BitbucketAPIError:
            logger.exception('Error calling BitBucket API')
            sentry.captureException()
        except InvalidSpecification as err:
            self._report_error(':umbrella: Invalid badwolf configuration: ' + str(err))
        except BadwolfException:
            pass
        finally:
            self.clean() 
Example #24
Source File: cloner.py    From badwolf with MIT License 5 votes vote down vote up
def get_conflicted_files(repo_path):
        gitcmd = git.Git(repo_path)
        try:
            return gitcmd.diff('--name-only', '--diff-filter=U')
        except git.GitCommandError:
            logger.exception('Error get conflicted files by git diff command')
            return None 
Example #25
Source File: phab2github.py    From llvm-premerge-checks with Apache License 2.0 5 votes vote down vote up
def _branches_identical(self, left, right) -> bool:
        """Check if two branches are identical."""
        try:
            patch = self.repo.git.diff(left, right)
        except git.GitCommandError:
            return False
        if len(patch) == 0:
            return True
        return False 
Example #26
Source File: cleanup_branches.py    From llvm-premerge-checks with Apache License 2.0 5 votes vote down vote up
def delete_old_branches(repo_path: str, max_age: datetime.datetime, branch_patterns: List[re.Pattern],
                        *, dry_run: bool = True, remote_name: str = 'origin') -> bool:
    """Deletes 'old' branches from a git repo.

    This script assumes that $repo_path contains a current checkout of the repository ot be cleaned up.
    :retrun True IFF branches could be deleted successfully.
    """
    repo = git.Repo(repo_path)
    remote = repo.remote(name=remote_name)
    repo.git.fetch('--prune')
    refs = remote.refs
    print('Found {} branches at {} in total.'.format(len(refs), remote_name))
    del_count = 0
    fail_count = 0
    if dry_run:
        print('DRY RUN. NO BRANCHES WILL BE DELETED', flush=True)
    print('Deleting: \n', flush=True)
    for reference in refs:
        committed_date = datetime.datetime.fromtimestamp(reference.commit.committed_date)
        if committed_date < max_age and _has_pattern_match(reference.name, branch_patterns):
            print(reference.name, flush=True)
            if not dry_run:
                try:
                    remote.push(refspec=':{}'.format(reference.remote_head))
                    del_count += 1
                except git.GitCommandError as err:
                    print('ERROR: Failed to delete "{}": {}'.format(reference.name, err), flush=True)
                    fail_count += 1
    print('Deleted {} branches.'.format(del_count))
    if fail_count > 0:
        print('Failed to delete {} branches.'.format(fail_count))
    return fail_count == 0 
Example #27
Source File: default.py    From termite-data-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def git_pull():
    """ Git Pull handler """
    app = get_app()
    if not have_git:
        session.flash = GIT_MISSING
        redirect(URL('site'))
    dialog = FORM.confirm(T('Pull'),
                          {T('Cancel'): URL('site')})
    if dialog.accepted:
        try:
            repo = git.Repo(os.path.join(apath(r=request), app))
            origin = repo.remotes.origin
            origin.fetch()
            origin.pull()
            session.flash = T("Application updated via git pull")
            redirect(URL('site'))

        except git.CheckoutError:
            session.flash = T("Pull failed, certain files could not be checked out. Check logs for details.")
            redirect(URL('site'))
        except git.UnmergedEntriesError:
            session.flash = T("Pull is not possible because you have unmerged files. Fix them up in the work tree, and then try again.")
            redirect(URL('site'))
        except git.GitCommandError:
            session.flash = T(
                "Pull failed, git exited abnormally. See logs for details.")
            redirect(URL('site'))
        except AssertionError:
            session.flash = T("Pull is not possible because you have unmerged files. Fix them up in the work tree, and then try again.")
            redirect(URL('site'))
    elif 'cancel' in request.vars:
        redirect(URL('site'))
    return dict(app=app, dialog=dialog) 
Example #28
Source File: default.py    From termite-data-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def git_pull():
    """ Git Pull handler """
    app = get_app()
    if not have_git:
        session.flash = GIT_MISSING
        redirect(URL('site'))
    dialog = FORM.confirm(T('Pull'),
                          {T('Cancel'): URL('site')})
    if dialog.accepted:
        try:
            repo = git.Repo(os.path.join(apath(r=request), app))
            origin = repo.remotes.origin
            origin.fetch()
            origin.pull()
            session.flash = T("Application updated via git pull")
            redirect(URL('site'))

        except git.CheckoutError:
            session.flash = T("Pull failed, certain files could not be checked out. Check logs for details.")
            redirect(URL('site'))
        except git.UnmergedEntriesError:
            session.flash = T("Pull is not possible because you have unmerged files. Fix them up in the work tree, and then try again.")
            redirect(URL('site'))
        except git.GitCommandError:
            session.flash = T(
                "Pull failed, git exited abnormally. See logs for details.")
            redirect(URL('site'))
        except AssertionError:
            session.flash = T("Pull is not possible because you have unmerged files. Fix them up in the work tree, and then try again.")
            redirect(URL('site'))
    elif 'cancel' in request.vars:
        redirect(URL('site'))
    return dict(app=app, dialog=dialog) 
Example #29
Source File: models.py    From paper-to-git with Apache License 2.0 5 votes vote down vote up
def commit_changes(self, push=False):
        git_repo = Repo(self.repo)
        git_repo.git.add('content/')
        try:
            git_repo.git.commit('-m', 'Commit added by PaperGit')
            self.last_run = time.time()
            self.save()
        except GitCommandError:
            print('Nothing to commit')
        if push:
            print("Pushing changes to remote")
            git_repo.git.push('origin') 
Example #30
Source File: test_wiki.py    From allura with Apache License 2.0 5 votes vote down vote up
def test_has_wiki_repo(self, repo, rmtree, mkdtemp):
        mkdtemp.return_value = 'fake path'
        i = GitHubWikiImporter()
        assert_equal(i.has_wiki_repo('fake url'), True)
        repo.clone_from.assert_called_once_with(
            'fake url', to_path='fake path', bare=True)
        rmtree.assert_called_once_with('fake path')

        def raise_error(*args, **kw):
            raise git.GitCommandError('bam', 'bam', 'bam')
        repo.clone_from.side_effect = raise_error
        assert_equal(i.has_wiki_repo('fake url'), False)