Skip to content

Commit 9341416

Browse files
authored
using File as a block so that it tears down once everything has completed, adding a API directly in stashes, and adding testing around it. (#378)
Signed-off-by: Vern Burton <me@vernburton.com>
1 parent 7ff5d78 commit 9341416

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

lib/git/lib.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,12 @@ def stashes_all
604604
arr = []
605605
filename = File.join(@git_dir, 'logs/refs/stash')
606606
if File.exist?(filename)
607-
File.open(filename).each_with_index { |line, i|
608-
m = line.match(/:(.*)$/)
609-
arr << [i, m[1].strip]
610-
}
607+
File.open(filename) do |f|
608+
f.each_with_index do |line, i|
609+
m = line.match(/:(.*)$/)
610+
arr << [i, m[1].strip]
611+
end
612+
end
611613
end
612614
arr
613615
end

lib/git/stashes.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ def initialize(base)
1313
@stashes.unshift(Git::Stash.new(@base, message, true))
1414
end
1515
end
16+
17+
#
18+
# Returns an multi-dimensional Array of elements that have been stash saved.
19+
# Array is based on position and name. See Example
20+
#
21+
# @example Returns Array of items that have been stashed
22+
# .all - [0, "testing-stash-all"]]
23+
# @return [Array]
24+
def all
25+
@base.lib.stashes_all
26+
end
1627

1728
def save(message)
1829
s = Git::Stash.new(@base, message)

tests/units/test_stashes.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,27 @@ def test_stash_unstash
3232
end
3333
end
3434
end
35+
36+
def test_stashes_all
37+
in_temp_dir do |path|
38+
g = Git.clone(@wbare, 'stash_test')
39+
Dir.chdir('stash_test') do
40+
assert_equal(0, g.branch.stashes.size)
41+
new_file('test-file1', 'blahblahblah1')
42+
new_file('test-file2', 'blahblahblah2')
43+
assert(g.status.untracked.assoc('test-file1'))
44+
45+
g.add
46+
47+
assert(g.status.added.assoc('test-file1'))
48+
49+
g.branch.stashes.save('testing-stash-all')
50+
51+
stashes = g.branch.stashes.all
52+
53+
assert(stashes[0].include?('testing-stash-all'))
54+
end
55+
end
56+
end
3557

3658
end

0 commit comments

Comments
 (0)