Exemplo n.º 1
0
def upconvert_bloom_to_config_branch():
    global _has_checked_bloom_branch
    if _has_checked_bloom_branch:
        return
    # Assert that this repository does not have multiple remotes
    check_for_multiple_remotes()
    if get_root() is None:
        # Not a git repository
        return
    track_branches(['bloom', BLOOM_CONFIG_BRANCH])
    if show('bloom', PLACEHOLDER_FILE) is not None:
        return
    if show('bloom', 'bloom.conf') is not None:
        # Wait for the bloom.conf upconvert...
        return
    if not branch_exists('bloom'):
        return
    _has_checked_bloom_branch = True
    info("Moving configurations from deprecated 'bloom' branch "
         "to the '{0}' branch.".format(BLOOM_CONFIG_BRANCH))
    tmp_dir = mkdtemp()
    git_root = get_root()
    try:
        # Copy the new upstream source into the temporary directory
        with inbranch('bloom'):
            ignores = ('.git', '.gitignore', '.svn', '.hgignore', '.hg', 'CVS')
            configs = os.path.join(tmp_dir, 'configs')
            my_copytree(git_root, configs, ignores)
            if [x for x in os.listdir(os.getcwd()) if x not in ignores]:
                execute_command('git rm -rf ./*')
            with open(PLACEHOLDER_FILE, 'w') as f:
                f.write("""\
This branch ('bloom') has been deprecated in favor of storing settings and overlay files in the master branch.

Please goto the master branch for anything which referenced the bloom branch.

You can delete this branch at your convenience.
""")
            execute_command('git add ' + PLACEHOLDER_FILE)
            if has_changes():
                execute_command('git commit -m "DEPRECATING BRANCH"')
        if not branch_exists(BLOOM_CONFIG_BRANCH):
            info("Creating '{0}' branch.".format(BLOOM_CONFIG_BRANCH))
            create_branch(BLOOM_CONFIG_BRANCH, orphaned=True)
        with inbranch(BLOOM_CONFIG_BRANCH):
            my_copytree(configs, git_root)
            execute_command('git add ./*')
            if has_changes():
                execute_command('git commit -m '
                                '"Moving configs from bloom branch"')
    finally:
        # Clean up
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
Exemplo n.º 2
0
def upconvert_bloom_to_config_branch():
    global _has_checked_bloom_branch
    if _has_checked_bloom_branch:
        return
    # Assert that this repository does not have multiple remotes
    check_for_multiple_remotes()
    if get_root() is None:
        # Not a git repository
        return
    track_branches(['bloom', BLOOM_CONFIG_BRANCH])
    if show('bloom', PLACEHOLDER_FILE) is not None:
        return
    if show('bloom', 'bloom.conf') is not None:
        # Wait for the bloom.conf upconvert...
        return
    if not branch_exists('bloom'):
        return
    _has_checked_bloom_branch = True
    info("Moving configurations from deprecated 'bloom' branch "
         "to the '{0}' branch.".format(BLOOM_CONFIG_BRANCH))
    tmp_dir = mkdtemp()
    git_root = get_root()
    try:
        # Copy the new upstream source into the temporary directory
        with inbranch('bloom'):
            ignores = ('.git', '.gitignore', '.svn', '.hgignore', '.hg', 'CVS')
            configs = os.path.join(tmp_dir, 'configs')
            my_copytree(git_root, configs, ignores)
            if [x for x in os.listdir(os.getcwd()) if x not in ignores]:
                execute_command('git rm -rf ./*')
            with open(PLACEHOLDER_FILE, 'w') as f:
                f.write("""\
This branch ('bloom') has been deprecated in favor of storing settings and overlay files in the master branch.

Please goto the master branch for anything which referenced the bloom branch.

You can delete this branch at your convenience.
""")
            execute_command('git add ' + PLACEHOLDER_FILE)
            if has_changes():
                execute_command('git commit -m "DEPRECATING BRANCH"')
        if not branch_exists(BLOOM_CONFIG_BRANCH):
            info("Creating '{0}' branch.".format(BLOOM_CONFIG_BRANCH))
            create_branch(BLOOM_CONFIG_BRANCH, orphaned=True)
        with inbranch(BLOOM_CONFIG_BRANCH):
            my_copytree(configs, git_root)
            execute_command('git add ./*')
            if has_changes():
                execute_command('git commit -m '
                                '"Moving configs from bloom branch"')
    finally:
        # Clean up
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
Exemplo n.º 3
0
def _set_trim_sub_dir(sub_dir, force, config, directory):
    debug("_set_trim_sub_dir(" + str(sub_dir) + ", " + str(force) + ", " + \
          str(config) + ", " + str(directory) + ")")
    if sub_dir is not None:
        if config['trim'] != '' and config['trim'] != sub_dir:
            warning("You are trying to set the trim sub directory to " + \
                    sub_dir + ", but it is already set to " + \
                    config['trim'] + ".")
            if not force:
                warning("Changing the sud directory is not advised. "
                        "If you are sure you want to do this, use "
                        "'--force'")
                return None
            else:
                warning("Forcing the change of the sub directory.")
        # Make the sub_dir absolute
        git_root = get_root(directory)
        sub_dir_abs = os.path.join(git_root, sub_dir)
        # Make sure it is a directory
        if not os.path.isdir(sub_dir_abs):
            error("The given sub directory, (" + sub_dir + ") does not "
                  "exist in the git repository at " + git_root)
            return None
        # Set the trim sub directory
        config['trim'] = sub_dir
    return config
