Terminal.Gui.PowerShell.Git.psm1 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <#
  2. .SYNOPSIS
  3. Creates a new branch with the specified name.
  4. .DESCRIPTION
  5. Creates a new branch with the specified name.
  6. .PARAMETER Name
  7. The name of the new branch.
  8. Always required.
  9. Must match the .net regex pattern "v2_\d{4}_[a-zA-Z0-9()_-]+".
  10. Must also otherwise be a valid identifier for a git branch and follow any other project guidelines.
  11. .PARAMETER NoSwitch
  12. If specified, does not automatically switch to your new branch after creating it.
  13. Default is to switch to the new branch after creating it.
  14. .PARAMETER Push
  15. If specified, automatically pushes the new branch to your remote after creating it.
  16. .PARAMETER Remote
  17. The name of the git remote, as configured.
  18. If you never explicitly set this yourself, it is typically "origin".
  19. If you only have one remote defined or have not explicitly set a remote yourself, do not provide this parameter; It will be detected automatically.
  20. .INPUTS
  21. None
  22. .OUTPUTS
  23. The name of the current branch after the operation, as a String.
  24. If NoSwitch was specified and the operation succeeded, this should be the source branch.
  25. If NoSwith was not specified or was explicitly set to $false and the operation succeeded, this should be the new branch.
  26. If an exception occurs, does not return. Exceptions are unhandled and are the responsibility of the caller.
  27. .NOTES
  28. Errors thrown by git commands are not explicitly handled.
  29. #>
  30. Function New-GitBranch {
  31. [CmdletBinding(PositionalBinding=$false, SupportsShouldProcess=$true, ConfirmImpact="Low", DefaultParameterSetName="Basic")]
  32. param(
  33. [Parameter(Mandatory=$true, ParameterSetName="Basic")]
  34. [Parameter(Mandatory=$true, ParameterSetName="NoSwitch")]
  35. [Parameter(Mandatory=$true, ParameterSetName="Push")]
  36. [ValidatePattern("v2_\d{4}_[a-zA-Z0-9()_-]+")]
  37. [string]$Name,
  38. [Parameter(Mandatory=$true,ParameterSetName="NoSwitch",DontShow)]
  39. [switch]$NoSwitch,
  40. [Parameter(Mandatory=$false, ParameterSetName="Basic")]
  41. [Parameter(Mandatory=$true, ParameterSetName="Push")]
  42. [switch]$Push,
  43. [Parameter(Mandatory=$false, ParameterSetName="Push")]
  44. [string]$Remote = $null
  45. )
  46. $currentBranch = (& git branch --show-current)
  47. if(!$PSCmdlet.ShouldProcess("Creating new branch named $Name from $currentBranch", $Name, "Creating branch")) {
  48. return $null
  49. }
  50. git branch $Name
  51. if(!$NoSwitch) {
  52. git switch $Name
  53. if($Push) {
  54. if([String]::IsNullOrWhiteSpace($Remote)) {
  55. $tempRemotes = (git remote show)
  56. if($tempRemotes -is [array]){
  57. # If we've gotten here, Push was specified, a remote was not specified or was blank, and there are multiple remotes defined locally.
  58. # Not going to support that. Just error out.
  59. Remove-Variable tempRemotes
  60. throw "No Remote specified and multiple remotes are defined. Cannot continue."
  61. } else {
  62. # Push is set, Remote wasn't, but there's only one defined. Safe to continue. Use the only remote.
  63. $Remote = $tempRemotes
  64. Remove-Variable tempRemotes
  65. }
  66. }
  67. # Push is set, and either Remote was specified or there's only one remote defined and we will use that.
  68. # Perform the push.
  69. git push --set-upstream $Remote $Name
  70. }
  71. } else{
  72. # NoSwitch was specified.
  73. # Return the current branch name.
  74. return $currentBranch
  75. }
  76. # If we made it to this point, return the Name that was specified.
  77. return $Name
  78. }
  79. <#
  80. .SYNOPSIS
  81. Checks if the command 'git' is available in the current session.
  82. .DESCRIPTION
  83. Checks if the command 'git' is available in the current session.
  84. Throws an error if not.
  85. Returns $true if git is available.
  86. Only intended for use in scripts and module manifests.
  87. .INPUTS
  88. None
  89. .OUTPUTS
  90. If git exists, $true.
  91. Otherwise, $false.
  92. #>
  93. Function Test-GitAvailable {
  94. [OutputType([Boolean])]
  95. [CmdletBinding()]
  96. param()
  97. if($null -eq (Get-Command git -ErrorAction Ignore)) {
  98. Write-Error -Message "git was not found. Git functionality will not work." -Category ObjectNotFound -TargetObject "git"
  99. return $false
  100. }
  101. return $true
  102. }
  103. Test-GitAvailable -ErrorAction Continue