Module: Rake::DevEiate::Hg

Defined in:
lib/rake/deveiate/hg.rb

Overview

Version-control tasks

Constant Summary collapse

COMMIT_MSG_FILE =

The name of the file to edit for the commit message

Pathname( 'commit-msg.txt' )
IGNORE_FILE =

The name of the ignore file

Rake::DevEiate::PROJECT_DIR + '.hgignore'
DEFAULT_RELEASE_TAG_PREFIX =

The prefix to use for release version tags by default

'v'
STATUS_COLORS =

Colors for presenting file statuses

{
	'M' => [:blue],                  # modified
	'A' => [:bold, :green],          # added
	'R' => [:bold, :black],          # removed
	'C' => [:white],                 # clean
	'!' => [:bold, :white, :on_red], # missing
	'?' => [:yellow],                # not tracked
	'I' => [:dim, :white],           # ignored
}
FILE_INDENT =

File indentation

""

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#release_tag_prefixObject

The prefix to use for version tags



49
50
51
# File 'lib/rake/deveiate/hg.rb', line 49

def release_tag_prefix
  @release_tag_prefix
end

Instance Method Details

#current_version_tagObject

Fetch the name of the current version’s tag.



465
466
467
# File 'lib/rake/deveiate/hg.rb', line 465

def current_version_tag
	return [ self.release_tag_prefix, self.version ].join
end

#define_tasksObject

Define version-control tasks



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rake/deveiate/hg.rb', line 53

def define_tasks
	super if defined?( super )

	return unless File.directory?( '.hg' )

	file COMMIT_MSG_FILE.to_s do |task|
		commit_log = Pathname( task.name )

		edit_commit_log( commit_log )
		unless commit_log.size?
			self.prompt.error "Empty commit message; aborting."
			commit_log.unlink if commit_log.exist?
			abort
		end
	end

	CLEAN.include( COMMIT_MSG_FILE.to_s )

	namespace :hg do

		desc "Prepare for a new release"
		task( :prerelease, &method(:do_hg_prerelease) )

		desc "Check for new files and offer to add/ignore/delete them."
		task( :newfiles, &method(:do_hg_newfiles) )
		task :add => :newfiles

		desc "Pull and update from the default repo"
		task( :pull, &method(:do_hg_pull) )

		desc "Pull and update without confirmation"
		task( :pull_without_confirmation, &method(:do_hg_pull_without_confirmation) )

		desc "Update to tip"
		task( :update, &method(:do_hg_update) )

		desc "Clobber all changes (hg up -C)"
		task( :update_and_clobber, &method(:do_hg_update_and_clobber) )

		desc "Mercurial-specific pre-checkin hook"
		task :precheckin => [ :pull, :newfiles, :check_for_changes ]

		desc "Check the current code in if tests pass"
		task( :checkin => COMMIT_MSG_FILE.to_s, &method(:do_hg_checkin) )

		desc "Mercurial-specific pre-release hook"
		task :prerelease => 'hg:check_history'

		desc "Mercurial-specific post-release hook"
		task( :postrelease, &method(:do_hg_postrelease) )

		desc "Push to the default origin repo (if there is one)"
		task( :push, &method(:do_hg_push) )

		desc "Push to the default repo without confirmation"
		task :push_without_confirmation do |task, args|
			self.hg.push
		end

		desc "Check the history file to ensure it contains an entry for each release tag"
		task( :check_history, &method(:do_hg_check_history) )

		desc "Generate and edit a new version entry in the history file"
		task( :update_history, &method(:do_hg_update_history) )

		task( :check_for_changes, &method(:do_hg_check_for_changes) )
		task( :debug, &method(:do_hg_debug) )
	end


	# Hook some generic tasks to the mercurial-specific ones
	task :checkin => 'hg:checkin'
	task :precheckin => 'hg:precheckin'

	task :prerelease => 'hg:prerelease'
	task :postrelease => 'hg:postrelease'

	desc "Update the history file with the changes since the last version tag."
	task :update_history => 'hg:update_history'

	task :debug => 'hg:debug'
rescue ::Exception => err
	$stderr.puts "%s while defining Mercurial tasks: %s" % [ err.class.name, err.message ]
	raise
end

#delete_extra_files(*filelist) ⇒ Object

Delete the files in the given filelist after confirming with the user.



538
539
540
541
542
543
544
545
546
547
548
# File 'lib/rake/deveiate/hg.rb', line 538

def delete_extra_files( *filelist )
	description = humanize_file_list( filelist, '	 ' )
	self.prompt.say "Files to delete:"
	self.prompt.say( description )

	if self.prompt.yes?( "Really delete them?" ) {|q| q.default(false) }
		filelist.each do |f|
			rm_rf( f, verbose: true )
		end
	end
