3

Currently, I'm writing a PowerShell module which automatically configures aliases for all git commands, inspired by git-sh.

Then I wrote functions below.
The Enable-GitAliases function is the entry point to configure aliases automatically.
it collects git's subcommands by Get-GitCommands, which parses git --help -a to get all git's subcommands.
Then it defines the wrapper functions for the collected git commands.

My question is: why is git --help -a called so many times (possibly infinitely) when invoking Enable-GitAliases, which causing significant slow down?

After writing the code, I found Enable-GitAliases takes too much time (I've never seen it finishes).
According to the Task Manager, the git --help -a command is launched and exits repeatedly.
I expected the git --help -a command is called only once.
Actually, Get-GitCommands | % { echo $_ } calls git --help -a only once.
What is the difference, and what is best way to fix?

function Get-GitCommands {
  -Split (git --help -a | select-string -pattern '^  [-a-zA-Z0-9.]+\s*')
}

function Enable-GitAliases($avoidConflicts = $true) {
  Get-GitCommands | % {
    $aliasName = $_
    if (-not ($avoidConflicts -and (Get-Command $aliasName 2> $null) -ne $null)) {
      Enable-GitAliases $aliasName
    }
  }
}

function Enable-GitAlias($commandName) {
  $wrapper = @'
    function global:{0} {{
      git {0} $args
    }}
'@ -f $commandName
  Invoke-Expression $wrapper
}

1 Answer 1

3

You call Enable-GitAliases recursively, but is this intended?

Is your intention this?

function Enable-GitAliases($avoidConflicts = $true) {
  Get-GitCommands | % {
    $aliasName = $_
    if (-not ($avoidConflicts -and (Get-Command $aliasName 2> $null) -ne $null)) {
      # Enable-GitAliases -> Enable-GitAlias
      Enable-GitAlias $aliasName
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.