Skip to content

Commit 7905a84

Browse files
Adding support for :all option on Git::Base#add and Git::Lib#add
Adding tests related ruby-git#46
1 parent 34f935e commit 7905a84

File tree

4 files changed

+135
-14
lines changed

4 files changed

+135
-14
lines changed

lib/git/base.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,23 @@ def diff(objectish = 'HEAD', obj2 = nil)
243243
Git::Diff.new(self, objectish, obj2)
244244
end
245245

246-
# adds files from the working directory to the git repository
247-
def add(path = '.')
248-
self.lib.add(path)
246+
# updates the repository index using the workig dorectory content
247+
#
248+
# @git.add('path/to/file')
249+
# @git.add(['path/to/file1','path/to/file2'])
250+
# @git.add(:all => true)
251+
#
252+
# options:
253+
# :all => true
254+
#
255+
# @param [String,Array] paths files paths to be added (optional, default='.')
256+
# @param [Hash] options
257+
def add(*args)
258+
if args[0].instance_of?(String) || args[0].instance_of?(Array)
259+
self.lib.add(args[0],args[1]||{})
260+
else
261+
self.lib.add('.', args[0]||{})
262+
end
249263
end
250264

251265
# removes file(s) from the git repository

lib/git/lib.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,29 @@ def config_set(name, value)
392392
def global_config_set(name, value)
393393
command('config', ['--global', name, value], false)
394394
end
395-
396-
def add(path = '.')
397-
arr_opts = ['--']
398-
if path.is_a?(Array)
399-
arr_opts += path
400-
else
401-
arr_opts << path
402-
end
395+
396+
# updates the repository index using the workig dorectory content
397+
#
398+
# lib.add('path/to/file')
399+
# lib.add(['path/to/file1','path/to/file2'])
400+
# lib.add(:all => true)
401+
#
402+
# options:
403+
# :all => true
404+
#
405+
# @param [String,Array] paths files paths to be added to the repository
406+
# @param [Hash] options
407+
def add(paths='.',options={})
408+
arr_opts = []
409+
410+
arr_opts << '--all' if options[:all]
411+
412+
arr_opts << '--'
413+
414+
arr_opts << paths
415+
416+
arr_opts.flatten!
417+
403418
command('add', arr_opts)
404419
end
405420

tests/test_helper.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,22 @@ def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
5555
FileUtils.rm_r(tmp_path) if remove_after
5656
end
5757

58+
def create_file(path, content)
59+
File.open(path,'w') do |file|
60+
file.puts(content)
61+
end
62+
end
63+
64+
def update_file(path, content)
65+
create_file(path,content)
66+
end
67+
68+
def delete_file(path)
69+
File.delete(path)
70+
end
5871

5972
def new_file(name, contents)
60-
File.open(name, 'w') do |f|
61-
f.puts contents
62-
end
73+
create_file(name,contents)
6374
end
6475

6576
def append_file(name, contents)

tests/units/test_base.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
class TestBase < Test::Unit::TestCase
6+
7+
def setup
8+
set_file_paths
9+
end
10+
11+
def test_add
12+
in_temp_dir do |path|
13+
git = Git.clone(@wdir, 'test_add')
14+
15+
create_file('test_add/test_file_1', 'content tets_file_1')
16+
create_file('test_add/test_file_2', 'content test_file_2')
17+
create_file('test_add/test_file_3', 'content test_file_3')
18+
create_file('test_add/test_file_4', 'content test_file_4')
19+
20+
assert(!git.status.added.assoc('test_file_1'))
21+
22+
# Adding a single file, usign String
23+
git.add('test_file_1')
24+
25+
assert(git.status.added.assoc('test_file_1'))
26+
assert(!git.status.added.assoc('test_file_2'))
27+
28+
# Adding a single file, using Array
29+
git.add(['test_file_2'])
30+
31+
assert(git.status.added.assoc('test_file_2'))
32+
assert(!git.status.added.assoc('test_file_3'))
33+
assert(!git.status.added.assoc('test_file_4'))
34+
35+
# Adding multiple files, using Array
36+
git.add(['test_file_3','test_file_4'])
37+
38+
assert(git.status.added.assoc('test_file_3'))
39+
assert(git.status.added.assoc('test_file_4'))
40+
41+
git.commit('test_add commit #1')
42+
43+
assert(git.status.added.empty?)
44+
45+
delete_file('test_add/test_file_3')
46+
update_file('test_add/test_file_4', 'content test_file_4 update #1')
47+
create_file('test_add/test_file_5', 'content test_file_5')
48+
49+
# Adding all files (new, updated or deleted), using :all
50+
git.add(:all => true)
51+
52+
assert(git.status.deleted.assoc('test_file_3'))
53+
assert(git.status.changed.assoc('test_file_4'))
54+
assert(git.status.added.assoc('test_file_5'))
55+
56+
git.commit('test_add commit #2')
57+
58+
assert(git.status.deleted.empty?)
59+
assert(git.status.changed.empty?)
60+
assert(git.status.added.empty?)
61+
62+
delete_file('test_add/test_file_4')
63+
update_file('test_add/test_file_5', 'content test_file_5 update #1')
64+
create_file('test_add/test_file_6', 'content test_fiile_6')
65+
66+
# Adding all files (new or updated), without params
67+
git.add
68+
69+
assert(git.status.deleted.assoc('test_file_4'))
70+
assert(git.status.changed.assoc('test_file_5'))
71+
assert(git.status.added.assoc('test_file_6'))
72+
73+
git.commit('test_add commit #3')
74+
75+
assert(!git.status.deleted.empty?)
76+
assert(git.status.changed.empty?)
77+
assert(git.status.added.empty?)
78+
end
79+
end
80+
81+
end

0 commit comments

Comments
 (0)