def main(argv):
    if FLAGS["help-summary"].value:
        usage(include_flags=False)
        sys.exit()

    # Get remote from command-line flag or config option, otherwise
    # fall back to flag default.
    if not FLAGS["remote"].present:
        remote = git.get_config_option("git-change.remote")
        if remote is not None:
            FLAGS.remote = remote

    # Get Gerrit ssh host from command-line flag or config option,
    # otherwise exit with an error.
    gerrit_ssh_host = FLAGS["gerrit-ssh-host"]
    if not gerrit_ssh_host.present:
        gerrit_ssh_host.value = git.get_config_option("git-change.gerrit-ssh-host")
    if gerrit_ssh_host.value is None:
        exit_error('Please define git config option "git-change.gerrit-ssh-host" ' "or pass --gerrit-ssh-host.")

    # --merge-commit implies --use-head-commit.
    if FLAGS["merge-commit"].value:
        FLAGS["use-head-commit"].value = True

    # Fail gracefully if run outside a git repository.
    git.run_command_or_die("git status")

    argc = len(argv)
    if argc > 2 and argv[1] == "download":
        subcommand = argv[1]
    elif argc > 2 or argc == 2 and argv[1] == "download":
        usage(include_flags=False)
        sys.exit(1)
    elif argc == 2:
        subcommand = argv[1]
    else:
        subcommand = "create"  # default subcommand

    if subcommand == "create":
        create_change()
    elif subcommand == "update":
        update_change()
    elif subcommand == "rebase":
        rebase()
    elif subcommand == "list":
        list_change_branches()
    elif subcommand == "submit":
        submit_change()
    elif subcommand == "gc":
        garbage_collect()
    elif subcommand == "print":
        print_push_command()
    elif subcommand == "download":
        download_review(argv[2])
    else:
        exit_error("Unknown subcommand: %s." % subcommand)
Beispiel #2
0
def list_change_branches():
    """Lists all temporary change branches.

    Lists the branches and prompts user with a menu to check one of
    them out.
    """
    branches = get_change_branches()
    if not branches:
        print 'You have no change branches to list'
        return

    not_merged_branches = git.run_command('git branch --no-merged',
                                          trap_stdout=True).strip().split('\n')
    not_merged_branches = [line.strip()[BRANCH_SHORT_LENGTH:] for line in not_merged_branches]

    print 'Change branches:\n'
    i = 0
    for branch in branches:
        i += 1

        output = git.run_command('git log --oneline -1 %s --' % branch, trap_stdout=True)
        change_id = output.split(' ')[0]
        description = ' '.join(output.split(' ')[1:])
        short_branch = branch[0:16]
        change_branch = branch.split('-')[1]

        # handle colors here
        use_color = git.get_config_option('git-change.color')
        use_color = (use_color != 'false')  # auto or yes or anything else count as True

        cid_url = git.get_config_option('git-change.cid-url') or ''

        if use_color and change_branch not in not_merged_branches:  # not not == is merged
            sys.stdout.write(COLOR_OBSOLETE)

        if use_color and change_branch == get_change_id_from_branch():
            sys.stdout.write(COLOR_CURRENT)
        sys.stdout.write('{i:>2}: {branch_id} {href}{cid} {name}'.format(
            i=i, branch_id=short_branch, href=cid_url, cid=change_id, name=description))
        if use_color:
            sys.stdout.write(COLOR_CLEAR)
    try:
        selection = raw_input('\nSelect a branch number to check out, '
                              'or hit enter to exit: ')
    except (EOFError, KeyboardInterrupt):
        # User pressed or Ctrl-D or Ctrl-C.
        return
    if selection.isdigit() and int(selection) <= len(branches):
        git.run_command_or_die('git checkout %s' % branches[int(selection) - 1])
    elif selection:
        print 'Not a valid selection'
    else:
        pass  # User hit enter; just exit.
Beispiel #3
0
def configure():
    """Configures git-change.

    Verifies that required configuration options are in place and
    merges command-line flags with config options.
    """
    # Make sure the notes.rewriteRef config option contains the
    # git-change notes ref so that external commands that rewrite
    # commits (e.g. git-commit --amend) copy notes to the rewritten
    # commits.
    rewrite_ref = git.get_config_option('notes.rewriteRef')
    if not rewrite_ref:
        rewrite_ref = git.NOTES_REF
        git.set_config_option('notes.rewriteRef', rewrite_ref)
    elif not git.NOTES_REF in rewrite_ref:
        rewrite_ref = '%s:%s' % (rewrite_ref, git.NOTES_REF)
        git.set_config_option('notes.rewriteRef', rewrite_ref)

    # Get remote from command-line flag or config option, otherwise
    # fall back to flag default.
    if not FLAGS['remote'].present:
        remote = git.get_config_option('git-change.remote')
        if remote is not None:
            FLAGS.remote = remote

    # Get Gerrit ssh host from command-line flag or config option,
    # otherwise exit with an error.
    gerrit_ssh_host = FLAGS['gerrit-ssh-host']
    if not gerrit_ssh_host.present:
        gerrit_ssh_host.value = git.get_config_option('git-change.gerrit-ssh-host')
    if gerrit_ssh_host.value is None:
        exit_error('Please define git config option "git-change.gerrit-ssh-host" '
                   'or pass --gerrit-ssh-host.')

    # --merge-commit implies --use-head-commit.
    if FLAGS['merge-commit'].value:
        FLAGS['use-head-commit'].value = True
Beispiel #4
0
def get_reviewers_for_change():
    """Gets the reviewers for this change from command flag and OWNERS files.

    Combines two sets of Gerrit reviewer usernames to create one set of
    reviewers for this change:
        1. Reviewer usernames passed in with the command line flag 'reviewers'.
        2. Reviewer usernames listed in relevant OWNERS files, if the repo is
            configured for OWNERS files and the commit author has not passed the
            flag 'ignore-owners=True'. See the git_owners module for more
            information about OWNERS files.

    Returns:
        A list of strings representing Gerrit Code Review usernames.
    """
    reviewers = set()
    reviewers.update(FLAGS.reviewers)

    repo_configured_for_owners = git.get_config_option('git-change.include-owners') == 'true'
    ignore_owners_flag = FLAGS['ignore-owners'].value
    if repo_configured_for_owners and not ignore_owners_flag:
        reviewers.update(git_owners.get_change_owners())

    return [r for r in reviewers if r]