Skip to content

Commit 555d7db

Browse files
authored
fix: fixes #2138 stash push/pop issue with correct stash drop and adds regression test (#2150)
* test: add regression test for stash push/pop issue (#2138) * fix: ensure newline at end of reflog entries and correct last stash commit retrieval. fixes #2138 * fix: reverse order of stashes in list and update reflog writing logic
1 parent ea52fe1 commit 555d7db

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,16 @@
837837
"userTesting"
838838
]
839839
},
840+
{
841+
"login": "bekatan",
842+
"name": "Bekatan Satyev",
843+
"avatar_url": "https://avatars.githubusercontent.com/u/19550476?v=4",
844+
"profile": "https://github.com/bekatan",
845+
"contributions": [
846+
"code",
847+
"test"
848+
]
849+
},
840850
{
841851
"login": "hemanthkini",
842852
"name": "Hemanth Kini",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
391391
<td align="center"><a href="https://github.com/ARBhosale"><img src="https://avatars.githubusercontent.com/u/26981417?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Aniket Bhosale</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=ARBhosale" title="Code">💻</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=ARBhosale" title="Documentation">📖</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=ARBhosale" title="Tests">⚠️</a></td>
392392
<td align="center"><a href="https://github.com/gnillev"><img src="https://avatars.githubusercontent.com/u/8965094?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Mathias Nisted Velling</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=gnillev" title="Code">💻</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=gnillev" title="Tests">⚠️</a></td>
393393
<td align="center"><a href="https://github.com/acandoo"><img src="https://avatars.githubusercontent.com/u/117209328?v=4?s=60" width="60px;" alt=""/><br /><sub><b>acandoo</b></sub></a><br /><a href="#platform-acandoo" title="Packaging/porting to new platform">📦</a> <a href="#userTesting-acandoo" title="User Testing">📓</a></td>
394+
<td align="center"><a href="https://github.com/bekatan"><img src="https://avatars.githubusercontent.com/u/19550476?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Bekatan Satyev</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=bekatan" title="Code">💻</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=bekatan" title="Tests">⚠️</a></td>
394395
<td align="center"><a href="https://github.com/hemanthkini"><img src="https://avatars.githubusercontent.com/u/3934055?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Hemanth Kini</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=hemanthkini" title="Code">💻</a></td>
395396
</tr>
396397
</table>

__tests__/test-stash.js

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,8 @@ describe('stash drop', () => {
654654

655655
const stashList = await stash({ fs, dir, gitdir, op: 'list' })
656656
expect(stashList).toEqual([
657-
'stash@{0}: stash one: 3ca31f1 initial commit',
658-
'stash@{1}: stash three: 3ca31f1 initial commit',
657+
'stash@{0}: stash three: 3ca31f1 initial commit',
658+
'stash@{1}: stash one: 3ca31f1 initial commit',
659659
])
660660
})
661661
})
@@ -804,12 +804,72 @@ describe('stash pop', () => {
804804

805805
const stashList = await stash({ fs, dir, gitdir, op: 'list' })
806806
expect(stashList).toEqual([
807-
'stash@{0}: stash one: 3ca31f1 initial commit',
808-
'stash@{1}: stash three: 3ca31f1 initial commit',
807+
'stash@{0}: stash three: 3ca31f1 initial commit',
808+
'stash@{1}: stash one: 3ca31f1 initial commit',
809809
])
810810
const aContent = await fs.read(`${dir}/a.txt`)
811811
expect(aContent.toString()).toEqual(aNewContent) // make sure the 2nd staged changes are applied
812812
const bContent = await fs.read(`${dir}/b.js`)
813813
expect(bContent.toString()).toEqual(bNewContent) // make sure the 2nd staged changes are applied
814814
})
815815
})
816+
817+
describe('stash regression #2138', () => {
818+
it('should not lose stashes after stash pop followed by stash push', async () => {
819+
const { fs, dir, gitdir } = await makeFixtureStash('stashRegression')
820+
await addUserConfig(fs, dir, gitdir)
821+
822+
// --- stash 1 ---
823+
await fs.write(dir + '/a.txt', 'change 1')
824+
await stash({ fs, dir, gitdir, message: 'stash 1', op: 'push' })
825+
// --- stash 2 ---
826+
await fs.write(dir + '/a.txt', 'change 2')
827+
await stash({ fs, dir, gitdir, message: 'stash 2', op: 'push' })
828+
829+
let stashes = await stash({ fs, dir, gitdir, op: 'list' })
830+
expect(stashes.length).toBe(2)
831+
832+
// Pop stash 2
833+
await stash({ fs, dir, gitdir, op: 'pop' })
834+
835+
stashes = await stash({ fs, dir, gitdir, op: 'list' })
836+
expect(stashes.length).toBe(1)
837+
// Push stash 2 again
838+
await stash({ fs, dir, gitdir, message: 'stash 2', op: 'push' })
839+
840+
stashes = await stash({ fs, dir, gitdir, op: 'list' })
841+
expect(stashes.length).toBe(2)
842+
})
843+
844+
it('stash list order before and after stash drop', async () => {
845+
const { fs, dir, gitdir } = await makeFixtureStash('stashRegression')
846+
await addUserConfig(fs, dir, gitdir)
847+
848+
// --- stash 1 ---
849+
await fs.write(dir + '/a.txt', 'change 1')
850+
await stash({ fs, dir, gitdir, message: 'stash 1', op: 'push' })
851+
// --- stash 2 ---
852+
await fs.write(dir + '/a.txt', 'change 2')
853+
await stash({ fs, dir, gitdir, message: 'stash 2', op: 'push' })
854+
855+
// --- stash 3 ---
856+
await fs.write(dir + '/a.txt', 'change 3')
857+
await stash({ fs, dir, gitdir, message: 'stash 3', op: 'push' })
858+
859+
let stashes = await stash({ fs, dir, gitdir, op: 'list' })
860+
expect(stashes).toEqual([
861+
'stash@{0}: stash 3: 3ca31f1 initial commit',
862+
'stash@{1}: stash 2: 3ca31f1 initial commit',
863+
'stash@{2}: stash 1: 3ca31f1 initial commit',
864+
])
865+
866+
// drop stash 3
867+
await stash({ fs, dir, gitdir, op: 'drop' })
868+
869+
stashes = await stash({ fs, dir, gitdir, op: 'list' })
870+
expect(stashes).toEqual([
871+
'stash@{0}: stash 2: 3ca31f1 initial commit',
872+
'stash@{1}: stash 1: 3ca31f1 initial commit',
873+
])
874+
})
875+
})

src/commands/stash.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ export async function _stashDrop({ fs, dir, gitdir, refIdx = 0 }) {
174174
const stashReflogPath = stashMgr.refLogsStashPath
175175
await acquireLock({ reflogEntries, stashReflogPath, stashMgr }, async () => {
176176
if (reflogEntries.length) {
177-
await fs.write(stashReflogPath, reflogEntries.join('\n'), 'utf8')
177+
await fs.write(
178+
stashReflogPath,
179+
reflogEntries.reverse().join('\n') + '\n',
180+
'utf8'
181+
)
178182
const lastStashCommit = reflogEntries[reflogEntries.length - 1].split(
179183
' '
180184
)[1]

0 commit comments

Comments
 (0)