<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012-2019 SoftBank Robotics. All rights reserved.
# Use of this source code is governed by a BSD-style license (see the COPYING file).
""" A Pythonic git API """
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function

import os
import functools
import contextlib
import subprocess
import six

import qisys
import qisys.command
from qisys import ui


class Git(object):
    """ The Git represent a git tree """

    def __init__(self, repo):
        """
        Git Init
        :param repo: The path to the tree
        """
        self.repo = repo
        self._transaction = None

    def call(self, *args, **kwargs):
        """
        Call a git command.
        :param args: The arguments of the command.
                     For instance ["frobnicate", "--spam=eggs"]

        :param kwargs: Will be passed to subprocess.check_call()
                       command, with the following changes:
           * if cwd is not given it will be self.repo instead
           * if env is not given it will be read from the config file
           * if raises is False, no exception will be raised if command
             fails, and a (retcode, output) tuple will be returned.
        """
        if not self._transaction:
            return self._call(*args, **kwargs)
        if not self._transaction.ok:
            # Do not run any more command if transaction failed:
            return None
        # Force raises to False
        kwargs["raises"] = False
        (retcode, out) = self._call(*args, **kwargs)
        if retcode != 0:
            self._transaction.ok = False
            self._transaction.output += "git %s failed\n" % (" ".join(args))
            self._transaction.output += out
        return retcode, out

    def _call(self, *args, **kwargs):
        """ Helper for self.call """
        assert bool(args)
        keep_last_newline_whitelist = (
            "show",
        )
        ui.debug("git", " ".join(args), "in", self.repo)
        if "cwd" not in kwargs.keys():
            kwargs["cwd"] = self.repo
        if "quiet" not in kwargs.keys():
            kwargs["quiet"] = False
        # Allow caller to keep the ending new line of the git command outputs
        keep_last_newline = kwargs.get("keep_last_newline", args[0] in keep_last_newline_whitelist)
        if "keep_last_newline" in kwargs:
            del kwargs["keep_last_newline"]
        git = qisys.command.find_program("git", raises=True)
        cmd = [git]
        cmd.extend(args)
        raises = kwargs.get("raises", True)
        if "raises" in kwargs:
            del kwargs["raises"]
        env = os.environ.copy()
        for key in env.keys():
            if key.startswith("GIT_"):
                del env[key]
        env_str = {}
        for key, value in env.items():
            if six.PY2:
                if isinstance(key, unicode):
                    key = key.encode('utf-8')
                if isinstance(value, unicode):
                    value = value.encode('utf-8')
            env_str[key] = value
        if not raises:
            del kwargs["quiet"]
            process = subprocess.Popen(cmd,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.STDOUT,
                                       env=env_str,
                                       **kwargs)
            out, _err = process.communicate()
            out = out.decode("utf-8", "ignore")
            if not keep_last_newline:
                # Usually, don't want useless blank lines
                out = out.rstrip("\n")
            ui.debug("out:", out)<commit_msg>prevent iterator change in the loop<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012-2019 SoftBank Robotics. All rights reserved.
# Use of this source code is governed by a BSD-style license (see the COPYING file).
""" A Pythonic git API """
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function

import os
import functools
import contextlib
import subprocess
import six

import qisys
import qisys.command
from qisys import ui


class Git(object):
    """ The Git represent a git tree """

    def __init__(self, repo):
        """
        Git Init
        :param repo: The path to the tree
        """
        self.repo = repo
        self._transaction = None

    def call(self, *args, **kwargs):
        """
        Call a git command.
        :param args: The arguments of the command.
                     For instance ["frobnicate", "--spam=eggs"]

        :param kwargs: Will be passed to subprocess.check_call()
                       command, with the following changes:
           * if cwd is not given it will be self.repo instead
           * if env is not given it will be read from the config file
           * if raises is False, no exception will be raised if command
             fails, and a (retcode, output) tuple will be returned.
        """
        if not self._transaction:
            return self._call(*args, **kwargs)
        if not self._transaction.ok:
            # Do not run any more command if transaction failed:
            return None
        # Force raises to False
        kwargs["raises"] = False
        (retcode, out) = self._call(*args, **kwargs)
        if retcode != 0:
            self._transaction.ok = False
            self._transaction.output += "git %s failed\n" % (" ".join(args))
            self._transaction.output += out
        return retcode, out

    def _call(self, *args, **kwargs):
        """ Helper for self.call """
        assert bool(args)
        keep_last_newline_whitelist = (
            "show",
        )
        ui.debug("git", " ".join(args), "in", self.repo)
        if "cwd" not in kwargs.keys():
            kwargs["cwd"] = self.repo
        if "quiet" not in kwargs.keys():
            kwargs["quiet"] = False
        # Allow caller to keep the ending new line of the git command outputs
        keep_last_newline = kwargs.get("keep_last_newline", args[0] in keep_last_newline_whitelist)
        if "keep_last_newline" in kwargs:
            del kwargs["keep_last_newline"]
        git = qisys.command.find_program("git", raises=True)
        cmd = [git]
        cmd.extend(args)
        raises = kwargs.get("raises", True)
        if "raises" in kwargs:
            del kwargs["raises"]
        env = os.environ.copy()
        for key in os.environ.keys():
            if key.startswith("GIT_"):
                del env[key]
        env_str = {}
        for key, value in env.items():
            if six.PY2:
                if isinstance(key, unicode):
                    key = key.encode('utf-8')
                if isinstance(value, unicode):
                    value = value.encode('utf-8')
            env_str[key] = value
        if not raises:
            del kwargs["quiet"]
            process = subprocess.Popen(cmd,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.STDOUT,
                                       env=env_str,
                                       **kwargs)
            out, _err = process.communicate()
            out = out.decode("utf-8", "ignore")
            if not keep_last_newline:
                # Usually, don't want useless blank lines
                out = out.rstrip("\n")
            ui.debug("out:", out)
{'fraction_non_alphanumeric': 0.09966909331568498, 'fraction_numerical': 0.003970880211780278, 'mean_word_length': 1.7228828828828828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '875930', 'n_tokens_mistral': 2076, 'n_tokens_neox': 2025, 'n_words': 668}