From 4993310ca9f3a20e699dc44f1f0b43356d18424d Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 16:31:30 -0400 Subject: [PATCH 1/8] Git wrapper from https://github.com/mroth/scmpuff/issues/14#issuecomment-291001581 --- commands/inits/data/git_wrapper.ps1 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 commands/inits/data/git_wrapper.ps1 diff --git a/commands/inits/data/git_wrapper.ps1 b/commands/inits/data/git_wrapper.ps1 new file mode 100644 index 0000000..8b881f0 --- /dev/null +++ b/commands/inits/data/git_wrapper.ps1 @@ -0,0 +1,22 @@ +Remove-Item alias:\git +Remove-Item function:\git + +$SCMPUFF_GIT_CMD = Get-Command git | Select-Object -ExpandProperty Definition + +function git { + switch -regex -casesensitive($args[0]) { + "^(commit|blame|log|rebase|merge)$" { + & scmpuff expand -- $SCMPUFF_GIT_CMD $args + } + "^(checkout|diff|rm|reset)$" { + & scmpuff expand --relative -- $SCMPUFF_GIT_CMD $args + } + "^add$" { + & scmpuff expand -- $SCMPUFF_GIT_CMD $args + scmpuff status + } + default { + & $SCMPUFF_GIT_CMD $args + } + } +} From dad5da6b4bb0c01f3de6533d59509b8c5e5a37cf Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 16:33:15 -0400 Subject: [PATCH 2/8] Powershell aliases --- commands/inits/data/aliases.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 commands/inits/data/aliases.ps1 diff --git a/commands/inits/data/aliases.ps1 b/commands/inits/data/aliases.ps1 new file mode 100644 index 0000000..a8067f4 --- /dev/null +++ b/commands/inits/data/aliases.ps1 @@ -0,0 +1,6 @@ +function gs { scmpuff_status } +function ga { git add } +function gd { git diff } +function gl { git log } +function gco { git checkout } +function grs { git reset } From 566b007eddaf35b761a17f76fc260724ff2b9322 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 16:52:12 -0400 Subject: [PATCH 3/8] Ignore Windows executables --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 565462b..aad2bdc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tmp/ _site/ Gemfile.lock .goxc.local.json +*.exe From 7786d01404df1cfa58ffcf1362f01f8339cb3695 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 16:52:31 -0400 Subject: [PATCH 4/8] Powershell init script --- commands/inits/init.go | 8 ++++++-- commands/inits/scripts.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/commands/inits/init.go b/commands/inits/init.go index 5aaca0f..7369d95 100644 --- a/commands/inits/init.go +++ b/commands/inits/init.go @@ -53,6 +53,10 @@ There are a number of flags to customize the shell integration. fmt.Println(fishCollection.Output(wrapGit, includeAliases)) os.Exit(0) + case "pwsh": + fmt.Println(pwshCollection.Output(wrapGit, includeAliases)) + os.Exit(0) + default: fmt.Fprintf(os.Stderr, "Unrecognized shell '%s'\n", shellType) os.Exit(1) @@ -88,7 +92,7 @@ There are a number of flags to customize the shell integration. InitCmd.Flags().StringVarP( &shellType, "shell", "s", "", - "Output shell type: sh | bash | zsh | fish", + "Output shell type: sh | bash | zsh | fish | pwsh", ) InitCmd.Flag("shell").NoOptDefVal = defaultShellType() @@ -101,7 +105,7 @@ func defaultShellType() string { if shellenv, ok := os.LookupEnv("SHELL"); ok { base := filepath.Base(shellenv) switch base { - case "sh", "bash", "zsh", "fish": + case "sh", "bash", "zsh", "fish", "pwsh": return base } } diff --git a/commands/inits/scripts.go b/commands/inits/scripts.go index 6787679..e6b25ab 100644 --- a/commands/inits/scripts.go +++ b/commands/inits/scripts.go @@ -11,6 +11,9 @@ var scriptStatusShortcuts string //go:embed data/status_shortcuts.fish var scriptStatusShortcutsFish string +////go:embed data/status_shortcuts.pwsh +//var scriptStatusShortcutsPwsh string + //go:embed data/aliases.sh var scriptAliases string @@ -20,6 +23,9 @@ var scriptGitWrapper string //go:embed data/git_wrapper.fish var scriptGitWrapperFish string +//go:embed data/git_wrapper.ps1 +var scriptGitWrapperPwsh string + type scriptCollection struct { statusShortcuts string gitWrapper string @@ -38,6 +44,12 @@ var fishCollection = scriptCollection{ aliases: scriptAliases, } +var pwshCollection = scriptCollection{ + //statusShortcuts: scriptStatusShortcutsPwsh, + gitWrapper: scriptGitWrapperPwsh, + aliases: scriptAliases, +} + func (sc scriptCollection) Output(wrapGit, aliases bool) string { var b strings.Builder b.WriteString(sc.statusShortcuts) From 030a3f501b720b4461b988a0e456e822c7f1334b Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 19:22:16 -0400 Subject: [PATCH 5/8] Powershell status commands --- commands/inits/data/git_wrapper.ps1 | 2 +- commands/inits/data/status_shortcuts.ps1 | 42 ++++++++++++++++++++++++ commands/inits/scripts.go | 6 ++-- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 commands/inits/data/status_shortcuts.ps1 diff --git a/commands/inits/data/git_wrapper.ps1 b/commands/inits/data/git_wrapper.ps1 index 8b881f0..9dc4a15 100644 --- a/commands/inits/data/git_wrapper.ps1 +++ b/commands/inits/data/git_wrapper.ps1 @@ -13,7 +13,7 @@ function git { } "^add$" { & scmpuff expand -- $SCMPUFF_GIT_CMD $args - scmpuff status + scmpuff_status } default { & $SCMPUFF_GIT_CMD $args diff --git a/commands/inits/data/status_shortcuts.ps1 b/commands/inits/data/status_shortcuts.ps1 new file mode 100644 index 0000000..eae3826 --- /dev/null +++ b/commands/inits/data/status_shortcuts.ps1 @@ -0,0 +1,42 @@ +function scmpuff_status { + $scmpuff_env_char = "e" + + # Run scmpuff status command and capture the output + $cmd_output = & scmpuff status --filelist @args + + # if there was an error, exit prematurely, and pass along the exit code + $es = $LastExitCode + if ($es -ne 0) { + return $es + } + + # Fetch list of files (from first line of script output) + $files = ($cmd_output | Select-Object -First 1) -split '\s+' + + # Export numbered env variables for each file + scmpuff_clear_vars + $e = 1 + foreach ($file in $files) { + Set-Item "env:$($scmpuff_env_char + $e)" $file + $e++ + } + + # Print status (from line two onward) + $cmd_output | Select-Object -Skip 1 +} + +# Clear numbered env variables +function scmpuff_clear_vars { + # Define the environment variable character + $scmpuff_env_char = "e" + + # Iterate through environment variables and unset those starting with 'e' + for ($i = 1; $i -le 999; $i++) { + $env_var_i = $scmpuff_env_char + $i + if (Get-Item "env:$($env_var_i)" -ErrorAction Ignore) { + Remove-Item "env:$env_var_i" + } else { + break + } + } +} diff --git a/commands/inits/scripts.go b/commands/inits/scripts.go index e6b25ab..c221210 100644 --- a/commands/inits/scripts.go +++ b/commands/inits/scripts.go @@ -11,8 +11,8 @@ var scriptStatusShortcuts string //go:embed data/status_shortcuts.fish var scriptStatusShortcutsFish string -////go:embed data/status_shortcuts.pwsh -//var scriptStatusShortcutsPwsh string +//go:embed data/status_shortcuts.ps1 +var scriptStatusShortcutsPwsh string //go:embed data/aliases.sh var scriptAliases string @@ -45,7 +45,7 @@ var fishCollection = scriptCollection{ } var pwshCollection = scriptCollection{ - //statusShortcuts: scriptStatusShortcutsPwsh, + statusShortcuts: scriptStatusShortcutsPwsh, gitWrapper: scriptGitWrapperPwsh, aliases: scriptAliases, } From 01664caf7e57a6c8a4967cba5b87849492a9bbf8 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 19:41:48 -0400 Subject: [PATCH 6/8] Fix git alias --- commands/inits/data/git_wrapper.ps1 | 9 +++------ commands/inits/scripts.go | 5 ++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/commands/inits/data/git_wrapper.ps1 b/commands/inits/data/git_wrapper.ps1 index 9dc4a15..d9c24a5 100644 --- a/commands/inits/data/git_wrapper.ps1 +++ b/commands/inits/data/git_wrapper.ps1 @@ -1,18 +1,15 @@ -Remove-Item alias:\git -Remove-Item function:\git - $SCMPUFF_GIT_CMD = Get-Command git | Select-Object -ExpandProperty Definition function git { switch -regex -casesensitive($args[0]) { "^(commit|blame|log|rebase|merge)$" { - & scmpuff expand -- $SCMPUFF_GIT_CMD $args + & scmpuff exec -- $SCMPUFF_GIT_CMD $args } "^(checkout|diff|rm|reset)$" { - & scmpuff expand --relative -- $SCMPUFF_GIT_CMD $args + & scmpuff exec --relative -- $SCMPUFF_GIT_CMD $args } "^add$" { - & scmpuff expand -- $SCMPUFF_GIT_CMD $args + & scmpuff exec -- $SCMPUFF_GIT_CMD $args scmpuff_status } default { diff --git a/commands/inits/scripts.go b/commands/inits/scripts.go index c221210..d50a59c 100644 --- a/commands/inits/scripts.go +++ b/commands/inits/scripts.go @@ -17,6 +17,9 @@ var scriptStatusShortcutsPwsh string //go:embed data/aliases.sh var scriptAliases string +//go:embed data/aliases.ps1 +var scriptAliasesPwsh string + //go:embed data/git_wrapper.sh var scriptGitWrapper string @@ -47,7 +50,7 @@ var fishCollection = scriptCollection{ var pwshCollection = scriptCollection{ statusShortcuts: scriptStatusShortcutsPwsh, gitWrapper: scriptGitWrapperPwsh, - aliases: scriptAliases, + aliases: scriptAliasesPwsh, } func (sc scriptCollection) Output(wrapGit, aliases bool) string { From ca69313f5eaacab3a0bd6059c925f546a70b9fad Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 19:46:25 -0400 Subject: [PATCH 7/8] Add Powershell init example --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8cd8e43..2a0b46e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,10 @@ This will define the scmpuff shell functions as well as some handy shortcuts. [fish]: https://fishshell.com/ +For Powershell on Windows, add the following to your `$PROFILE` file: + + scmpuff init --shell=pwsh | Out-String | Invoke-Expression + ## Usage From 836ec6f5e6a0db5cece4d04be6bee60fc418b9e5 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 19:50:01 -0400 Subject: [PATCH 8/8] Update Go modules --- go.mod | 4 ++-- go.sum | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index c7cea41..bb5d0a7 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,9 @@ module github.com/mroth/scmpuff go 1.18 -require github.com/spf13/cobra v1.4.0 +require github.com/spf13/cobra v1.8.1 require ( - github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 0dd8697..912390a 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= -github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=