Skip to content

Commit 180895a

Browse files
author
Joe Moore
committed
Add git clean support
command: git clean --force -d - Forces a clean regardless of configuration - removes directories, too.
1 parent 9c73adf commit 180895a

File tree

3 files changed

+115
-80
lines changed

3 files changed

+115
-80
lines changed

lib/git/base.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ def revert(commitish = nil, opts = {})
270270
self.lib.revert(commitish, opts)
271271
end
272272

273+
# cleans the working directory, removing directories, too.
274+
def clean(opts = {})
275+
self.lib.clean(opts)
276+
end
277+
273278
# commits all pending changes in the index file to the git repository
274279
#
275280
# options:

lib/git/lib.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ def revert(commit_or_range, opts = {})
436436
command('revert', arr_opts)
437437
end
438438

439+
def clean(opts = {})
440+
arr_opts = ["--force"] # Some configurations require a --force
441+
arr_opts << ["-d"] # Remove untracked directories in addition to untracked files.
442+
command('clean', arr_opts)
443+
end
444+
439445
def apply(patch_file)
440446
arr_opts = []
441447
arr_opts << '--' << patch_file if patch_file

tests/units/test_index_ops.rb

Lines changed: 104 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@ def setup
99
@git = Git.open(@wdir)
1010
end
1111

12-
# def test_add
13-
# in_temp_dir do |path|
14-
# g = Git.clone(@wbare, 'new')
15-
# Dir.chdir('new') do
16-
# assert_equal('100644', g.status['example.txt'].mode_index)
17-
18-
# new_file('test-file', 'blahblahblah')
19-
# assert(g.status.untracked.assoc('test-file'))
20-
21-
# g.add
22-
# assert(g.status.added.assoc('test-file'))
23-
# assert(!g.status.untracked.assoc('test-file'))
24-
# assert(!g.status.changed.assoc('example.txt'))
25-
26-
# new_file('example.txt', 'hahahaha')
27-
# assert(g.status.changed.assoc('example.txt'))
28-
29-
# g.add
30-
# assert(g.status.changed.assoc('example.txt'))
31-
32-
# g.commit('my message')
33-
# assert(!g.status.changed.assoc('example.txt'))
34-
# assert(!g.status.added.assoc('test-file'))
35-
# assert(!g.status.untracked.assoc('test-file'))
36-
# assert_equal('hahahaha', g.status['example.txt'].blob.contents)
37-
# end
38-
# end
39-
# end
12+
def test_add
13+
in_temp_dir do |path|
14+
g = Git.clone(@wbare, 'new')
15+
Dir.chdir('new') do
16+
assert_equal('100644', g.status['example.txt'].mode_index)
17+
18+
new_file('test-file', 'blahblahblah')
19+
assert(g.status.untracked.assoc('test-file'))
20+
21+
g.add
22+
assert(g.status.added.assoc('test-file'))
23+
assert(!g.status.untracked.assoc('test-file'))
24+
assert(!g.status.changed.assoc('example.txt'))
25+
26+
new_file('example.txt', 'hahahaha')
27+
assert(g.status.changed.assoc('example.txt'))
28+
29+
g.add
30+
assert(g.status.changed.assoc('example.txt'))
31+
32+
g.commit('my message')
33+
assert(!g.status.changed.assoc('example.txt'))
34+
assert(!g.status.added.assoc('test-file'))
35+
assert(!g.status.untracked.assoc('test-file'))
36+
assert_equal('hahahaha', g.status['example.txt'].blob.contents)
37+
end
38+
end
39+
end
4040

4141
def test_revert
4242
in_temp_dir do |path|
@@ -60,57 +60,81 @@ def test_revert
6060
end
6161
end
6262

