@@ -289,6 +289,7 @@ import {
289289 ErrorWithMetadata ,
290290 CheckoutError ,
291291 DiscardChangesError ,
292+ StashChangesError ,
292293} from '../error-with-metadata'
293294import {
294295 ShowSideBySideDiffDefault ,
@@ -377,6 +378,7 @@ const confirmRepoRemovalDefault: boolean = true
377378const showCommitLengthWarningDefault : boolean = false
378379const confirmDiscardChangesDefault : boolean = true
379380const confirmDiscardChangesPermanentlyDefault : boolean = true
381+ const confirmStashChangesDefault : boolean = true
380382const confirmDiscardStashDefault : boolean = true
381383const confirmCheckoutCommitDefault : boolean = true
382384const askForConfirmationOnForcePushDefault = true
@@ -385,6 +387,7 @@ const askToMoveToApplicationsFolderKey: string = 'askToMoveToApplicationsFolder'
385387const confirmRepoRemovalKey : string = 'confirmRepoRemoval'
386388const showCommitLengthWarningKey : string = 'showCommitLengthWarning'
387389const confirmDiscardChangesKey : string = 'confirmDiscardChanges'
390+ const confirmStashChangesKey : string = 'confirmStashChanges'
388391const confirmDiscardStashKey : string = 'confirmDiscardStash'
389392const confirmCheckoutCommitKey : string = 'confirmCheckoutCommit'
390393const confirmDiscardChangesPermanentlyKey : string =
@@ -517,6 +520,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
517520 private confirmDiscardChanges : boolean = confirmDiscardChangesDefault
518521 private confirmDiscardChangesPermanently : boolean =
519522 confirmDiscardChangesPermanentlyDefault
523+ private confirmStashChanges : boolean = confirmStashChangesDefault
520524 private confirmDiscardStash : boolean = confirmDiscardStashDefault
521525 private confirmCheckoutCommit : boolean = confirmCheckoutCommitDefault
522526 private askForConfirmationOnForcePush = askForConfirmationOnForcePushDefault
@@ -1044,6 +1048,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
10441048 askForConfirmationOnDiscardChanges : this . confirmDiscardChanges ,
10451049 askForConfirmationOnDiscardChangesPermanently :
10461050 this . confirmDiscardChangesPermanently ,
1051+ askForConfirmationOnStashChanges : this . confirmStashChanges ,
10471052 askForConfirmationOnDiscardStash : this . confirmDiscardStash ,
10481053 askForConfirmationOnCheckoutCommit : this . confirmCheckoutCommit ,
10491054 askForConfirmationOnForcePush : this . askForConfirmationOnForcePush ,
@@ -2200,6 +2205,11 @@ export class AppStore extends TypedBaseStore<IAppState> {
22002205 confirmDiscardChangesPermanentlyDefault
22012206 )
22022207
2208+ this . confirmStashChanges = getBoolean (
2209+ confirmStashChangesKey ,
2210+ confirmStashChangesDefault
2211+ )
2212+
22032213 this . confirmDiscardStash = getBoolean (
22042214 confirmDiscardStashKey ,
22052215 confirmDiscardStashDefault
@@ -5000,6 +5010,32 @@ export class AppStore extends TypedBaseStore<IAppState> {
50005010 return this . _refreshRepository ( repository )
50015011 }
50025012
5013+ public async _stashChanges (
5014+ repository : Repository ,
5015+ files : ReadonlyArray < WorkingDirectoryFileChange >
5016+ ) {
5017+ try {
5018+ const repositoryState = this . repositoryStateCache . get ( repository )
5019+ const tip = repositoryState . branchesState . tip
5020+ const currentBranch = tip . kind === TipState . Valid ? tip . branch : null
5021+
5022+ if ( currentBranch === null ) {
5023+ return
5024+ }
5025+
5026+ await this . createStashEntries ( repository , currentBranch , files )
5027+ } catch ( error ) {
5028+ if ( ! ( error instanceof StashChangesError ) ) {
5029+ log . error ( 'Failed stashing changes' , error )
5030+ }
5031+
5032+ this . emitError ( error )
5033+ return
5034+ }
5035+
5036+ return this . _refreshRepository ( repository )
5037+ }
5038+
50035039 public async _discardChangesFromSelection (
50045040 repository : Repository ,
50055041 filePath : string ,
@@ -5757,6 +5793,15 @@ export class AppStore extends TypedBaseStore<IAppState> {
57575793 return Promise . resolve ( )
57585794 }
57595795
5796+ public _setConfirmStashChangesSetting ( value : boolean ) : Promise < void > {
5797+ this . confirmStashChanges = value
5798+
5799+ setBoolean ( confirmStashChangesKey , value )
5800+ this . emitUpdate ( )
5801+
5802+ return Promise . resolve ( )
5803+ }
5804+
57605805 public _setConfirmDiscardChangesPermanentlySetting (
57615806 value : boolean
57625807 ) : Promise < void > {
@@ -6875,13 +6920,36 @@ export class AppStore extends TypedBaseStore<IAppState> {
68756920 }
68766921
68776922 private async createStashEntry ( repository : Repository , branch : Branch ) {
6923+ const stashEntry = await getLastDesktopStashEntryForBranch ( repository , branch )
6924+
6925+ if ( stashEntry !== null ) {
6926+ await this . _popStashEntry ( repository , stashEntry )
6927+ }
6928+
68786929 const { changesState } = this . repositoryStateCache . get ( repository )
68796930 const { workingDirectory } = changesState
68806931 const untrackedFiles = getUntrackedFiles ( workingDirectory )
68816932
68826933 return createDesktopStashEntry ( repository , branch , untrackedFiles )
68836934 }
68846935
6936+ private async createStashEntries ( repository : Repository , branch : Branch , files : ReadonlyArray < WorkingDirectoryFileChange > ) {
6937+ const { changesState } = this . repositoryStateCache . get ( repository )
6938+ const { workingDirectory } = changesState
6939+
6940+ const stashEntry = await getLastDesktopStashEntryForBranch ( repository , branch )
6941+
6942+ if ( stashEntry !== null ) {
6943+ await this . _popStashEntry ( repository , stashEntry )
6944+ }
6945+
6946+ const newChangesState = this . repositoryStateCache . get ( repository ) . changesState
6947+ const newWorkingDirectory = newChangesState . workingDirectory
6948+ const stashPoppedFiles = newWorkingDirectory . files . filter ( stashFile => workingDirectory . files . findIndex ( file => file . path === stashFile . path ) === - 1 )
6949+
6950+ return createDesktopStashEntry ( repository , branch , [ ...files , ...stashPoppedFiles ] )
6951+ }
6952+
68856953 /** This shouldn't be called directly. See `Dispatcher`. */
68866954 public async _popStashEntry ( repository : Repository , stashEntry : IStashEntry ) {
68876955 await popStashEntry ( repository , stashEntry . stashSha )
0 commit comments