class BetweenMeals::Repo::Hg

Public Instance Methods

changes(start_ref, end_ref) click to toggle source

Return files changed between two revisions

# File lib/between_meals/repo/hg.rb, line 47
def changes(start_ref, end_ref)
  valid_ref?(start_ref)
  valid_ref?(end_ref) if end_ref
  stdout = @cmd.status(start_ref, end_ref).stdout
  begin
    parse_status(stdout).compact
  rescue StandardError => e
    # We've seen some weird non-reproducible failures here
    @logger.error(
      'Something went wrong. Please report this output.',
    )
    @logger.error(e)
    stdout.lines.each do |line|
      @logger.error(line.strip)
    end
    exit(1)
  end
end
checkout(url) click to toggle source
# File lib/between_meals/repo/hg.rb, line 42
def checkout(url)
  @cmd.clone(url, @repo_path)
end
email() click to toggle source
# File lib/between_meals/repo/hg.rb, line 116
def email
  username[2]
rescue StandardError
  nil
end
exists?() click to toggle source
# File lib/between_meals/repo/hg.rb, line 34
def exists?
  Dir.exist?(Pathname.new(@repo_path).join('.hg'))
end
files() click to toggle source

Return all files

# File lib/between_meals/repo/hg.rb, line 75
def files
  @cmd.manifest.stdout.split("\n").map do |x|
    { :path => x, :status => :created }
  end
end
head_parents() click to toggle source
# File lib/between_meals/repo/hg.rb, line 81
def head_parents
  [{
    :time => Time.parse(@cmd.log('date|isodate', 'master').stdout),
    :rev => @cmd.log('node', 'master').stdout,
  }]
rescue StandardError
  [{
    :time => nil,
    :rev => nil,
  }]
end
head_rev() click to toggle source
# File lib/between_meals/repo/hg.rb, line 38
def head_rev
  @cmd.log('node').stdout
end
last_author() click to toggle source
# File lib/between_meals/repo/hg.rb, line 93
def last_author
  [
    /^.*<(.*)>$/,
    /^(.*@.*)$/,
  ].each do |re|
    m = @cmd.log('author').stdout.match(re)
    return { :email => m[1] } if m
  end
  return { :email => nil }
end
last_msg() click to toggle source
# File lib/between_meals/repo/hg.rb, line 104
def last_msg
  @cmd.log('desc').stdout
rescue StandardError
  nil
end
last_msg=(msg) click to toggle source
# File lib/between_meals/repo/hg.rb, line 110
def last_msg=(msg)
  if last_msg.strip != msg.strip
    @cmd.amend(msg.strip)
  end
end
name() click to toggle source
# File lib/between_meals/repo/hg.rb, line 122
def name
  username[1]
rescue StandardError
  nil
end
setup() click to toggle source
# File lib/between_meals/repo/hg.rb, line 25
def setup
  @bin = 'hg'
  @cmd = BetweenMeals::Repo::Hg::Cmd.new(
    :bin => @bin,
    :cwd => @repo_path,
    :logger => @logger,
  )
end
status() click to toggle source
# File lib/between_meals/repo/hg.rb, line 128
def status
  @cmd.status.stdout
end
update() click to toggle source
# File lib/between_meals/repo/hg.rb, line 66
def update
  @cmd.pull.stdout
rescue StandardError => e
  @logger.error('Something went wrong with hg!')
  @logger.error(e)
  raise
end
upstream?(rev) click to toggle source
# File lib/between_meals/repo/hg.rb, line 132
def upstream?(rev)
  # Check if commit is an ancestor of master
  # Returns the diff if common ancestor is found,
  # returns nothing if not
  if @cmd.rev("'ancestor(master,#{rev}) & #{rev}'").stdout.empty?
    return false
  else
    return true
  end
end
valid_ref?(ref) click to toggle source
# File lib/between_meals/repo/hg.rb, line 143
def valid_ref?(ref)
  @cmd.rev(ref)
  return true
rescue StandardError
  raise Changeset::ReferenceError
end

Private Instance Methods

parse_status(changes) click to toggle source
# File lib/between_meals/repo/hg.rb, line 156
def parse_status(changes)
  # The codes used to show the status of files are:
  #
  #  M = modified
  #  A = added
  #  R = removed
  #  C = clean
  #  ! = missing (deleted by non-hg command, but still tracked)
  #  ? = not tracked
  #  I = ignored
  #    = origin of the previous file (with --copies)

  changes.lines.map do |line|
    parts = line.chomp.split(nil, 2)
    case parts[0]
    when 'A'
      {
        :status => :added,
        :path => parts[1],
      }
    when 'C'
      {
        :status => :clean,
        :path => parts[1],
      }
    when 'R'
      {
        :status => :deleted,
        :path => parts[1],
      }
    when 'M'
      {
        :status => :modified,
        :path => parts[1],
      }
    when '!'
      {
        :status => :missing,
        :path => parts[1],
      }
    when '?'
      {
        :status => :untracked,
        :path => parts[1],
      }
    when 'I'
      {
        :status => :ignored,
        :path => parts[1],
      }
    else
      fail 'Failed to parse repo diff line.'
    end
  end
end
username() click to toggle source
# File lib/between_meals/repo/hg.rb, line 152
def username
  @cmd.username.stdout.lines.first.strip.match(/^(.*?)(?:\s<(.*)>)?$/)
end