class Mercurial::Commit

The class represents Mercurial changeset. Obtained by running an +hg log+ command. Contains a lot of information, including commit's ID, author name, email, list of changed files, etc.

The class represents Commit object itself, {Mercurial::CommitFactory CommitFactory} is responsible for assembling instances of Commit. For the list of all possible commit-related operations check {Mercurial::CommitFactory CommitFactory}.

For general information on Mercurial commits:

mercurial.selenic.com/wiki/Commit

Attributes

author[R]

Name of the user committed the change.

author_email[R]

Email of the user committed the change.

branch_name[R]

Name of a branch associated with the commit.

changed_files[R]

Array of {Mercurial::ChangedFile ChangedFile} objects.

date[R]

Exact date and time of the commit. Contains Ruby Time object.

hash_id[R]

Mercurial changeset ID. 40-chars long SHA1 hash.

id[R]

Mercurial changeset ID. 40-chars long SHA1 hash.

message[R]

Full commit message, with line breaks and other stuff.

parents_ids[R]

Array of commit's parents.

repository[R]

Instance of {Mercurial::Repository Repository}.

tags_names[R]

Array of commit's tags.

Public Instance Methods

ancestors() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 83
def ancestors
  repository.commits.ancestors_of(hash_id)
end
blank?() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 67
def blank?
  hash_id == '0'*40
end
diffs(cmd_options={}) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 71
def diffs(cmd_options={})
  repository.diffs.for_commit(self, cmd_options)
end
exist_in_branches() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 91
def exist_in_branches
  repository.branches.for_commit(hash_id)
end
merge?() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 63
def merge?
  parents.size > 1
end
parent_id() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 87
def parent_id
  parents_ids.first
end
parents() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 75
def parents
  repository.commits.by_hash_ids(parents_ids)
end
short_hash_id() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 95
def short_hash_id
  hash_id.to_s[0,12]
end
stats(cmd_options={}) click to toggle source

Returns a Hash of diffstat-style summary of changes for the commit.

# File lib/mercurial-ruby/commit.rb, line 102
def stats(cmd_options={})
  raw = hg(["log -r ? --stat --template '{node}\n'", hash_id], cmd_options)
  result = raw.scan(/(\d+) files changed, (\d+) insertions\(\+\), (\d+) deletions\(\-\)$/).flatten.map{|r| r.to_i}
  return {} if result.empty? # that commit has no stats
  {
    'files'     => result[0],
    'additions' => result[1],
    'deletions' => result[2],
    'total'     => result[1] + result[2]
  }
end
to_hash() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 114
def to_hash
  {
    'id'       => hash_id,
    'parents'  => parents_ids,
    'branch'   => branch_name,
    'tags'     => tags_names,
    'message'  => message,
    'date'     => date,
    'author'   => {
      'name'  => author,
      'email' => author_email
    }
  }
end
trivial_parents_ids() click to toggle source
# File lib/mercurial-ruby/commit.rb, line 79
def trivial_parents_ids
  hg(["parents -r ? --template '{node}\n'", hash_id]).split("\n")
end

Protected Instance Methods

files_to_array(array) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 131
def files_to_array(array)
  [].tap do |returning|
    array.each do |files|
      if files
        files.split(';').map do |file_with_mode|
          returning << Mercurial::ChangedFileFactory.new_from_hg(file_with_mode)
        end
      end
    end
    
    remove_files_duplicates(returning)
  end
end
hg_hash_to_hash_id(hg_hash) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 173
def hg_hash_to_hash_id(hg_hash)
  hg_hash.split(':').last
end
parents_to_array(string) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 155
def parents_to_array(string)
  string_to_array(string) do |returning|
    string.split(' ').map do |hg_hash|
      returning << hg_hash_to_hash_id(hg_hash)
    end
  end
end
remove_files_duplicates(files) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 145
def remove_files_duplicates(files)
  Mercurial::ChangedFileFactory.delete_hg_artefacts(files)
end
string_to_array(string, &block) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 163
def string_to_array(string, &block)
  if string && !string.empty?
    [].tap do |returning|
      block.call(returning)
    end
  else
    []
  end
end
tags_to_array(tags_str) click to toggle source
# File lib/mercurial-ruby/commit.rb, line 149
def tags_to_array(tags_str)
  string_to_array(tags_str) do |returning|
    returning << tags_str
  end
end