Class: RJGit::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/commit.rb

Direct Known Subclasses

TrackingCommit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, commit) ⇒ Commit

Returns a new instance of Commit.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/commit.rb', line 17

def initialize(repository, commit)
  @jrepo = RJGit.repository_type(repository)
  @jcommit = commit
  @id = ObjectId.to_string(commit.get_id)
  @actor = Actor.new_from_person_ident(@jcommit.get_author_ident)
  @committer = Actor.new_from_person_ident(@jcommit.get_committer_ident)
  @committed_date = Time.at(@jcommit.commit_time)
  @authored_date = Time.at(@jcommit.get_author_ident.when.time/1000)
  @message = @jcommit.get_full_message
  @short_message = @jcommit.get_short_message
  @parent_count = @jcommit.get_parent_count
end

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



11
12
13
# File 'lib/commit.rb', line 11

def actor
  @actor
end

#authored_dateObject (readonly)

Returns the value of attribute authored_date.



11
12
13
# File 'lib/commit.rb', line 11

def authored_date
  @authored_date
end

#committed_dateObject (readonly)

Returns the value of attribute committed_date.



11
12
13
# File 'lib/commit.rb', line 11

def committed_date
  @committed_date
end

#committerObject (readonly)

Returns the value of attribute committer.



11
12
13
# File 'lib/commit.rb', line 11

def committer
  @committer
end

#idObject (readonly) Also known as: get_name

Returns the value of attribute id.



11
12
13
# File 'lib/commit.rb', line 11

def id
  @id
end

#jcommitObject (readonly)

Returns the value of attribute jcommit.



12
13
14
# File 'lib/commit.rb', line 12

def jcommit
  @jcommit
end

#messageObject (readonly)

Returns the value of attribute message.



12
13
14
# File 'lib/commit.rb', line 12

def message
  @message
end

#parent_countObject (readonly)

Returns the value of attribute parent_count.



12
13
14
# File 'lib/commit.rb', line 12

def parent_count
  @parent_count
end

#parentsObject (readonly)

Returns the value of attribute parents.



11
12
13
# File 'lib/commit.rb', line 11

def parents
  @parents
end

#short_messageObject (readonly)

Returns the value of attribute short_message.



12
13
14
# File 'lib/commit.rb', line 12

def short_message
  @short_message
end

Class Method Details

.find_all(repository, ref, options) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/commit.rb', line 130

def self.find_all(repository, ref, options)
  repository = RJGit.repository_type(repository)
  return nil if repository.nil?
  begin
    walk = RevWalk.new(repository)
    objhead = repository.resolve(ref)
    root = walk.parse_commit(objhead)
    walk.mark_start(root)
    commits = walk.map { |commit| Commit.new(repository, commit) }
    return commits.first(options[:limit])
  rescue java.lang.NullPointerException => e
    return Array.new
  end
end

.find_head(repository, ref = Constants::HEAD) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/commit.rb', line 118

def self.find_head(repository, ref = Constants::HEAD)
  repository = RJGit.repository_type(repository)
  return nil if repository.nil?
  begin
    walk = RevWalk.new(repository)
    objhead = repository.resolve(ref)
    return Commit.new(repository, walk.parseCommit(objhead))
  rescue java.lang.NullPointerException => e
    return nil
  end
end

.new_with_tree(repository, tree, message, actor, parents = nil) ⇒ Object

Pass an empty array for parents if the commit should have no parents



111
112
113
114
115
116
# File 'lib/commit.rb', line 111

def self.new_with_tree(repository, tree, message, actor, parents = nil)
  repository = RJGit.repository_type(repository)
  parents = parents ? parents : repository.resolve("refs/heads/#{Constants::MASTER}")
  new_commit = RJGit::Plumbing::Index.new(repository).do_commit(message, actor, parents, tree)
  Commit.new(repository, RevWalk.new(repository).parseCommit(new_commit))
end

Instance Method Details

#diff(options = {}) ⇒ Object



92
93
94
# File 'lib/commit.rb', line 92

def diff(options = {})
  self.diffs(options).join
end

#diffs(options = {context: 2}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/commit.rb', line 96

def diffs(options = {context: 2})
  out_stream = ByteArrayOutputStream.new
  formatter = DiffFormatter.new(out_stream)
  formatter.set_repository(@jrepo)
  formatter.set_context(options[:context])
  parent_commit = @jcommit.parent_count > 0 ? @jcommit.get_parents.first : nil
  entries = formatter.scan(parent_commit, @jcommit)
  entries.each do |entry|
    formatter.format(entry)
    out_stream.write('custom_git_delimiter'.to_java_bytes)
  end
  out_stream.to_s.split('custom_git_delimiter')
end

#note(ref = Note::DEFAULT_REF) ⇒ Object



34
35
36
37
# File 'lib/commit.rb', line 34

def note(ref=Note::DEFAULT_REF)
  jnote = Git.new(@jrepo).notes_show().set_notes_ref(ref).setObjectId(@jcommit).call()
  jnote ? Note.new(@jrepo, jnote) : nil
end

#note=(message, ref = Note::DEFAULT_REF) ⇒ Object



39
40
41
# File 'lib/commit.rb', line 39

def note=(message, ref=Note::DEFAULT_REF)
  Note.new(@jrepo, Git.new(@jrepo).notes_add.set_message(message).set_notes_ref(ref).setObjectId(@jcommit).call())
end

#remove_note(ref = Note::DEFAULT_REF) ⇒ Object



43
44
45
46
# File 'lib/commit.rb', line 43

def remove_note(ref=Note::DEFAULT_REF)
  Git.new(@jrepo).notes_remove.set_notes_ref(ref).setObjectId(@jcommit).call()
  return nil
end

#statsObject



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
# File 'lib/commit.rb', line 54

def stats
  df = DiffFormatter.new(DisabledOutputStream::INSTANCE)
  df.set_repository(@jrepo)
  df.set_context(0)
  df.set_detect_renames(true)
  parent_commit = @jcommit.parent_count > 0 ? @jcommit.get_parents[0] : nil
  entries = df.scan(parent_commit, @jcommit)

  results = []
  total_del = 0
  total_ins = 0
  entries.each do |entry|
    file = df.toFileHeader(entry)
    del = 0
    ins = 0
    file.getHunks.each do |hunk|
        hunk.toEditList.each do |edit|
          del += edit.getEndA - edit.getBeginA
          ins += edit.getEndB - edit.getBeginB
        end
    end
    total_del += del
    total_ins += ins
    results << {
      :new_file => file.getNewPath,
      :old_file => file.getOldPath,
      :new_additions => ins,
      :new_deletions => del,
      :changes => ins + del
    }
  end
  return {
    :total_additions => total_ins,
    :total_deletions => total_del,
    :files => results
  }
end

#treeObject



30
31
32
# File 'lib/commit.rb', line 30

def tree
  @tree ||= Tree.new(@jrepo, TREE_TYPE, nil, @jcommit.get_tree)
end