Module: Rake::DevEiate::Hg

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

Overview

Mercurial 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'
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

" • "
DEFAULT_RELEASE_BRANCH =

The name of the branch releases should be created from

'default'

Instance Method Summary collapse

Instance Method Details

#current_hg_version_tag ⇒ Object

Fetch the name of the current version's tag.



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

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

#define_tasks ⇒ Object

Define version-control tasks



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rake/deveiate/hg.rb', line 40

def define_tasks
  super if defined?( super )

  return unless self.is_hg_working_copy?

  # :TODO: Actually check for this? Doesn't make much as much sense given my
  # Mercurial workflow.
  self.release_branch = DEFAULT_RELEASE_BRANCH

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

    hg_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

#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.



328
329
330
331
332
333
# File 'lib/rake/deveiate/hg.rb', line 328

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.



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

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_hg_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.



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

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.



405
406
407
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
436
# File 'lib/rake/deveiate/hg.rb', line 405

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

  if self.is_hg_working_copy?
    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_hg_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_hg_unhistoried_version_tags.each do |tag|
      self.prompt.say( '- ' )
      self.prompt.say( tag, color: :bold )
    end
  else
    self.prompt.say( "Doesn't appear to be a Mercurial repository." )
  end

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

#do_hg_newfiles(task, args) ⇒ Object

The body of the hg:newfiles task.



197
198
199
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
# File 'lib/rake/deveiate/hg.rb', line 197

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.



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

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.



138
139
140
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
# File 'lib/rake/deveiate/hg.rb', line 138

def do_hg_prerelease( task, args )
  uncommitted_files = self.hg.status( n: true )
  unless uncommitted_files.empty?
    self.show_hg_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_hg_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.



242
243
244
245
246
247
248
249
250
251
# File 'lib/rake/deveiate/hg.rb', line 242

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.



255
256
257
# File 'lib/rake/deveiate/hg.rb', line 255

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

#do_hg_push(task, args) ⇒ Object

The body of the push task.



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rake/deveiate/hg.rb', line 292

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.



261
262
263
# File 'lib/rake/deveiate/hg.rb', line 261

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.



267
268
269
# File 'lib/rake/deveiate/hg.rb', line 267

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.



337
338
339
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
# File 'lib/rake/deveiate/hg.rb', line 337

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_hg_version_tag
  previous_tag = self.previous_hg_version_tag
  self.prompt.say "Updating history for %s..." % [ version_tag ]

  if self.get_history_file_versions.include?( version_tag )
    self.trace "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

#get_hg_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.



487
488
489
490
491
492
493
494
495
496
# File 'lib/rake/deveiate/hg.rb', line 487

def get_hg_unhistoried_version_tags( include_current_version: true )
  release_tags = self.get_hg_version_tag_names
  release_tags.unshift( self.current_hg_version_tag ) if include_current_version

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

  return release_tags
end

#get_hg_version_tag_names ⇒ Object

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



479
480
481
482
# File 'lib/rake/deveiate/hg.rb', line 479

def get_hg_version_tag_names
  tag_pattern = /#{self.release_tag_pattern}$/
  return self.hg.tags.map( &:name ).grep( tag_pattern )
end

#hg ⇒ Object

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



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

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

#hg_edit_commit_log(logfile) ⇒ Object

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



500
501
502
503
504
# File 'lib/rake/deveiate/hg.rb', line 500

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

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

#hg_ignore_files(*pathnames) ⇒ Object

Add the list of pathnames to the .hgignore list.



508
509
510
511
512
513
514
515
516
517
# File 'lib/rake/deveiate/hg.rb', line 508

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

#is_hg_working_copy? ⇒ Boolean

Returns true if the current directory looks like a Mercurial working copy.

Returns:

  • (Boolean)


132
133
134
# File 'lib/rake/deveiate/hg.rb', line 132

def is_hg_working_copy?
  return File.directory?( '.hg' )
end

#previous_hg_version_tag ⇒ Object

Fetch the name of the tag for the previous version.



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

def previous_hg_version_tag
  return self.get_hg_version_tag_names.first
end

#show_hg_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.



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

def show_hg_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