<commit_before>import argparse
import logging
import os
import time

__author__ = 'tusharmakkar08'

logger = logging.getLogger('git-del-br')
FORMAT = "%(asctime)-15s:@%(lineno)s %(message)s"
formatter = logging.Formatter(FORMAT)
file_handler = logging.FileHandler("git-del-br.log")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)


def delete_remote_merged_branches(branches):
    logger.debug("Deleting remote branches %s", branches)
    pass


def delete_local_merged_branches(branches):
    logger.debug("Deleting local branches %s", branches)
    for branch in branches:
        cmd = 'git branch -D ' + branch
        answer = os.popen(cmd).read()
        logger.debug("%s", answer)


def get_merged_branches(branch_name, remote_flag=False):
    logger.debug("Getting Merged Branches %s %s", branch_name, remote_flag)
    remote_var = ''
    if remote_flag:
        remote_var = '-r'
    cmd = 'git branch ' + remote_var + ' --merged ' + branch_name + """ | grep -v "\*" | grep -v master"""
    answer = os.popen(cmd).read()
    return [i.strip() for i in answer.split('\n') if i]


def filter_time(branches, time_to_remove):
    logger.debug("Filtering on basis of time %s time %s", branches, time_to_remove)
    if time_to_remove == -1:
        return branches
    filtered_branches = []
    for branch in branches:
        cmd = 'echo `git show --format="%ct" ' + branch + '| head -n 1`;'
        answer = os.popen(cmd).read()
        if (int(time.time()) - int(answer)) / 86400 > time_to_remove:
            filtered_branches.append(branches)
    return filtered_branches


def filter_suffix(branches, suffix):
    logger.debug("Filtering on basis of suffix %s suffix %s", branches, suffix)
    return [branch for branch in branches if branch.endswith(suffix)]


def filter_prefix(branches, prefix):
    logger.debug("Filtering on basis of prefix %s prefix %s", branches, prefix)
    return [branch for branch in branches if branch.startswith(prefix)]


def view_and_delete_branches(list_flag, branch_name, prefix, suffix, remote_flag, local_flag, time_to_remove):
    if remote_flag:
        branches_to_remove = filter_time(
            filter_suffix(filter_prefix(get_merged_branches(branch_name, remote_flag), prefix), suffix),
            time_to_remove)
        logger.debug("Viewing remote %s delete flag %s", branches_to_remove, list_flag)
        if not list_flag:
            delete_remote_merged_branches(branches_to_remove)
    if local_flag:
        branches_to_remove = filter_time(
            filter_suffix(filter_prefix(get_merged_branches(branch_name), prefix), suffix), time_to_remove)
        logger.debug("Viewing local branches %s delete flag %s", branches_to_remove, list_flag)<commit_msg>Deleting remote branches as well<commit_after>import argparse
import logging
import os
import time

__author__ = 'tusharmakkar08'

logger = logging.getLogger('git-del-br')
FORMAT = "%(asctime)-15s:@%(lineno)s %(message)s"
formatter = logging.Formatter(FORMAT)
file_handler = logging.FileHandler("git-del-br.log")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)


def delete_remote_merged_branches(branches):
    logger.debug("Deleting remote branches %s", branches)
    for branch in branches:
        cmd = 'git push origin --delete ' + ''.join(branch.split('origin/')[1:])
        answer = os.popen(cmd).read()
        logger.debug("%s", answer)
    pass


def delete_local_merged_branches(branches):
    logger.debug("Deleting local branches %s", branches)
    for branch in branches:
        cmd = 'git branch -D ' + branch
        answer = os.popen(cmd).read()
        logger.debug("%s", answer)


def get_merged_branches(branch_name, remote_flag=False):
    logger.debug("Getting Merged Branches %s %s", branch_name, remote_flag)
    remote_var = ''
    if remote_flag:
        remote_var = '-r'
    cmd = 'git branch ' + remote_var + ' --merged ' + branch_name + """ | grep -v "\*" | grep -v master"""
    answer = os.popen(cmd).read()
    return [i.strip() for i in answer.split('\n') if i]


def filter_time(branches, time_to_remove):
    logger.debug("Filtering on basis of time %s time %s", branches, time_to_remove)
    if time_to_remove == -1:
        return branches
    filtered_branches = []
    for branch in branches:
        cmd = 'echo `git show --format="%ct" ' + branch + '| head -n 1`;'
        answer = os.popen(cmd).read()
        if (int(time.time()) - int(answer)) / 86400 > time_to_remove:
            filtered_branches.append(branches)
    return filtered_branches


def filter_suffix(branches, suffix):
    logger.debug("Filtering on basis of suffix %s suffix %s", branches, suffix)
    return [branch for branch in branches if branch.endswith(suffix)]


def filter_prefix(branches, prefix):
    logger.debug("Filtering on basis of prefix %s prefix %s", branches, prefix)
    return [branch for branch in branches if branch.startswith(prefix)]


def view_and_delete_branches(list_flag, branch_name, prefix, suffix, remote_flag, local_flag, time_to_remove):
    if remote_flag:
        branches_to_remove = filter_time(
            filter_suffix(filter_prefix(get_merged_branches(branch_name, remote_flag), prefix), suffix),
            time_to_remove)
        logger.debug("Viewing remote %s delete flag %s", branches_to_remove, not list_flag)
        if not list_flag:
            delete_remote_merged_branches(branches_to_remove)
    if local_flag:
        branches_to_remove = filter_time(
            filter_suffix(filter_prefix(get_merged_branches(branch_name), prefix), suffix), time_to_remove)
        logger.debug("Viewing local branches %s delete flag %s", branches_to_remove, not list_flag)
{'fraction_non_alphanumeric': 0.11450912250217203, 'fraction_numerical': 0.003996524761077324, 'mean_word_length': 3.824811399832355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '451063', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1788, 'n_words': 451}