Example #1
0
def tag_all_remote_branches(b_tag_after_update, repo_abs_path, repo):
    """
    Tag all remote branches with timestamps and branch names
    """
    # preserve current status
    # current_section_branch = git.get_current_branch()
    # probably section path
    section_path = os.getcwd()

    if b_tag_after_update:
        # need to obtain the branch list in the repository
        os.chdir(repo_abs_path)

        # preserve repository status
        current_repo_branch = git.get_current_branch()

        # branch name loop
        for repo_branch in git.get_remote_branch_list():
            # A remote branch would be like : remote_name/branch_name/##
            tag_stamp(b_tag_after_update,
                      repo_abs_path,
                      repo,
                      branch=repo_branch,
                      commit=repo_branch)

        # restore repository branch
        git.checkout(current_repo_branch)
        if 'master' != git.get_current_branch().strip():
            print("branch = {branch}, repo path = {path}".format(
                branch=git.get_current_branch(), path=repo_abs_path))

    # return to section path
    os.chdir(section_path)
Example #2
0
def garbage_collect():
    """Removes temporary change branches which are fully merged."""
    current_branch = git.get_current_branch()
    if current_branch.startswith('change-I'):
        exit_error('`git-change gc` cannot be run from a change branch.')

    unmerged_branches = []
    deleted = False
    for branch in get_change_branches():
        try:
            # Note: git branch -d prints 'Deleted branch ...' to stdout.
            git.run_command('git branch -d %s' % branch, trap_stderr=True, output_on_error=False)
        except git.CalledProcessError:
            unmerged_branches.append(branch)
        else:
            deleted = True

    if unmerged_branches:
        if deleted:
            print  # Blank line between deleted branches and the message below.
        print ('The following change branches could not be deleted, probably because they\n'
               'are not fully merged into the current branch. You might try first running\n'
               'git-pull or git-change rebase in order to sync with remote.\n')
        for branch in unmerged_branches:
            print branch
Example #3
0
def rebase():
    """Rebases the target and temporary change branches.

    Rebases the target branch (the branch from which the temporary
    change branch was created) and then rebases the temporary change
    branch. This can be used to pull upstream changes down to both
    branches to resolve a failed Gerrit submission due to a path
    conflict.

    If there are conflicts with either rebase operation, the process
    terminates and it is up to the user to resolve the conflicts.
    """
    check_for_change_branch()
    target_branch = get_target_branch()
    change_branch = git.get_current_branch()

    git.run_command_or_die('git checkout %s' % target_branch)
    try:
        git.run_command('git pull --rebase', output_on_error=False)
    except git.CalledProcessError, e:
        print ('Rebase failed for branch %s. After resolving merge failure(s),\n'
               'check out the change branch (%s) and run "git change rebase" again.\n'
               'See "git help rebase" for help on resolving merge conflicts.' %
               (target_branch, change_branch))
        sys.exit(e.returncode)
Example #4
0
def determine_branches():
    """Determines the current and target branches.

    The current branch is the current HEAD, and the target branch is
    the branch to which this change is to be merged. The current
    branch may or may not be a temporary change branch but the target
    branch is always a tracking branch.

    Exits with a non-zero status if --chain is true and the current
    branch is *not* a change branch, or if --chain is false and the
    current branch *is* a change branch.

    Returns:
        A tuple of two strings: the name of the current branch and
        that of the target branch.
    """
    current_branch = git.get_current_branch()
    if FLAGS.chain:
        # Extract the target branch from the current change branch.
        target_branch = get_target_branch()
        if target_branch is None:
            exit_error('The current branch must be a change branch when you specify --chain.')
    else:
        if current_branch.startswith('change-I'):
            exit_error('You are in a temporary change branch. '
                       'If you wish to chain commits, pass --chain.')
        target_branch = current_branch

    return current_branch, target_branch
Example #5
0
def run():
    git = git.Git('./test_folder', 'https://github.com/mascanio/test-priv.git',
                  'mascanio', env['token'])
    status = git.status()

    print(status)
    print(status.is_clean())

    git.add_all()

    git.commit('master')

    status = git.status()
    print(status)
    print(status.is_clean())

    print(git.get_current_branch())

    # print(git.push(git.get_current_branch()))
    # print(git.pull('dev', rebase=True))
    git.rebase_stash('master')

    status = git.status()
    print(status)
    print(status.is_clean())