end

#do_hg_check_for_changes(task, args) ⇒ Object

Check the status of the repo and ensure there are outstanding changes. If there are no changes, abort.



331
332
333
334
335
336
# File 'lib/rake/deveiate/hg.rb', line 331

def do_hg_check_for_changes( task, args )
	unless self.hg.dirty?
		self.prompt.error "Working copy is clean."
		abort
	end
end

#do_hg_check_history(task, args) ⇒ Object

Check the history file against the list of release tags in the working copy and ensure there’s an entry for each tag.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/rake/deveiate/hg.rb', line 312

def do_hg_check_history( task, args )
	unless self.history_file.readable?
		self.prompt.error "History file is missing or unreadable."
		abort
	end

	self.prompt.say "Checking history..."
	missing_tags = self.get_unhistoried_version_tags

	unless missing_tags.empty?
		self.prompt.error "%s needs updating; missing entries for tags: %s" %
			[ self.history_file, missing_tags.join(', ') ]
		abort
	end
end

#do_hg_checkin(task, args) ⇒ Object

The body of the checkin task.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rake/deveiate/hg.rb', line 276

def do_hg_checkin( task, args )
	targets = args.extras
	commit_msg = COMMIT_MSG_FILE.read.strip

	self.prompt.say( "---", color: :cyan )
	self.prompt.say( commit_msg )
	self.prompt.say( "---", color: :cyan )

	if self.prompt.yes?( "Continue with checkin?" )
		self.hg.commit( *targets, logfile: COMMIT_MSG_FILE.to_s )
		rm_f COMMIT_MSG_FILE
	else
		abort
	end
	Rake::Task[ 'hg:push' ].invoke
end

#do_hg_debug(task, args) ⇒ Object

Show debugging information.



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/rake/deveiate/hg.rb', line 408

def do_hg_debug( task, args )
	self.prompt.say( "Hg Info", color: :bright_green )

	self.prompt.say( "Mercurial version: " )
	self.prompt.say( Hglib.version, color: :bold )
	self.prompt.say( "Release tag prefix: " )
	self.prompt.say( self.release_tag_prefix, color: :bold )

	self.prompt.say( "Version tags:" )
	self.get_version_tag_names.each do |tag|
		self.prompt.say( '- ' )
		self.prompt.say( tag, color: :bold )
	end

	self.prompt.say( "History file versions:" )
	self.get_history_file_versions.each do |tag|
		self.prompt.say( '- ' )
		self.prompt.say( tag, color: :bold )
	end

	self.prompt.say( "Unhistoried version tags:" )
	self.get_unhistoried_version_tags.each do |tag|
		self.prompt.say( '- ' )
		self.prompt.say( tag, color: :bold )
	end

	self.prompt.say( "\n" )
end

#do_hg_newfiles(task, args) ⇒ Object

The body of the hg:newfiles task.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rake/deveiate/hg.rb', line 200

def do_hg_newfiles( task, args )
	self.prompt.say "Checking for new files..."

	entries = self.hg.status( no_status: true, unknown: true )

	unless entries.empty?
		files_to_add = []
		files_to_ignore = []
		files_to_delete = []

		entries.each do |entry|
			description = "  %s: %s" % [ entry.path, entry.status_description ]
			action = self.prompt.select( description ) do |menu|
				menu.choice "add", :a
				menu.choice "ignore", :i
				menu.choice "skip", :s
				menu.choice "delete", :d
			end

			case action
			when :a
				files_to_add << entry.path
			when :i
				files_to_ignore << entry.path
			when :d
				files_to_delete << entry.path
			end
		end

		unless files_to_add.empty?
			self.hg.add( *files_to_add )
		end

		unless files_to_ignore.empty?
			hg_ignore_files( *files_to_ignore )
		end

		unless files_to_delete.empty?
			delete_extra_files( *files_to_delete )
		end
	end
end

#do_hg_postrelease(task, args) ⇒ Object

The body of the hg:postrelease task.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rake/deveiate/hg.rb', line 176

def do_hg_postrelease( task, args )
	if self.hg.status( 'checksum', unknown: true ).any?
		self.prompt.say "Adding release artifacts..."
		self.hg.add( 'checksum' )
		self.hg.commit( 'checksum', message: "Adding release checksum." )
	end

	if self.prompt.yes?( "Move released changesets to public phase?" )
		self.prompt.say "Publicising changesets..."
		self.hg.phase( public: true )
	end

	if self.hg.extension_enabled?( :topic )
		current_topic = self.hg.topic
		if current_topic && self.prompt.yes?( "Clear the current topic (%s)?" %[current_topic] )
			self.hg.topic( clear: true )
		end
	end

	Rake::Task['hg:push'].invoke
end

#do_hg_prerelease(task, args) ⇒ Object

