From 4993310ca9f3a20e699dc44f1f0b43356d18424d Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Thu, 11 Jul 2024 16:31:30 -0400 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 04/12] 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 05/12] 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 06/12] 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 07/12] 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 08/12] 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= From 21a8e478b6ff18eb1a7d6f01abffe6d095e70df9 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Wed, 16 Jul 2025 19:19:01 -0400 Subject: [PATCH 09/12] Revert Makefile changes --- Makefile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Makefile b/Makefile index bf686c8..f86ef52 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,7 @@ # build the binary # note starting in go1.18 vcs information will be available via go version -m build: - # $(eval VERSION := $(shell git describe --tags HEAD 2>/dev/null || echo unknown)) - - # Add Git commit and branch to version string - $(eval VERSION := $(shell V=$$(git describe --tags HEAD 2>/dev/null); B=$$(git rev-parse --abbrev-ref HEAD 2>/dev/null); echo "$$V-$$B")) + $(eval VERSION := $(shell git describe --tags HEAD 2>/dev/null || echo unknown)) @echo "Building as version: $(VERSION)" go build -o bin/scmpuff -mod=readonly -ldflags "-X main.version=$(VERSION)" From c357546995c983954ffcc1bff3034d16a7d07517 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Wed, 16 Jul 2025 20:00:27 -0400 Subject: [PATCH 10/12] Embed VERSION for --- Makefile | 6 ++---- VERSION | 1 + main.go | 8 +++++--- 3 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 VERSION diff --git a/Makefile b/Makefile index f86ef52..bc5e21e 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,8 @@ # build the binary # note starting in go1.18 vcs information will be available via go version -m build: - $(eval VERSION := $(shell git describe --tags HEAD 2>/dev/null || echo unknown)) - - @echo "Building as version: $(VERSION)" - go build -o bin/scmpuff -mod=readonly -ldflags "-X main.version=$(VERSION)" + git describe --tags > VERSION + go build -o bin/scmpuff -mod=readonly # run unit tests test: diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..7cb335f --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +v0.5.0-powershell-1-g21a8e47 diff --git a/main.go b/main.go index 1a422cc..9446ddd 100644 --- a/main.go +++ b/main.go @@ -10,11 +10,13 @@ import ( "github.com/kmatt/scmpuff/internal/commands/status" "github.com/spf13/cobra" + + _ "embed" ) -// version is the default version of the program -// ...in almost all cases this should be overriden by the buildscript. -var version = "0.0.0-development" +// Set version from git tag +//go:embed VERSION +var version string var puffCmd = &cobra.Command{ Use: "scmpuff", From 820d5bd7b0880fc72052d374decc89840c11c1e2 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Wed, 16 Jul 2025 20:36:04 -0400 Subject: [PATCH 11/12] Set version in main.go on make --- Makefile | 5 +++-- VERSION | 1 - main.go | 4 +--- 3 files changed, 4 insertions(+), 6 deletions(-) delete mode 100644 VERSION diff --git a/Makefile b/Makefile index bc5e21e..703dbfa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ # build the binary -# note starting in go1.18 vcs information will be available via go version -m build: - git describe --tags > VERSION + # Set version in main.go + $(eval VERSION := $(shell git describe --tags HEAD 2>/dev/null || echo unknown)) + $(shell sed -i -e "s/^var version = \"0.0.0\"/var version = \"$(VERSION)\"/" main.go) go build -o bin/scmpuff -mod=readonly # run unit tests diff --git a/VERSION b/VERSION deleted file mode 100644 index 7cb335f..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -v0.5.0-powershell-1-g21a8e47 diff --git a/main.go b/main.go index 9446ddd..947abce 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,7 @@ import ( _ "embed" ) -// Set version from git tag -//go:embed VERSION -var version string +var version = "0.0.0" var puffCmd = &cobra.Command{ Use: "scmpuff", From 693ef1b993cf76cd6dc78ca42f1d91e40f1b1709 Mon Sep 17 00:00:00 2001 From: Matt Keranen Date: Wed, 16 Jul 2025 21:37:41 -0400 Subject: [PATCH 12/12] Set version in main.go on make --- Makefile | 7 +++---- main.go | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 703dbfa..da16e6e 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,8 @@ -# build the binary +# set version in main.go for `go install` and build binary build: - # Set version in main.go $(eval VERSION := $(shell git describe --tags HEAD 2>/dev/null || echo unknown)) - $(shell sed -i -e "s/^var version = \"0.0.0\"/var version = \"$(VERSION)\"/" main.go) - go build -o bin/scmpuff -mod=readonly + $(shell sed -E -i '' -e "s/^var version = \"[A-Za-z0-9\.-_]+\"/var version = \"$(VERSION)\"/" main.go) + go build -o bin/scmpuff -mod=readonly -ldflags "-X main.version=$(VERSION)" # run unit tests test: diff --git a/main.go b/main.go index 947abce..b983317 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( _ "embed" ) -var version = "0.0.0" +var version = "v0.5.0" var puffCmd = &cobra.Command{ Use: "scmpuff",