Skip to content

Commit be47ad8

Browse files
author
scott Chacon
committed
added checkout_index
1 parent 613757c commit be47ad8

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

README

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,20 @@ Some examples of more low-level index and tree operations
228228
end
229229
end
230230

231-
g.set_index('/path/to/index')
231+
g.set_index('/path/to/index')
232+
233+
234+
g.with_index(path) do
235+
# calls set_index, then switches back after
236+
end
237+
238+
g.with_working(dir) do
239+
# calls set_working, then switches back after
240+
end
241+
242+
g.with_temp_working(dir) do
243+
g.checkout_index(:prefix => dir, :path_limiter => path)
244+
# do file work
245+
g.commit # commits to index
246+
end
247+

lib/git/base.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ def with_temp_index &blk
361361
with_index(temp_path, &blk)
362362
end
363363

364+
def checkout_index(opts = {})
365+
self.lib.checkout_index(opts)
366+
end
367+
364368
def read_tree(treeish, opts = {})
365369
self.lib.read_tree(treeish, opts)
366370
end
@@ -382,6 +386,7 @@ def update_ref(branch, commit)
382386
branch(branch).update_ref(commit)
383387
end
384388

389+
385390
def ls_files
386391
self.lib.ls_files
387392
end

lib/git/lib.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ def update_ref(branch, commit)
380380
command('update-ref', [branch.to_s, commit.to_s])
381381
end
382382

383+
def checkout_index(opts = {})
384+
arr_opts = []
385+
arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
386+
arr_opts << "--force" if opts[:force]
387+
arr_opts << "--all" if opts[:all]
388+
arr_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
389+
command('checkout-index', arr_opts)
390+
end
383391

384392
# creates an archive file
385393
#

tests/units/test_tree_ops.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ def test_read_tree
106106

107107
assert_equal('b40f7a9072cdec637725700668f8fdebe39e6d38', c.gtree.sha)
108108

109+
g.with_temp_working do
110+
assert(!File.directory?('b1'))
111+
g.checkout_index
112+
assert(!File.directory?('b1'))
113+
g.checkout_index(:all => true)
114+
assert(File.directory?('b1'))
115+
end
116+
109117
end
110118
end
111119
end

0 commit comments

Comments
 (0)