Skip to content

Commit c162d15

Browse files
authored
Merge pull request #798 from ccutrer/commit_diff_default_args
fix default arguments for Commit#diff when nothing is passed
2 parents 6b97949 + b330919 commit c162d15

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

lib/rugged/commit.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ def header_field?(field)
1919

2020
# Return a diff between this commit and its first parent or another commit or tree.
2121
#
22+
# The commit is treated as the new side of the diff by default
23+
#
2224
# See Rugged::Tree#diff for more details.
2325
def diff(*args)
24-
args.unshift(parents.first) if args.size == 1 && args.first.is_a?(Hash)
25-
self.tree.diff(*args)
26+
raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0..2") if args.length > 2
27+
other, opts = args
28+
if other.is_a?(Hash)
29+
opts = other
30+
other = nil
31+
end
32+
opts ||= {}
33+
# if other is not provided at all (as opposed to explicitly nil, or given)
34+
# then diff against the prior commit
35+
if args.empty? || args.first.is_a?(Hash)
36+
other = parents.first
37+
opts[:reverse] = !opts[:reverse]
38+
end
39+
self.tree.diff(other, opts)
2640
end
2741

2842
# Return a diff between this commit and the workdir.

test/diff_test.rb

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,50 @@ def test_diff_with_parent
217217
assert_equal 5, deltas.size
218218
assert_equal 5, patches.size
219219

220-
assert_equal 0, deltas.select(&:added?).size
221-
assert_equal 3, deltas.select(&:deleted?).size
220+
assert_equal 3, deltas.select(&:added?).size
221+
assert_equal 0, deltas.select(&:deleted?).size
222222
assert_equal 2, deltas.select(&:modified?).size
223223

224224
assert_equal 4, hunks.size
225225

226226
assert_equal(51, lines.size)
227227
assert_equal(2, lines.select(&:context?).size)
228-
assert_equal(3, lines.select(&:addition?).size)
229-
assert_equal(46, lines.select(&:deletion?).size)
228+
assert_equal(46, lines.select(&:addition?).size)
229+
assert_equal(3, lines.select(&:deletion?).size)
230+
end
231+
232+
def test_diff_with_parent_no_options
233+
repo = FixtureRepo.from_libgit2("attr")
234+
commit = Rugged::Commit.lookup(repo, "605812a")
235+
236+
diff = commit.diff
237+
238+
deltas = diff.deltas
239+
patches = diff.patches
240+
hunks = patches.map(&:hunks).flatten
241+
lines = hunks.map(&:lines).flatten
242+
243+
assert_equal 5, diff.size
244+
assert_equal 5, deltas.size
245+
assert_equal 5, patches.size
246+
247+
assert_equal 3, deltas.select(&:added?).size
248+
assert_equal 0, deltas.select(&:deleted?).size
249+
assert_equal 2, deltas.select(&:modified?).size
250+
251+
assert_equal 4, hunks.size
252+
253+
assert_equal(53, lines.size)
254+
assert_equal(4, lines.select(&:context?).size)
255+
assert_equal(46, lines.select(&:addition?).size)
256+
assert_equal(3, lines.select(&:deletion?).size)
230257
end
231258

232259
def test_diff_with_parent_for_initial_commit
233260
repo = FixtureRepo.from_libgit2("attr")
234261
commit = Rugged::Commit.lookup(repo, "6bab5c79cd5140d0f800917f550eb2a3dc32b0da")
235262

236-
diff = commit.diff(:context_lines => 1, :interhunk_lines => 1, :reverse => true)
263+
diff = commit.diff(:context_lines => 1, :interhunk_lines => 1)
237264

238265
deltas = diff.deltas
239266
patches = diff.patches

0 commit comments

Comments
 (0)