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
 
 
28
class UpdateCommand( Command ):
 
 
35
        def print_help( self ):
 
 
36
                print "Usage: " + the.program.name + " update [--repo=REPO]"
 
 
38
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
 
 
39
                print "Update a local repository and deploy changes."
 
 
41
                print "Files in your home directory are copied in to the local repository, the local"
 
 
42
                print "repository is then updated (i.e., changes made to it are pulled down and merged)"
 
 
43
                print "and files from the local repository are then redeployed to your home directory."
 
 
45
                print "If any conflicts occur, either during the merge or during the redeployment, no"
 
 
46
                print "files will be deployed and you will be expected to manually intervene.  Merge"
 
 
47
                print "conflicts should be dealt with the same way as you'd expect for version control."
 
 
48
                print "Conflicts that arise from files already existing in your home directory must be"
 
 
49
                print "dealt with by moving those files aside (currently)."
 
 
51
                print "After a failed update, you can list outstanding conflicts by typing:"
 
 
52
                print "    " + the.program.name + " conflicts"
 
 
54
                print "After fixing outstanding conflicts, you can re-attempt the redeployment of"
 
 
55
                print "your repository by typing:"
 
 
56
                print "    " + the.program.name + " resolve"
 
 
58
                print "To back out of trying to update and revert the local repository, type:"
 
 
59
                print "    " + the.program.name + " stage-revert"
 
 
62
                print "      --quiet      do not report changes to the home directory"
 
 
63
                print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
 
 
64
                print "  -v, --verbose    display information about what is being done"
 
 
65
                print "      --help       display help and exit"
 
 
69
        def parse_command_line( self ):
 
 
70
                opts, args = getopt.gnu_getopt(
 
 
71
                        sys.argv[ 1: ], "qr:v",
 
 
72
                        [ "quiet", "repo=", "verbose", "help" ] )
 
 
73
                for opt, optarg in opts:
 
 
76
                        elif opt in [ '--repo', '-r' ]:
 
 
77
                                if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
 
 
78
                                        raise the.program.FatalError(
 
 
79
                                                'invalid repository name: ' + optarg )
 
 
81
                        elif opt in [ '--verbose', '-v' ]:
 
 
86
                # discard first argument (the command)
 
 
91
                        raise the.program.UsageError( 'too many arguments' )
 
 
96
                # set up repo and check it exists
 
 
97
                the.repo.check_dir_exists()
 
 
99
                # initialise deployment (and check it)
 
 
100
                deployment = Deployment()
 
 
101
                deployment.check_ongoing( False )
 
 
103
                # check for local changes
 
 
104
                if the.repo.vcs.has_changes():
 
 
105
                        raise the.program.FatalError(
 
 
106
                                'repo has local changes: %s\n'
 
 
107
                                'Hint: see "%s stage-revert --help"' %
 
 
108
                                ( the.repo.name, the.program.name ) )
 
 
110
                # copy-in changes to repo
 
 
114
                if the.verbose >= 1: print "updating %s" % the.repo.dir
 
 
115
                updated_files = the.repo.vcs.update()
 
 
119
                # check for conflicts in repo
 
 
120
                files = the.repo.vcs.get_conflicts()
 
 
122
                        message += 'conflicts in %s:\n  %s' % \
 
 
123
                                           ( the.repo.name, '\n  '.join( files ) )
 
 
125
                # check for deployment conclicts
 
 
126
                conflicts = deployment.get_conflicts( updated_files )
 
 
128
                        message += 'deployment conflicts:\n  %s' % \
 
 
129
                                           '\n  '.join( conflicts )
 
 
131
                # stop if there are conflicts
 
 
133
                        raise the.program.FatalError(
 
 
134
                                'there were conflicts...\n' + message )
 
 
136
                # copy-out changes from repo
 
 
137
                deployment.copy_out( self.quiet )
 
 
139
                # now we've copied-out, revert any copied-in changes!
 
 
140
                if the.verbose >= 1: print "reverting %s" % the.repo.dir
 
 
141
                the.repo.vcs.revert()