<commit_before>#!/usr/bin/env python3

import sys
import os
import shutil

import mvtools_envvars
import path_utils
import detect_repo_type
import collect_git_patch
import apply_git_patch

def _test_repo_path(path):

    v, r = detect_repo_type.detect_repo_type(path)
    if not v:
        return False, r
    if r is None:
        return False, "Path [%s] does not point to a supported repository." % path
    return True, None

def port_git_repo_stash(temp_path, source_repo, target_repo):

    report = []

    stash_files = None
    v, r = collect_git_patch.collect_git_patch_stash(source_repo, temp_path)
    if not v:
        return False, "Failed collecting stash from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    stash_files = reversed(r)

    v, r = apply_git_patch.apply_git_patch_stash(target_repo, stash_files)
    if not v:
        return False, "Failed patching stash from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_previous(temp_path, source_repo, target_repo, previous_count):

    report = []

    previous_files = None
    v, r = collect_git_patch.collect_git_patch_previous(source_repo, temp_path, previous_count)
    if not v:
        return False, "Failed porting previous (during collect-previous) from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    previous_files = reversed(r)

    # previous commits will be stacked up ontop of head - no autocommitting is available (on purpose)
    v, r = apply_git_patch.apply_git_patch_head(target_repo, previous_files)
    if not v:
        return False, "Failed porting previous (during head-apply) from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_staged(temp_path, source_repo, target_repo):

    report = []

    staged_files = None
    v, r = collect_git_patch.collect_git_patch_staged(source_repo, temp_path)
    if not v:
        return False, "Failed collecting the staging area from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    staged_files = [r]

    v, r = apply_git_patch.apply_git_patch_staged(target_repo, staged_files)
    if not v:
        return False, "Failed patching the staging area from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_head(temp_path, source_repo, target_repo):

    report = []

    head_files = None
    v, r = collect_git_patch.collect_git_patch_head(source_repo, temp_path)
    if not v:
        if r == collect_git_patch.ERRMSG_EMPTY:
            return True, [] # ignore if target head is unmodified
        return False, "Failed collecting head from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    head_files = [r]

    v, r = apply_git_patch.apply_git_patch_head(target_repo, head_files)
    if not v:
        return False, "Failed patching head from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_unversioned(temp_path, source_repo, target_repo):

    report = []

    unversioned_files = None
    v, r = collect_git_patch.collect_git_patch_unversioned(source_repo, temp_path)
    if not v:
        return False, "Failed collecting unversioned from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    unversioned_files = r
    combined_base = path_utils.concat_path(temp_path, source_repo, "unversioned")

    uvf_param = []
    for uf in unversioned_files:
        uvf_param.append( (combined_base, uf) )

    v, r = apply_git_patch.apply_git_patch_unversioned(target_repo, uvf_param)
    if not v:
        return False, "Failed porting unversioned from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo(source_repo, target_repo, head, staged, stash, unversioned, previous):

    v, r = mvtools_envvars.mvtools_envvar_read_temp_path()
    if not v:
        return False, [r]
    temp_path = r
    if not os.path.exists(temp_path):
        return False, ["Can't apply patches. MVTOOLS_TEMP_PATH envvar is not defined or the path does not exist."]

    temp_path_patches = path_utils.concat_path(temp_path, "temp_path_patches")
    if os.path.exists(temp_path_patches):
        return False, ["Can't apply patches. Temporary path [%s] already exists." % temp_path_patches]
    os.mkdir(temp_path_patches)

    v, r = _port_git_repo_delegate(temp_path_patches, source_repo, target_repo, head, staged, stash, unversioned, previous)
    shutil.rmtree(temp_path_patches)
    return v, r

def _port_git_repo_delegate(temp_path, source_repo, target_repo, head, staged, stash, unversioned, previous):

    source_repo = path_utils.filter_remove_trailing_sep(source_repo)
    source_repo = os.path.abspath(source_repo)
    target_repo = path_utils.filter_remove_trailing_sep(target_repo)
    target_repo = os.path.abspath(target_repo)

    if not os.path.exists(source_repo):
        return False, ["Source repository %s does not exist" % source_repo]

    if not os.path.exists(target_repo):
        return False, ["Target repository %s does not exist" % target_repo]<commit_msg>port_git_repo: added timestamp to generated temporary path<commit_after>#!/usr/bin/env python3

import sys
import os
import shutil

import mvtools_envvars
import maketimestamp
import path_utils
import detect_repo_type
import collect_git_patch
import apply_git_patch

