<commit_before>import os
import re
import sys

from rbtools.clients import SCMClient, RepositoryInfo
from rbtools.clients.svn import SVNClient, SVNRepositoryInfo
from rbtools.utils.checks import check_install
from rbtools.utils.process import die, execute


class GitClient(SCMClient):
    """
    A wrapper around git that fetches repository information and generates
    compatible diffs. This will attempt to generate a diff suitable for the
    remote repository, whether git, SVN or Perforce.
    """
    def __init__(self, **kwargs):
        super(GitClient, self).__init__(**kwargs)
        # Store the 'correct' way to invoke git, just plain old 'git' by
        # default.
        self.git = 'git'

    def _strip_heads_prefix(self, ref):
        """ Strips prefix from ref name, if possible """
        return re.sub(r'^refs/heads/', '', ref)

    def _set_guesses(self, summary_log_revrange, description_log_revrange):
        """ Sets summary/descr/etc if needed and --guess-foo is specified """
        if self.options.guess_summary and not self.options.summary:
            s = execute([self.git, "log", "--pretty=format:%s",
                         summary_log_revrange],
                        ignore_errors=True)
            self.options.summary = s.replace('\n', ' ').strip()

        if self.options.guess_description and not self.options.description:
            self.options.description = execute(
                [self.git, "log", "--pretty=format:%s%n%n%b",
                 description_log_revrange],
                ignore_errors=True).strip()

        # When updating an existing review, we use the description
        # to set the 'update' comment, rather than the overall
        # description.
        # TODO(csilvers): this seems to put all the text since the
        # last push.  Maybe it *would* be better to update the
        # 'regular' description instead?
        # TODO(csilvers): if we keep this, add a --guess-change-description
        # flag to suppress this behavior.
        if self.options.rid and not self.options.change_description:
            self.options.change_description = execute(
                [self.git, "log", "--pretty=format:%s%n%n%b",
                 description_log_revrange],
                ignore_errors=True).strip()

    def _github_paths(self, url):
        """ Given one github path, return a list of all of them """
        github_re = re.compile(r'(.*://)github.com/(.*?)(.git)?')
        m = github_re.match(url)
        if not m:
            return url    # probably not a github url
        repos = m.group(2)
        # The code in postreview.py wants this to be a list, not a tuple/etc.
        return ['http://github.com/%s' % repos,
                'git://github.com/%s' % repos,
                'git://github.com/%s.git' % repos,
                ]

    def get_repository_info(self):
        if not check_install('git --help'):
            # CreateProcess (launched via subprocess, used by check_install)
            # does not automatically append .cmd for things it finds in PATH.
            # If we're on Windows, and this works, save it for further use.
            if (sys.platform.startswith('win') and
                check_install('git.cmd --help')):
                self.git = 'git.cmd'
            else:
                return None

        git_dir = execute([self.git, "rev-parse", "--git-dir"],
                          ignore_errors=True).rstrip("\n")

        if git_dir.startswith("fatal:") or not os.path.isdir(git_dir):
            return None
        self.bare = execute([self.git, "config",
                             "core.bare"]).strip() == 'true'

        # post-review in directories other than the top level of
        # of a work-tree would result in broken diffs on the server
        if not self.bare:<commit_msg>Fix a typo in my github regexp, and also add https://... as an alias.<commit_after>import os
import re
import sys

from rbtools.clients import SCMClient, RepositoryInfo
from rbtools.clients.svn import SVNClient, SVNRepositoryInfo
from rbtools.utils.checks import check_install
from rbtools.utils.process import die, execute


class GitClient(SCMClient):
    """
    A wrapper around git that fetches repository information and generates
    compatible diffs. This will attempt to generate a diff suitable for the
    remote repository, whether git, SVN or Perforce.
    """
    def __init__(self, **kwargs):
        super(GitClient, self).__init__(**kwargs)
        # Store the 'correct' way to invoke git, just plain old 'git' by
        # default.
        self.git = 'git'

    def _strip_heads_prefix(self, ref):
        """ Strips prefix from ref name, if possible """
        return re.sub(r'^refs/heads/', '', ref)

    def _set_guesses(self, summary_log_revrange, description_log_revrange):
        """ Sets summary/descr/etc if needed and --guess-foo is specified """
        if self.options.guess_summary and not self.options.summary:
            s = execute([self.git, "log", "--pretty=format:%s",
                         summary_log_revrange],
                        ignore_errors=True)
            self.options.summary = s.replace('\n', ' ').strip()

        if self.options.guess_description and not self.options.description:
            self.options.description = execute(
                [self.git, "log", "--pretty=format:%s%n%n%b",
                 description_log_revrange],
                ignore_errors=True).strip()

        # When updating an existing review, we use the description
        # to set the 'update' comment, rather than the overall
        # description.
        # TODO(csilvers): this seems to put all the text since the
        # last push.  Maybe it *would* be better to update the
        # 'regular' description instead?
        # TODO(csilvers): if we keep this, add a --guess-change-description
        # flag to suppress this behavior.
        if self.options.rid and not self.options.change_description:
            self.options.change_description = execute(
                [self.git, "log", "--pretty=format:%s%n%n%b",
                 description_log_revrange],
                ignore_errors=True).strip()

    def _github_paths(self, url):
        """ Given one github path, return a list of all of them """
        github_re = re.compile(r'(.*://)github.com/(.*?)(.git)?$')
        m = github_re.match(url)
        if not m:
            return url    # probably not a github url
        repos = m.group(2)
        # The code in postreview.py wants this to be a list, not a tuple/etc.
        return ['http://github.com/%s' % repos,
                'https://github.com/%s' % repos,
                'git://github.com/%s' % repos,
                'git://github.com/%s.git' % repos,
                ]

    def get_repository_info(self):
        if not check_install('git --help'):
            # CreateProcess (launched via subprocess, used by check_install)
            # does not automatically append .cmd for things it finds in PATH.
            # If we're on Windows, and this works, save it for further use.
            if (sys.platform.startswith('win') and
                check_install('git.cmd --help')):
                self.git = 'git.cmd'
            else:
                return None

        git_dir = execute([self.git, "rev-parse", "--git-dir"],
                          ignore_errors=True).rstrip("\n")

        if git_dir.startswith("fatal:") or not os.path.isdir(git_dir):
            return None
        self.bare = execute([self.git, "config",
                             "core.bare"]).strip() == 'true'

        # post-review in directories other than the top level of
        # of a work-tree would result in broken diffs on the server
        if not self.bare:
{'fraction_non_alphanumeric': 0.10992268041237113, 'fraction_numerical': 0.0002577319587628866, 'mean_word_length': 2.1056422569027613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1965515', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2101, 'n_words': 674}