From 0c50003b78a5543d0d0624a32cc3bb0eca729050 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Fri, 20 Sep 2019 23:17:03 -0600 Subject: [PATCH 1/4] Add tab expansion support for git restore / switch --- src/GitParamTabExpansion.ps1 | 15 +++++++++++++++ src/GitTabExpansion.ps1 | 33 +++++++++++++++++++++++---------- test/TabExpansion.Tests.ps1 | 11 +++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/GitParamTabExpansion.ps1 b/src/GitParamTabExpansion.ps1 index 1ee5f4782..9ce494af8 100644 --- a/src/GitParamTabExpansion.ps1 +++ b/src/GitParamTabExpansion.ps1 @@ -28,12 +28,14 @@ $shortGitParams = @{ rebase = 'm s X S q v n C f i p x' remote = 'v' reset = 'q p' + restore = 's p W S q m' revert = 'e m n S s X' rm = 'f n r q' shortlog = 'n s e w' stash = 'p k u a q' status = 's b u z' submodule = 'q b f n N' + switch = 'c C d f m q t' tag = 'a s u f d v n l m F' whatchanged = 'p' } @@ -70,6 +72,7 @@ $longGitParams = @{ reflog = 'stale-fix expire= expire-unreachable= all updateref rewrite verbose' remote = 'verbose' reset = 'patch quiet soft mixed hard merge keep' + restore = 'source= patch worktree staged quiet progress no-progress ours theirs merge conflict= ignore-unmerged ignore-skip-worktree-bits overlay no-overlay' revert = 'edit mainline no-edit no-commit gpg-sign signoff strategy= strategy-option continue quit abort' rm = 'force dry-run cached ignore-unmatch quiet' shortlog = 'numbered summary email format=' @@ -77,6 +80,7 @@ $longGitParams = @{ stash = 'patch no-keep-index keep-index include-untracked all quiet index' status = 'short branch porcelain long untracked-files ignore-submodules ignored column no-column' submodule = 'quiet branch force cached files summary-limit remote no-fetch checkout merge rebase init name reference recursive depth' + switch = 'create force-create detach guess no-guess force discard-changes merge conflict= quiet no-progress track no-track orphan ignore-other-worktrees recurse-submodules no-recurse-submodules' tag = 'annotate sign local-user force delete verify list sort column no-column contains points-at message file cleanup' whatchanged = 'since' } @@ -149,6 +153,14 @@ $gitParamValues = @{ rebase = @{ strategy = 'resolve recursive octopus ours subtree' } + restore = @{ + conflict = 'merge diff3' + source = { + param($ref) + gitBranches $matches['ref'] $true + gitTags $matches['ref'] + } + } revert = @{ strategy = 'resolve recursive octopus ours subtree' } @@ -161,4 +173,7 @@ $gitParamValues = @{ 'untracked-files' = 'no normal all' 'ignore-submodules' = 'none untracked dirty all' } + switch = @{ + conflict = 'merge diff3' + } } diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index 10af884f5..3e3abc3aa 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -35,8 +35,8 @@ function script:gitCmdOperations($commands, $command, $filter) { $script:someCommands = @('add','am','annotate','archive','bisect','blame','branch','bundle','checkout','cherry', 'cherry-pick','citool','clean','clone','commit','config','describe','diff','difftool','fetch', 'format-patch','gc','grep','gui','help','init','instaweb','log','merge','mergetool','mv', - 'notes','prune','pull','push','rebase','reflog','remote','rerere','reset','revert','rm', - 'shortlog','show','stash','status','submodule','svn','tag','whatchanged', 'worktree') + 'notes','prune','pull','push','rebase','reflog','remote','rerere','reset','restore','revert','rm', + 'shortlog','show','stash','status','submodule','svn','switch','tag','whatchanged', 'worktree') $script:gitCommandsWithLongParams = $longGitParams.Keys -join '|' $script:gitCommandsWithShortParams = $shortGitParams.Keys -join '|' @@ -218,10 +218,16 @@ function script:expandShortParams($cmd, $filter) { } function script:expandParamValues($cmd, $param, $filter) { - $gitParamValues[$cmd][$param] -split ' ' | - Where-Object { $_ -like "$filter*" } | - Sort-Object | - ForEach-Object { -join ("--", $param, "=", $_) } + $paramValues = $gitParamValues[$cmd][$param] + + $completions = if ($paramValues -is [scriptblock]) { + & $paramValues $filter | Where-Object { $_ -like "$filter*" } + } + else { + $paramValues.Trim() -split ' ' | Where-Object { $_ -like "$filter*" } | Sort-Object + } + + $completions | ForEach-Object { -join ("--", $param, "=", $_) } } function Expand-GitCommand($Command) { @@ -337,8 +343,15 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { gitAddFiles $GitStatus $matches['files'] } - # Handles git checkout -- - "^checkout.* -- (?\S*)$" { + # Handles git restore -s - must come before the next regex case + "^restore.* -s\s*(?\S*)$" { + gitBranches $matches['ref'] $true + gitTags $matches['ref'] + break + } + + # Handles git checkout -- and git restore + "^(?:checkout.* --|restore.*) (?\S*)$" { gitCheckoutFiles $GitStatus $matches['files'] } @@ -357,8 +370,8 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { gitMergeFiles $GitStatus $matches['files'] } - # Handles git checkout - "^(?:checkout).* (?\S*)$" { + # Handles git checkout|switch + "^(?:checkout|switch).* (?\S*)$" { & { gitBranches $matches['ref'] $true gitRemoteUniqueBranches $matches['ref'] diff --git a/test/TabExpansion.Tests.ps1 b/test/TabExpansion.Tests.ps1 index 8e0571c00..20dda97c9 100644 --- a/test/TabExpansion.Tests.ps1 +++ b/test/TabExpansion.Tests.ps1 @@ -127,6 +127,17 @@ Describe 'TabExpansion Tests' { } } + Context 'Restore Source Branch TabExpansion Tests' { + It 'Tab completes source branches -s' { + $result = & $module GitTabExpansionInternal 'git restore -s mas' + $result | Should BeExactly 'master' + } + It 'Tab completes source branches --source=' { + $result = & $module GitTabExpansionInternal 'git restore --source=mas' + $result | Should BeExactly '--source=master' + } + } + Context 'Add/Reset/Checkout TabExpansion Tests' { BeforeEach { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] From ad58a4016fb76d0f8e7f87586dacb9bc4e8540c8 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 26 Sep 2019 20:22:43 -0600 Subject: [PATCH 2/4] Address PR feedback, case-insenstive match -s and handle -S/--staged --- src/GitTabExpansion.ps1 | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index 3e3abc3aa..a6a17719a 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -165,6 +165,10 @@ function script:gitCheckoutFiles($GitStatus, $filter) { gitFiles $filter (@($GitStatus.Working.Unmerged) + @($GitStatus.Working.Modified) + @($GitStatus.Working.Deleted)) } +function script:gitDeleted($GitStatus, $filter) { + gitFiles $filter $GitStatus.Working.Deleted +} + function script:gitDiffFiles($GitStatus, $filter, $staged) { if ($staged) { gitFiles $filter $GitStatus.Index.Modified @@ -178,8 +182,13 @@ function script:gitMergeFiles($GitStatus, $filter) { gitFiles $filter $GitStatus.Working.Unmerged } -function script:gitDeleted($GitStatus, $filter) { - gitFiles $filter $GitStatus.Working.Deleted +function script:gitRestoreFiles($GitStatus, $filter, $staged) { + if ($staged) { + gitFiles $filter (@($GitStatus.Index.Added) + @($GitStatus.Index.Modified) + @($GitStatus.Index.Deleted)) + } + else { + gitFiles $filter (@($GitStatus.Working.Unmerged) + @($GitStatus.Working.Modified) + @($GitStatus.Working.Deleted)) + } } function script:gitAliases($filter) { @@ -343,16 +352,21 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { gitAddFiles $GitStatus $matches['files'] } + # Handles git checkout -- + "^checkout.* -- (?\S*)$" { + gitCheckoutFiles $GitStatus $matches['files'] + } + # Handles git restore -s - must come before the next regex case - "^restore.* -s\s*(?\S*)$" { + "^restore.* (?-i)-s\s*(?\S*)$" { gitBranches $matches['ref'] $true gitTags $matches['ref'] break } - # Handles git checkout -- and git restore - "^(?:checkout.* --|restore.*) (?\S*)$" { - gitCheckoutFiles $GitStatus $matches['files'] + # Handles git restore + "^restore(?:.* (?(?:-S|--staged))|.*) (?\S*)$" { + gitRestoreFiles $GitStatus $matches['files'] $matches['staged'] } # Handles git rm From 1a820274fcf1ce218b9201c81324c4d543251900 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sun, 6 Oct 2019 17:22:27 -0600 Subject: [PATCH 3/4] Address PR feedback - case-sensitive match on -S --- src/GitTabExpansion.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index a6a17719a..8166e6333 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -365,7 +365,7 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { } # Handles git restore - "^restore(?:.* (?(?:-S|--staged))|.*) (?\S*)$" { + "^restore(?:.* (?(?:(?-i)-S|--staged))|.*) (?\S*)$" { gitRestoreFiles $GitStatus $matches['files'] $matches['staged'] } From 559eeb80718ccf5020f0c14d528b131dafee3e0e Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 8 Feb 2020 18:28:26 +0000 Subject: [PATCH 4/4] Fix git user details breaking Tests From https://github.com/dahlbyk/posh-git/blob/b428c3ceb8dd54f447d545b37db37636724cbe55/test/Get-GitDirectory.Tests.ps1#L53-L55 --- test/Get-GitDirectory.Tests.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/Get-GitDirectory.Tests.ps1 b/test/Get-GitDirectory.Tests.ps1 index c3053746b..b2a06f6fd 100644 --- a/test/Get-GitDirectory.Tests.ps1 +++ b/test/Get-GitDirectory.Tests.ps1 @@ -40,6 +40,11 @@ Describe 'Get-GitDiretory Tests' { git init $repoPath Set-Location $repoPath + + # Git rid of Git warnings about configuring user for the commit we do below + git config user.email "you@example.com" + git config user.name "Pester User" + 'foo' > .\README.md git add .\README.md # Quoting is a hack due to our use of the global:git function and how it converts args for invoke-expression