Exemplo n.º 1
0
def rebuild_staging(config):
    all_configs = list(config.span_configs())
    context_manager = contextlib.nested(
        *[OriginalBranch(get_git(path)) for path, _ in all_configs])
    with context_manager:
        for path, config in all_configs:
            git = get_git(path)
            git.checkout('-B', config.name, origin(config.trunk), '--no-track')
            for branch in config.branches:
                if not has_local(git, branch):
                    branch = origin(branch)
                print "  [{cwd}] Merging {branch} into {name}".format(
                    cwd=path, branch=branch, name=config.name)
                git.merge(branch, '--no-edit')
            if config.submodules:
                for submodule in config.submodules:
                    git.add(submodule)
                git.commit('-m', "update submodule refs", '--no-edit',
                           '--allow-empty')
            # stupid safety check
            assert config.name != 'master'
            print "  [{cwd}] Force pushing to origin {name}".format(
                cwd=path,
                name=config.name,
            )
            force_push(git, config.name)
Exemplo n.º 2
0
def check_merges(config, print_details=True):
    merge_conflicts = []
    not_found = []
    base_config = config
    for path, config in base_config.span_configs():
        git = get_git(path)
        with OriginalBranch(git):
            trunk = origin(config.trunk)
            git.checkout('-B', config.name, trunk, '--no-track')
            for branch in config.branches:
                if not has_local(git, branch):
                    branch = origin(branch)
                print "  [{cwd}] {trunk} => {branch}".format(
                    cwd=format_cwd(path),
                    trunk=trunk,
                    branch=branch,
                ),
                try:
                    git.checkout(branch)
                except sh.ErrorReturnCode_1 as e:
                    assert ("error: pathspec '%s' did not "
                            "match any file(s) known to git." %
                            branch) in e.stderr, e.stderr
                    not_found.append((path, branch))
                    print "NOT FOUND"
                    continue
                if not git_check_merge(config.name, branch, git=git):
                    merge_conflicts.append(
                        (path, origin(config.trunk), branch))
                    print "FAIL"
                else:
                    print "ok"
    if not_found:
        print "You must remove the following branches before rebuilding:"
        for cwd, branch in not_found:
            print "  [{cwd}] {branch}".format(
                cwd=format_cwd(cwd),
                branch=branch,
            )
    if merge_conflicts:
        print "You must fix the following merge conflicts before rebuilding:"
        for cwd, trunk, branch in merge_conflicts:
            print "  [{cwd}] {trunk} => {branch}".format(
                cwd=format_cwd(cwd),
                branch=branch,
                trunk=trunk,
            )
            git = get_git(cwd)
            if print_details:
                print_merge_details(branch, trunk, git)

    if merge_conflicts or not_found:
        exit(1)
    else:
        print "No merge conflicts"
 def _make_full_config(path):
     path_prefix = '{}/'.format(path) if path else ''
     git = get_git(path)
     with OriginalBranch(git):
         branches = get_unmerged_remote_branches(git)
         config = BranchConfig(
             branches=branches,
             submodules={
                 submodule: _make_full_config(path_prefix + submodule)
                 for submodule in git_submodules(git)
             })
         return config
Exemplo n.º 4
0
def sync_local_copies(config, push=True):
    base_config = config
    unpushed_branches = []

    def _count_commits(compare_spec):
        return int(sh.wc(git.log(compare_spec, '--oneline', _piped=True),
                         '-l'))

    for path, config in base_config.span_configs():
        git = get_git(path)
        with OriginalBranch(git):
            for branch in [config.trunk] + config.branches:
                if ":" in branch or not has_local(git, branch):
                    continue
                git.checkout(branch)
                unpushed = _count_commits('origin/{0}..{0}'.format(branch))
                unpulled = _count_commits('{0}..origin/{0}'.format(branch))
                if unpulled or unpushed:
                    print(
                        "  [{cwd}] {branch}: {unpushed} ahead "
                        "and {unpulled} behind origin").format(
                            cwd=path,
                            branch=branch,
                            unpushed=unpushed,
                            unpulled=unpulled,
                        )
                else:
                    print "  [{cwd}] {branch}: Everything up-to-date.".format(
                        cwd=path,
                        branch=branch,
                    )
                if unpushed:
                    unpushed_branches.append((path, branch))
                elif unpulled:
                    print "  Fastforwarding your branch to origin"
                    git.merge('--ff-only', origin(branch))
    if unpushed_branches and push:
        print "The following branches have commits that need to be pushed:"
        for path, branch in unpushed_branches:
            print "  [{cwd}] {branch}".format(cwd=path, branch=branch)
        exit(1)
    else:
        print "All branches up-to-date."
