Skip to content

Commit 3ec51b6

Browse files
committed
Add git clean support
command: git clean --force -d - Forces a clean regardless of configuration - removes directories.
1 parent a720b6e commit 3ec51b6

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/git/base.rb

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

267+
268+
# cleans the working directory, removing directories, too.
269+
def clean(opts = {})
270+
self.lib.clean(opts)
271+
end
272+
267273
# commits all pending changes in the index file to the git repository
268274
#
269275
# 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 clean(opts = {})
434+
arr_opts = ["--force"] # Some configurations require a --force
435+
arr_opts << ["-d"] # Remove untracked directories in addition to untracked files.
436+
command('clean', 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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,31 @@ def test_add
3737
end
3838
end
3939
end
40-
40+
41+
def test_clean
42+
in_temp_dir do |path|
43+
g = Git.clone(@wbare, 'clean_me')
44+
Dir.chdir('clean_me') do
45+
new_file('test-file', 'blahblahbal')
46+
g.add
47+
g.commit("first commit")
48+
49+
new_file('file-to-clean', 'blablahbla')
50+
FileUtils.mkdir_p("dir_to_clean")
51+
52+
Dir.chdir('dir_to_clean') do
53+
new_file('clean-me-too', 'blablahbla')
54+
end
55+
56+
assert(File.exists?('file-to-clean'))
57+
assert(File.exists?('dir_to_clean'))
58+
g.clean
59+
assert(!File.exists?('file-to-clean'))
60+
assert(!File.exists?('dir_to_clean'))
61+
end
62+
end
63+
end
64+
4165
def test_add_array
4266
in_temp_dir do |path|
4367
g = Git.clone(@wbare, 'new')

0 commit comments

Comments
 (0)