From dc02b2357ab840babc2eaf8058b09ec430299b42 Mon Sep 17 00:00:00 2001 From: ShafinKhadem Date: Sat, 4 Mar 2023 14:49:24 +0600 Subject: [PATCH] Fixes stashing staged changes --- CHANGELOG.md | 1 + src/commands/git/stash.ts | 3 ++- src/commands/stashSave.ts | 26 ++++++++++++++++---------- src/env/node/git/git.ts | 13 ++++++++++++- src/env/node/git/localGitProvider.ts | 2 +- src/git/actions/stash.ts | 11 +++++++++-- src/git/gitProvider.ts | 2 +- src/git/gitProviderService.ts | 2 +- src/git/models/repository.ts | 6 +++++- src/plus/github/githubGitProvider.ts | 2 +- 10 files changed, 49 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0a13f5..37a6571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Fixes [#2549](https://github.com/gitkraken/vscode-gitlens/issues/2549) - toggle code lens does not work with gitlens.codeLens.enabled == false - Fixes [#2553](https://github.com/gitkraken/vscode-gitlens/issues/2553) - Can't add remote url with git@ format +- Fixes [#2083](https://github.com/gitkraken/vscode-gitlens/issues/2083), [#2539](https://github.com/gitkraken/vscode-gitlens/issues/2539) - Fix stashing staged changes; thanks to [PR #2540](https://github.com/gitkraken/vscode-gitlens/pull/2540) by Nafiur Rahman Khadem ([@ShafinKhadem](https://github.com/ShafinKhadem)) ## [13.3.2] - 2023-03-06 diff --git a/src/commands/git/stash.ts b/src/commands/git/stash.ts index 7fccd2b..b3db949 100644 --- a/src/commands/git/stash.ts +++ b/src/commands/git/stash.ts @@ -75,7 +75,7 @@ interface PopState { reference: GitStashReference; } -type PushFlags = '--include-untracked' | '--keep-index'; +export type PushFlags = '--include-untracked' | '--keep-index' | '--staged'; interface PushState { subcommand: 'push'; @@ -519,6 +519,7 @@ export class StashGitCommand extends QuickCommand { await state.repo.stashSave(state.message, state.uris, { includeUntracked: state.flags.includes('--include-untracked'), keepIndex: state.flags.includes('--keep-index'), + onlyStaged: state.flags.includes('--staged'), }); } catch (ex) { Logger.error(ex, context.title); diff --git a/src/commands/stashSave.ts b/src/commands/stashSave.ts index 9bd0bfd..2736662 100644 --- a/src/commands/stashSave.ts +++ b/src/commands/stashSave.ts @@ -19,6 +19,7 @@ export interface StashSaveCommandArgs { repoPath?: string; uris?: Uri[]; keepStaged?: boolean; + onlyStaged?: boolean; } @command() @@ -55,16 +56,21 @@ export class StashSaveCommand extends Command { } } else if (context.type === 'scm-groups') { args = { ...args }; - args.uris = context.scmResourceGroups.reduce( - (a, b) => a.concat(b.resourceStates.map(s => s.resourceUri)), - [], - ); - args.repoPath = (await this.container.git.getOrOpenRepository(args.uris[0]))?.path; - const status = await this.container.git.getStatusForRepo(args.repoPath); - if (status?.computeWorkingTreeStatus().staged) { - if (!context.scmResourceGroups.some(g => g.id === 'index')) { - args.keepStaged = true; + if (context.scmResourceGroups.every(g => g.id === 'index')) { + args.onlyStaged = true; + } else { + args.uris = context.scmResourceGroups.reduce( + (a, b) => a.concat(b.resourceStates.map(s => s.resourceUri)), + [], + ); + args.repoPath = (await this.container.git.getOrOpenRepository(args.uris[0]))?.path; + + const status = await this.container.git.getStatusForRepo(args.repoPath); + if (status?.computeWorkingTreeStatus().staged) { + if (!context.scmResourceGroups.some(g => g.id === 'index')) { + args.keepStaged = true; + } } } } @@ -73,6 +79,6 @@ export class StashSaveCommand extends Command { } execute(args?: StashSaveCommandArgs) { - return push(args?.repoPath, args?.uris, args?.message, args?.keepStaged); + return push(args?.repoPath, args?.uris, args?.message, args?.keepStaged, args?.onlyStaged); } } diff --git a/src/env/node/git/git.ts b/src/env/node/git/git.ts index 2fdc83d..56bb689 100644 --- a/src/env/node/git/git.ts +++ b/src/env/node/git/git.ts @@ -1811,9 +1811,16 @@ export class Git { { includeUntracked, keepIndex, + onlyStaged, pathspecs, stdin, - }: { includeUntracked?: boolean; keepIndex?: boolean; pathspecs?: string[]; stdin?: boolean } = {}, + }: { + includeUntracked?: boolean; + keepIndex?: boolean; + onlyStaged?: boolean; + pathspecs?: string[]; + stdin?: boolean; + } = {}, ): Promise { const params = ['stash', 'push']; @@ -1825,6 +1832,10 @@ export class Git { params.push('-k'); } + if (onlyStaged) { + params.push('--staged'); + } + if (message) { params.push('-m', message); } diff --git a/src/env/node/git/localGitProvider.ts b/src/env/node/git/localGitProvider.ts index e17e046..45ac8c4 100644 --- a/src/env/node/git/localGitProvider.ts +++ b/src/env/node/git/localGitProvider.ts @@ -4691,7 +4691,7 @@ export class LocalGitProvider implements GitProvider, Disposable { repoPath: string, message?: string, uris?: Uri[], - options?: { includeUntracked?: boolean; keepIndex?: boolean }, + options?: { includeUntracked?: boolean; keepIndex?: boolean; onlyStaged?: boolean }, ): Promise { if (uris == null) { await this.git.stash__push(repoPath, message, options); diff --git a/src/git/actions/stash.ts b/src/git/actions/stash.ts index 75aa77c..db40334 100644 --- a/src/git/actions/stash.ts +++ b/src/git/actions/stash.ts @@ -1,4 +1,5 @@ import type { Uri } from 'vscode'; +import type { PushFlags } from '../../commands/git/stash'; import { Container } from '../../container'; import { executeGitCommand } from '../actions'; import type { GitStashCommit } from '../models/commit'; @@ -26,7 +27,13 @@ export function pop(repo?: string | Repository, ref?: GitStashReference) { }); } -export function push(repo?: string | Repository, uris?: Uri[], message?: string, keepStaged: boolean = false) { +export function push( + repo?: string | Repository, + uris?: Uri[], + message?: string, + keepStaged: boolean = false, + onlyStaged: boolean = false, +) { return executeGitCommand({ command: 'stash', state: { @@ -34,7 +41,7 @@ export function push(repo?: string | Repository, uris?: Uri[], message?: string, repo: repo, uris: uris, message: message, - flags: keepStaged ? ['--keep-index'] : undefined, + flags: [...(keepStaged ? ['--keep-index'] : []), ...(onlyStaged ? ['--staged'] : [])] as PushFlags[], }, }); } diff --git a/src/git/gitProvider.ts b/src/git/gitProvider.ts index a0aa90b..666c959 100644 --- a/src/git/gitProvider.ts +++ b/src/git/gitProvider.ts @@ -451,7 +451,7 @@ export interface GitProvider extends Disposable { repoPath: string, message?: string, uris?: Uri[], - options?: { includeUntracked?: boolean | undefined; keepIndex?: boolean | undefined }, + options?: { includeUntracked?: boolean | undefined; keepIndex?: boolean | undefined; onlyStaged?: boolean }, ): Promise; createWorktree?( diff --git a/src/git/gitProviderService.ts b/src/git/gitProviderService.ts index 2b096b5..74c7e6e 100644 --- a/src/git/gitProviderService.ts +++ b/src/git/gitProviderService.ts @@ -2534,7 +2534,7 @@ export class GitProviderService implements Disposable { repoPath: string | Uri, message?: string, uris?: Uri[], - options?: { includeUntracked?: boolean; keepIndex?: boolean }, + options?: { includeUntracked?: boolean; keepIndex?: boolean; onlyStaged?: boolean }, ): Promise { const { provider, path } = this.getProvider(repoPath); return provider.stashSave(path, message, uris, options); diff --git a/src/git/models/repository.ts b/src/git/models/repository.ts index a76c124..775cc31 100644 --- a/src/git/models/repository.ts +++ b/src/git/models/repository.ts @@ -1014,7 +1014,11 @@ export class Repository implements Disposable { @gate() @log() - async stashSave(message?: string, uris?: Uri[], options?: { includeUntracked?: boolean; keepIndex?: boolean }) { + async stashSave( + message?: string, + uris?: Uri[], + options?: { includeUntracked?: boolean; keepIndex?: boolean; onlyStaged?: boolean }, + ) { await this.container.git.stashSave(this.path, message, uris, options); this.fireChange(RepositoryChange.Stash); diff --git a/src/plus/github/githubGitProvider.ts b/src/plus/github/githubGitProvider.ts index 3e25421..f03e406 100644 --- a/src/plus/github/githubGitProvider.ts +++ b/src/plus/github/githubGitProvider.ts @@ -2933,7 +2933,7 @@ export class GitHubGitProvider implements GitProvider, Disposable { _repoPath: string, _message?: string, _uris?: Uri[], - _options?: { includeUntracked?: boolean; keepIndex?: boolean }, + _options?: { includeUntracked?: boolean; keepIndex?: boolean; onlyStaged?: boolean }, ): Promise {} @gate()