Exemplo n.º 4
0
def commit_summary():
    global _summary_file
    if get_root() is None:
        return
    if _summary_file is None:
        return
    if not os.path.exists(_summary_file.name):
        return
    try:
        with inbranch('master'):
            readme_name = 'README.md'
            readme = ''
            if os.path.isfile(readme_name):
                with open(readme_name, 'r') as f:
                    readme = f.read()
            _summary_file.close()
            with open(_summary_file.name, 'r') as f:
                readme = f.read() + "\n\n" + readme
            with open(readme_name, 'w') as f:
                f.write(readme)
            execute_command('git add ' + readme_name)
            if has_changes():
                execute_command('git commit -m "Updating README.md"')
    finally:
        if _summary_file is not None:
            _summary_file.close()
            if os.path.exists(_summary_file.name):
                os.remove(_summary_file.name)
Exemplo n.º 5
0
def _set_trim_sub_dir(sub_dir, force, config, directory):
    debug("_set_trim_sub_dir(" + str(sub_dir) + ", " + str(force) + ", " +
          str(config) + ", " + str(directory) + ")")
    if sub_dir is not None:
        if config['trim'] != '' and config['trim'] != sub_dir:
            warning("You are trying to set the trim sub directory to " +
                    sub_dir + ", but it is already set to " + config['trim'] +
                    ".")
            if not force:
                warning("Changing the sud directory is not advised. "
                        "If you are sure you want to do this, use "
                        "'--force'")
                return None
            else:
                warning("Forcing the change of the sub directory.")
        # Make the sub_dir absolute
        git_root = get_root(directory)
        sub_dir_abs = os.path.join(git_root, sub_dir)
        # Make sure it is a directory
        if not os.path.isdir(sub_dir_abs):
            error("The given sub directory, (" + sub_dir + ") does not "
                  "exist in the git repository at " + git_root)
            return None
        # Set the trim sub directory
        config['trim'] = sub_dir
    return config
