#!/code/env/gitz-test/bin/python
from gitz import combine
from gitz import git_functions
from gitz import git_root
from gitz.program import PROGRAM

SUMMARY = 'Cherry-pick multiple commits, with an optional squash'

HELP = """
Cherry pick each commit one after another.

If there is a -s/--squash argument, squash the commits down into one,
using the argument to -s/--squash as the commit message.
"""

EXAMPLES = """
git multi-pick d2dfe0c a2833bc
  Cherry-picks the commit d2dfe0c and then a2833bc on top of it.

git multi-pick d2dfe0c a2833bc --squash='Squashed commit!'
  Cherry-picks the commit d2dfe0c and then a2833bc on top of it,
  and then squashes them into one commit with the commit message
  'Squashed commit!'
"""


def git_multi_pick():
    git_root.check_clean_workspace()
    commit_ids = git_functions.commit_ids(PROGRAM.args.commit_ids)
    commits = combine.combine(commit_ids, PROGRAM.args.squash)

    n = len(commit_ids)
    s = '' if n == 1 else 's'
    names = ', '.join(commits)
    PROGRAM.message('Combined {n} commit{s} into {names}'.format(**locals()))


def add_arguments(parser):
    add_arg = parser.add_argument
    add_arg('commit_ids', nargs='+', help='List of commit IDs to cherry pick')
    combine.add_arguments(parser)


if __name__ == '__main__':
    PROGRAM.start()
