/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# diff.py
#
# Copyright (C) 2014 Tim Marston <tim@edm.am>
#
# This file is part of stdhome (hereafter referred to as "this program").
# See http://ed.am/dev/stdhome for more information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys, re, getopt, os
from subprocess import call
from command import Command
import stdhome.the as the
from stdhome.deployment import Deployment
from stdhome.walker.status import StatusWalker


class DiffCommand( Command ):


	def print_help( self ):
		print "Usage: " + the.program.name + " diff [--repo=REPO] [FILE]..."
		print
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
		print "Show differences between the files in the local repository and those in the home"
		print "directory."
		print
		print "This runs the diff command, showing you differences made to files in the home"
		print "directory as compared to the files in the local repository.  The differences"
		print "between all files in the local repository are shown by default, or you can"
		print "specify files that you are interested in."
		print
		print "To see a list files that this command will show differences for, you can go:"
		print "    " + the.program.name + " status"
		print
		print "Options:"
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
		print "  -v, --verbose    display information about what is being done"
		print "      --help       display help and exit"
		exit( 0 )


	def parse_command_line( self ):
		opts, args = getopt.gnu_getopt(
			sys.argv[ 1: ], "r:v",
			[ "repo=", "verbose", "help" ] )
		for opt, optarg in opts:
			if opt in [ '--repo', '-r' ]:
				if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
					raise the.program.FatalError(
						'invalid repository name: ' + optarg )
				the.repo = optarg
			elif opt in [ '--verbose', '-v' ]:
				the.verbose += 1
			elif opt == "--help":
				self.print_help()

		# discard first argument (the command)
		args.pop( 0 )

		# remaining arguments
		self.files = args


	def run( self ):

		# set up repo and check it exists
		the.repo.check_dir_exists()

		# determine files
		files = self.expand_files( self.files )

		# initialise deployment and ensure it's not ongoing
		deployment = Deployment()
		deployment.check_ongoing( False )

		# check status
		walker = StatusWalker( files if files else None )
		walker.walk()

		# information (rather than modifications) come first
		message = ''
		for file in walker.missing:
			message += "\033[33m=== missing file '%s'\n" % file
		for file in walker.changed:
			message += "\033[33m=== kind changed '%s'\n" % file
		if message: print message.rstrip()

		# call colordiff
		for file in walker.modified:
			print ( "\033[33m=== modified '%s'\n" % file ).rstrip()
			sys.stdout.flush()
			call( [ 'colordiff', '-ud',
					'--label', file,
					os.path.join( the.repo.full_dir, file ),
					'--label', file,
					os.path.join( the.full_home_dir, file ) ] )