The body of the hg:prerelease task.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rake/deveiate/hg.rb', line 141

def do_hg_prerelease( task, args )
	uncommitted_files = self.hg.status( n: true )
	unless uncommitted_files.empty?
		self.show_file_statuses( uncommitted_files )

		fail unless self.prompt.yes?( "Release anyway?" ) do |q|
			q.default( false )
		end

		self.prompt.warn "Okay, releasing with uncommitted versions."
	end

	pkg_version_tag = self.current_version_tag
	rev = self.hg.identity.id

	# Look for a tag for the current release version, and if it exists abort
	if self.hg.tags.find {|tag| tag.name == pkg_version_tag }
		self.prompt.error "Version #{self.version} already has a tag."
		fail
	end

	# Tag the current rev
	self.prompt.ok "Tagging rev %s as %s" % [ rev, pkg_version_tag ]
	self.hg.tag( pkg_version_tag, rev: rev )

	# Sign the tag
	if self.hg.extension_enabled?( :gpg )
		if self.prompt.yes?( "Sign %s?" % [pkg_version_tag] )
			self.hg.sign( pkg_version_tag, message: "Signing %s" % [pkg_version_tag] )
		end
	end
end

#do_hg_pull(task, args) ⇒ Object

The body of the hg:pull task.



245
246
247
248
249
250
251
252
253
254
# File 'lib/rake/deveiate/hg.rb', line 245

def do_hg_pull( task, args )
	paths = self.hg.paths
	if origin_url = paths[:default]
		if self.prompt.yes?( "Pull and update from '#{origin_url}'?" )
			self.hg.pull_update
		end
	else
		trace "Skipping pull: No 'default' path."
	end
end

#do_hg_pull_without_confirmation(task, args) ⇒ Object

The body of the hg:pull_without_confirmation task.



258
259
260
# File 'lib/rake/deveiate/hg.rb', line 258

def do_hg_pull_without_confirmation( task, args )
	self.hg.pull
end

#do_hg_push(task, args) ⇒ Object

The body of the push task.



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/rake/deveiate/hg.rb', line 295

def do_hg_push( task, args )
	paths = self.hg.paths
	if origin_url = paths[:default]
		if self.prompt.yes?( "Push to '#{origin_url}'?" ) {|q| q.default(false) }
			self.hg.push
			self.prompt.ok "Done."
		else
			abort
		end
	else
		trace "Skipping push: No 'default' path."
	end
end

#do_hg_update(task, args) ⇒ Object

The body of the hg:update task.



264
265
266
# File 'lib/rake/deveiate/hg.rb', line 264

def do_hg_update( task, args )
	self.hg.pull_update
end

#do_hg_update_and_clobber(task, args) ⇒ Object

The body of the hg:update_and_clobber task.



270
271
272
# File 'lib/rake/deveiate/hg.rb', line 270

def do_hg_update_and_clobber( task, args )
	self.hg.update( clean: true )
end

#do_hg_update_history(task, args) ⇒ Object

Generate a new history file entry for the current version.



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/rake/deveiate/hg.rb', line 340

def do_hg_update_history( task, args ) # Needs refactoring
	unless self.history_file.readable?
		self.prompt.error "History file is missing or unreadable."
		abort
	end

	version_tag = self.current_version_tag
	previous_tag = self.previous_version_tag
	self.prompt.say "Updating history for %s..." % [ version_tag ]

	if self.get_history_file_versions.include?( version_tag )
		self.log.ok "History file already includes a section for %s" % [ version_tag ]
		abort
	end

	header, rest = self.history_file.read( encoding: 'utf-8' ).
		split( /(?<=^---)/m, 2 )

	self.trace "Rest is: %p" % [ rest ]
	if !rest || rest.empty?
		self.prompt.warn "History file needs a header with a `---` marker to support updating."
		self.prompt.say "Adding an auto-generated one."
		rest = header
		header = self.load_and_render_template( 'History.erb', self.history_file )
	end

	header_char = self.header_char_for( self.history_file )
	ext = self.history_file.extname
	log_entries = if previous_tag
			self.hg.log( rev: "#{previous_tag}~-2::" )
		else
			self.hg.log
		end

	Tempfile.create( ['History', ext], encoding: 'utf-8' ) do |tmp_copy|
		tmp_copy.print( header )
		tmp_copy.puts

		tmp_copy.puts "%s %s [%s] %s" % [
			header_char * 2,
			version_tag,
			Date.today.strftime( '%Y-%m-%d' ),
			self.authors.first,
		]

		tmp_copy.puts
		log_entries.each do |entry|
			tmp_copy.puts "- %s" % [ entry.summary ]
		end
		tmp_copy.puts
		tmp_copy.puts

		tmp_copy.print( rest )
		tmp_copy.close

		TTY::Editor.open( tmp_copy.path )

		if File.size?( tmp_copy.path )
			cp( tmp_copy.path, self.history_file )
		else
			self.prompt.error "Empty file: aborting."
		end
	end

