#! /usr/bin/env python
#
# Used to record the current state of the working directory without reverting it. This is effectively a shortcut to
# `git stash save` <message> followed by `git stash apply`.
#

import argparse
import sys

from commands import snapshot
from commands.utils import directories

# specific usage message needed to include the '--' part
_USAGE_MESSAGE = 'git snapshot [MESSAGE] [-h] [-v] [-q] [-- FILE [FILE ...]]'


def main():

    parser = argparse.ArgumentParser(
        prog='git snapshot',
        version='git-snapshot 0.7.0',
        usage=_USAGE_MESSAGE,
        description=snapshot.__doc__,
        epilog='for more detail, use: git help snapshot'
    )

    parser.add_argument('message', help='the message when creating the underlying stash', nargs='?', metavar='MESSAGE')
    parser.add_argument('-q', '--quiet', help='suppress all non-error output', action='store_true', default=False)

    # -- <files> ...
    # NOTE: this is so the files argument is listed in the argparse output. All file arguments are handled manually.
    parser.add_argument('files', help='files to create a snapshot of', nargs='?', metavar='FILE')

    # check for args that match the format: '-- <files> [<files> ..]
    args = sys.argv[1:]
    file_args = []
    if '--' in args:
        delimiter_index = args.index('--') if '--' in args else len(args)
        file_args = args[delimiter_index + 1:]
        args = args[:delimiter_index]

    args = parser.parse_args(args)
    args.files = file_args
    directories.exit_if_not_git_repository()
    snapshot.snapshot(**vars(args))


if __name__ == '__main__':
    main()
