rjgit 5.6.0.0 → 5.6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -6
- data/README.md +7 -0
- data/lib/commit.rb +12 -3
- data/lib/git.rb +53 -54
- data/lib/java/jars/{org.eclipse.jgit-5.6.0.201912101111-r.jar → org.eclipse.jgit-5.6.1.202002131546-r.jar} +0 -0
- data/lib/rjgit.rb +160 -62
- data/lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11fd7309b1391fba839301750f2c1fe51d0b38abbb72a36088c39f729ad0368f
|
4
|
+
data.tar.gz: e890015d41e4408358b9d000d8d5d94a251a8dfe9a4815f75c22858f607c01b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8afbaa28cfd0a432c3d0d3ad9aa54e34a9e262bbe924d6554a3b7745abbe3025508015ccd97c8bdadae38a89a87f04ef71eb3c052ba9508129d398dfc9bdec4
|
7
|
+
data.tar.gz: fc84910f12f9c6b5a768e9b06ef7336363fa5f308192d19493c6f2ae2047f3e7032392305795dff9c1225b372ff903838d843f02b4cab6c7aefa47d49fa56ba2
|
data/Gemfile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem
|
4
|
-
gem
|
3
|
+
gem 'mime-types', '~> 2.6.2'
|
4
|
+
gem 'rake', '>= 12.3.3'
|
5
5
|
|
6
|
-
gem 'coveralls', require: false
|
6
|
+
gem 'coveralls', '~> 0.8.23', require: false
|
7
7
|
|
8
8
|
group :test do
|
9
|
-
gem
|
10
|
-
gem
|
11
|
-
gem
|
9
|
+
gem 'rspec', '~> 3.4.0'
|
10
|
+
gem 'rspec-collection_matchers', '~> 1.1.2'
|
11
|
+
gem 'simplecov'
|
12
12
|
end
|
data/README.md
CHANGED
@@ -75,6 +75,13 @@ repo.find('959329025f67539fb82e76b02782322fad032821')
|
|
75
75
|
repo.find('959329025f67539fb82e76b02782322fad032821', :commit) # Find a specific :commit, :blob, :tree, or :tag
|
76
76
|
```
|
77
77
|
|
78
|
+
### Logs
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
repo.git.log # Returns an Array of Commits constituting the log for the default branch
|
82
|
+
repo.git.log("follow-rename.txt", "HEAD", follow: true, list_renames: true) # Log for a specific path, tracking the pathname over renames. Returns an Array of TrackingCommits, which store the tracked filename: [#<RJGit::TrackingCommit:0x773014d3 @tracked_pathname="follow-rename.txt" ...>]
|
83
|
+
```
|
84
|
+
|
78
85
|
### Getting diffs
|
79
86
|
```ruby
|
80
87
|
sha1 = repo.head.id
|
data/lib/commit.rb
CHANGED
@@ -4,7 +4,7 @@ module RJGit
|
|
4
4
|
import 'org.eclipse.jgit.revwalk.RevCommit'
|
5
5
|
import 'org.eclipse.jgit.diff.DiffFormatter'
|
6
6
|
import 'org.eclipse.jgit.util.io.DisabledOutputStream'
|
7
|
-
|
7
|
+
|
8
8
|
class Commit
|
9
9
|
|
10
10
|
attr_reader :id, :parents, :actor, :committer, :authored_date, :committed_date
|
@@ -100,12 +100,12 @@ module RJGit
|
|
100
100
|
Commit.new(repository, RevWalk.new(repository).parseCommit(new_commit))
|
101
101
|
end
|
102
102
|
|
103
|
-
def self.find_head(repository)
|
103
|
+
def self.find_head(repository, ref = Constants::HEAD)
|
104
104
|
repository = RJGit.repository_type(repository)
|
105
105
|
return nil if repository.nil?
|
106
106
|
begin
|
107
107
|
walk = RevWalk.new(repository)
|
108
|
-
objhead = repository.resolve(
|
108
|
+
objhead = repository.resolve(ref)
|
109
109
|
return Commit.new(repository, walk.parseCommit(objhead))
|
110
110
|
rescue java.lang.NullPointerException => e
|
111
111
|
return nil
|
@@ -128,4 +128,13 @@ module RJGit
|
|
128
128
|
end
|
129
129
|
|
130
130
|
end
|
131
|
+
|
132
|
+
class TrackingCommit < Commit
|
133
|
+
attr_reader :tracked_pathname # This commit is part of a log for a single pathname. The tracked_pathname attribute tracks the pathname over renames.
|
134
|
+
|
135
|
+
def initialize(repository, commit, tracked_pathname = nil)
|
136
|
+
super(repository, commit)
|
137
|
+
@tracked_pathname = tracked_pathname
|
138
|
+
end
|
139
|
+
end
|
131
140
|
end
|
data/lib/git.rb
CHANGED
@@ -15,6 +15,10 @@ module RJGit
|
|
15
15
|
import 'org.eclipse.jgit.api.TransportConfigCallback'
|
16
16
|
import 'org.eclipse.jgit.transport.JschConfigSessionFactory'
|
17
17
|
import 'org.eclipse.jgit.transport.SshTransport'
|
18
|
+
import 'org.eclipse.jgit.revwalk.FollowFilter'
|
19
|
+
import 'org.eclipse.jgit.revwalk.TreeRevFilter'
|
20
|
+
|
21
|
+
class PatchApplyException < StandardError; end
|
18
22
|
|
19
23
|
class RubyGit
|
20
24
|
|
@@ -47,67 +51,58 @@ module RJGit
|
|
47
51
|
return [] unless ref
|
48
52
|
jcommits = Array.new
|
49
53
|
|
54
|
+
logs = @jgit.log
|
55
|
+
logs.add(ref)
|
56
|
+
|
50
57
|
if path && options[:follow]
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
start = jcommit
|
59
|
-
end
|
60
|
-
current_path = follow_renames(start, current_path) if start
|
61
|
-
break if current_path.nil?
|
62
|
-
end
|
58
|
+
cfg = Configuration.new(nil)
|
59
|
+
cfg.add_setting('renames', true, 'diffs', nil)
|
60
|
+
follow = FollowFilter.create(path, cfg.jconfig.get(org.eclipse.jgit.diff.DiffConfig::KEY))
|
61
|
+
logs.set_rev_filter(TreeRevFilter.new(RevWalk.new(jrepo), follow))
|
62
|
+
elsif path
|
63
|
+
logs.addPath(path)
|
64
|
+
end
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
revwalk = RevWalk.new(jrepo)
|
73
|
-
since_commit = revwalk.parseCommit(jrepo.resolve(options[:since]))
|
74
|
-
until_commit = revwalk.parseCommit(jrepo.resolve(options[:until]))
|
75
|
-
logs.addRange(since_commit, until_commit)
|
76
|
-
end
|
77
|
-
if options[:not]
|
78
|
-
revwalk = RevWalk.new(jrepo)
|
79
|
-
options[:not].each do |ref|
|
80
|
-
logs.not(revwalk.parseCommit(jrepo.resolve(ref)))
|
81
|
-
end
|
82
|
-
end
|
83
|
-
jcommits = logs.call
|
66
|
+
logs.setMaxCount(options[:max_count]) if options[:max_count]
|
67
|
+
logs.setSkip(options[:skip]) if options[:skip]
|
68
|
+
|
69
|
+
if (options[:since] && options[:until])
|
70
|
+
revwalk = RevWalk.new(jrepo)
|
71
|
+
since_commit = revwalk.parseCommit(jrepo.resolve(options[:since]))
|
72
|
+
until_commit = revwalk.parseCommit(jrepo.resolve(options[:until]))
|
73
|
+
logs.addRange(since_commit, until_commit)
|
84
74
|
end
|
85
75
|
|
86
|
-
|
87
|
-
|
76
|
+
if options[:not]
|
77
|
+
revwalk = RevWalk.new(jrepo)
|
78
|
+
options[:not].each do |ref|
|
79
|
+
logs.not(revwalk.parseCommit(jrepo.resolve(ref)))
|
80
|
+
end
|
81
|
+
end
|
88
82
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
83
|
+
if options[:follow] && options[:list_renames]
|
84
|
+
df = DiffFormatter.new(DisabledOutputStream::INSTANCE)
|
85
|
+
df.set_repository(jrepo)
|
86
|
+
df.set_context(0)
|
87
|
+
df.set_path_filter(follow)
|
88
|
+
df.set_detect_renames(true)
|
89
|
+
prev_commit = nil
|
90
|
+
pathname = path
|
91
|
+
end
|
92
|
+
|
93
|
+
commits = logs.call.map do |jcommit|
|
94
|
+
if path && options[:follow] && options[:list_renames]
|
95
|
+
entries = df.scan(jcommit, prev_commit).to_a
|
96
|
+
pathname = entries.empty? ? pathname : entries.last.get_old_path
|
97
|
+
prev_commit = jcommit
|
98
|
+
TrackingCommit.new(jrepo, jcommit, pathname)
|
99
|
+
else
|
100
|
+
Commit.new(jrepo, jcommit)
|
106
101
|
end
|
107
102
|
end
|
108
|
-
return nil
|
109
|
-
end
|
110
103
|
|
104
|
+
commits
|
105
|
+
end
|
111
106
|
|
112
107
|
def branch_list
|
113
108
|
branch = @jgit.branch_list
|
@@ -289,7 +284,11 @@ module RJGit
|
|
289
284
|
end
|
290
285
|
|
291
286
|
def apply(input_stream)
|
292
|
-
|
287
|
+
begin
|
288
|
+
apply_result = @jgit.apply.set_patch(input_stream).call
|
289
|
+
rescue Java::OrgEclipseJgitApiErrors::PatchApplyException
|
290
|
+
raise RJGit::PatchApplyException
|
291
|
+
end
|
293
292
|
updated_files = apply_result.get_updated_files
|
294
293
|
updated_files_parsed = []
|
295
294
|
updated_files.each do |file|
|
Binary file
|
data/lib/rjgit.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
module RJGit
|
2
|
-
|
3
2
|
begin
|
4
3
|
require 'java'
|
5
4
|
Dir["#{File.dirname(__FILE__)}/java/jars/*.jar"].each { |jar| require jar }
|
@@ -10,7 +9,7 @@ module RJGit
|
|
10
9
|
def self.version
|
11
10
|
VERSION
|
12
11
|
end
|
13
|
-
|
12
|
+
|
14
13
|
require 'uri'
|
15
14
|
require 'stringio'
|
16
15
|
# gem requires
|
@@ -19,15 +18,16 @@ module RJGit
|
|
19
18
|
require "#{File.dirname(__FILE__)}/rjgit_helpers.rb"
|
20
19
|
# require everything else
|
21
20
|
begin
|
22
|
-
Dir["#{File.dirname(__FILE__)}/*.rb"].each do |file|
|
21
|
+
Dir["#{File.dirname(__FILE__)}/*.rb"].each do |file|
|
23
22
|
require file
|
24
23
|
end
|
25
24
|
end
|
26
|
-
|
25
|
+
|
27
26
|
import 'org.eclipse.jgit.lib.ObjectId'
|
28
|
-
|
27
|
+
|
29
28
|
module Porcelain
|
30
|
-
|
29
|
+
|
30
|
+
import 'org.eclipse.jgit.lib.Constants'
|
31
31
|
import 'org.eclipse.jgit.api.AddCommand'
|
32
32
|
import 'org.eclipse.jgit.api.CommitCommand'
|
33
33
|
import 'org.eclipse.jgit.api.BlameCommand'
|
@@ -36,20 +36,19 @@ module RJGit
|
|
36
36
|
import 'org.eclipse.jgit.treewalk.CanonicalTreeParser'
|
37
37
|
import 'org.eclipse.jgit.diff.DiffFormatter'
|
38
38
|
|
39
|
-
|
40
39
|
# http://wiki.eclipse.org/JGit/User_Guide#Porcelain_API
|
41
40
|
def self.add(repository, file_pattern)
|
42
41
|
repository.add(file_pattern)
|
43
42
|
end
|
44
|
-
|
43
|
+
|
45
44
|
def self.commit(repository, message="")
|
46
45
|
repository.commit(message)
|
47
46
|
end
|
48
|
-
|
47
|
+
|
49
48
|
def self.object_for_tag(repository, tag)
|
50
49
|
repository.find(tag.object.name, RJGit.sym_for_type(tag.object_type))
|
51
50
|
end
|
52
|
-
|
51
|
+
|
53
52
|
# http://dev.eclipse.org/mhonarc/lists/jgit-dev/msg00558.html
|
54
53
|
def self.cat_file(repository, blob)
|
55
54
|
jrepo = RJGit.repository_type(repository)
|
@@ -65,7 +64,7 @@ module RJGit
|
|
65
64
|
bytes = jrepo.open(jblob.id).get_bytes
|
66
65
|
return bytes.to_a.pack('c*').force_encoding('UTF-8')
|
67
66
|
end
|
68
|
-
|
67
|
+
|
69
68
|
def self.ls_tree(repository, path=nil, treeish=Constants::HEAD, options={})
|
70
69
|
options = {recursive: false, print: false, io: $stdout, path_filter: nil}.merge options
|
71
70
|
jrepo = RJGit.repository_type(repository)
|
@@ -95,7 +94,7 @@ module RJGit
|
|
95
94
|
treewalk.set_recursive(options[:recursive])
|
96
95
|
treewalk.set_filter(PathFilter.create(options[:path_filter])) if options[:path_filter]
|
97
96
|
entries = []
|
98
|
-
|
97
|
+
|
99
98
|
while treewalk.next
|
100
99
|
entry = {}
|
101
100
|
mode = treewalk.get_file_mode(0)
|
@@ -107,8 +106,8 @@ module RJGit
|
|
107
106
|
end
|
108
107
|
options[:io].puts RJGit.stringify(entries) if options[:print]
|
109
108
|
entries
|
110
|
-
end
|
111
|
-
|
109
|
+
end
|
110
|
+
|
112
111
|
def self.blame(repository, file_path, options={})
|
113
112
|
options = {:print => false, :io => $stdout}.merge(options)
|
114
113
|
jrepo = RJGit.repository_type(repository)
|
@@ -130,28 +129,22 @@ module RJGit
|
|
130
129
|
options[:io].puts RJGit.stringify(blame) if options[:print]
|
131
130
|
return blame
|
132
131
|
end
|
133
|
-
|
132
|
+
|
134
133
|
def self.diff(repository, options = {})
|
135
134
|
options = {:namestatus => false, :patch => false}.merge(options)
|
136
135
|
repo = RJGit.repository_type(repository)
|
137
136
|
git = RubyGit.new(repo).jgit
|
138
137
|
diff_command = git.diff
|
139
|
-
|
138
|
+
[:old, :new].each do |which_rev|
|
139
|
+
if rev = options["#{which_rev}_rev".to_sym]
|
140
140
|
reader = repo.new_object_reader
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
diff_command.set_old_tree(old_tree_iter)
|
145
|
-
end
|
146
|
-
if options[:new_rev]
|
147
|
-
reader = repo.new_object_reader unless reader
|
148
|
-
new_tree = repo.resolve("#{options[:new_rev]}^{tree}")
|
149
|
-
new_tree_iter = CanonicalTreeParser.new
|
150
|
-
new_tree_iter.reset(reader, new_tree)
|
151
|
-
diff_command.set_new_tree(new_tree_iter)
|
141
|
+
parser = CanonicalTreeParser.new
|
142
|
+
parser.reset(reader, repo.resolve("#{rev}^{tree}"))
|
143
|
+
diff_command.send("set_#{which_rev}_tree".to_sym, parser)
|
152
144
|
end
|
145
|
+
end
|
153
146
|
diff_command.set_path_filter(PathFilter.create(options[:file_path])) if options[:file_path]
|
154
|
-
diff_command.set_show_name_and_status_only(true) if options[:namestatus]
|
147
|
+
diff_command.set_show_name_and_status_only(true) if options[:namestatus]
|
155
148
|
diff_command.set_cached(true) if options[:cached]
|
156
149
|
diff_entries = diff_command.call
|
157
150
|
diff_entries = diff_entries.to_array.to_ary
|
@@ -169,34 +162,34 @@ module RJGit
|
|
169
162
|
diff_entries = options[:patch] ? result : diff_entries.map {|entry| [entry]}
|
170
163
|
RJGit.convert_diff_entries(diff_entries)
|
171
164
|
end
|
172
|
-
|
165
|
+
|
173
166
|
end
|
174
|
-
|
167
|
+
|
175
168
|
module Plumbing
|
176
169
|
import org.eclipse.jgit.lib.Constants
|
177
|
-
|
170
|
+
|
178
171
|
class TreeBuilder
|
179
172
|
import org.eclipse.jgit.lib.FileMode
|
180
173
|
import org.eclipse.jgit.lib.TreeFormatter
|
181
|
-
|
182
|
-
|
174
|
+
import org.eclipse.jgit.patch.Patch
|
175
|
+
|
183
176
|
attr_accessor :treemap
|
184
177
|
attr_reader :log
|
185
|
-
|
178
|
+
|
186
179
|
def initialize(repository)
|
187
180
|
@jrepo = RJGit.repository_type(repository)
|
188
181
|
@treemap = {}
|
189
182
|
init_log
|
190
183
|
end
|
191
|
-
|
184
|
+
|
192
185
|
def object_inserter
|
193
186
|
@object_inserter ||= @jrepo.newObjectInserter
|
194
187
|
end
|
195
|
-
|
188
|
+
|
196
189
|
def init_log
|
197
190
|
@log = {:deleted => [], :added => [] }
|
198
191
|
end
|
199
|
-
|
192
|
+
|
200
193
|
def only_contains_deletions(hashmap)
|
201
194
|
hashmap.each do |key, value|
|
202
195
|
if value.is_a?(Hash)
|
@@ -207,7 +200,7 @@ module RJGit
|
|
207
200
|
end
|
208
201
|
true
|
209
202
|
end
|
210
|
-
|
203
|
+
|
211
204
|
def build_tree(start_tree, treemap = nil, flush = false)
|
212
205
|
existing_trees = {}
|
213
206
|
untouched_objects = {}
|
@@ -232,7 +225,7 @@ module RJGit
|
|
232
225
|
end
|
233
226
|
end
|
234
227
|
end
|
235
|
-
|
228
|
+
|
236
229
|
sorted_treemap = treemap.inject({}) {|h, (k,v)| v.is_a?(Hash) ? h["#{k}/"] = v : h[k] = v; h }.merge(untouched_objects).sort
|
237
230
|
sorted_treemap.each do |object_name, data|
|
238
231
|
case data
|
@@ -252,27 +245,27 @@ module RJGit
|
|
252
245
|
end
|
253
246
|
object_inserter.insert(formatter)
|
254
247
|
end
|
255
|
-
|
248
|
+
|
256
249
|
def write_blob(contents, flush = false)
|
257
250
|
blobid = object_inserter.insert(Constants::OBJ_BLOB, contents.to_java_bytes)
|
258
251
|
object_inserter.flush if flush
|
259
252
|
blobid
|
260
253
|
end
|
261
|
-
|
254
|
+
|
262
255
|
end
|
263
|
-
|
256
|
+
|
264
257
|
class Index
|
265
258
|
import org.eclipse.jgit.lib.CommitBuilder
|
266
|
-
|
259
|
+
|
267
260
|
attr_accessor :treemap, :current_tree
|
268
261
|
attr_reader :jrepo
|
269
|
-
|
262
|
+
|
270
263
|
def initialize(repository)
|
271
264
|
@treemap = {}
|
272
265
|
@jrepo = RJGit.repository_type(repository)
|
273
266
|
@treebuilder = TreeBuilder.new(@jrepo)
|
274
267
|
end
|
275
|
-
|
268
|
+
|
276
269
|
def add(path, data)
|
277
270
|
path = path[1..-1] if path[0] == '/'
|
278
271
|
path = path.split('/')
|
@@ -289,24 +282,24 @@ module RJGit
|
|
289
282
|
current[filename] = data
|
290
283
|
@treemap
|
291
284
|
end
|
292
|
-
|
285
|
+
|
293
286
|
def delete(path)
|
294
287
|
path = path[1..-1] if path[0] == '/'
|
295
288
|
path = path.split('/')
|
296
289
|
last = path.pop
|
297
|
-
|
290
|
+
|
298
291
|
current = self.treemap
|
299
|
-
|
292
|
+
|
300
293
|
path.each do |dir|
|
301
294
|
current[dir] ||= {}
|
302
295
|
node = current[dir]
|
303
296
|
current = node
|
304
297
|
end
|
305
|
-
|
298
|
+
|
306
299
|
current[last] = false
|
307
300
|
@treemap
|
308
301
|
end
|
309
|
-
|
302
|
+
|
310
303
|
def do_commit(message, author, parents, new_tree)
|
311
304
|
commit_builder = CommitBuilder.new
|
312
305
|
person = author.person_ident
|
@@ -323,14 +316,11 @@ module RJGit
|
|
323
316
|
@treebuilder.object_inserter.flush
|
324
317
|
result
|
325
318
|
end
|
326
|
-
|
327
|
-
def commit(message, author, parents = nil, ref =
|
328
|
-
|
329
|
-
@current_tree = @current_tree ? RJGit.tree_type(@current_tree) : @jrepo.resolve("refs/heads/#{Constants::MASTER}^{tree}")
|
330
|
-
@treebuilder.treemap = @treemap
|
331
|
-
new_tree = @treebuilder.build_tree(@current_tree)
|
319
|
+
|
320
|
+
def commit(message, author, parents = nil, ref = "refs/heads/#{Constants::MASTER}", force = false)
|
321
|
+
new_tree = build_new_tree(@treemap, "#{ref}^{tree}")
|
332
322
|
return false if @current_tree && new_tree.name == @current_tree.name
|
333
|
-
|
323
|
+
|
334
324
|
parents = parents ? parents : @jrepo.resolve(ref+"^{commit}")
|
335
325
|
new_head = do_commit(message, author, parents, new_tree)
|
336
326
|
|
@@ -341,21 +331,129 @@ module RJGit
|
|
341
331
|
ru.setRefLogIdent(author.person_ident)
|
342
332
|
ru.setRefLogMessage("commit: #{message}", false)
|
343
333
|
res = ru.update.to_string
|
344
|
-
|
345
|
-
# @treebuilder.object_inserter.release
|
334
|
+
|
346
335
|
@current_tree = new_tree
|
347
336
|
log = @treebuilder.log
|
348
337
|
@treebuilder.init_log
|
349
338
|
sha = ObjectId.to_string(new_head)
|
350
339
|
return res, log, sha
|
351
340
|
end
|
352
|
-
|
341
|
+
|
353
342
|
def self.successful?(result)
|
354
343
|
["NEW", "FAST_FORWARD", "FORCED"].include?(result)
|
355
344
|
end
|
356
345
|
|
346
|
+
private
|
347
|
+
|
348
|
+
def build_new_tree(treemap, ref)
|
349
|
+
@treebuilder.treemap = treemap
|
350
|
+
new_tree = @treebuilder.build_tree(@current_tree ? RJGit.tree_type(@current_tree) : @jrepo.resolve(ref))
|
351
|
+
end
|
352
|
+
|
353
|
+
end
|
354
|
+
|
355
|
+
class ApplyPatchToIndex < RJGit::Plumbing::Index
|
356
|
+
|
357
|
+
import org.eclipse.jgit.patch.Patch
|
358
|
+
import org.eclipse.jgit.diff.DiffEntry
|
359
|
+
|
360
|
+
ADD = org.eclipse.jgit.diff.DiffEntry::ChangeType::ADD
|
361
|
+
COPY = org.eclipse.jgit.diff.DiffEntry::ChangeType::COPY
|
362
|
+
MODIFY = org.eclipse.jgit.diff.DiffEntry::ChangeType::MODIFY
|
363
|
+
DELETE = org.eclipse.jgit.diff.DiffEntry::ChangeType::DELETE
|
364
|
+
RENAME = org.eclipse.jgit.diff.DiffEntry::ChangeType::RENAME
|
365
|
+
|
366
|
+
# Take the result of RJGit::Porcelain.diff with options[:patch] = true and return a patch String
|
367
|
+
def self.diffs_to_patch(diffs)
|
368
|
+
diffs.inject(""){|result, diff| result << diff[:patch]}
|
369
|
+
end
|
370
|
+
|
371
|
+
def initialize(repository, patch, ref = Constants::HEAD)
|
372
|
+
super(repository)
|
373
|
+
@ref = ref
|
374
|
+
@patch = Patch.new
|
375
|
+
@patch.parse(ByteArrayInputStream.new(patch.to_java_bytes))
|
376
|
+
raise_patch_apply_error unless @patch.getErrors.isEmpty()
|
377
|
+
@current_tree = Commit.find_head(@jrepo, ref).tree
|
378
|
+
end
|
379
|
+
|
380
|
+
def commit(message, author, parents = nil, force = false)
|
381
|
+
super(message, author, parents, @ref, force)
|
382
|
+
end
|
383
|
+
|
384
|
+
def build_map
|
385
|
+
raise_patch_apply_error if @patch.getFiles.isEmpty()
|
386
|
+
@patch.getFiles.each do |file_header|
|
387
|
+
case file_header.getChangeType
|
388
|
+
when ADD
|
389
|
+
add(file_header.getNewPath, apply('', file_header))
|
390
|
+
when MODIFY
|
391
|
+
add(file_header.getOldPath, apply(getData(file_header.getOldPath), file_header))
|
392
|
+
when DELETE
|
393
|
+
delete(file_header.getOldPath)
|
394
|
+
when RENAME
|
395
|
+
delete(file_header.getOldPath)
|
396
|
+
add(file_header.getNewPath, getData(file_header.getOldPath))
|
397
|
+
when COPY
|
398
|
+
add(file_header.getNewPath, getData(file_header.getOldPath))
|
399
|
+
end
|
400
|
+
end
|
401
|
+
@treemap
|
402
|
+
end
|
403
|
+
|
404
|
+
# Build the new tree based on the patch, but don't commit it
|
405
|
+
# Return the String object id of the new tree, and an Array of affected paths
|
406
|
+
def new_tree
|
407
|
+
map = build_map
|
408
|
+
return ObjectId.to_string(build_new_tree(map, @ref)), map.keys
|
409
|
+
end
|
410
|
+
|
411
|
+
private
|
412
|
+
|
413
|
+
def raise_patch_apply_error
|
414
|
+
raise ::RJGit::PatchApplyException.new('Patch failed to apply')
|
415
|
+
end
|
416
|
+
|
417
|
+
def getData(path)
|
418
|
+
begin
|
419
|
+
(@current_tree / path).data
|
420
|
+
rescue NoMethodError
|
421
|
+
raise_patch_apply_error
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
def hunk_sanity_check(hunk, hunk_line, pos, newLines)
|
426
|
+
raise_patch_apply_error unless newLines[hunk.getNewStartLine - 1 + pos] == hunk_line[1..-1]
|
427
|
+
end
|
428
|
+
|
429
|
+
def apply(original, file_header)
|
430
|
+
newLines = original.lines
|
431
|
+
file_header.getHunks.each do |hunk|
|
432
|
+
length = hunk.getEndOffset - hunk.getStartOffset
|
433
|
+
buffer_text = hunk.getBuffer.to_s.slice(hunk.getStartOffset, length)
|
434
|
+
pos = 0
|
435
|
+
buffer_text.each_line do |hunk_line|
|
436
|
+
case hunk_line[0]
|
437
|
+
when ' '
|
438
|
+
hunk_sanity_check(hunk, hunk_line, pos, newLines)
|
439
|
+
pos += 1
|
440
|
+
when '-'
|
441
|
+
if hunk.getNewStartLine == 0
|
442
|
+
newLines = []
|
443
|
+
else
|
444
|
+
hunk_sanity_check(hunk, hunk_line, pos, newLines)
|
445
|
+
newLines.slice!(hunk.getNewStartLine - 1 + pos)
|
446
|
+
end
|
447
|
+
when '+'
|
448
|
+
newLines.insert(hunk.getNewStartLine - 1 + pos, hunk_line[1..-1])
|
449
|
+
pos += 1
|
450
|
+
end
|
451
|
+
end
|
452
|
+
end
|
453
|
+
newLines.join
|
454
|
+
end
|
357
455
|
end
|
358
|
-
|
456
|
+
|
359
457
|
end
|
360
|
-
|
458
|
+
|
361
459
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rjgit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.6.
|
4
|
+
version: 5.6.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maarten Engelen
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2020-03-14 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
@@ -49,7 +49,7 @@ files:
|
|
49
49
|
- lib/java/jars/bcpkix-jdk15on-161.jar
|
50
50
|
- lib/java/jars/bcprov-jdk15on-161.jar
|
51
51
|
- lib/java/jars/jsch-0.1.54.jar
|
52
|
-
- lib/java/jars/org.eclipse.jgit-5.6.
|
52
|
+
- lib/java/jars/org.eclipse.jgit-5.6.1.202002131546-r.jar
|
53
53
|
- lib/java/jars/slf4j-api-1.7.2.jar
|
54
54
|
- lib/java/jars/slf4j-simple-1.7.12.jar
|
55
55
|
- lib/repo.rb
|