3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
5
# This file is part of stdhome (hereafter referred to as "this program").
6
# See http://ed.am/dev/stdhome for more information.
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
import sys, re, getopt
23
from command import Command
24
import stdhome.the as the
25
from stdhome.deployment import Deployment
26
from stdhome.walker.copy_out import CopyOutWalker
29
class RevertCommand( Command ):
37
def print_help( self ):
38
print "Usage: " + the.program.name + " revert [--repo=REPO] [--all] [FILE]..."
40
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
41
print "Revert any local changes to a file in the home directory."
43
print "This reverts the content of the named files, in the local filesystem, specified"
44
print "relative to the home directory, to its state in the repo. Specifying individual"
45
print "files and directories will revert them (recursively, in the case of"
46
print "directories). Or you can revert all files in your home directory by specifying"
47
print "no files and explicitly using the --all option."
49
print "To see a list files that can be reverted, type:"
50
print " " + the.program.name + " status"
53
print " --all confirm that you want to revert all files"
54
print " --quiet do not report changes to the home directory"
55
print " -r, --repo=REPO select the repo to check-out or create (defaults to 'home')"
56
print " -v, --verbose display information about what is being done"
57
print " --help display help and exit"
61
def parse_command_line( self ):
62
opts, args = getopt.gnu_getopt(
63
sys.argv[ 1: ], "r:v",
64
[ "all", "quiet", "repo=", "verbose", "help" ] )
65
for opt, optarg in opts:
68
elif opt == "--quiet":
70
elif opt in [ '--repo', '-r' ]:
71
if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
72
raise the.program.FatalError(
73
'invalid repository name: ' + optarg )
75
elif opt in [ '-v', '--verbose' ]:
80
# discard first argument (the command)
86
# check that either files or --all was given
87
if len( self.files ) == 0 and not self.all:
88
raise the.program.UsageError(
89
"no files were specified and --all was not given" )
90
if len( self.files ) > 0 and self.all:
91
raise the.program.UsageError(
92
"files specified, which conflicts with using --all" )
97
# set up repo and check it exists
98
the.repo.check_dir_exists()
101
files = self.expand_files( self.files )
103
# initialise deployment and ensure it's not ongoing
104
deployment = Deployment()
105
deployment.check_ongoing( False )
107
# check for local changes
108
if the.repo.vcs.has_changes():
109
raise the.program.FatalError(
110
'repo has local changes: %s\n'
111
'Hint: see "%s stage-revert --help"' %
112
( the.repo.name, the.program.name ) )
115
walker = CopyOutWalker( files if files else None, not self.quiet )