stash
stash api, supports {'push' | 'pop' | 'apply' | 'drop' | 'list' | 'clear'} StashOp
param | type [= default] | description |
---|---|---|
****fs**** | FsClient | a file system client |
dir | string | The working tree directory path |
gitdir | string = join(dir,'.git') | [optional] The git directory path |
op | 'push' | 'pop' | 'apply' | 'drop' | 'list' | 'clear' = 'push' | [optional] name of stash operation, default to 'push' |
message | string = '' | [optional] message to be used for the stash entry, only applicable when op === 'push' |
refIdx | number = 0 | [optional - Number] stash ref index of entry, only applicable when op === ['apply' | 'drop' | 'pop'], refIdx >= 0 and < num of stash pushed |
return | Promise<(string | void)> | Resolves successfully when stash operations are complete |
note,
- all stash operations are done on tracked files only with loose objects, no packed objects
- when op === 'push', both working directory and index (staged) changes will be stashed, tracked files only
- when op === 'push', message is optional, and only applicable when op === 'push'
- when op === 'apply | pop', the stashed changes will overwrite the working directory, no abort when conflicts
Example Code:
// stash changes in the working directory and index
let dir = '/tutorial'
await fs.promises.writeFile(`${dir}/a.txt`, 'original content - a')
await fs.promises.writeFile(`${dir}/b.js`, 'original content - b')
await git.add({ fs, dir, filepath: [`a.txt`,`b.txt`] })
let sha = await git.commit({
fs,
dir,
author: {
name: 'Mr. Stash',
email: 'mstasher@stash.com',
},
message: 'add a.txt and b.txt to test stash'
})
console.log(sha)
await fs.promises.writeFile(`${dir}/a.txt`, 'stashed chang- a')
await git.add({ fs, dir, filepath: `${dir}/a.txt` })
await fs.promises.writeFile(`${dir}/b.js`, 'work dir change. not stashed - b')
await git.stash({ fs, dir }) // default gitdir and op
console.log(await git.status({ fs, dir, filepath: 'a.txt' })) // 'unmodified'
console.log(await git.status({ fs, dir, filepath: 'b.txt' })) // 'unmodified'
const refLog = await git.stash({ fs, dir, op: 'list' })
console.log(refLog) // [{stash{#} message}]
await git.stash({ fs, dir, op: 'apply' }) // apply the stash
console.log(await git.status({ fs, dir, filepath: 'a.txt' })) // 'modified'
console.log(await git.status({ fs, dir, filepath: 'b.txt' })) // '*modified'
Tip: If you need a clean slate, expand and run this snippet to clean up the file system.
window.fs = new LightningFS('fs', { wipe: true })
window.pfs = window.fs.promises
console.log('done')