From 14d05f567b4f582eb3c8b7c98e5939394ca0b85e Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 13 Jun 2022 03:55:49 -0400 Subject: [PATCH] Fixes #2033 untracked files in stashes --- CHANGELOG.md | 3 ++- src/commands/diffWithPrevious.ts | 3 ++- src/commands/gitCommands.actions.ts | 17 ++++++++++++++--- src/git/gitUri.ts | 9 ++++++++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed888b1..0dfd572 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ## Fixed +- Fixes [#2033](https://github.com/gitkraken/vscode-gitlens/issues/2033) - Diffing, applying, and restoring untracked files in a stash doesn't work - Fixes [#2028](https://github.com/gitkraken/vscode-gitlens/issues/2028) - Branch names with special characters '<' also causes errors on the command line — thanks to [PR #2030](https://github.com/gitkraken/vscode-gitlens/pull/2030) by mcy-kylin ([@mcy-kylin](https://github.com/mcy-kylin)) - Fixes [#2028](https://github.com/gitkraken/vscode-gitlens/issues/2028) - Branch names with special characters like ';$|>' causes errors on the command line (terminal executed git commands) - Fixes [#2021](https://github.com/gitkraken/vscode-gitlens/issues/2021) - GitLab remote provider uses legacy routes — thanks to [PR #2022](https://github.com/gitkraken/vscode-gitlens/pull/2022) by Brian Williams ([@Brcrwilliams](https://github.com/Brcrwilliams)) @@ -4211,7 +4212,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Initial release but still heavily a work in progress. -[Unreleased]: https://github.com/gitkraken/vscode-gitlens/compare/v12.0.7...HEAD +[unreleased]: https://github.com/gitkraken/vscode-gitlens/compare/v12.0.7...HEAD [12.0.6]: https://github.com/gitkraken/vscode-gitlens/compare/v12.0.6...gitkraken:v12.0.7 [12.0.6]: https://github.com/gitkraken/vscode-gitlens/compare/v12.0.5...gitkraken:v12.0.6 [12.0.5]: https://github.com/gitkraken/vscode-gitlens/compare/v12.0.4...gitkraken:v12.0.5 diff --git a/src/commands/diffWithPrevious.ts b/src/commands/diffWithPrevious.ts index 377fc13..8472c0a 100644 --- a/src/commands/diffWithPrevious.ts +++ b/src/commands/diffWithPrevious.ts @@ -56,7 +56,8 @@ export class DiffWithPreviousCommand extends ActiveEditorCommand { uri: args.commit.file.originalUri ?? args.commit.file.uri, }, rhs: { - sha: args.commit.sha || '', + // If the file is `?` (untracked), then this must be a stash, so get the ^3 commit to access the untracked file + sha: args.commit.file.status === '?' ? `${args.commit.sha}^3` : args.commit.sha || '', uri: args.commit.file.uri, }, line: args.line, diff --git a/src/commands/gitCommands.actions.ts b/src/commands/gitCommands.actions.ts index a48c35a..03a62e7 100644 --- a/src/commands/gitCommands.actions.ts +++ b/src/commands/gitCommands.actions.ts @@ -169,9 +169,16 @@ export namespace GitActions { ) { // Open the working file to ensure undo will work void (await GitActions.Commit.openFile(file, ref1, { preserveFocus: true, preview: false })); + + let ref = ref1.ref; + // If the file is `?` (untracked), then this must be a stash, so get the ^3 commit to access the untracked file + if (typeof file !== 'string' && file.status === '?') { + ref = `${ref}^3`; + } + void (await Container.instance.git.applyChangesToWorkingFile( - GitUri.fromFile(file, ref1.repoPath, ref1.ref), - ref1.ref, + GitUri.fromFile(file, ref1.repoPath, ref), + ref, ref2?.ref, )); } @@ -502,7 +509,10 @@ export namespace GitActions { options = refOrOptions as TextDocumentShowOptions; } else { const ref = refOrOptions as GitRevisionReference; + uri = GitUri.fromFile(fileOrUri, ref.repoPath, ref.ref); + // If the file is `?` (untracked), then this must be an untracked file in a stash, so just return + if (typeof fileOrUri !== 'string' && fileOrUri.status === '?') return; } options = { preserveFocus: true, preview: false, ...options }; @@ -663,7 +673,8 @@ export namespace GitActions { ref = revision.ref; } else { path = file.path; - ref = file.status === 'D' ? `${revision.ref}^` : revision.ref; + ref = + file.status === `?` ? `${revision.ref}^3` : file.status === 'D' ? `${revision.ref}^` : revision.ref; } void (await Container.instance.git.checkout(revision.repoPath, ref, { path: path })); diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index 8bd19b9..6964aca 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -230,7 +230,14 @@ export class GitUri extends (Uri as any as UriEx) { typeof file === 'string' ? file : (original && file.originalPath) || file.path, repoPath, ); - return !ref ? new GitUri(uri, repoPath) : new GitUri(uri, { repoPath: repoPath, sha: ref }); + + return !ref + ? new GitUri(uri, repoPath) + : new GitUri(uri, { + repoPath: repoPath, + // If the file is `?` (untracked), then this must be a stash, so get the ^3 commit to access the untracked file + sha: typeof file !== 'string' && file.status === '?' ? `${ref}^3` : ref, + }); } static fromRepoPath(repoPath: string, ref?: string) {