Skip to content

Commit 070381b

Browse files
author
scott Chacon
committed
added remove and reset
1 parent 440ec51 commit 070381b

File tree

5 files changed

+154
-4
lines changed

5 files changed

+154
-4
lines changed

EXAMPLES

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,27 @@ g.config('user.email', 'email@email.com')
7979

8080
g.add('.')
8181
g.add([file1, file2])
82+
83+
g.remove('file.txt')
84+
g.remove(['file.txt', 'file2.txt'])
8285

8386
g.commit('message')
8487
g.commit_all('message')
8588

86-
87-
***** IMPLEMENTED *****
88-
89-
g.remove('file.txt').and_file
89+
g = Git.clone(repo, 'myrepo')
90+
Dir.chdir('myrepo') do
91+
new_file('test-file', 'blahblahblah')
92+
g.status.untracked.each do |file|
93+
puts file.blob(:index).contents
94+
end
95+
end
9096

9197
g.reset # defaults to HEAD
9298
g.reset_hard(Git::Commit)
9399

100+
101+
***** IMPLEMENTED *****
102+
94103
g.branch('new_branch')
95104
g.branch('new_branch').delete
96105

lib/git/base.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ def add(path = '.')
141141
self.lib.add(path)
142142
end
143143

144+
def remove(path = '.', opts = {})
145+
self.lib.remove(path, opts)
146+
end
147+
148+
def reset(commitish = nil, opts = {})
149+
self.lib.reset(commitish, opts)
150+
end
151+
152+
def reset_hard(commitish = nil, opts = {})
153+
opts = {:hard => true}.merge(opts)
154+
self.lib.reset(path, opts)
155+
end
156+
144157
def commit(message, opts = {})
145158
self.lib.commit(message, opts)
146159
end

lib/git/lib.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,29 @@ def add(path = '.')
212212
command('add', path)
213213
end
214214

215+
def remove(path = '.', opts = {})
216+
path = path.join(' ') if path.is_a?(Array)
217+
218+
arr_opts = ['-f'] # overrides the up-to-date check by default
219+
arr_opts << ['-r'] if opts[:recursive]
220+
arr_opts << path
221+
222+
command('rm', arr_opts)
223+
end
224+
215225
def commit(message, opts = {})
216226
arr_opts = ["-m '#{message}'"]
217227
arr_opts << '-a' if opts[:add_all]
218228
command('commit', arr_opts)
219229
end
230+
231+
def reset(commit, opts = {})
232+
arr_opts = []
233+
arr_opts << '--hard' if opts[:hard]
234+
arr_opts << commit.to_s if commit
235+
command('reset', arr_opts)
236+
end
237+
220238

221239
private
222240

lib/git/status.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def changed
1818
def added
1919
@files.select { |k, f| f.type == 'A' }
2020
end
21+
22+
def deleted
23+
@files.select { |k, f| f.type == 'D' }
24+
end
2125

2226
def untracked
2327
@files.select { |k, f| f.untracked }

tests/units/test_index_ops.rb

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
class TestIndexOps < Test::Unit::TestCase
6+
7+
def setup
8+
set_file_paths
9+
@git = Git.open(@wdir)
10+
end
11+
12+
def test_add
13+
in_temp_dir(false) 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+
append_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
40+
41+
def test_add_array
42+
in_temp_dir do |path|
43+
g = Git.clone(@wbare, 'new')
44+
Dir.chdir('new') do
45+
46+
new_file('test-file1', 'blahblahblah1')
47+
new_file('test-file2', 'blahblahblah2')
48+
assert(g.status.untracked.assoc('test-file1'))
49+
50+
g.add(['test-file1', 'test-file2'])
51+
assert(g.status.added.assoc('test-file1'))
52+
assert(g.status.added.assoc('test-file1'))
53+
assert(!g.status.untracked.assoc('test-file1'))
54+
55+
g.commit('my message')
56+
assert(!g.status.added.assoc('test-file1'))
57+
assert(!g.status.untracked.assoc('test-file1'))
58+
assert_equal('blahblahblah1', g.status['test-file1'].blob.contents)
59+
end
60+
end
61+
end
62+
63+
def test_remove
64+
in_temp_dir do |path|
65+
g = Git.clone(@wbare, 'remove_test')
66+
Dir.chdir('remove_test') do
67+
assert(g.status['example.txt'])
68+
g.remove('example.txt')
69+
assert(g.status.deleted.assoc('example.txt'))
70+
g.commit('deleted file')
71+
assert(!g.status['example.txt'])
72+
end
73+
end
74+
end
75+
76+
def test_reset
77+
in_temp_dir do |path|
78+
g = Git.clone(@wbare, 'reset_test')
79+
Dir.chdir('reset_test') do
80+
new_file('test-file1', 'blahblahblah1')
81+
new_file('test-file2', 'blahblahblah2')
82+
assert(g.status.untracked.assoc('test-file1'))
83+
84+
g.add(['test-file1', 'test-file2'])
85+
assert(!g.status.untracked.assoc('test-file1'))
86+
87+
g.reset
88+
assert(g.status.untracked.assoc('test-file1'))
89+
assert(!g.status.added.assoc('test-file1'))
90+
end
91+
end
92+
end
93+
94+
def new_file(name, contents)
95+
File.open(name, 'w') do |f|
96+
f.puts contents
97+
end
98+
end
99+
100+
def append_file(name, contents)
101+
File.open(name, 'a') do |f|
102+
f.puts contents
103+
end
104+
end
105+
106+
end

0 commit comments

Comments
 (0)