end

#edit_commit_log(logfile) ⇒ Object

Generate a commit log and invoke the user’s editor on it.



517
518
519
520
521
# File 'lib/rake/deveiate/hg.rb', line 517

def edit_commit_log( logfile )
	diff = self.hg.diff

	TTY::Editor.open( logfile, content: diff )
end

#get_history_file_versionsObject

Fetch the list of the versions of releases that have entries in the history file.



493
494
495
496
497
498
499
# File 'lib/rake/deveiate/hg.rb', line 493

def get_history_file_versions
	tag_pattern = self.release_tag_pattern

	return IO.readlines( self.history_file ).grep( tag_pattern ).map do |line|
		line[ /^(?:h\d\.|#+|=+)\s+(#{tag_pattern})\s+/, 1 ]
	end.compact
end

#get_unhistoried_version_tags(include_current_version: true) ⇒ Object

Read the list of tags and return any that don’t have a corresponding section in the history file.



504
505
506
507
508
509
510
511
512
513
# File 'lib/rake/deveiate/hg.rb', line 504

def get_unhistoried_version_tags( include_current_version: true )
	release_tags = self.get_version_tag_names
	release_tags.unshift( self.current_version_tag ) if include_current_version

	self.get_history_file_versions.each do |tag|
		release_tags.delete( tag )
	end

	return release_tags
end

#get_version_tag_namesObject

Fetch the list of names of tags that match the versioning scheme of this project.



485
486
487
488
# File 'lib/rake/deveiate/hg.rb', line 485

def get_version_tag_names
	tag_pattern = self.release_tag_pattern
	return self.hg.tags.map( &:name ).grep( tag_pattern )
end

#hgObject

Return an Hglib::Repo for the directory rake was invoked in, creating it if necessary.



443
444
445
# File 'lib/rake/deveiate/hg.rb', line 443

def hg
	@hg ||= Hglib.repo( Rake::DevEiate::PROJECT_DIR )
end

#hg_ignore_files(*pathnames) ⇒ Object

Add the list of pathnames to the .hgignore list.



525
526
527
528
529
530
531
532
533
534
# File 'lib/rake/deveiate/hg.rb', line 525

def hg_ignore_files( *pathnames )
	patterns = pathnames.flatten.collect do |path|
		'^' + Regexp.escape( path.to_s ) + '$'
	end
	self.trace "Ignoring %d files." % [ pathnames.length ]

	IGNORE_FILE.open( File::CREAT|File::WRONLY|File::APPEND, 0644 ) do |fh|
		fh.puts( patterns )
	end
end

#humanize_file_list(list, indent = FILE_INDENT) ⇒ Object

Returns a human-scannable file list by joining and truncating the list if it’s too long.



552
553
554
555
556
557
558
559
# File 'lib/rake/deveiate/hg.rb', line 552

def humanize_file_list( list, indent=FILE_INDENT )
	listtext = list[0..5].join( "\n#{indent}" )
	if list.length > 5
		listtext << " (and %d other/s)" % [ list.length - 5 ]
	end

	return listtext
end

#previous_version_tagObject

Fetch the name of the tag for the previous version.



471
472
473
# File 'lib/rake/deveiate/hg.rb', line 471

def previous_version_tag
	return self.get_version_tag_names.first
end

#release_tag_patternObject

Return a Regexp that matches the project’s convention for versions.



477
478
479
480
# File 'lib/rake/deveiate/hg.rb', line 477

def release_tag_pattern
	prefix = self.release_tag_prefix
	return /#{prefix}\d+(\.\d+)+/
end

#setup(_name, **options) ⇒ Object

Set up defaults



40
41
42
43
44
# File 'lib/rake/deveiate/hg.rb', line 40

def setup( _name, **options )
	super if defined?( super )

	@release_tag_prefix = options[:release_tag_prefix] || DEFAULT_RELEASE_TAG_PREFIX
end

#show_file_statuses(statuses) ⇒ Object

Given a status_hash like that returned by Hglib::Repo.status, return a string description of the files and their status.



450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/rake/deveiate/hg.rb', line 450

def show_file_statuses( statuses )
	lines = statuses.map do |entry|
		status_color = STATUS_COLORS[ entry.status ]
		"	%s: %s" % [
			self.pastel.white( entry.path.to_s ),
			self.pastel.decorate( entry.status_description, *status_color ),
		]
	end

	self.prompt.say( self.pastel.headline "Uncommitted files:" )
	self.prompt.say( lines.join("\n") )
end