Class: Mercurial::Commit

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mercurial-ruby/commit.rb

Overview

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, CommitFactory is responsible for assembling instances of Commit. For the list of all possible commit-related operations check CommitFactory.

For general information on Mercurial commits:

mercurial.selenic.com/wiki/Commit

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#hg, #hg_to_array, #shell

Constructor Details

#initialize(repository, opts = {}) ⇒ Commit

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mercurial-ruby/commit.rb', line 50

def initialize(repository, opts={}) #:nodoc:
  @repository     = repository
  @hash_id        = opts[:hash_id]
  @author         = opts[:author]
  @author_email   = opts[:author_email]
  @date           = Time.iso8601(opts[:date])
  @message        = opts[:message]
  @changed_files  = files_to_array(opts[:changed_files])
  @branch_name    = opts[:branch_name]
  @tags_names     = tags_to_array(opts[:tags_names])
  @parents_ids    = parents_to_array(opts[:parents])
end

Instance Attribute Details

#authorObject (readonly)

Name of the user committed the change.



25
26
27
# File 'lib/mercurial-ruby/commit.rb', line 25

def author
  @author
end

#author_emailObject (readonly)

Email of the user committed the change.



28
29
30
# File 'lib/mercurial-ruby/commit.rb', line 28

def author_email
  @author_email
end

#branch_nameObject (readonly)

Name of a branch associated with the commit.



40
41
42
# File 'lib/mercurial-ruby/commit.rb', line 40

def branch_name
  @branch_name
end

#changed_filesObject (readonly)

Array of ChangedFile objects.



37
38
39
# File 'lib/mercurial-ruby/commit.rb', line 37

def changed_files
  @changed_files
end

#dateObject (readonly)

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



31
32
33
# File 'lib/mercurial-ruby/commit.rb', line 31

def date
  @date
end

#hash_idObject (readonly) Also known as: id

Mercurial changeset ID. 40-chars long SHA1 hash.



22
23
24
# File 'lib/mercurial-ruby/commit.rb', line 22

def hash_id
  @hash_id
end

#messageObject (readonly)

Full commit message, with line breaks and other stuff.



34
35
36
# File 'lib/mercurial-ruby/commit.rb', line 34

def message
  @message
end

#parents_idsObject (readonly)

Array of commit’s parents.



46
47
48
# File 'lib/mercurial-ruby/commit.rb', line 46

def parents_ids
  @parents_ids
end

#repositoryObject (readonly)

Instance of Repository.



19
20
21
# File 'lib/mercurial-ruby/commit.rb', line 19

def repository
  @repository
end

#tags_namesObject (readonly)

Array of commit’s tags.



43
44
45
# File 'lib/mercurial-ruby/commit.rb', line 43

def tags_names
  @tags_names
end

Instance Method Details

#ancestorsObject



83
84
85
# File 'lib/mercurial-ruby/commit.rb', line 83

def ancestors
  repository.commits.ancestors_of(hash_id)
end

#blank?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mercurial-ruby/commit.rb', line 67

def blank?
  hash_id == '0'*40
end

#diffs(cmd_options = {}) ⇒ Object



71
72
73
# File 'lib/mercurial-ruby/commit.rb', line 71

def diffs(cmd_options={})
  repository.diffs.for_commit(self, cmd_options)
end

#exist_in_branchesObject



91
92
93
# File 'lib/mercurial-ruby/commit.rb', line 91

def exist_in_branches
  repository.branches.for_commit(hash_id)
end

#merge?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/mercurial-ruby/commit.rb', line 63

def merge?
  parents.size > 1
end

#parent_idObject



87
88
89
# File 'lib/mercurial-ruby/commit.rb', line 87

def parent_id
  parents_ids.first
end

#parentsObject



75
76
77
# File 'lib/mercurial-ruby/commit.rb', line 75

def parents
  repository.commits.by_hash_ids(parents_ids)
end

#short_hash_idObject



95
96
97
# File 'lib/mercurial-ruby/commit.rb', line 95

def short_hash_id
  hash_id.to_s[0,12]
end

#stats(cmd_options = {}) ⇒ Object

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



102
103
104
105
106
107
108
109
110
111
112
# 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_hashObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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_idsObject



79
80
81
# File 'lib/mercurial-ruby/commit.rb', line 79

def trivial_parents_ids
  hg(["parents -r ? --template '{node}\n'", hash_id]).split("\n")
end