#!/usr/bin/env python3
from gitz import combine
from gitz.git_functions import COMMIT_ID_LENGTH
from gitz.program import PROGRAM

SUMMARY = 'Combine multiple commits into one'
USAGE = 'git-combine <commit> [...<additional-commit>] [-b/--base=<commit>]'
HELP = """
Equivalent to hard resetting to the base commit, then cherry picking
each subsequent commit.
"""
EXAMPLES = """
git combine d2dfe0c a2833bc
  Goes to the commit in `master` and then cherry picks the two commits
  d2dfe0c and a2833bc on top of it.
"""
DANGER = 'Rewrites history!'


def git_combine():
    args = PROGRAM.args
    commits = combine.combine(args, args.base, *args.commit_id)
    n = len(args.commit_id)
    s = '' if n == 1 else 's'
    names = ', '.join(c[:COMMIT_ID_LENGTH] for c in commits)
    PROGRAM.message('Combined {n} commit{s} into {names}'.format(**locals()))


def add_arguments(parser):
    add_arg = parser.add_argument
    add_arg('commit_id', nargs='+', help='List of commit IDs to cherry pick')
    add_arg('-b', '--base', help='Base commit to start from')
    combine.add_arguments(parser)


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