63-
# def test_add_array
64-
# in_temp_dir do |path|
65-
# g = Git.clone(@wbare, 'new')
66-
# Dir.chdir('new') do
67-
68-
# new_file('test-file1', 'blahblahblah1')
69-
# new_file('test-file2', 'blahblahblah2')
70-
# assert(g.status.untracked.assoc('test-file1'))
71-
72-
# g.add(['test-file1', 'test-file2'])
73-
# assert(g.status.added.assoc('test-file1'))
74-
# assert(g.status.added.assoc('test-file1'))
75-
# assert(!g.status.untracked.assoc('test-file1'))
76-
77-
# g.commit('my message')
78-
# assert(!g.status.added.assoc('test-file1'))
79-
# assert(!g.status.untracked.assoc('test-file1'))
80-
# assert_equal('blahblahblah1', g.status['test-file1'].blob.contents)
81-
# end
82-
# end
83-
# end
84-
85-
# def test_remove
86-
# in_temp_dir do |path|
87-
# g = Git.clone(@wbare, 'remove_test')
88-
# Dir.chdir('remove_test') do
89-
# assert(g.status['example.txt'])
90-
# g.remove('example.txt')
91-
# assert(g.status.deleted.assoc('example.txt'))
92-
# g.commit('deleted file')
93-
# assert(!g.status['example.txt'])
94-
# end
95-
# end
96-
# end
97-
98-
# def test_reset
99-
# in_temp_dir do |path|
100-
# g = Git.clone(@wbare, 'reset_test')
101-
# Dir.chdir('reset_test') do
102-
# new_file('test-file1', 'blahblahblah1')
103-
# new_file('test-file2', 'blahblahblah2')
104-
# assert(g.status.untracked.assoc('test-file1'))
105-
106-
# g.add(['test-file1', 'test-file2'])
107-
# assert(!g.status.untracked.assoc('test-file1'))
108-
109-
# g.reset
110-
# assert(g.status.untracked.assoc('test-file1'))
111-
# assert(!g.status.added.assoc('test-file1'))
112-
# end
113-
# end
114-
# end
63+
def test_clean
64+
in_temp_dir do |path|
65+
g = Git.clone(@wbare, 'clean_me')
66+
Dir.chdir('clean_me') do
67+
new_file('test-file', 'blahblahbal')
68+
g.add
69+
g.commit("first commit")
70+
71+
new_file('file-to-clean', 'blablahbla')
72+
FileUtils.mkdir_p("dir_to_clean")
73+
74+
Dir.chdir('dir_to_clean') do
75+
new_file('clean-me-too', 'blablahbla')
76+
end
77+
78+
assert(File.exists?('file-to-clean'))
79+
assert(File.exists?('dir_to_clean'))
80+
g.clean
81+
assert(!File.exists?('file-to-clean'))
82+
assert(!File.exists?('dir_to_clean'))
83+
end
84+
end
85+
end
86+
87+
def test_add_array
88+
in_temp_dir do |path|
89+
g = Git.clone(@wbare, 'new')
90+
Dir.chdir('new') do
91+
92+
new_file('test-file1', 'blahblahblah1')
93+
new_file('test-file2', 'blahblahblah2')
94+
assert(g.status.untracked.assoc('test-file1'))
95+
96+
g.add(['test-file1', 'test-file2'])
97+
assert(g.status.added.assoc('test-file1'))
98+
assert(g.status.added.assoc('test-file1'))
99+
assert(!g.status.untracked.assoc('test-file1'))
100+
101+
g.commit('my message')
102+
assert(!g.status.added.assoc('test-file1'))
103+
assert(!g.status.untracked.assoc('test-file1'))
104+
assert_equal('blahblahblah1', g.status['test-file1'].blob.contents)
105+
end
106+
end
107+
end
108+
109+
def test_remove
110+
in_temp_dir do |path|
111+
g = Git.clone(@wbare, 'remove_test')
112+
Dir.chdir('remove_test') do
113+
assert(g.status['example.txt'])
114+
g.remove('example.txt')
115+
assert(g.status.deleted.assoc('example.txt'))
116+
g.commit('deleted file')
117+
assert(!g.status['example.txt'])
118+
end
119+
end
120+
end
121+
122+
def test_reset
123+
in_temp_dir do |path|
124+
g = Git.clone(@wbare, 'reset_test')
125+
Dir.chdir('reset_test') do
126+
new_file('test-file1', 'blahblahblah1')
127+
new_file('test-file2', 'blahblahblah2')
128+
assert(g.status.untracked.assoc('test-file1'))
129+
130+
g.add(['test-file1', 'test-file2'])
131+
assert(!g.status.untracked.assoc('test-file1'))
132+
133+
g.reset
134+
assert(g.status.untracked.assoc('test-file1'))
135+
assert(!g.status.added.assoc('test-file1'))
136+
end
137+
end
138+
end
115139

116140
end

0 commit comments

Comments
 (0)