Skip to content

Commit 2e79dbe

Browse files
costajcouball
authored andcommitted
Fixed "unbranched" stash message support:
- the tests are generously provided by James Couball <jcouball@yahoo.com> - more proper stash metadata parsing introduced - supporting both "branched" ("On <BRANCH>: ...") and "unbranched" messages - which might affect the future 3.x behaviour wrt "un/branched" stashes
1 parent da6fa6e commit 2e79dbe

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

lib/git/lib.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,10 @@ def stashes_all
11341134
if File.exist?(filename)
11351135
File.open(filename) do |f|
11361136
f.each_with_index do |line, i|
1137-
m = line.match(/:(.*)$/)
1138-
arr << [i, m[1].strip]
1137+
_, msg = line.split("\t")
1138+
# NOTE this logic may be removed/changed in 3.x
1139+
m = msg.match(/^[^:]+:(.*)$/)
1140+
arr << [i, (m ? m[1] : msg).strip]
11391141
end
11401142
end
11411143
end

tests/units/test_stashes.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,107 @@ def test_stashes_all
4444
assert(stashes[0].include?('testing-stash-all'))
4545
end
4646
end
47+
test 'Git::Lib#stashes_all' do
48+
in_bare_repo_clone do |g|
49+
assert_equal(0, g.branch.stashes.size)
50+
new_file('test-file1', 'blahblahblah1')
51+
new_file('test-file2', 'blahblahblah2')
52+
assert(g.status.untracked.assoc('test-file1'))
53+
54+
g.add
55+
56+
assert(g.status.added.assoc('test-file1'))
57+
58+
g.branch.stashes.save('testing-stash-all')
59+
60+
# puts `cat .git/logs/refs/stash`
61+
# 0000000000000000000000000000000000000000 b9b008cd179b0e8c4b8cda35bac43f7011a0836a James Couball <jcouball@yahoo.com> 1729463252 -0700 On master: testing-stash-all
62+
63+
stashes = assert_nothing_raised { g.lib.stashes_all }
64+
65+
expected_stashes = [
66+
[0, 'testing-stash-all']
67+
]
68+
69+
assert_equal(expected_stashes, stashes)
70+
end
71+
end
72+
73+
test 'Git::Lib#stashes_all - stash message has colon' do
74+
in_bare_repo_clone do |g|
75+
assert_equal(0, g.branch.stashes.size)
76+
new_file('test-file1', 'blahblahblah1')
77+
new_file('test-file2', 'blahblahblah2')
78+
assert(g.status.untracked.assoc('test-file1'))
79+
80+
g.add
81+
82+
assert(g.status.added.assoc('test-file1'))
83+
84+
g.branch.stashes.save('saving: testing-stash-all')
85+
86+
# puts `cat .git/logs/refs/stash`
87+
# 0000000000000000000000000000000000000000 b9b008cd179b0e8c4b8cda35bac43f7011a0836a James Couball <jcouball@yahoo.com> 1729463252 -0700 On master: saving: testing-stash-all
88+
89+
stashes = assert_nothing_raised { g.lib.stashes_all }
90+
91+
expected_stashes = [
92+
[0, 'saving: testing-stash-all']
93+
]
94+
95+
assert_equal(expected_stashes, stashes)
96+
end
97+
end
98+
99+
test 'Git::Lib#stashes_all -- git stash message with no branch and no colon' do
100+
in_temp_dir do
101+
`git init`
102+
`echo "hello world" > file1.txt`
103+
`git add file1.txt`
104+
`git commit -m "First commit"`
105+
`echo "update" > file1.txt`
106+
commit = `git stash create "stash message"`.chomp
107+
# Create a stash with this message: 'custom message'
108+
`git stash store -m "custom message" #{commit}`
109+
110+
# puts `cat .git/logs/refs/stash`
111+
# 0000000000000000000000000000000000000000 0550a54ed781eda364ca3c22fcc46c37acae4bd6 James Couball <jcouball@yahoo.com> 1729460302 -0700 custom message
112+
113+
git = Git.open('.')
114+
115+
stashes = assert_nothing_raised { git.lib.stashes_all }
116+
117+
expected_stashes = [
118+
[0, 'custom message']
119+
]
120+
121+
assert_equal(expected_stashes, stashes)
122+
end
123+
end
124+
125+
test 'Git::Lib#stashes_all -- git stash message with no branch and explicit colon' do
126+
in_temp_dir do
127+
`git init`
128+
`echo "hello world" > file1.txt`
129+
`git add file1.txt`
130+
`git commit -m "First commit"`
131+
`echo "update" > file1.txt`
132+
commit = `git stash create "stash message"`.chomp
133+
# Create a stash with this message: 'custom message'
134+
`git stash store -m "testing: custom message" #{commit}`
135+
136+
# puts `cat .git/logs/refs/stash`
137+
# 0000000000000000000000000000000000000000 eadd7858e53ea4fb8b1383d69cade1806d948867 James Couball <jcouball@yahoo.com> 1729462039 -0700 testing: custom message
138+
139+
git = Git.open('.')
140+
141+
stashes = assert_nothing_raised { git.lib.stashes_all }
142+
143+
expected_stashes = [
144+
[0, 'custom message']
145+
]
146+
147+
assert_equal(expected_stashes, stashes)
148+
end
149+
end
47150
end

0 commit comments

Comments
 (0)