<commit_before>#!/usr/bin/env python
"""Find pull request from given commit hash and open it in a Web browser.

Depends on: git
Supported Services: GitHub, Bitbucket
"""

import argparse
import re
import subprocess
import sys
import webbrowser

_pull_request_url = {
    'github.com': 'https://github.com/{module}/pull/{number}',
    'bitbucket.org': 'https://bitbucket.org/{module}/pull-requests/{number}',
}


def extract_service_and_module(repo_url):
    """Extract service and module from repository url.

    :param str repo_url: repository url

    :return (service, module)
    :rtype (str, str)
    """
    m = re.match('.+[/@]([^\.]+\.[^\.]+)[:/]([^/]+/[^/]+)\.git$', repo_url)
    if not m:
        raise Exception(
            'cannot detect service and module from {0}'.format(repo_url))
    service = m.group(1)
    module = m.group(2)
    if service not in _pull_request_url.keys():
        raise Exception(
            'service not supported: {0}'.format(service))
    return (service, module)


def get_remote_url():
    """Get remote repository url of current git repository.

    :return: remote repository url
    :rtype: str
    """
    args = ['git', 'remote', 'get-url', 'origin']
    return subprocess.check_output(args).strip().decode('utf-8')


def get_pull_request_number(revision, base_branch):
    """Get pull request number from commit messages.

    :param str revision: revision string
    :param str base_branch: branch against which pull request are merged

    :return: pull request number
    :rtype: str
    """
    if not re.match('^[0-9a-f]+$', revision):
        raise Exception('invalid revision: {0}'.format(revision))
    args = ['git', 'log',
            '--merges', '--oneline', '--reverse', '--ancestry-path',<commit_msg>cause error explicitly if the commit is not merged yet<commit_after>#!/usr/bin/env python
"""Find pull request from given commit hash and open it in a Web browser.

Depends on: git
Supported Services: GitHub, Bitbucket
"""

import argparse
import re
import subprocess
import sys
import webbrowser

_pull_request_url = {
    'github.com': 'https://github.com/{module}/pull/{number}',
    'bitbucket.org': 'https://bitbucket.org/{module}/pull-requests/{number}',
}


def extract_service_and_module(repo_url):
    """Extract service and module from repository url.

    :param str repo_url: repository url

    :return (service, module)
    :rtype (str, str)
    """
    m = re.match('.+[/@]([^\.]+\.[^\.]+)[:/]([^/]+/[^/]+)\.git$', repo_url)
    if not m:
        raise Exception(
            'cannot detect service and module from {0}'.format(repo_url))
    service = m.group(1)
    module = m.group(2)
    if service not in _pull_request_url.keys():
        raise Exception(
            'service not supported: {0}'.format(service))
    return (service, module)


def get_remote_url():
    """Get remote repository url of current git repository.

    :return: remote repository url
    :rtype: str
    """
    args = ['git', 'remote', 'get-url', 'origin']
    return subprocess.check_output(args).strip().decode('utf-8')


def get_pull_request_number(revision, base_branch):
    """Get pull request number from commit messages.

    :param str revision: revision string
    :param str base_branch: branch against which pull request are merged

    :return: pull request number
    :rtype: str
    """
    if not re.match('^[0-9a-f]+$', revision):
        raise Exception('invalid revision: {0}'.format(revision))
    args = ['git', 'merge-base',
            '--is-ancestor',
            revision,
            base_branch]
    try:
        subprocess.check_output(args)
    except subprocess.CalledProcessError as e:
        if e.returncode == 1:
            raise Exception(
                '{revision} is not merged to {base_branch} yet'.format(
                    **{'revision': revision, 'base_branch': base_branch}))
        raise
    args = ['git', 'log',
            '--merges', '--oneline', '--reverse', '--ancestry-path',
{'fraction_non_alphanumeric': 0.1498998998998999, 'fraction_numerical': 0.004254254254254255, 'mean_word_length': 3.017085427135678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '6452477', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1202, 'n_words': 359}