Exemplo n.º 6
0
def non_git_rebase(upstream_branch, directory=None):
    # Create a temporary storage directory
    tmp_dir = mkdtemp()
    # Get the root of the git repository
    git_root = get_root(directory)
    try:
        # Copy the new upstream source into the temporary directory
        with inbranch(upstream_branch):
            ignores = (".git", ".gitignore", ".svn", ".hgignore", ".hg", "CVS")
            parent_source = os.path.join(tmp_dir, "parent_source")
            my_copytree(git_root, parent_source, ignores)

        # Clear out the local branch
        execute_command("git rm -rf *", cwd=directory)
        # Collect .* files (excluding .git)
        dot_items = []
        for item in os.listdir(git_root):
            if item in [".git", "..", "."]:
                continue
            if item.startswith("."):
                dot_items.append(item)
        # Remove and .* files missed by 'git rm -rf *'
        if len(dot_items) > 0:
            execute_command("git rm -rf " + " ".join(dot_items), cwd=directory)
        # Clear out any untracked files
        execute_command("git clean -fdx", cwd=directory)  # for good measure?

        # Copy the parent source into the newly cleaned directory
        my_copytree(parent_source, git_root)

        # Commit changes to the repository
        execute_command("git add ./*", cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in [".git", "..", "."]:
                continue
            if item.startswith("."):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command("git add " + " ".join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command("git clean -dXf", cwd=directory)
        # Only if we have local changes commit
        # (not true if the upstream didn't change any files)
        cmd = "git commit "
        if not has_changes(directory):
            cmd += "--allow-empty "
        cmd += "-m \"Rebase from '" + upstream_branch + "'"
        if not has_changes(directory):
            cmd += " (no changes)"
        cmd += '"'
        execute_command(cmd, cwd=directory)
    finally:
        # Clean up
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
Exemplo n.º 7
0
def non_git_rebase(upstream_branch, directory=None):
    # Create a temporary storage directory
    tmp_dir = mkdtemp()
    # Get the root of the git repository
    git_root = get_root(directory)
    try:
        # Copy the new upstream source into the temporary directory
        with inbranch(upstream_branch):
            ignores = ('.git', '.gitignore', '.svn', '.hgignore', '.hg', 'CVS')
            parent_source = os.path.join(tmp_dir, 'parent_source')
            my_copytree(git_root, parent_source, ignores)

        # Clear out the local branch
        execute_command('git rm -rf *', cwd=directory)
        # Collect .* files (excluding .git)
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Remove and .* files missed by 'git rm -rf *'
        if len(dot_items) > 0:
            execute_command('git rm -rf ' + ' '.join(dot_items), cwd=directory)
        # Clear out any untracked files
        execute_command('git clean -fdx', cwd=directory)  # for good measure?

        # Copy the parent source into the newly cleaned directory
        my_copytree(parent_source, git_root)

        # Commit changes to the repository
        execute_command('git add ./*', cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command('git add ' + ' '.join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command('git clean -dXf', cwd=directory)
        # Only if we have local changes commit
        # (not true if the upstream didn't change any files)
        cmd = 'git commit '
        if not has_changes(directory):
            cmd += '--allow-empty '
        cmd += '-m "Rebase from \'' + upstream_branch + "'"
        if not has_changes(directory):
            cmd += " (no changes)"
        cmd += '"'
        execute_command(cmd, cwd=directory)
    finally:
        # Clean up
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
Exemplo n.º 8
0
def non_git_rebase(upstream_branch, directory=None):
    # Create a temporary storage directory
    tmp_dir = mkdtemp()
    # Get the root of the git repository
    git_root = get_root(directory)
    try:
        # Copy the new upstream source into the temporary directory
        with inbranch(upstream_branch):
            ignores = ('.git', '.gitignore', '.svn', '.hgignore', '.hg', 'CVS')
            parent_source = os.path.join(tmp_dir, 'parent_source')
            my_copytree(git_root, parent_source, ignores)
        # Clear out any untracked files
        execute_command('git clean -fdx', cwd=directory)  # for good measure?
        # Collect files (excluding .git)
        items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            items.append(item)
        # Remove all files
        if len(items) > 0:
            execute_command('git rm -rf ' + ' '.join(items), cwd=directory)

        # Copy the parent source into the newly cleaned directory
        my_copytree(parent_source, git_root)

        # Commit changes to the repository
        execute_command('git add ./*', cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command('git add ' + ' '.join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command('git clean -dXf', cwd=directory)
        # Only if we have local changes commit
        # (not true if the upstream didn't change any files)
        cmd = 'git commit '
        if not has_changes(directory):
            cmd += '--allow-empty '
        cmd += '-m "Rebase from \'' + upstream_branch + "'"
        if not has_changes(directory):
            cmd += " (no changes)"
        cmd += '"'
        execute_command(cmd, cwd=directory)
    finally:
        # Clean up
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
Exemplo n.º 9
0
def check_for_multiple_remotes():
    if get_root() is None:
        return
    remotes = get_remotes()
    if len(remotes) < 0:
        error("Current git repository has no remotes. "
              "If you are running bloom-release, please change directories.",
              exit=True)
    if len(remotes) > 1:
        error("Current git repository has multiple remotes. "
              "If you are running bloom-release, please change directories.",
              exit=True)
Exemplo n.º 10
0
def check_for_multiple_remotes():
    if get_root() is None:
        return
    remotes = get_remotes()
    if len(remotes) < 0:
        error("Current git repository has no remotes. "
              "If you are running bloom-release, please change directories.",
              exit=True)
    if len(remotes) > 1:
        error("Current git repository has multiple remotes. "
              "If you are running bloom-release, please change directories.",
              exit=True)
Exemplo n.º 11
0
def export_upstream(uri, tag, vcs_type, output_dir, show_uri, name):
    tag = tag if tag != ":{none}" else None
    output_dir = output_dir or os.getcwd()
    if uri.startswith("git@"):
        uri_is_path = False
    else:
        uri_parsed = urlparse(uri)
        uri = uri if uri_parsed.scheme else uri_parsed.path
        uri_is_path = False if uri_parsed.scheme else True
    name = name or "upstream"
    with temporary_directory() as tmp_dir:
        info(
            "Checking out repository at '{0}'".format(show_uri or uri)
            + (" to reference '{0}'.".format(tag) if tag else ".")
        )
        if uri_is_path:
            upstream_repo = get_vcs_client(vcs_type, uri)
        else:
            repo_path = os.path.join(tmp_dir, "upstream")
            upstream_repo = get_vcs_client(vcs_type, repo_path)
            if not upstream_repo.checkout(uri, tag or ""):
                error(
                    "Failed to clone repository at '{0}'".format(uri)
                    + (" to reference '{0}'.".format(tag) if tag else "."),
                    exit=True,
                )
        if get_root() is not None and has_submodules(upstream_repo.get_path()):
            error(
                """\
bloom does not support exporting git repositories with submodules, see:

- https://github.com/ros-infrastructure/bloom/issues/202
- https://github.com/ros-infrastructure/bloom/issues/217
- https://github.com/vcstools/vcstools/issues/84
""",
                exit=True,
            )
        tarball_prefix = "{0}-{1}".format(name, tag) if tag else name
        tarball_path = os.path.join(output_dir, tarball_prefix)
        full_tarball_path = tarball_path + ".tar.gz"
        info("Exporting to archive: '{0}'".format(full_tarball_path))
        if not upstream_repo.export_repository(tag or "", tarball_path):
            error("Failed to create archive of upstream repository at '{0}'".format(show_uri))
            if tag and vcs_type == "git":  # can only check for git repos
                with change_directory(upstream_repo.get_path()):
                    if not tag_exists(tag):
                        warning("'{0}' is not a tag in the upstream repository...".format(tag))
                    if not branch_exists(tag):
                        warning("'{0}' is not a branch in the upstream repository...".format(tag))
        if not os.path.exists(full_tarball_path):
            error("Tarball was not created.", exit=True)
        info("md5: {0}".format(calculate_file_md5(full_tarball_path)))
Exemplo n.º 12
0
def check_git_init():
    if get_root() is None:
        error("Not in a valid git repository", exit=True)
    cmd = 'git show-ref --heads'
    result = execute_command(cmd, autofail=False,
                             silent_error=True)
    if result != 0:
        info("Freshly initialized git repository detected.")
        info("An initial empty commit is going to be made.")
        if not maybe_continue():
            error("Answered no to continue, exiting.", exit=True)
        # Make an initial empty commit
        execute_command('git commit --allow-empty -m "Initial commit"', silent=True)
Exemplo n.º 13
0
def check_git_init():
    if get_root() is None:
        error("Not in a valid git repository", exit=True)
    cmd = 'git show-ref --heads'
    result = execute_command(cmd, autofail=False, silent_error=True)
    if result != 0:
        info("Freshly initialized git repository detected.")
        info("An initial empty commit is going to be made.")
        if not maybe_continue():
            error("Answered no to continue, exiting.", exit=True)
        # Make an initial empty commit
        execute_command('git commit --allow-empty -m "Initial commit"',
                        silent=True)
Exemplo n.º 14
0
def get_upstream_meta(upstream_dir, ros_distro):
    meta = None
    directory = os.getcwd()
    with change_directory(upstream_dir):
        if get_root() is not None:  # If in a git repo
            current_branch = get_current_branch()
        else:
            current_branch = None
        name, version, packages = get_package_data(current_branch,
                                                   quiet=False,
                                                   release_directory=directory)
    meta = {'name': name, 'version': version, 'type': 'package.xml'}
    return meta
Exemplo n.º 15
0
def export_upstream(uri, tag, vcs_type, output_dir, show_uri, name):
    tag = tag if tag != ':{none}' else None
    output_dir = output_dir or os.getcwd()
    if uri.startswith('git@'):
        uri_is_path = False
    else:
        uri_parsed = urlparse(uri)
        uri = uri if uri_parsed.scheme else uri_parsed.path
        uri_is_path = False if uri_parsed.scheme else True
    name = name or 'upstream'
    with temporary_directory() as tmp_dir:
        info("Checking out repository at '{0}'".format(show_uri or uri) +
             (" to reference '{0}'.".format(tag) if tag else '.'))
        if uri_is_path:
            upstream_repo = get_vcs_client(vcs_type, uri)
        else:
            repo_path = os.path.join(tmp_dir, 'upstream')
            upstream_repo = get_vcs_client(vcs_type, repo_path)
            if not upstream_repo.checkout(uri, tag or ''):
                error("Failed to clone repository at '{0}'".format(uri) +
                      (" to reference '{0}'.".format(tag) if tag else '.'),
                      exit=True)
        if get_root() is not None and has_submodules(upstream_repo.get_path()):
            error("""\
bloom does not support exporting git repositories with submodules, see:

- https://github.com/ros-infrastructure/bloom/issues/202
- https://github.com/ros-infrastructure/bloom/issues/217
- https://github.com/vcstools/vcstools/issues/84
""",
                  exit=True)
        tarball_prefix = '{0}-{1}'.format(name, tag) if tag else name
        tarball_path = os.path.join(output_dir, tarball_prefix)
        full_tarball_path = tarball_path + '.tar.gz'
        info("Exporting to archive: '{0}'".format(full_tarball_path))
        if not upstream_repo.export_repository(tag or '', tarball_path):
            error("Failed to create archive of upstream repository at '{0}'".
                  format(show_uri))
            if tag and vcs_type == 'git':  # can only check for git repos
                with change_directory(upstream_repo.get_path()):
                    if not tag_exists(tag):
                        warning(
                            "'{0}' is not a tag in the upstream repository...".
                            format(tag))
                    if not branch_exists(tag):
                        warning(
                            "'{0}' is not a branch in the upstream repository..."
                            .format(tag))
        if not os.path.exists(full_tarball_path):
            error("Tarball was not created.", exit=True)
        info("md5: {0}".format(calculate_file_md5(full_tarball_path)))
Exemplo n.º 16
0
def get_upstream_meta(upstream_dir, ros_distro):
    meta = None
    with change_directory(upstream_dir):
        if get_root() is not None:  # If in a git repo
            current_branch = get_current_branch()
        else:
            current_branch = None
        name, version, packages = get_package_data(current_branch, quiet=False)
    meta = {
        'name': name,
        'version': version,
        'type': 'package.xml'
    }
    return meta
Exemplo n.º 17
0
def check_git_init():
    if get_root() is None:
        error("Not is a valid git repository")
        return code.NOT_A_GIT_REPOSITORY

    cmd = 'git show-ref --heads'
    result = execute_command(cmd, shell=True, autofail=False,
                             silent_error=True)
    if result != 0:
        info("Freshly initialized git repository detected.")
        info("An initial empty commit is going to be made.")
        if not maybe_continue():
            error("Answered no to continue, exiting.")
            return 1
        # Make an initial empty commit
        execute_command('git commit -m "initial commit" --allow-empty')
    return 0
Exemplo n.º 18
0
def get_upstream_meta(upstream_dir, ros_distro):
    meta = None
    with change_directory(upstream_dir):
        if get_root() is not None:  # If in a git repo
            current_branch = get_current_branch()
        else:
            current_branch = None
        name, version, stackages = get_package_data(
            current_branch,
            quiet=False,
            fuerte=(ros_distro == 'fuerte'))
    meta = {
        'name': name,
        'version': version,
        'type': 'package.xml' if isinstance(stackages, dict) else 'stack.xml'
    }
    return meta
Exemplo n.º 19
0
def _trim(config, force, directory):
    debug("_trim(" + str(config) + ", " + str(force) + ", " + \
          str(directory) + ")")
    if config['trimbase'] != '':
        warning("It looks like the trim operation has already been done, "
                "nested trimming is not supported.")
        if force:
            warning("Proceeding anyways because of '--force'")
        else:
            warning("If you would like to continue anyways use '--force'")
            return None
    config['trimbase'] = get_commit_hash(get_current_branch(directory))
    tmp_dir = tempfile.mkdtemp()
    try:
        # Buckup trim sub directory
        git_root = get_root()
        sub_dir = os.path.join(git_root, config['trim'])
        storage = os.path.join(tmp_dir, config['trim'])
        shutil.copytree(sub_dir, storage)
        # Clear out the git repo
        execute_command('git rm -rf ./*', cwd=directory)
        # Copy the sub directory back
        for item in os.listdir(storage):
            src = os.path.join(storage, item)
            dst = os.path.join(git_root, item)
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            else:
                shutil.copy(src, dst)
        # Stage
        execute_command('git add ./*', cwd=directory)
        # Commit
        cmd = 'git commit -m "Trimmed the branch to only the ' + \
              config['trim'] + ' sub directory"'
        execute_command(cmd, cwd=directory)
        # Update the patch base to be this commit
        config['base'] = get_commit_hash(get_current_branch(directory))
    finally:
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
    return config
Exemplo n.º 20
0
def main(sysargs=None):
    parser = get_argument_parser()
    add_global_arguments(parser)
    args = parser.parse_args(sysargs)
    handle_global_arguments(args)
    retcode = "command not run"
    if get_root() is None:
        parser.print_help()
        error("This command must be run in a valid git repository.", exit=True)
    ensure_git_root()
    try:
        retcode = args.func(args) or 0
    except CalledProcessError as err:
        # Problem calling out to git probably
        print_exc(traceback.format_exc())
        error(str(err))
        retcode = 2
    except Exception as err:
        # Unhandled exception, print traceback
        print_exc(traceback.format_exc())
        error(str(err))
        retcode = 3
    sys.exit(retcode)
Exemplo n.º 21
0
def main(sysargs=None):
    parser = get_argument_parser()
    add_global_arguments(parser)
    args = parser.parse_args(sysargs)
    handle_global_arguments(args)
    retcode = "command not run"
    if get_root() is None:
        parser.print_help()
        error("This command must be run in a valid git repository.", exit=True)
    ensure_git_root()
    try:
        retcode = args.func(args) or 0
    except CalledProcessError as err:
        # Problem calling out to git probably
        print_exc(traceback.format_exc())
        error(str(err))
        retcode = 2
    except Exception as err:
        # Unhandled exception, print traceback
        print_exc(traceback.format_exc())
        error(str(err))
        retcode = 3
    sys.exit(retcode)
Exemplo n.º 22
0
def main():
    if len(sys.argv) > 1:
        command = sys.argv[1]
    else:
        error("You must specify a command, e.g. git-bloom-patch <command>")
        usage()
    if get_root() == None:

        error("This command must be run in a valid git repository.")
        usage()
    try:
        if command == "export":
            retcode = export_cmd.main()
        elif command == "import":
            retcode = import_cmd.main()
        elif command == "remove":
            retcode = remove_cmd.main()
        elif command == "rebase":
            retcode = rebase_cmd.main()
        elif command == "trim":
            retcode = trim_cmd.main()
        else:
            error("Invalid command specified: {0}".format(command))
            usage(False)
            retcode = 1
    except CalledProcessError as err:
        # Problem calling out to git probably
        print_exc(traceback.format_exc())
        error(str(err))
        retcode = 2
    except Exception as err:
        # Unhandled exception, print traceback
        print_exc(traceback.format_exc())
        error(str(err))
        retcode = 3
    sys.exit(retcode)
Exemplo n.º 23
0
def _trim(config, force, directory):
    debug("_trim(" + str(config) + ", " + str(force) + ", " + str(directory) + ")")
    if config["trimbase"] != "":
        warning("It looks like the trim operation has already been done, " "nested trimming is not supported.")
        if force:
            warning("Proceeding anyways because of '--force'")
        else:
            warning("If you would like to continue anyways use '--force'")
            return None
    current_branch = get_current_branch(directory)
    if current_branch is None:
        error("Could not determine current branch.", exit=True)
    config["trimbase"] = get_commit_hash(current_branch)
    tmp_dir = tempfile.mkdtemp()
    try:
        # Buckup trim sub directory
        git_root = get_root()
        sub_dir = os.path.join(git_root, config["trim"])
        storage = os.path.join(tmp_dir, config["trim"])
        shutil.copytree(sub_dir, storage)
        # Collect al files (excluding .git)
        items = []
        for item in os.listdir(git_root):
            if item in [".git", "..", "."]:
                continue
            items.append(item)
        # Remove and .* files missed by 'git rm -rf *'
        if len(items) > 0:
            execute_command("git rm -rf " + " ".join(items), cwd=directory)
        # Clear out any untracked files
        execute_command("git clean -fdx", cwd=directory)
        # Copy the sub directory back
        for item in os.listdir(storage):
            src = os.path.join(storage, item)
            dst = os.path.join(git_root, item)
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            else:
                shutil.copy(src, dst)
        # Stage
        execute_command("git add ./*", cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in [".git", "..", "."]:
                continue
            if item.startswith("."):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command("git add " + " ".join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command("git clean -dXf", cwd=directory)
        # Commit
        cmd = 'git commit -m "Trimmed the branch to only the ' + config["trim"] + ' sub directory"'
        execute_command(cmd, cwd=directory)
        # Update the patch base to be this commit
        current_branch = get_current_branch(directory)
        if current_branch is None:
            error("Could not determine current branch.", exit=True)
        config["base"] = get_commit_hash(current_branch)
    finally:
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
    return config
Exemplo n.º 24
0
def _trim(config, force, directory):
    debug("_trim(" + str(config) + ", " + str(force) + ", " + \
          str(directory) + ")")
    if config['trimbase'] != '':
        warning("It looks like the trim operation has already been done, "
                "nested trimming is not supported.")
        if force:
            warning("Proceeding anyways because of '--force'")
        else:
            warning("If you would like to continue anyways use '--force'")
            return None
    config['trimbase'] = get_commit_hash(get_current_branch(directory))
    tmp_dir = tempfile.mkdtemp()
    try:
        # Buckup trim sub directory
        git_root = get_root()
        sub_dir = os.path.join(git_root, config['trim'])
        storage = os.path.join(tmp_dir, config['trim'])
        shutil.copytree(sub_dir, storage)
        # Clear out the git repo
        execute_command('git rm -rf ./*', cwd=directory)
        # Collect .* files (excluding .git)
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Remove and .* files missed by 'git rm -rf *'
        if len(dot_items) > 0:
            execute_command('git rm -rf ' + ' '.join(dot_items), cwd=directory)
        # Clear out any untracked files
        execute_command('git clean -fdx', cwd=directory)
        # Copy the sub directory back
        for item in os.listdir(storage):
            src = os.path.join(storage, item)
            dst = os.path.join(git_root, item)
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            else:
                shutil.copy(src, dst)
        # Stage
        execute_command('git add ./*', cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command('git add ' + ' '.join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command('git clean -dXf', cwd=directory)
        # Commit
        cmd = 'git commit -m "Trimmed the branch to only the ' + \
              config['trim'] + ' sub directory"'
        execute_command(cmd, cwd=directory)
        # Update the patch base to be this commit
        config['base'] = get_commit_hash(get_current_branch(directory))
    finally:
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
    return config
Exemplo n.º 25
0
def non_git_rebase(upstream_branch, directory=None):
    # Create a temporary storage directory
    tmp_dir = mkdtemp()
    # Get the root of the git repository
    git_root = get_root(directory)
    try:
        # Copy the new upstream source into the temporary directory
        with inbranch(upstream_branch):
            ignores = ('.git', '.gitignore', '.svn', '.hgignore', '.hg', 'CVS')
            parent_source = os.path.join(tmp_dir, 'parent_source')
            try:
                # Try catch this to handle dangling symbolic links
                # See: http://bugs.python.org/issue6547
                shutil.copytree(git_root, parent_source,
                                ignore=shutil.ignore_patterns(*ignores))
            except shutil.Error as e:
                for src, dst, error in e.args[0]:
                    if not os.path.islink(src):
                        raise
                    else:
                        linkto = os.readlink(src)
                        if os.path.exists(linkto):
                            raise
                    # dangling symlink found.. ignoring..

        # Clear out the local branch
        execute_command('git rm -rf *', cwd=directory)
        # Collect .* files (excluding .git)
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Remove and .* files missed by 'git rm -rf *'
        if len(dot_items) > 0:
            execute_command('git rm -rf ' + ' '.join(dot_items), cwd=directory)
        # Clear out any untracked files
        execute_command('git clean -fdx', cwd=directory)  # for good measure?

        # Copy the parent source into the newly cleaned directory
        for item in os.listdir(parent_source):
            src = os.path.join(parent_source, item)
            dst = os.path.join(git_root, item)
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            else:
                shutil.copy(src, dst)

        # Commit changes to the repository
        execute_command('git add ./*', cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command('git add ' + ' '.join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command('git clean -dXf', cwd=directory)
        # Only if we have local changes commit
        # (not true if the upstream didn't change any files)
        if has_changes(directory):
            cmd = 'git commit -m "Rebase from \'' + upstream_branch + "'"
            data = get_package_data(upstream_branch, quiet=True)
            if type(data) in [list, tuple]:
                cmd += " @ version '{0}'".format(data[1])
            cmd += '"'
            execute_command(cmd, cwd=directory)
    finally:
        # Clean up
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
Exemplo n.º 26
0
def _trim(config, force, directory):
    debug("_trim(" + str(config) + ", " + str(force) + ", " + str(directory) +
          ")")
    if config['trimbase'] != '':
        warning("It looks like the trim operation has already been done, "
                "nested trimming is not supported.")
        if force:
            warning("Proceeding anyways because of '--force'")
        else:
            warning("If you would like to continue anyways use '--force'")
            return None
    current_branch = get_current_branch(directory)
    if current_branch is None:
        error("Could not determine current branch.", exit=True)
    config['trimbase'] = get_commit_hash(current_branch)
    tmp_dir = tempfile.mkdtemp()
    try:
        # Buckup trim sub directory
        git_root = get_root()
        sub_dir = os.path.join(git_root, config['trim'])
        storage = os.path.join(tmp_dir, config['trim'])
        shutil.copytree(sub_dir, storage)
        # Clear out any untracked files
        execute_command('git clean -fdx', cwd=directory)
        # Collect al files (excluding .git)
        items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            items.append(item)
        # Remove and .* files missed by 'git rm -rf *'
        if len(items) > 0:
            execute_command('git rm -rf ' + ' '.join(items), cwd=directory)
        # Copy the sub directory back
        for item in os.listdir(storage):
            src = os.path.join(storage, item)
            dst = os.path.join(git_root, item)
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            else:
                shutil.copy(src, dst)
        # Stage
        execute_command('git add ./*', cwd=directory)
        # Collect .* files
        dot_items = []
        for item in os.listdir(git_root):
            if item in ['.git', '..', '.']:
                continue
            if item.startswith('.'):
                dot_items.append(item)
        # Add any .* files missed by 'git add ./*'
        if len(dot_items) > 0:
            execute_command('git add ' + ' '.join(dot_items), cwd=directory)
        # Remove any straggling untracked files
        execute_command('git clean -dXf', cwd=directory)
        # Commit
        cmd = 'git commit -m "Trimmed the branch to only the ' + \
              config['trim'] + ' sub directory"'
        execute_command(cmd, cwd=directory)
        # Update the patch base to be this commit
        current_branch = get_current_branch(directory)
        if current_branch is None:
            error("Could not determine current branch.", exit=True)
        config['base'] = get_commit_hash(current_branch)
    finally:
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
    return config