Skip to content

Commit a4fa110

Browse files
author
scott Chacon
committed
added the commit(), changed base.commit, base.tree, base.blob to gcommit, gtree, gblob
1 parent a123767 commit a4fa110

File tree

8 files changed

+98
-45
lines changed

8 files changed

+98
-45
lines changed

EXAMPLES

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ g.log # returns array of Git::Commit objects
1515
g.log.since('2 weeks ago')
1616
g.log.between('v2.5', 'v2.6')
1717
g.log.each {|l| puts l.sha }
18-
g.blob('v2.5:Makefile').log.since('2 weeks ago')
18+
g.gblob('v2.5:Makefile').log.since('2 weeks ago')
1919

2020
g.object('HEAD^').to_s # git show / git rev-parse
2121
g.object('HEAD^').contents
2222
g.object('v2.5:Makefile').size
2323
g.object('v2.5:Makefile').sha
2424

25-
g.tree(treeish)
26-
g.blob(treeish)
27-
g.commit(treeish)
25+
g.gtree(treeish)
26+
g.gblob(treeish)
27+
g.gcommit(treeish)
2828

2929
g.revparse('v2.5:Makefile')
3030

@@ -40,13 +40,13 @@ g.tag('v2.5').grep('hello', 'docs/')
4040

4141
g.diff(commit1, commit2).size
4242
g.diff(commit1, commit2).stats
43-
g.tree('v2.5').diff('v2.6').insertions
43+
g.gtree('v2.5').diff('v2.6').insertions
4444
g.diff('gitsearch1', 'v2.5').path('lib/')
45-
g.diff('gitsearch1', @git.tree('v2.5'))
45+
g.diff('gitsearch1', @git.gtree('v2.5'))
4646
g.diff('gitsearch1', 'v2.5').path('docs/').patch
47-
g.tree('v2.5').diff('v2.6').patch
47+
g.gtree('v2.5').diff('v2.6').patch
4848

49-
g.tree('v2.5').diff('v2.6').each do |file_diff|
49+
g.gtree('v2.5').diff('v2.6').each do |file_diff|
5050
puts file_diff.path
5151
puts file_diff.patch
5252
puts file_diff.blob(:src).contents
@@ -57,18 +57,11 @@ g.config # returns whole config hash
5757

5858
***** IMPLEMENTED *****
5959

60-
g.ls_files
61-
g.ls_files(:stage => true)
62-
6360
g.tag # returns array of Git::Tag objects
6461

6562

6663

6764

68-
69-
70-
71-
7265
# needs write permission
7366

7467

