Class: RJGit::RubyGit

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

Constant Summary collapse

RESET_MODES =
["HARD", "SOFT", "KEEP", "MERGE", "MIXED"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ RubyGit

Returns a new instance of RubyGit.



25
26
27
28
# File 'lib/git.rb', line 25

def initialize(repository)
  @jrepo = RJGit.repository_type(repository)
  @jgit = Git.new(@jrepo)
end

Instance Attribute Details

#jgitObject

Returns the value of attribute jgit.



18
19
20
# File 'lib/git.rb', line 18

def jgit
  @jgit
end

#jrepoObject

Returns the value of attribute jrepo.



19
20
21
# File 'lib/git.rb', line 19

def jrepo
  @jrepo
end

Class Method Details

.clone(remote, local, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/git.rb', line 111

def self.clone(remote, local, options = {})
  clone_command = Git.clone_repository
  clone_command.setURI(remote)
  clone_command.set_directory(java.io.File.new(local))
  clone_command.set_bare(true) if options[:is_bare]
  if options[:branch]
    if options[:branch] == :all
      clone_command.set_clone_all_branches(true)
    else
      clone_command.set_branch(options[:branch])
    end
  end
  if options[:username]
    clone_command.set_credentials_provider(UsernamePasswordCredentialsProvider.new(options[:username], options[:password]))
  end
  clone_command.call
  Repo.new(local)
end

Instance Method Details

#add(file_pattern) ⇒ Object



130
131
132
# File 'lib/git.rb', line 130

def add(file_pattern)
  @jgit.add.add_filepattern(file_pattern).call
end

#apply(input_stream) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/git.rb', line 201

def apply(input_stream)
  apply_result = @jgit.apply.set_patch(input_stream).call
  updated_files = apply_result.get_updated_files
  updated_files_parsed = []
  updated_files.each do |file|
    updated_files_parsed << file.get_absolute_path
  end
  updated_files_parsed
end

#apply_file(patch_file) ⇒ Object



216
217
218
219
# File 'lib/git.rb', line 216

def apply_file(patch_file)
  input_stream = FileInputStream.new(patch_file)
  apply(input_stream)
end

#apply_patch(patch_content) ⇒ Object



211
212
213
214
# File 'lib/git.rb', line 211

def apply_patch(patch_content)
  input_stream = ByteArrayInputStream.new(patch_content.to_java_bytes)
  apply(input_stream)
end

#branch_listObject



86
87
88
89
90
91
92
93
# File 'lib/git.rb', line 86

def branch_list
  branch = @jgit.branch_list
  array = Array.new
  branch.call.each do |b|
    array << b.get_name
  end
  array
end

#checkout(branch_name = "master", options = {}) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/git.rb', line 183

def checkout(branch_name = "master", options = {})
  checkout_command = @jgit.checkout.set_name(branch_name)
  checkout_command.set_start_point(options[:commit])
  options[:paths].each {|path| checkout_command.add_path(path)} if options[:paths]
  checkout_command.set_create_branch(true) if options[:create]
  checkout_command.set_force(true) if options[:force]
  result = {}
  begin
    checkout_command.call
    result[:success] = true
    result[:result] = @jgit.get_repository.get_full_branch
  rescue Java::OrgEclipseJgitApiErrors::CheckoutConflictException => conflict
    result[:success] = false
    result[:result] = conflict.get_conflicting_paths
  end
  result
end

#clean(options = {}) ⇒ Object



221
222
223
224
225
226
# File 'lib/git.rb', line 221

def clean(options = {})
  clean_command = @jgit.clean
  clean_command.set_dry_run(true) if options[:dryrun]
  clean_command.set_paths(java.util.Arrays.asList(options[:paths])) if options[:paths]
  clean_command.call
end

#clone(remote, local, options = {}) ⇒ Object



107
108
109
# File 'lib/git.rb', line 107

def clone(remote, local, options = {})
  RubyGit.clone(remote, local, options)
end

#commit(message, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/git.rb', line 95

def commit(message, options = {})
  commit_cmd = @jgit.commit.set_message(message)
  commit_cmd.set_all(options[:all]) unless options[:all].nil?
  commit_cmd.set_amend(options[:amend]) unless options[:amend].nil?
  commit_cmd.set_author(options[:author].person_ident) unless options[:author].nil?
  commit_cmd.set_committer(options[:committer].person_ident) unless options[:committer].nil?
  commit_cmd.set_insert_change_id(options[:insert_change_id]) unless options[:insert_change_id].nil?
  options[:only_paths].each {|path| commit_cmd.set_only(path)} unless options[:only_paths].nil?
  commit_cmd.set_reflog_comment(options[:reflog_comment]) unless options[:reflog_comment].nil?
  Commit.new(jrepo, commit_cmd.call)
end

#create_branch(name) ⇒ Object



171
172
173
# File 'lib/git.rb', line 171

def create_branch(name)
  @jgit.branch_create.setName(name).call
end

#delete_branch(name) ⇒ Object



175
176
177
# File 'lib/git.rb', line 175

def delete_branch(name)
  @jgit.branch_delete.set_branch_names(name).call
end

#follow_renames(jcommit, path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/git.rb', line 63

def follow_renames(jcommit, path)
  commits = @jgit.log.add(jcommit).call
  commits.each do |jcommit_prev|
    tree_start = jcommit.getTree
    tree_prev  = jcommit_prev.getTree
    treewalk = TreeWalk.new(jrepo)
    #treewalk.setFilter(PathFilter.create(File.dirname(path)))
    treewalk.addTree(tree_prev)
    treewalk.addTree(tree_start)
    treewalk.setRecursive(true)
    rename_detector = RenameDetector.new(jrepo)
    rename_detector.addAll(DiffEntry.scan(treewalk))
    diff_entries = rename_detector.compute
    diff_entries.each do |entry|
      if ((entry.getChangeType == DiffEntry::ChangeType::RENAME || entry.getChangeType == DiffEntry::ChangeType::COPY) && entry.getNewPath.match(path))
        return entry.getOldPath
      end
    end
  end
  return nil
end

#log(path = nil, revstring = Constants::HEAD, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/git.rb', line 30

def log(path = nil, revstring = Constants::HEAD, options = {})
  ref = jrepo.resolve(revstring)
  jcommits = Array.new
  
  if path && options[:follow]
    current_path = path    
    start = nil
    loop do
      logs = @jgit.log.add(ref).addPath(current_path).call
      logs.each do |jcommit|
        next if jcommits.include?(jcommit)
        jcommits << jcommit
        start = jcommit
      end
      current_path = follow_renames(start, current_path)
      break if current_path.nil?
    end
    
  else      
    logs = @jgit.log
    logs.add(ref)
    logs.addPath(path) if path
    logs.setMaxCount(options[:max_count]) if options[:max_count]
    logs.setSkip(options[:skip]) if options[:skip]
    # These options need tests
    # logs.addRange(options[:since], options[:until]) if (options[:since] && options[:until])
    # logs.not(options[:not]) if options[:not]
    jcommits = logs.call
  end
  
  jcommits.map{ |jcommit| Commit.new(jrepo, jcommit) }
end

#merge(commit) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/git.rb', line 138

def merge(commit)
  merge_command = @jgit.merge
  merge_command.include(commit.jcommit)
  result = merge_command.call
  if result.get_merge_status.to_string == 'Conflicting'
    return result.get_conflicts.to_hash.keys
  else
    return result
  end
end

#pull(remote = nil, remote_ref = nil, options = {}) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
# File 'lib/git.rb', line 279

def pull(remote = nil, remote_ref = nil, options = {})
  pull_command = @jgit.pull
  pull_command.set_dry_run(true) if options[:dryrun]
  pull_command.set_rebase(options[:rebase]) if options[:rebase]
  pull_command.set_remote(remote) if remote
  pull_command.set_remote_branch_name(remote_ref) if remote_ref
  if options[:username]
    pull_command.set_credentials_provider(UsernamePasswordCredentialsProvider.new(options[:username], options[:password]))
  end
  pull_command.call
end

#push(remote = nil, refs = [], options = {}) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/git.rb', line 265

def push(remote = nil, refs = [], options = {})
  push_command = @jgit.push
  push_command.set_dry_run(true) if options[:dryrun]
  push_command.set_remote(remote) if remote
  if(refs.to_a.size > 0)
    refs.map!{|ref| RefSpec.new(ref)}
    push_command.set_ref_specs(refs)
  end
  if options[:username]
    push_command.set_credentials_provider(UsernamePasswordCredentialsProvider.new(options[:username], options[:password]))
  end
  push_command.call
end

#push_all(remote, options = {}) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/git.rb', line 253

def push_all(remote, options = {})
  push_command = @jgit.push
  push_command.set_dry_run(true) if options[:dryrun]
  push_command.set_remote(remote)
  push_command.set_push_all
  push_command.set_push_tags
  if options[:username]
    push_command.set_credentials_provider(UsernamePasswordCredentialsProvider.new(options[:username], options[:password]))
  end
  push_command.call
end

#remove(file_pattern) ⇒ Object



134
135
136
# File 'lib/git.rb', line 134

def remove(file_pattern)
  @jgit.rm.add_filepattern(file_pattern).call
end

#rename_branch(old_name, new_name) ⇒ Object



179
180
181
# File 'lib/git.rb', line 179

def rename_branch(old_name, new_name)
  @jgit.branch_rename.set_old_name(old_name).set_new_name(new_name).call
end

#reset(ref, mode = "HARD", paths = nil) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/git.rb', line 228

def reset(ref, mode = "HARD", paths = nil)
  return nil if mode != nil && !RESET_MODES.include?(mode)
  reset_command = @jgit.reset
  if paths then
    paths.each do |path|
      reset_command.addPath(path)
    end
  end
  reset_command.setRef(ref.id)
  reset_command.setMode(org.eclipse.jgit.api.ResetCommand::ResetType.valueOf(mode)) unless mode == nil
  reset_command.call
end

#resolve_tag(tagref) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/git.rb', line 162

def resolve_tag(tagref)
  begin
    walk = RevWalk.new(@jrepo)
    walk.parse_tag(tagref.get_object_id)
  rescue Java::OrgEclipseJgitErrors::IncorrectObjectTypeException
    nil
  end
end

#revert(commits) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/git.rb', line 241

def revert(commits)
  revert_command = @jgit.revert
  commits.each do |commit|
    revert_command.include commit.jcommit
  end
  Commit.new(jrepo, revert_command.call)
end

#statusObject



249
250
251
# File 'lib/git.rb', line 249

def status
  @jgit.status.call
end

#tag(name, message = "", commit_or_revision = nil, actor = nil, force = false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/git.rb', line 149

def tag(name, message = "", commit_or_revision = nil, actor = nil, force = false)
  tag_command = @jgit.tag
  tag_command.set_name(name)
  tag_command.set_force_update(force)
  tag_command.set_message(message)
  tag_command.set_object_id(RJGit.commit_type(commit_or_revision)) if commit_or_revision
  if actor
    actor = RJGit.actor_type(actor)
    tag_command.set_tagger(actor)
  end
  tag_command.call
end