# Copyright (C) 2006,
#   Stefano Zacchiroli	<zack@debian.org>
#   Filippo Giunchedi	<filippo@debian.org>
#
# This is free software, you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 or above, as published by
# the Free Software Foundation.
#
# Created:       Sat, 04 Nov 2006 19:18:50 +0100 zack
# Last-Modified: Sat, 04 Nov 2006 20:54:55 +0100 zack

import datetime
import os

from bzrlib.commands import Command, display_command, register_command
from bzrlib.builtins import tree_files
from bzrlib.add import smart_add_tree
from bzrlib.errors import PointlessCommit

def guess_author():
    names = ['EMAIL', 'DEBEMAIL', 'SUDO_USER']
    for name in names:
        if os.environ.has_key(name):
            return os.environ[name]
    return 'unknown author'

class cmd_snapshot(Command):
    """Take a snapshot committing the current status.
    """

    takes_args = []
    takes_options = []
    aliases = ['sn', 'snap']

    @display_command
    def run(self, file_list=None):
        tree, _ = tree_files([])
        smart_add_tree(tree, tree.unknowns())
        message = '%s: snapshot taken on %s' \
                % (guess_author(), datetime.datetime.now().isoformat())
        try:
            tree.commit(message, allow_pointless=False, strict=True)
        except PointlessCommit:
            pass

register_command(cmd_snapshot)

