#!/usr/bin/env python

from pykfs.script import Script
import subprocess

class Grollback(Script):
    """
    Rolls back git commits.  This process is destructive, only use if you are okay losing
    the commits in question.
    """

    args = [
        {
            "name": "steps", "type": int, "action": "store", "metavar": "N",
            "help": "The number of commits to roll back",
        },
        {
            "option_strings": ["-d", "--dryrun"], "name": "dryrun", "const": True,
            "action": "store_const", "default": False, 
            "help": "Print the command to be run, but do not execute."
        }
    ]

    def do_script(self):
        command = "git reset --hard HEAD~{}".format(self.steps)
        if self.dryrun:
            print "Command to be run:"
            print "    {}".format(command)
        else:
            subprocess.check_call(command, shell = True)


if __name__ == "__main__":
    Grollback.execute()
