/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
1
# diff.py
2
#
3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
4
#
5
# This file is part of stdhome (hereafter referred to as "this program").
6
# See http://ed.am/dev/stdhome for more information.
7
#
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.
12
#
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.
17
#
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/>.
20
21
22
import sys, re, getopt, os
23
from subprocess import call
24
from command import Command
25
import stdhome.the as the
26
from stdhome.deployment import Deployment
27
from stdhome.walker.status import StatusWalker
28
29
30
class DiffCommand( Command ):
31
32
33
	def print_help( self ):
34
		print "Usage: " + the.program.name + " diff [--repo=REPO] [FILE]..."
35
		print
36
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
37
		print "Show differences between the files in the local repository and those in the home"
38
		print "directory."
39
		print
40
		print "This runs the diff command, showing you differences made to files in the home"
41
		print "directory as compared to the files in the local repository.  The differences"
42
		print "between all files in the local repository are shown by default, or you can"
43
		print "specify files that you are interested in."
44
		print
45
		print "To see a list files that this command will show differences for, you can go:"
46
		print "    " + the.program.name + " status"
47
		print
48
		print "Options:"
49
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
50
		print "  -v, --verbose    display information about what is being done"
51
		print "      --help       display help and exit"
52
		exit( 0 )
53
54
55
	def parse_command_line( self ):
56
		opts, args = getopt.gnu_getopt(
57
			sys.argv[ 1: ], "r:v",
58
			[ "repo=", "verbose", "help" ] )
59
		for opt, optarg in opts:
60
			if opt in [ '--repo', '-r' ]:
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
61
				if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
62
					raise the.program.FatalError(
63
						'invalid repository name: ' + optarg )
17 by Tim Marston
read ~/.stdhomerc; commands set repo before run(); program performs late
64
				the.repo = optarg
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
65
			elif opt == "--help":
66
				self.print_help()
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
67
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
68
		# discard first argument (the command)
69
		args.pop( 0 )
70
71
		# remaining arguments
72
		self.files = args
73
74
75
	def run( self ):
76
77
		# set up repo and check it exists
78
		the.repo.check_dir_exists()
79
80
		# determine files
81
		files = self.expand_files( self.files )
82
83
		# initialise deployment and ensure it's not ongoing
84
		deployment = Deployment()
85
		deployment.check_ongoing( False )
86
87
		# check status
88
		walker = StatusWalker( files if files else None )
89
		walker.walk()
90
91
		# information (rather than modifications) come first
92
		message = ''
93
		for file in walker.missing:
94
			message += "\033[33m=== missing file '%s'\n" % file
95
		for file in walker.changed:
96
			message += "\033[33m=== kind changed '%s'\n" % file
97
		if message: print message.rstrip()
98
99
		# call colordiff
100
		for file in walker.modified:
101
			print ( "\033[33m=== modified '%s'\n" % file ).rstrip()
46 by Tim Marston
fixed diff command out-of-order output issue
102
			sys.stdout.flush()
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
103
			call( [ 'colordiff', '-ud',
104
					'--label', file,
105
					os.path.join( the.repo.full_dir, file ),
106
					'--label', file,
107
					os.path.join( the.full_home_dir, file ) ] )