Skip to content

Commit dcb44d2

Browse files
committed
Add git revert --no-edit support.
1 parent a720b6e commit dcb44d2

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lib/git/base.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ def reset_hard(commitish = nil, opts = {})
264264
self.lib.reset(commitish, opts)
265265
end
266266

267+
# reverts the working directory to the provided commitish.
268+
# Accepts a range, such as comittish..HEAD
269+
# Note: automatically adds --no-edit flag.
270+
def revert(commitish = nil, opts = {})
271+
self.lib.revert(commitish, opts)
272+
end
273+
267274
# commits all pending changes in the index file to the git repository
268275
#
269276
# options:

lib/git/lib.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,13 @@ def reset(commit, opts = {})
429429
arr_opts << commit if commit
430430
command('reset', arr_opts)
431431
end
432-
432+
433+
def revert(commit_or_range, opts = {})
434+
arr_opts = ["--no-edit"]
435+
arr_opts << commit_or_range if commit_or_range
436+
command('revert', arr_opts)
437+
end
438+
433439
def apply(patch_file)
434440
arr_opts = []
435441
arr_opts << '--' << patch_file if patch_file

tests/units/test_index_ops.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,29 @@ def test_add
3737
end
3838
end
3939
end
40-
40+
41+
def test_revert
42+
in_temp_dir do |path|
43+
g = Git.clone(@wbare, 'new')
44+
Dir.chdir('new') do
45+
new_file('test-file', 'blahblahbal')
46+
g.add
47+
g.commit("first commit")
48+
first_commit = g.gcommit('HEAD')
49+
50+
new_file('test-file2', 'blablahbla')
51+
g.add
52+
g.commit("second-commit")
53+
second_commit = g.gcommit('HEAD')
54+
55+
commits = g.log(1e4).count
56+
g.revert(first_commit.sha)
57+
assert_equal(commits + 1, g.log(1e4).count)
58+
assert(!File.exists?('test-file2'))
59+
end
60+
end
61+
end
62+
4163
def test_add_array
4264
in_temp_dir do |path|
4365
g = Git.clone(@wbare, 'new')

0 commit comments

Comments
 (0)