Example #6
0
def generate_test_lists(maven_project_root,
                        changes_only=True,
                        unit_test_output=None,
                        integration_test_output=None):
    project = load_maven_project(maven_project_root)
    print("Maven project contains %d modules." % len(project.modules))

    current_branch = get_current_branch()
    print("Current branch: %s" % current_branch)
    parent_branch = get_parent_branch(maven_project_root)
    print("Parent branch: %s" % parent_branch)

    if current_branch != parent_branch and changes_only:
        files_changed = get_changed_files(maven_project_root)
        print("Files with changes:")
        for file_changed in files_changed:
            print(file_changed)

        modules_with_changes = project.get_modules_related_to(files_changed)
        print("Modules with changes:")
        for module_with_changes in modules_with_changes:
            print(module_with_changes)

        module_users = project.get_module_users(modules_with_changes)
        modules_to_consider = set(module_users).union(
            set(modules_with_changes))
    else:
        # Consider all modules
        modules_to_consider = project.modules

    print("Modules to consider:")
    for module_to_consider in modules_to_consider:
        print(module_to_consider)

    # Open our file handles
    unit_file = None
    if unit_test_output is not None:
        unit_file = open(unit_test_output, 'w')

    itest_file = None
    if integration_test_output is not None:
        itest_file = open(integration_test_output, 'w')

    print("Modules with tests:")
    for m in modules_to_consider:
        if m.has_tests():
            print(m)
            for test in m.find_tests():
                print("\t%s - %s (%s)" %
                      (test.file, test.classname,
                       "Failsafe" if test.is_integration_test else "Surefire"))

                if not test.is_integration_test:
                    if unit_file:
                        unit_file.write("%s\n" % test.classname)
                else:
                    if itest_file:
                        itest_file.write("%s\n" % test.classname)
Example #7
0
def print_push_command():
    """Prints the command to push a change to Gerrit."""
    change_id = get_change_id_from_branch()
    if change_id is not None:
        change = get_change(change_id)
        target_branch = change['branch']
    else:
        target_branch = git.get_current_branch()
    print build_push_command(target_branch)
Example #8
0
def get_change_id_from_branch():
    """Returns the change ID embedded in the current branch name.

    Assumes the current branch is a temporary change branch created by
    a previous run of git-change. Example branch name:
    'change-Id06774ede265426f85d36cca50bba69d8aa54ed8'.

    Returns:
        A string representing the change ID embedded in the current
        branch name, or None if the current branch is not a temporary
        change branch.
    """
    branch = git.get_current_branch()
    if branch.startswith('change-I'):
        _, change_id = branch.split('-')
        return change_id
    return None
Example #9
0
"""
try behave
"""
import os
import git

import semantic_version
__author__ = 'Mike Carifio <*****@*****.**>'

here = os.path.dirname(__file__)
version = '0.0.1'
version_suffix = git.get_current_tag(here, git.get_current_branch(here))
if version_suffix:
    __version__ = semantic_version.Version(version + '+git' + version_suffix)
else:
    __version__ = semantic_version.Version(version)


import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('{levelname}:{pathname}:{lineno} | {message}', style='{')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug('why does format matter?')

if '__main__' == __name__:
    logger.debug('start main')
Example #10
0
def build_if_need():
    git_info = config.config_dic['git']

    project_path = config.config_dic['project_path']

    branch = 'master'
    current_branch = git.get_current_branch(project_path)
    if current_branch:
        branch = current_branch

    if git_info['pull_before_build']:
        print('Pulling latest code...')

        if git_info['branch']:
            if git_info['branch'] == branch:
                git.pull(project_path, branch=branch)
            else:
                git.checkout_and_pull(project_path, branch=git_info['branch'])
        else:
            git.pull(project_path, branch=branch)

        print('Pull code complete!')

    current_commit = git.get_current_commit(project_path)

    last_try_file = config.config_dic['log_path'] + 'last_try_build.txt'
    last_build_file = config.config_dic['log_path'] + 'last_build.txt'

    last_try_commit = '0'
    last_build_commit = '0'

    if os.path.exists(last_try_file):
        with open(last_try_file, 'r') as f:
            last_try_commit = f.read()

    if os.path.exists(last_build_file):
        with open(last_build_file, 'r') as f:
            last_build_commit = f.read()

    if last_try_commit == current_commit:
        print('Build have tried, exit!')
        return (False, None)

    commit_msg = git.get_latest_commit_msg(project_path)

    build_target = None
    for key in config.config_dic['build']:
        if config.config_dic['build'][key]['build_identifier'] in commit_msg:
            build_target = key
            break

    if not build_target:
        print('No build identifier, exit!')
        return (False, None)

    if last_build_commit == current_commit:
        print('This build has been builded, exit!')
        return (False, None)

    print('Build identifier detect, build start...')
    print('Build info {}'.format(config.config_dic['build'][build_target]))
    return (True, build_target)