def _test_repo_path(path):

    v, r = detect_repo_type.detect_repo_type(path)
    if not v:
        return False, r
    if r is None:
        return False, "Path [%s] does not point to a supported repository." % path
    return True, None

def port_git_repo_stash(temp_path, source_repo, target_repo):

    report = []

    stash_files = None
    v, r = collect_git_patch.collect_git_patch_stash(source_repo, temp_path)
    if not v:
        return False, "Failed collecting stash from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    stash_files = reversed(r)

    v, r = apply_git_patch.apply_git_patch_stash(target_repo, stash_files)
    if not v:
        return False, "Failed patching stash from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_previous(temp_path, source_repo, target_repo, previous_count):

    report = []

    previous_files = None
    v, r = collect_git_patch.collect_git_patch_previous(source_repo, temp_path, previous_count)
    if not v:
        return False, "Failed porting previous (during collect-previous) from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    previous_files = reversed(r)

    # previous commits will be stacked up ontop of head - no autocommitting is available (on purpose)
    v, r = apply_git_patch.apply_git_patch_head(target_repo, previous_files)
    if not v:
        return False, "Failed porting previous (during head-apply) from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_staged(temp_path, source_repo, target_repo):

    report = []

    staged_files = None
    v, r = collect_git_patch.collect_git_patch_staged(source_repo, temp_path)
    if not v:
        return False, "Failed collecting the staging area from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    staged_files = [r]

    v, r = apply_git_patch.apply_git_patch_staged(target_repo, staged_files)
    if not v:
        return False, "Failed patching the staging area from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_head(temp_path, source_repo, target_repo):

    report = []

    head_files = None
    v, r = collect_git_patch.collect_git_patch_head(source_repo, temp_path)
    if not v:
        if r == collect_git_patch.ERRMSG_EMPTY:
            return True, [] # ignore if target head is unmodified
        return False, "Failed collecting head from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    head_files = [r]

    v, r = apply_git_patch.apply_git_patch_head(target_repo, head_files)
    if not v:
        return False, "Failed patching head from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo_unversioned(temp_path, source_repo, target_repo):

    report = []

    unversioned_files = None
    v, r = collect_git_patch.collect_git_patch_unversioned(source_repo, temp_path)
    if not v:
        return False, "Failed collecting unversioned from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)
    unversioned_files = r
    combined_base = path_utils.concat_path(temp_path, source_repo, "unversioned")

    uvf_param = []
    for uf in unversioned_files:
        uvf_param.append( (combined_base, uf) )

    v, r = apply_git_patch.apply_git_patch_unversioned(target_repo, uvf_param)
    if not v:
        return False, "Failed porting unversioned from [%s] to [%s]: [%s]" % (source_repo, target_repo, r)

    return True, report

def port_git_repo(source_repo, target_repo, head, staged, stash, unversioned, previous):

    v, r = mvtools_envvars.mvtools_envvar_read_temp_path()
    if not v:
        return False, [r]
    temp_path = r
    if not os.path.exists(temp_path):
        return False, ["Can't apply patches. MVTOOLS_TEMP_PATH envvar is not defined or the path does not exist."]

    timestamp_now = maketimestamp.get_timestamp_now_compact()
    temp_path_patches = path_utils.concat_path(temp_path, ("temp_path_patches_%s" % timestamp_now))
    if os.path.exists(temp_path_patches):
        return False, ["Can't apply patches. Temporary path [%s] already exists." % temp_path_patches]
    os.mkdir(temp_path_patches)

    v, r = _port_git_repo_delegate(temp_path_patches, source_repo, target_repo, head, staged, stash, unversioned, previous)
    shutil.rmtree(temp_path_patches)
    return v, r

def _port_git_repo_delegate(temp_path, source_repo, target_repo, head, staged, stash, unversioned, previous):

    source_repo = path_utils.filter_remove_trailing_sep(source_repo)
    source_repo = os.path.abspath(source_repo)
    target_repo = path_utils.filter_remove_trailing_sep(target_repo)
    target_repo = os.path.abspath(target_repo)

    if not os.path.exists(source_repo):
        return False, ["Source repository %s does not exist" % source_repo]

    if not os.path.exists(target_repo):
        return False, ["Target repository %s does not exist" % target_repo]
{'fraction_non_alphanumeric': 0.09903780736709107, 'fraction_numerical': 0.0001943823500826125, 'mean_word_length': 3.959036144578313, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3668720', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3481, 'n_words': 942}