From 0301d5422557f321ab3a0a59e87ca38ff53c75d6 Mon Sep 17 00:00:00 2001 From: Vern Burton Date: Tue, 31 Jul 2018 19:42:41 -0500 Subject: [PATCH] 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. Signed-off-by: Vern Burton --- lib/git/lib.rb | 10 ++++++---- lib/git/stashes.rb | 11 +++++++++++ tests/units/test_stashes.rb | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 71cc7fad..fc390af5 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -604,10 +604,12 @@ def stashes_all arr = [] filename = File.join(@git_dir, 'logs/refs/stash') if File.exist?(filename) - File.open(filename).each_with_index { |line, i| - m = line.match(/:(.*)$/) - arr << [i, m[1].strip] - } + File.open(filename) do |f| + f.each_with_index do |line, i| + m = line.match(/:(.*)$/) + arr << [i, m[1].strip] + end + end end arr end diff --git a/lib/git/stashes.rb b/lib/git/stashes.rb index 8bb71af5..0ebb9bed 100644 --- a/lib/git/stashes.rb +++ b/lib/git/stashes.rb @@ -13,6 +13,17 @@ def initialize(base) @stashes.unshift(Git::Stash.new(@base, message, true)) end end + + # + # Returns an multi-dimensional Array of elements that have been stash saved. + # Array is based on position and name. See Example + # + # @example Returns Array of items that have been stashed + # .all - [0, "testing-stash-all"]] + # @return [Array] + def all + @base.lib.stashes_all + end def save(message) s = Git::Stash.new(@base, message) diff --git a/tests/units/test_stashes.rb b/tests/units/test_stashes.rb index 2306061b..c47ab1d9 100644 --- a/tests/units/test_stashes.rb +++ b/tests/units/test_stashes.rb @@ -32,5 +32,27 @@ def test_stash_unstash end end end + + def test_stashes_all + in_temp_dir do |path| + g = Git.clone(@wbare, 'stash_test') + Dir.chdir('stash_test') do + assert_equal(0, g.branch.stashes.size) + new_file('test-file1', 'blahblahblah1') + new_file('test-file2', 'blahblahblah2') + assert(g.status.untracked.assoc('test-file1')) + + g.add + + assert(g.status.added.assoc('test-file1')) + + g.branch.stashes.save('testing-stash-all') + + stashes = g.branch.stashes.all + + assert(stashes[0].include?('testing-stash-all')) + end + end + end end \ No newline at end of file