def rebuild_staging(config, print_details=True, push=True):
    merge_conflicts = []
    not_found = []
    all_configs = list(config.span_configs())
    with ExitStack() as stack:
        for path, _ in all_configs:
            stack.enter_context(OriginalBranch(get_git(path)))
        for path, config in all_configs:
            git = get_git(path)
            try:
                git.checkout('-B', config.name, origin(config.trunk),
                             '--no-track')
            except Exception:
                git.checkout('-B', config.name, config.trunk, '--no-track')
            for branch in config.branches:
                remote = ":" in branch
                if remote or not has_local(git, branch):
                    if remote:
                        remote_branch = branch.replace(":", "/", 1)
                    else:
                        remote_branch = origin(branch)
                    if not has_remote(git, remote_branch):
                        not_found.append((path, branch))
                        print("  [{cwd}] {branch} NOT FOUND".format(
                            cwd=format_cwd(path),
                            branch=branch,
                        ))
                        continue
                    branch = remote_branch
                print("  [{cwd}] Merging {branch} into {name}".format(
                    cwd=path, branch=branch, name=config.name),
                      end=' ')
                try:
                    git.merge(branch, '--no-edit')
                except sh.ErrorReturnCode_1:
                    merge_conflicts.append((path, branch, config))
                    try:
                        git.merge("--abort")
                    except sh.ErrorReturnCode_128:
                        pass
                    print("FAIL")
                else:
                    print("ok")
            for pr in config.pull_requests:
                branch = "enterprise-{pr}".format(pr=pr)
                print("  [{cwd}] Merging {pr} into {name}".format(
                    cwd=path, pr=pr, name=config.name),
                      end=' ')
                try:
                    git.merge(branch, '--no-edit')
                except sh.ErrorReturnCode_1:
                    merge_conflicts.append((path, branch, config))
                    try:
                        git.merge("--abort")
                    except sh.ErrorReturnCode_128:
                        pass
                    print("FAIL")
                else:
                    print("ok")
            if config.submodules:
                for submodule in config.submodules:
                    git.add(submodule)
                git.commit('-m', "update submodule refs", '--no-edit',
                           '--allow-empty')
        if push and not (merge_conflicts or not_found):
            for path, config in all_configs:
                # stupid safety check
                assert config.name != 'master', path
                print("  [{cwd}] Force pushing to origin {name}".format(
                    cwd=path,
                    name=config.name,
                ))
                force_push(get_git(path), config.name)

    if not_found:
        print("You must remove the following branches before rebuilding:")
        for cwd, branch in not_found:
            print("  [{cwd}] {branch}".format(
                cwd=format_cwd(cwd),
                branch=branch,
            ))
    if merge_conflicts:
        print("You must fix the following merge conflicts before rebuilding:")
        for cwd, branch, config in merge_conflicts:
            print("\n[{cwd}] {branch} => {name}".format(
                cwd=format_cwd(cwd),
                branch=branch,
                name=config.name,
            ))
            git = get_git(cwd)
            if print_details:
                print_conflicts(branch, config, git)

    if merge_conflicts or not_found:
        exit(1)
Exemplo n.º 6
0
def rebuild_staging(config, print_details=True, push=True):
    merge_conflicts = []
    not_found = []
    all_configs = list(config.span_configs())
    context_manager = contextlib.nested(
        *[OriginalBranch(get_git(path)) for path, _ in all_configs])
    with context_manager:
        for path, config in all_configs:
            git = get_git(path)
            git.checkout('-B', config.name, origin(config.trunk), '--no-track')
            for branch in config.branches:
                remote = ":" in branch
                if remote or not has_local(git, branch):
                    if remote:
                        remote_branch = branch.replace(":", "/", 1)
                    else:
                        remote_branch = origin(branch)
                    if not has_remote(git, remote_branch):
                        not_found.append((path, branch))
                        print "  [{cwd}] {branch} NOT FOUND".format(
                            cwd=format_cwd(path),
                            branch=branch,
                        )
                        continue
                    branch = remote_branch
                print "  [{cwd}] Merging {branch} into {name}".format(
                    cwd=path, branch=branch, name=config.name),
                try:
                    git.merge(branch, '--no-edit')
                except sh.ErrorReturnCode_1:
                    merge_conflicts.append((path, branch, config.name))
                    try:
                        git.merge("--abort")
                    except sh.ErrorReturnCode_128:
                        pass
                    print "FAIL"
                else:
                    print "ok"
            if config.submodules:
                for submodule in config.submodules:
                    git.add(submodule)
                git.commit('-m', "update submodule refs", '--no-edit',
                           '--allow-empty')
            # stupid safety check
            assert config.name != 'master'
            if push:
                print "  [{cwd}] Force pushing to origin {name}".format(
                    cwd=path,
                    name=config.name,
                )
                force_push(git, config.name)

    if not_found:
        print "You must remove the following branches before rebuilding:"
        for cwd, branch in not_found:
            print "  [{cwd}] {branch}".format(
                cwd=format_cwd(cwd),
                branch=branch,
            )
    if merge_conflicts:
        print "You must fix the following merge conflicts before rebuilding:"
        for cwd, branch, name in merge_conflicts:
            print "  [{cwd}] {branch} => {name}".format(
                cwd=format_cwd(cwd),
                branch=branch,
                name=name,
            )
            git = get_git(cwd)
            if print_details:
                print_merge_details(branch, name, git)

    if merge_conflicts or not_found:
        exit(1)