@@ -84,18 +77,17 @@ g = Git.clone(URI, :name => 'name', :path => '/tmp/checkout'
8477
g.config('user.name', 'Scott Chacon')
8578
g.config('user.email', 'email@email.com')
8679

80+
g.add('.')
81+
g.add([file1, file2])
8782

88-
***** IMPLEMENTED *****
83+
g.commit('message')
84+
g.commit_all('message')
8985

9086

91-
g.add('.')
92-
g.add([file1, file2])
87+
***** IMPLEMENTED *****
9388

9489
g.remove('file.txt').and_file
9590

96-
g.commit('message')
97-
g.commit_a('message')
98-
9991
g.reset # defaults to HEAD
10092
g.reset_hard(Git::Commit)
10193

lib/git/base.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ def config(name = nil, value = nil)
107107
def object(objectish)
108108
Git::Object.new(self, objectish)
109109
end
110-
alias_method :tree, :object
111-
alias_method :commit, :object
112-
alias_method :blob, :object
110+
alias_method :gtree, :object
111+
alias_method :gcommit, :object
112+
alias_method :gblob, :object
113113

114114

115115
def log(count = 30)
@@ -140,6 +140,15 @@ def diff(objectish = 'HEAD', obj2 = nil)
140140
def add(path = '.')
141141
self.lib.add(path)
142142
end
143+
144+
def commit(message, opts = {})
145+
self.lib.commit(message, opts)
146+
end
147+
148+
def commit_all(message, opts = {})
149+
opts = {:add_all => true}.merge(opts)
150+
self.lib.commit(message, opts)
151+
end
143152

144153
# convenience methods
145154

lib/git/lib.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def diff_files
150150
command_lines('diff-files').each do |line|
151151
(info, file) = line.split("\t")
152152
(mode_src, mode_dest, sha_src, sha_dest, type) = info.split
153-
hsh[file] = {:path => file, :mode_file => mode_src, :mode_index => mode_dest,
153+
hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
154154
:sha_file => sha_src, :sha_index => sha_dest, :type => type}
155155
end
156156
hsh
@@ -162,7 +162,7 @@ def diff_index(treeish)
162162
command_lines('diff-index', treeish).each do |line|
163163
(info, file) = line.split("\t")
164164
(mode_src, mode_dest, sha_src, sha_dest, type) = info.split
165-
hsh[file] = {:path => file, :mode_repo => mode_src, :mode_index => mode_dest,
165+
hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
166166
:sha_repo => sha_src, :sha_index => sha_dest, :type => type}
167167
end
168168
hsh
@@ -208,10 +208,16 @@ def config_set(name, value)
208208
end
209209

210210
def add(path = '.')
211+
path = path.join(' ') if path.is_a?(Array)
211212
command('add', path)
212213
end
213214

214-
215+
def commit(message, opts = {})
216+
arr_opts = ["-m '#{message}'"]
217+
arr_opts << '-a' if opts[:add_all]
218+
command('commit', arr_opts)
219+
end
220+
215221
private
216222

217223
def command_lines(cmd, opts = {})
@@ -222,10 +228,15 @@ def command(cmd, opts = {})
222228
ENV['GIT_DIR'] = @git_dir if @git_dir
223229
ENV['GIT_INDEX_FILE'] = @git_index_file if @git_index_file
224230
ENV['GIT_WORK_DIR'] = @git_work_dir if @git_work_dir
225-
Dir.chdir(@git_work_dir || @git_dir || @path) do
231+
path = @git_work_dir || @git_dir || @path
232+
Dir.chdir(path) do
226233
opts = opts.to_a.join(' ')
227-
#puts "git #{cmd} #{opts}"
228234
out = `git #{cmd} #{opts} 2>&1`.chomp
235+
#puts path
236+
#puts "gd: #{@git_work_dir}"
237+
#puts "gi: #{@git_index_file}"
238+
#puts "pp: #{@path}"
239+
#puts "git #{cmd} #{opts}"
229240
#puts out
230241
#puts
231242
if $?.exitstatus > 1

lib/git/status.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def pretty
2727
out = ''
2828
self.each do |file|
2929
out << file.path
30-
out << "\n\tsha(r) " + file.sha_repo.to_s
31-
out << "\n\tsha(i) " + file.sha_index.to_s
30+
out << "\n\tsha(r) " + file.sha_repo.to_s + ' ' + file.mode_repo.to_s
31+
out << "\n\tsha(i) " + file.sha_index.to_s + ' ' + file.mode_index.to_s
3232
out << "\n\ttype " + file.type.to_s
3333
out << "\n\tstage " + file.stage.to_s
3434
out << "\n\tuntrac " + file.untracked.to_s
@@ -55,7 +55,10 @@ class StatusFile
5555
attr_accessor :mode_index, :mode_repo
5656
attr_accessor :sha_index, :sha_repo
5757

58-
def initialize(hash)
58+
@base = nil
59+
60+
def initialize(base, hash)
61+
@base = base
5962
@path = hash[:path]
6063
@type = hash[:type]
6164
@stage = hash[:stage]
@@ -66,6 +69,14 @@ def initialize(hash)
6669
@untracked = hash[:untracked]
6770
end
6871

72+
def blob(type = :index)
73+
if type == :repo
74+
@base.object(@sha_repo)
75+
else
76+
@base.object(@sha_index) rescue @base.object(@sha_repo)
77+
end
78+
end
79+
6980

7081
end
7182

@@ -85,16 +96,16 @@ def construct_status
8596

8697
# find modified in tree
8798
@base.lib.diff_files.each do |path, data|
88-
@files[path].merge!(data)
99+
@files[path] ? @files[path].merge!(data) : @files[path] = data
89100
end
90101

91102
# find added but not committed - new files
92103
@base.lib.diff_index('HEAD').each do |path, data|
93-
@files[path].merge!(data)
104+
@files[path] ? @files[path].merge!(data) : @files[path] = data
94105
end
95106

96107
@files.each do |k, file_hash|
97-
@files[k] = StatusFile.new(file_hash)
108+
@files[k] = StatusFile.new(@base, file_hash)
98109
end
99110
end
100111

tests/all_tests.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Dir.chdir(File.dirname(__FILE__)) do
22
Dir.glob('**/test_*.rb') { |test_case| require test_case }
3+
#Dir.glob('**/test_index.rb') { |test_case| require test_case }
34
end

tests/units/test_diff.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ def test_diff_path
3131
end
3232

3333
def test_diff_objects
34-
d = @git.diff('gitsearch1', @git.tree('v2.5'))
34+
d = @git.diff('gitsearch1', @git.gtree('v2.5'))
3535
assert(3, d.size)
3636
end
3737

3838
def test_object_diff
39-
d = @git.tree('v2.5').diff('gitsearch1')
39+
d = @git.gtree('v2.5').diff('gitsearch1')
4040
assert_equal(3, d.size)
4141
assert_equal(74, d.lines)
4242
assert_equal(10, d.insertions)
4343
assert_equal(64, d.deletions)
4444

45-
d = @git.tree('v2.6').diff(@git.tree('gitsearch1'))
45+
d = @git.gtree('v2.6').diff(@git.gtree('gitsearch1'))
4646
assert_equal(2, d.size)
4747
assert_equal(9, d.lines)
4848
end

tests/units/test_index.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,53 @@ def setup
1111

1212
def test_add
1313
in_temp_dir do |path|
14-
#puts path
1514
g = Git.clone(@wbare, 'new')
1615
Dir.chdir('new') do
17-
assert_equal('100644', g.status['example.txt'].mode_index)
16+
puts `pwd`
17+
#assert_equal('100644', g.status['example.txt'].mode_index)
18+
1819
new_file('test-file', 'blahblahblah')
1920
assert(g.status.untracked.assoc('test-file'))
21+
2022
g.add
2123
assert(g.status.added.assoc('test-file'))
2224
assert(!g.status.untracked.assoc('test-file'))
2325
assert(!g.status.changed.assoc('example.txt'))
26+
2427
append_file('example.txt', 'hahahaha')
28+
puts g.status.pretty
2529
assert(g.status.changed.assoc('example.txt'))
30+
2631
g.add
2732
assert(g.status.changed.assoc('example.txt'))
33+
2834
g.commit('my message')
2935
assert(!g.status.changed.assoc('example.txt'))
3036
assert(!g.status.added.assoc('test-file'))
31-
assert(!g.status.untracked.assoc('test-file'))
37+
assert(!g.status.untracked.assoc('test-file'))
38+
assert_equal('hahahaha', g.status['example.txt'].blob.contents)
39+
end
40+
end
41+
end
42+
43+
def test_add_array
44+
in_temp_dir do |path|
45+
g = Git.clone(@wbare, 'new')
46+
Dir.chdir('new') do
47+
48+
new_file('test-file1', 'blahblahblah1')
49+
new_file('test-file2', 'blahblahblah2')
50+
assert(g.status.untracked.assoc('test-file1'))
51+
52+
g.add(['test-file1', 'test-file2'])
53+
assert(g.status.added.assoc('test-file1'))
54+
assert(g.status.added.assoc('test-file1'))
55+
assert(!g.status.untracked.assoc('test-file1'))
56+
57+
g.commit('my message')
58+
assert(!g.status.added.assoc('test-file1'))
59+
assert(!g.status.untracked.assoc('test-file1'))
60+
assert_equal('blahblahblah1', g.status['test-file1'].blob.contents)
3261
end
3362
end
3463
end

tests/units/test_object.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ def test_revparse
7777
end
7878

7979
def test_grep
80-
g = @git.tree('a3db7143944dcfa0').grep('search') # there
80+
g = @git.gtree('a3db7143944dcfa0').grep('search') # there
8181
assert_equal(3, g.to_a.flatten.size)
8282
assert_equal(1, g.size)
8383

84-
assert_equal({}, @git.tree('a3db7143944dcfa0').grep('34a566d193')) # not there
84+
assert_equal({}, @git.gtree('a3db7143944dcfa0').grep('34a566d193')) # not there
8585

86-
g = @git.commit('gitsearch1').grep('search') # there
86+
g = @git.gcommit('gitsearch1').grep('search') # there
8787
assert_equal(8, g.to_a.flatten.size)
8888
assert_equal(2, g.size)
8989

90-
g = @git.commit('gitsearch1').grep('search', 'scott/new*') # there
90+
g = @git.gcommit('gitsearch1').grep('search', 'scott/new*') # there
9191
assert_equal(3, g.to_a.flatten.size)
9292
assert_equal(1, g.size)
9393
end

0 commit comments

Comments
 (0)