<commit_before>from qisys import ui
import qisys.interact

import qisrc.manifest

def rebase_worktree(git_worktree, git_projects, branch=None,
                    push=False, dry_run=False, verbose=False):
    if not git_projects:
        return
    upstream_projects = git_worktree.get_projects_on_branch(branch)
    rebased_projects, errors = rebase_projects(git_projects, upstream_projects, branch, verbose)
    if errors:
        raise Exception("Failed to rebase some projects")

    if push:
        push_projects(rebased_projects, dry_run=dry_run)


def push_projects(git_projects, dry_run=False):
    if not git_projects:
        return
    ui.info(ui.green, "Pushing ", len(git_projects), "projects")
    for i, git_project in enumerate(git_projects):
        default_branch = git_project.default_branch.name
        remote_branch = git_project.default_branch.remote_branch
        ui.info_count(i, len(git_projects), git_project.src)
        git = qisrc.git.Git(git_project.path)
        if git_project.review:
            push_remote = git_project.review_remote
        else:
            push_remote = git_project.default_remote
        remote_ref = "%s/%s" % (push_remote.name, remote_branch)
        display_changes(git, default_branch, remote_ref)
        answer = qisys.interact.ask_yes_no("OK to push?", default=False)
        if not answer:
            return
        to_push = "%s:%s" % (default_branch, remote_branch)
        push_args = [push_remote.name, to_push]
        push_args.append("--force")
        if dry_run:
            push_args.append("--dry-run")
        rc, out = git.push(*push_args, raises=False)
        if rc == 0:
            ui.info(out)
        else:
            ui.error(out)


def rebase_projects(git_projects, upstream_projects, branch, verbose):
    rebased_projects = list()
    errors = list()
    max_src = max(len(x.src) for x in git_projects)
    fetch_quiet = False if verbose else True
    for i, git_project in enumerate(git_projects):
        ui.info_count(i, len(git_projects), git_project.src, end="\0")
        git = qisrc.git.Git(git_project.path)
        git.fetch(quiet=fetch_quiet)
        if not git_project.default_remote:
            ui.info("\n", ui.brown, git_project.src, "[skipped]")
            ui.info("No default remote", "\n")
            continue
        if not git_project.default_branch:
            ui.info("\n", ui.brown, git_project.src, "[skipped]")
            ui.info("No default branch", "\n")
            continue<commit_msg>python: qisrc.rebase: fix output bug in rebase_project function<commit_after>from qisys import ui
import qisys.interact

import qisrc.manifest

def rebase_worktree(git_worktree, git_projects, branch=None,
                    push=False, dry_run=False, verbose=False):
    if not git_projects:
        return
    upstream_projects = git_worktree.get_projects_on_branch(branch)
    rebased_projects, errors = rebase_projects(git_projects, upstream_projects, branch, verbose)
    if errors:
        raise Exception("Failed to rebase some projects")

    if push:
        push_projects(rebased_projects, dry_run=dry_run)


def push_projects(git_projects, dry_run=False):
    if not git_projects:
        return
    ui.info(ui.green, "Pushing ", len(git_projects), "projects")
    for i, git_project in enumerate(git_projects):
        default_branch = git_project.default_branch.name
        remote_branch = git_project.default_branch.remote_branch
        ui.info_count(i, len(git_projects), git_project.src)
        git = qisrc.git.Git(git_project.path)
        if git_project.review:
            push_remote = git_project.review_remote
        else:
            push_remote = git_project.default_remote
        remote_ref = "%s/%s" % (push_remote.name, remote_branch)
        display_changes(git, default_branch, remote_ref)
        answer = qisys.interact.ask_yes_no("OK to push?", default=False)
        if not answer:
            return
        to_push = "%s:%s" % (default_branch, remote_branch)
        push_args = [push_remote.name, to_push]
        push_args.append("--force")
        if dry_run:
            push_args.append("--dry-run")
        rc, out = git.push(*push_args, raises=False)
        if rc == 0:
            ui.info(out)
        else:
            ui.error(out)


def rebase_projects(git_projects, upstream_projects, branch, verbose):
    rebased_projects = list()
    errors = list()
    max_src = max(len(x.src) for x in git_projects)
    eol = "\n" if verbose else ""
    fetch_quiet = False if verbose else True
    for i, git_project in enumerate(git_projects):
        ui.info_count(i, len(git_projects), git_project.src, end=eol)
        git = qisrc.git.Git(git_project.path)
        git.fetch(quiet=fetch_quiet)
        if not git_project.default_remote:
            ui.info("\n", ui.brown, git_project.src, "[skipped]")
            ui.info("No default remote", "\n")
            continue
        if not git_project.default_branch:
            ui.info("\n", ui.brown, git_project.src, "[skipped]")
            ui.info("No default branch", "\n")
            continue
{'fraction_non_alphanumeric': 0.10381978452497552, 'fraction_numerical': 0.0005876591576885406, 'mean_word_length': 2.7544117647058823, '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': '6575507', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1574, 'n_words': 341}