diff --git a/CHANGELOG.md b/CHANGELOG.md index 7243d569..29acee5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,31 @@ > Love It? [Support It](https://github.com/sponsors/StartAutomating) +## ugit 0.4.7 + +* `git status` improvement + * `git.status.ahead` (#358) + * `git.status.behind` (#359) + * `git.status.modified` (#356) + * `git.status.deleted` (#357) + * `git.status.addded` (#361) + * `git.status` formatting improvement (#363) + * `git.status.WorkingDirectory` (#365) + * `git.status.Unmerged` (#367) + * `git.status.Diff` (#364) + * `git status --short` support (#343) + * Performance and fault tolerance (#366) +* Removing IO.FileInfo extended properties (#369) +* Internationalization support (#177) +* New Functions: + * `Get-GitPrompt` (#368) + * `Get-GitOctal` (#370) + +To use the new prompt, you can simply: + +Set-Alias prompt git.prompt + +--- ## ugit 0.4.6: diff --git a/Commands/Get-GitOctal.ps1 b/Commands/Get-GitOctal.ps1 new file mode 100644 index 00000000..5a53ebb6 --- /dev/null +++ b/Commands/Get-GitOctal.ps1 @@ -0,0 +1,48 @@ +function Get-GitOctal { + <# + .SYNOPSIS + Gets Git Octal strings + .DESCRIPTION + Gets Git Octal strings as the current output encoding. + #> + [Alias('git.octal')] + param( + # An octal string + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $OctalString + ) + + process { + # So we want to decode an octal string from git. + [Regex]::Replace( + # First we trim leading and trailing quotes. Easy. + $OctalString -replace '^"|"$', + # Then we look for any sequence of slash + 3 digits [0-7] + "(?:\\[0-7]{3}){1,}", + # And replace them with this short script + { + param($match) + # We want them back as UTF8 + $OutputEncoding.GetString( + # But these bytes are encoded with + # [ISO-8859-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1) + [Text.Encoding]::GetEncoding('ISO-8859-1').GetBytes( + # The match contains the sequence + @( + foreach ( + # so we split it up + # (which removes the slash) + $octalByte in $match -split '\\' -ne '' + ) { + # then we convert each byte in base 8 + [Convert]::ToByte($octalByte,8) -as + [char] # and make it a character + } + ) -join '' # then we join our characters, + ) # get their bytes, + ) # and output the replaced string. + } + ) + } +} diff --git a/Commands/Get-GitPrompt.ps1 b/Commands/Get-GitPrompt.ps1 new file mode 100644 index 00000000..ef47fd68 --- /dev/null +++ b/Commands/Get-GitPrompt.ps1 @@ -0,0 +1,364 @@ +function Get-GitPrompt { + <# + .SYNOPSIS + Git Prompt + .DESCRIPTION + Git friendly PowerShell prompt. + + Shows current branch, modified, untracked, added, and removed files. + #> + [Alias('git.prompt')] + param( + # The icon used for git branches. + # By default `⑆` (OCR Branch Bank Identification [0x2446]) + [string] + $BranchIcon = '⑆', + + # The prompt icon after a successful command. + # By default, `≻` (Succeeds [0x227b]) + [string] + $SuccessIcon = '≻', #'⊃', # '>', "➫", "⮚" + + # By default, `≻` (Does not Succeed [0x2281]) + [string] + $ErrorIcon = '⊁', + + # The home icon. + # This is displayed when the path is + # beneath the user home directory. + # By default: `⌂` (House [0x2302]) + [string] + $HomeIcon = '⌂', + + [string] + $BranchOpenIcon = '⟦', + + [string] + $BranchCloseIcon = '⟧', + + [string] + $JobIcon = '⏲', + + # The icon used for slash. + # This will separate directories. + # By default ` ⹊ ` (Dotted Solidus [0x2e4a]) + [string] + $Slash = " ⹊ ", # '⹊' #, '/' + + # The style to use when things succeed + [string] + $SuccessStyle = $PSStyle.Foreground.Green, + + # The style to use when things fail + [string] + $ErrorStyle = $PSStyle.Foreground.Red, + + # The style to use when warning + [string] + $WarningStyle = $PSStyle.Foreground.Yellow, + + # The style to use when cyan + [string] + $InfoStyle = $PSStyle.Foreground.Cyan, + + [string] + $UnknownStyle = $PSStyle.Foreground.Magenta, + + # The style to use when bolding text. + [string] + $BoldStyle = $psStyle.Bold, + + [string] + $BoldStyleOff = $psStyle.Bold.Off, + + # The string used to reset styles. + [string] + $ResetStyle = $PSStyle.Reset, + + # If set, will display the history ID + [switch] + $ShowHistoryId + ) + + # Capture if the last statement worked + # (if we do not do this first, it will not be accurate) + $worked = $? + + # If we have a nested prompt level greater than one + # we are debugging or in another nested context. + # Performance will be more important in this case + if ($NestedPromptLevel -gt 1) { + # so simply multiply our prompt icon by the nested prompt level + return ($SuccessIcon * $NestedPromptLevel) + } + + # We may want to pad the lines + $LeftPadding = + # If we are not showing the history ID + if (-not $ShowHistoryId) { + ' ' # we can padd with a single space + } else { + # Otherwise, pad with the history id length. + ' ' * "$($MyInvocation.HistoryId)".Length + } + + # Determine if nothing happened + # (someone just pressing `ENTER` will not increment the history ID) + $isNoOp = $script:LastHistoryId -eq $MyInvocation.HistoryId + + # Determine our display path. + $displayPath = + # If we're somewhere beneath `$home` + if ("$pwd".StartsWith("$home")) { + # Use the home icon + $HomeIcon, + # and the path beneath home + "$pwd".Substring( + "$home".Length + ) -replace # Fix slashes to unix style + '[\\/]+','/' -replace # then replace any duplicate slashes + '/+','/' -join + '' + } else { + # Otherwise, just fix slashes to unix style. + "$pwd" -replace '[\\/]+', '/' + } + + # Replace the last slash, and then replace slashes with our slash icon + $displayPath = $displayPath -replace '/{0,}$' -replace '/+',$Slash + + # If nothing happened + if ($isNoOp) { + # Output our prompt + return "$( + # If the last command worked, use the info style; + if ($worked) { $InfoStyle } + # otherwise, use the error style. + else { $ErrorStyle } + )$( + # If the last command worked, use the success icon; + if ($worked) {$SuccessIcon} + # otherwise, use the error icon. + else { $ErrorIcon } + )$($LeftPadding)$( + $ResetStyle + )" + } else { + # If something happened, update our history ID. + $script:LastHistoryId = $MyInvocation.HistoryId + } + + # Next we want to check for git remotes. + + # Cache any remote paths we find + if (-not $script:GitRemotePath) { + $script:GitRemotePath = [Ordered]@{} + } + # If we do not know the remote for the current directory + if (-not $script:GitRemotePath.Contains("$pwd")) { + # get the remote + $script:GitRemotePath["$pwd"] = git remote -ErrorAction Ignore | git remote show + } + + # There might be more than one remote. We want the first. + $gitRemoteUrl = @($script:GitRemotePath["$pwd"].RemoteUrls.Fetch)[0] + + $repoUrl = $gitRemoteUrl -replace + '^ssh://', 'https://' -replace + '\.git$' -replace + '\.wiki$', '/wiki' -as [uri] + + # Now git our status + $gitStatus = if ([IO.Directory]::Exists($pwd)) { + git status . -WarningAction Ignore + } else { + $null + } + + # If somehow that failed + if (-not $gitStatus) { + # If there was no status url, we're not in a repository. + # Output our prompt + return "$( + # Show the history ID (or not) + if ($ShowHistoryId) { + "$($myInvocation.HistoryId) " + } else { + '' + } + )$( + # Followed by the display path + $displayPath + ) $( + # Followed by the appropriate style + if ($worked) { $InfoStyle } + else { $ErrorStyle } + )$( + # and icon (based off of the success/failure of the last command) + if ($worked) {$SuccessIcon} + else {$ErrorIcon } + )$( + $ResetStyle + )$(' ' * 1)" + } + + # Collect the files that have been staged for addition + $stagedModified = @($gitStatus.Added) + # and deletion. + $stagedDeletes = @($gitStatus.Removed) + # Collect the files that have been locally modified + $modified = @($gitStatus.Modified) + # and deleted. + $deleted = @($gitStatus.Deleted) + + # Last but not least, collect untracked files. + $untracked = @($gitStatus.Untracked) + + # Create a link to our repo + $LinkToRepo = $PSStyle.FormatHyperlink( + # This will make our file tree link to the repo tree + $displayPath, + "$repoUrl" + ) + + # Get our git relative path. + $gitRelativePath = + "$pwd".Substring($gitStatus.GitRoot.Length) -replace '[\\/]', '/' + + # Prepare the branch info portion of our prompt + $gitBranchInfo = @( + # If the branch is in `main`, `master`, or `latest`, + # you might want to check out. + if ($GitStatus.BranchName -in 'main', 'master', 'latest') { + # So we will display that as a warning + $WarningStyle + } else { + # If we are behind the upstream + if ($gitCommitsBehind -or + # or have diverged, we're in trouble. + $gitStatus.Status -match 'diverged') { + $ErrorStyle # and should show the repo using the error style. + } elseif ($gitStatus.Staged) { + # If we've got commits staged for addition + $SuccessStyle # we're green / good to go. + } else { + # Otherwise, we're just informing you about the branch name. + $InfoStyle + } + } + + + # The rest of our git branch info is: + $BoldStyle # * Bold + $BranchIcon # * Branch Icon + $LeftPadding # * LeftPadding + $BranchOpenIcon # * BranchOpenIcon + $gitStatus.BranchName # * `$gitStatus.BranchName` + $BranchCloseIcon # * Close our branch. + $BoldStyleOff # * Bold Off + $ResetStyle # * Reset Style + ) + + # Now we just have to hyperlink things + $gitInfo = @( + # If we have a repo url + if ($repoUrl) { + # make a hyperlink to it and display the branch info + $PSStyle.FormatHyperlink( + "$($gitBranchInfo -join '')", + "$repoUrl/tree/$($gitStatus.BranchName)$GitRelativePath" + ) + } else { + $gitBranchInfo -join '' + } + + # If we are in a git subdirectory + if ($gitRelativePath) { + # hyperlink to this exact spot in the tree. + $PSStyle.FormatHyperlink( + ($gitRelativePath -replace '[\\/]', $Slash), + "$repoUrl/tree/$($gitStatus.BranchName)$GitRelativePath" + ) + } + + # If the branches have diverged + if ($gitStatus.Status -match 'diverged') { + # Make sure we notice. + "$($ErrorStyle) diverged!$($($ResetStyle))" + } + + # If we are behind + if ($gitStatus.Behind -gt 0) { + # make sure we notice. + "$($ErrorStyle)(-$($gitStatus.Behind))$($ResetStyle)" + } + + # If we are ahead + if ($gitStatus.Ahead -gt 0) { + # make sure we notice and feel successful. + "$($SuccessStyle)(+$($gitStatus.Ahead))$($ResetStyle)" + } + + # If we have any staged files, + if ($stagedModified) { + # make sure we know how many have been added. + $SuccessStyle, + " $($stagedModified.Length) added", + $ResetStyle + } + + # If we have any deleted files, + if ($stagedDeletes) { + # make sure we know how many have been removed. + $ErrorStyle, + " $($stagedDeletes.Length) removed", + $ResetStyle + } + + # If we have any modified files + if ($modified) { + # Display the info. + $InfoStyle, + " $($modified.Length) modified", + $ResetStyle + } + + # If we have any deleted files + if ($deleted) { + # Make sure we know. + $ErrorStyle, + " $($deleted.Length) deleted", + $ResetStyle + } + + # If we have any untracked files + if ($untracked) { + + $PSStyle.Foreground.Magenta, + " $($untracked.Count) untracked", + $ResetStyle + } + $ResetStyle + ) -join '' + + # ⮚ ⟣ ⤳ ⟫ ⟩ + "$( + if (-not $ShowHistoryId) { + '' + } else { + "$($myInvocation.HistoryId) " + } + )$LinkToRepo", + "$gitInfo", + "$( + if ($worked) { $InfoStyle } + else { $ErrorStyle } + )$( + if ($worked) {$SuccessIcon} + else {$ErrorIcon } + )$LeftPadding$( + $ResetStyle + )" -join + [Environment]::NewLine + +} \ No newline at end of file diff --git a/Extensions/Git.Status.UGit.Extension.ps1 b/Extensions/Git.Status.UGit.Extension.ps1 index fb5e09c5..fc64a471 100644 --- a/Extensions/Git.Status.UGit.Extension.ps1 +++ b/Extensions/Git.Status.UGit.Extension.ps1 @@ -3,32 +3,133 @@ git status extension .DESCRIPTION Returns git status as an object. + + git status provides a lot of useful information. .EXAMPLE + # Get the status of the current repository git status .EXAMPLE - git status | Select-Object -ExpandProperty Untracked + # Get the untracked files + git status | + Select-Object -ExpandProperty Untracked +.EXAMPLE + # Get the status of the repo in the current directory + git status . +.EXAMPLE + # Get untracked files in the current directory + git status . | + Select-Object -ExpandProperty Untracked +.EXAMPLE + # See all git status can do + git status | + Get-Member +.EXAMPLE + # We can also git status --short + git status --short +.EXAMPLE + # We can also ask for short status with a branch summary + git status --short --branch +.EXAMPLE + # We can use the shortform `-sb` + git status -sb #> [Management.Automation.Cmdlet("Out","Git")] [ValidatePattern('^git status')] -param( - -) +param() begin { <# If any of these parameters are used, we will skip processing. #> $SkipIf = 'porcelain' -join '|' - if ($gitCommand -match "\s-(?>$SkipIf)") { break } + if ($gitCommand -match "\s-(?>$SkipIf)") { break } + + $shortStatus = $gitCommand -match '(?>-s|--short)' $statusLines = @() + + filter gitOctal { + [Regex]::Replace( + # First we trim leading and trailing quotes. Easy. + $_ -replace '^"|"$', + # Then we look for any sequence of slash + 3 digits [0-7] + "(?:\\[0-7]{3}){1,}", + # And replace them with this short script + { + param($match) + # We want them back as UTF8 + [Text.Encoding]::UTF8.GetString( + # But these bytes are encoded with + # [ISO-8859-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1) + [Text.Encoding]::GetEncoding('ISO-8859-1').GetBytes( + # The match contains the sequence + @( + foreach ( + # so we split it up + # (which removes the slash) + $octalByte in $match -split '\\' -ne '' + ) { + # then we convert each byte in base 8 + [Convert]::ToByte($octalByte,8) -as + [char] # and make it a character + } + ) -join '' # then we join our characters, + ) # get their bytes, + ) # and output the replaced string. + } + ) + } } process { + # collect all the status lines $statusLines += "$gitOut" } -end { +end { + # --short status does not have too much information + if ($shortStatus) { + # It's just a series of lines we will turn into objects + # and put into a summary object + $ShortStatusObject = [Ordered]@{ + PSTypeName = 'Git.Status.Short' + Status = '' + StatusLines = $statusLines + Short = @() + WorkingDirectory = if ("$PWD".StartsWith($GitRoot)) { + $pwd + } else { + $gitRoot + } + } + + foreach ($shortLine in $statusLines) { + # The first character is any `staged` status + $stagedStatus = $shortLine.Substring(0,1) + # The second character is any `unstaged` status + $unstagedStatus = $shortline.Substring(1,1) + + $filePath = $shortLine.Substring(3) + + if ($stagedStatus -eq '#' -and $unstagedStatus -eq '#') { + $ShortStatusObject.Status = $filePath + continue + } + + $filePath = $filePath | gitOctal + + $fileStatus, $filePath = $shortLine -replace '^\s{0,}' -split '[\s\t]', 2 + $ShortStatusObject.Short += [PSCustomObject]( + [Ordered]@{ + Staged = $stagedStatus + Unstaged = $unstagedStatus + Path = $filePath | gitOctal + } + ) + } + return ([PSCustomObject]$ShortStatusObject) + } + $gitStatusOut = [Ordered]@{ PSTypeName = 'Git.Status' BranchName = '' @@ -36,8 +137,14 @@ end { Staged = @() Unstaged = @() Untracked = @() + Unmerged = @() GitRoot = $GitRoot - } + WorkingDirectory = if ("$PWD".StartsWith($GitRoot)) { + $pwd + } else { + $gitRoot + } + } $inPhase = '' @@ -54,6 +161,10 @@ end { if ($statusLines[$sln] -like "Changes to be committed:*") { $inPhase = 'Staged' } + if ($statusLines[$sln] -like "Unmerged*:*") { + $inPhase = 'Unmerged' + continue + } if ($statusLines[$sln] -like "Untracked files:*") { $inPhase = 'Untracked' continue @@ -74,13 +185,34 @@ end { '' } $changePath = $trimmedLine -replace "^[\w\s]+:\s+" - if ($inPhase -eq 'untracked') { - $gitStatusOut.$inPhase += Get-Item -ErrorAction SilentlyContinue -Path $changePath + + # If git quotes a status line, + # it means there are octal encoded characters in the path + if ($changePath -match '^"' -and $changePath -match '"$') { + # So we want to change our change path. + $changePath = $changePath | gitOctal + } + $resolvedChangePath = + try { + $resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($changePath) + $resolvedFile = [IO.FileInfo]"$resolvedPath" + if ($resolvedFile.Length) { + $resolvedFile + } elseif ( + $resolvedDirectory = [IO.DirectoryInfo]"$resolvedPath" + ) { + $resolvedDirectory + } + } catch { + Write-Verbose "Could not resolve path '$changePath' : $_" + } + if ($inPhase -eq 'untracked') { + $gitStatusOut.$inPhase += $resolvedChangePath } else { $gitStatusOut.$inPhase += [PSCustomObject]@{ ChangeType = $changeType -replace '\s' Path = $changePath - File = Get-Item -ErrorAction SilentlyContinue -Path $changePath + File = $resolvedChangePath } } diff --git a/README.md b/README.md index 3bdb9421..1aace68d 100644 --- a/README.md +++ b/README.md @@ -450,6 +450,7 @@ You might want to try giving some of these a try. ~~~PowerShell + # Get the status of the current repository git status ~~~ @@ -457,7 +458,59 @@ You might want to try giving some of these a try. ~~~PowerShell - git status | Select-Object -ExpandProperty Untracked + # Get the untracked files + git status | + Select-Object -ExpandProperty Untracked +~~~ + +### Git.Status Example 3 + + +~~~PowerShell + # Get the status of the repo in the current directory + git status . +~~~ + +### Git.Status Example 4 + + +~~~PowerShell + # Get untracked files in the current directory + git status . | + Select-Object -ExpandProperty Untracked +~~~ + +### Git.Status Example 5 + + +~~~PowerShell + # See all git status can do + git status | + Get-Member +~~~ + +### Git.Status Example 6 + + +~~~PowerShell + # We can also git status --short + git status --short +~~~ + +### Git.Status Example 7 + + +~~~PowerShell + # We can also ask for short status with a branch summary + git status --short --branch +~~~ + +### Git.Status Example 8 + + +~~~PowerShell + # We can use the shortform `-sb` + git status -sb ~~~ ### Git.SubModule.Status Example 1 diff --git a/Types/System.IO.FileInfo/Alias.psd1 b/Types/System.IO.FileInfo/Alias.psd1 deleted file mode 100644 index 2cf31c41..00000000 --- a/Types/System.IO.FileInfo/Alias.psd1 +++ /dev/null @@ -1,12 +0,0 @@ -@{ - 'GitLog' = 'GitLogs' - 'GitDifference' = 'GitDiff' - 'Diff' = 'GitDiff' - 'Logs' = 'GitLogs' - 'GitHistory' = 'GitChanges' - 'Changes' = 'GitChanges' - 'History' = 'GitChanges' - 'HasChanged' = 'GitDirty' - 'HasChanges' = 'GitDirty' - 'IsUnstaged' = 'GitDirty' -} \ No newline at end of file diff --git a/Types/System.IO.FileInfo/get_GitDiff.ps1 b/Types/System.IO.FileInfo/get_GitDiff.ps1 deleted file mode 100644 index f9cef3b4..00000000 --- a/Types/System.IO.FileInfo/get_GitDiff.ps1 +++ /dev/null @@ -1,3 +0,0 @@ -Push-Location $this.Directory -(git log -n 1 $this.Name).Diff() -Pop-Location diff --git a/Types/System.IO.FileInfo/get_GitDirty.ps1 b/Types/System.IO.FileInfo/get_GitDirty.ps1 deleted file mode 100644 index ab112367..00000000 --- a/Types/System.IO.FileInfo/get_GitDirty.ps1 +++ /dev/null @@ -1,3 +0,0 @@ -Push-Location $this.Directory -$(git status $this.Name '-s') -as [bool] -Pop-Location diff --git a/Types/System.IO.FileInfo/get_GitLogs.ps1 b/Types/System.IO.FileInfo/get_GitLogs.ps1 deleted file mode 100644 index c95a437a..00000000 --- a/Types/System.IO.FileInfo/get_GitLogs.ps1 +++ /dev/null @@ -1,3 +0,0 @@ -Push-Location $this.Directory -git log $this.Name -Pop-Location diff --git a/Types/git.status.short/Git.Status.Short.format.ps1 b/Types/git.status.short/Git.Status.Short.format.ps1 new file mode 100644 index 00000000..4333bf3e --- /dev/null +++ b/Types/git.status.short/Git.Status.Short.format.ps1 @@ -0,0 +1,28 @@ +Write-FormatView -TypeName Git.Status.Short -Action { + $in = $_ + + @( + if ($in.Status) { + $in.Status + } + foreach ($short in $in.Short) { + @( + foreach ($letter in $short.Staged,$short.Unstaged) { + switch ($letter) { + M { $PSStyle.Foreground.Cyan; $_} + R { $PSStyle.Foreground.Red;$_} + N { $PSStyle.Foreground.Green;$_} + ? { $PSStyle.Foreground.Magenta;$_} + default { $_ } + } + } + + ' ' + $short.Path + $PSStyle.Reset + ) -join '' + } + ) -join [Environment]::NewLine + +} + diff --git a/Types/git.status/Diff.ps1 b/Types/git.status/Diff.ps1 new file mode 100644 index 00000000..408318cd --- /dev/null +++ b/Types/git.status/Diff.ps1 @@ -0,0 +1,15 @@ +<# +.SYNOPSIS + Gets Status Difference +.DESCRIPTION + Gets the diff of a status. + + This will show any modifications to files. +#> +param() + +Push-Location $this.WorkingDirectory +git status . | + Select-Object -ExpandProperty Modified | + git diff +Pop-Location \ No newline at end of file diff --git a/Types/git.status/Git.Status.format.ps1 b/Types/git.status/Git.Status.format.ps1 index bae270a9..7fe2b1f2 100644 --- a/Types/git.status/Git.Status.format.ps1 +++ b/Types/git.status/Git.Status.format.ps1 @@ -1,45 +1,114 @@ Write-FormatView -TypeName Git.Status -Action { Write-FormatViewExpression -Text "On branch: " - Write-FormatViewExpression -ScriptBlock { $_.BranchName } -if { $_.BranchName -notin 'main', 'master' } -ForegroundColor Verbose - Write-FormatViewExpression -ScriptBlock { $_.BranchName } -if { $_.BranchName -in 'main', 'master' } -ForegroundColor Warning + + Write-FormatViewExpression -ScriptBlock { + $branchName = $_.BranchName + @( + if ($branchName -notin 'main', 'master', 'latest') { + $PSStyle.Foreground.Cyan + $PSStyle.Bold + } else { + $PSStyle.Formatting.Warning + } + $branchName + $PSStyle.Reset + ) -join '' + } Write-FormatViewExpression -Newline - Write-FormatViewExpression -If { $_.Status -notlike 'Nothing*' } -ScriptBlock { $_.Status + [Environment]::NewLine } + + Write-FormatViewExpression -ScriptBlock { + $gitStatus = $_ + if ($gitStatus.Status -match 'nothing') { + return '' + } + @( + if ($gitStatus.Ahead -gt 0) { + $PSStyle.Foreground.Green + $PSStyle.Bold + } elseif ($gitStatus.Behind -gt 0 -or + $gitStatus.Status -match 'diverged') { + $PSStyle.Formatting.Error + } + else { + $PSStyle.Foreground.Cyan + } + $gitStatus.Status + [Environment]::NewLine + $PSStyle.Reset + ) -join '' + } + Write-formatviewExpression -If { $_.Staged } -ScriptBlock { "Changes Staged For Commit: (use git commit -m to commit)" + [Environment]::NewLine } + Write-FormatViewExpression -If { $_.Staged } -ScriptBlock { - (@(foreach ($line in $($_.Staged | Select-Object ChangeType, Path | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 8)) -split '(?>\r\n|\n)') { - (" " * 4) + $line - }) -join [Environment]::NewLine) + [Environment]::NewLine - } -ForegroundColor Green + (@( + foreach ($staged in $_.Staged) { + "`t$($staged.ChangeType):`t$($staged.Path)" + } + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) + } Write-FormatViewExpression -If { $_.Unstaged } -ScriptBlock { - "Changes Not Staged For Commit: - (use git add to add, git restore to discard changes)" + [Environment]::NewLine + (@( + "Changes $($PSStyle.Foreground.Cyan + $PSStyle.Bold)Not Staged$($PSStyle.Reset) for commit:" + ' (use "git add ..." to update what will be committed)' + ' (use "git restore ..." to discard changes in working directory)' + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) } - Write-FormatViewExpression -If { $_.Unstaged} -ScriptBlock { - (@(foreach ($line in $($_.Unstaged | Select-Object ChangeType, Path | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 8)) -split '(?>\r\n|\n)') { - (" " * 4) + $line - }) -join [Environment]::NewLine) + [Environment]::NewLine - } -ForegroundColor Red + Write-FormatViewExpression -If { $_.Unstaged } -ScriptBlock { + (@( + foreach ($staged in $_.Unstaged) { + "`t$( + if ($staged.changetype -eq 'modified') { + $PSStyle.Foreground.Cyan + } + elseif ($PSStyle.changetype -eq 'newfile') { + $PSStyle.Foreground.Green + } + elseif ($staged.changetype -eq 'deleted') { + $PSStyle.Foreground.Red + } else { + $PSStyle.Foreground.White + } + $psStyle.Bold + )$($staged.ChangeType):`t$($staged.Path)$($PSStyle.Reset)" + } + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) + } Write-FormatViewExpression -If { $_.Untracked } -ScriptBlock { - "Untracked Files: - (use git add to include in commit)" + [Environment]::NewLine + (@( + "$($PSStyle.Foreground.Magenta + $PSStyle.Bold)Untracked$($PSStyle.Reset) Files:" + ' (use "git add ..." to include in what will be committed)' + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) } Write-FormatViewExpression -If { $_.Untracked} -ScriptBlock { - @(foreach ($line in $($_.Untracked | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 8)) -split '(?>\r\n|\n)') { - (" " * 4) + $line - }) -join [Environment]::NewLine - } -ForegroundColor Yellow + (@( + $gitRoot = $_.GitRoot + foreach ($untracked in $_.Untracked) { + "`t$( + $untracked.FullName.Substring($gitRoot.Length + 1) -replace '[\\/]', '/' + )" + } + ) -join [Environment]::NewLine) + + [Environment]::NewLine + [Environment]::NewLine + } -ForegroundColor Magenta Write-FormatViewExpression -ScriptBlock { if(($_.Untracked.Count + $_.Unstaged.Count + $_.Staged.Count) -eq 0) { "Nothing to commit, working tree clean" } - } + } -ForegroundColor Success } diff --git a/Types/git.status/get_Added.ps1 b/Types/git.status/get_Added.ps1 new file mode 100644 index 00000000..f684946b --- /dev/null +++ b/Types/git.status/get_Added.ps1 @@ -0,0 +1,11 @@ +<# +.SYNOPSIS + Gets staged added files +.DESCRIPTION + Gets staged added files. These are all files that are new or modified. +#> +@(foreach ($staged in $this.staged) { + if ($staged.ChangeType -in 'newfile','modified') { + $staged.File + } +}) \ No newline at end of file diff --git a/Types/git.status/get_Ahead.ps1 b/Types/git.status/get_Ahead.ps1 new file mode 100644 index 00000000..b72d1b7e --- /dev/null +++ b/Types/git.status/get_Ahead.ps1 @@ -0,0 +1,11 @@ +<# +.SYNOPSIS + Gets the commits ahead +.DESCRIPTION + Gets the number of commits ahead of the remote branch. +#> +if ($this.Status -match 'ahead') { + $this.Status -replace '\D' -as [int] +} else { + 0 +} \ No newline at end of file diff --git a/Types/git.status/get_Behind.ps1 b/Types/git.status/get_Behind.ps1 new file mode 100644 index 00000000..c4d72ae3 --- /dev/null +++ b/Types/git.status/get_Behind.ps1 @@ -0,0 +1,11 @@ +<# +.SYNOPSIS + Gets the commits behind +.DESCRIPTION + Gets the number of commits behind the remote branch. +#> +if ($this.Status -match 'behind') { + @($this.Status -replace '[\D-[,]]','' -split ',',2)[0] -as [int] +} else { + 0 +} diff --git a/Types/git.status/get_Deleted.ps1 b/Types/git.status/get_Deleted.ps1 new file mode 100644 index 00000000..544da56f --- /dev/null +++ b/Types/git.status/get_Deleted.ps1 @@ -0,0 +1,11 @@ +<# +.SYNOPSIS + Gets unstaged deleted paths. +.DESCRIPTION + Gets unstaged deleted paths. +#> +@(foreach ($unstaged in $this.Unstaged) { + if ($unstaged.ChangeType -eq 'deleted') { + $unstaged.Path + } +}) \ No newline at end of file diff --git a/Types/git.status/get_Modified.ps1 b/Types/git.status/get_Modified.ps1 new file mode 100644 index 00000000..a7fe470f --- /dev/null +++ b/Types/git.status/get_Modified.ps1 @@ -0,0 +1,11 @@ +<# +.SYNOPSIS + Gets modified files +.DESCRIPTION + Gets unstaged modified files +#> +@(foreach ($unstaged in $this.Unstaged) { + if ($unstaged.ChangeType -eq 'modified') { + $unstaged.File + } +}) \ No newline at end of file diff --git a/Types/git.status/get_Removed.ps1 b/Types/git.status/get_Removed.ps1 new file mode 100644 index 00000000..d930bee8 --- /dev/null +++ b/Types/git.status/get_Removed.ps1 @@ -0,0 +1,13 @@ +<# +.SYNOPSIS + Gets staged removed files +.DESCRIPTION + Gets staged removed files. + + These are all files that are staged for removal. +#> +@(foreach ($staged in $this.staged) { + if ($staged.ChangeType -in 'deleted') { + $staged.Path + } +}) \ No newline at end of file diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 41469d48..de1501e9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,31 @@ > Love It? [Support It](https://github.com/sponsors/StartAutomating) +## ugit 0.4.7 + +* `git status` improvement + * `git.status.ahead` (#358) + * `git.status.behind` (#359) + * `git.status.modified` (#356) + * `git.status.deleted` (#357) + * `git.status.addded` (#361) + * `git.status` formatting improvement (#363) + * `git.status.WorkingDirectory` (#365) + * `git.status.Unmerged` (#367) + * `git.status.Diff` (#364) + * `git status --short` support (#343) + * Performance and fault tolerance (#366) +* Removing IO.FileInfo extended properties (#369) +* Internationalization support (#177) +* New Functions: + * `Get-GitPrompt` (#368) + * `Get-GitOctal` (#370) + +To use the new prompt, you can simply: + +Set-Alias prompt git.prompt + +--- ## ugit 0.4.6: diff --git a/docs/Get-GitOctal.md b/docs/Get-GitOctal.md new file mode 100644 index 00000000..dcb1789a --- /dev/null +++ b/docs/Get-GitOctal.md @@ -0,0 +1,28 @@ +Get-GitOctal +------------ + +### Synopsis +Gets Git Octal strings + +--- + +### Description + +Gets Git Octal strings as the current output encoding. + +--- + +### Parameters +#### **OctalString** +An octal string + +|Type |Required|Position|PipelineInput | +|----------|--------|--------|---------------------| +|`[String]`|false |1 |true (ByPropertyName)| + +--- + +### Syntax +```PowerShell +Get-GitOctal [[-OctalString] ] [] +``` diff --git a/docs/Get-GitPrompt.md b/docs/Get-GitPrompt.md new file mode 100644 index 00000000..c18cac6d --- /dev/null +++ b/docs/Get-GitPrompt.md @@ -0,0 +1,144 @@ +Get-GitPrompt +------------- + +### Synopsis +Git Prompt + +--- + +### Description + +Git friendly PowerShell prompt. + +Shows current branch, modified, untracked, added, and removed files. + +--- + +### Parameters +#### **BranchIcon** +The icon used for git branches. +By default `⑆` (OCR Branch Bank Identification [0x2446]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |1 |false | + +#### **SuccessIcon** +The prompt icon after a successful command. +By default, `≻` (Succeeds [0x227b]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |2 |false | + +#### **ErrorIcon** +By default, `≻` (Does not Succeed [0x2281]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |3 |false | + +#### **HomeIcon** +The home icon. +This is displayed when the path is +beneath the user home directory. +By default: `⌂` (House [0x2302]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |4 |false | + +#### **BranchOpenIcon** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |5 |false | + +#### **BranchCloseIcon** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |6 |false | + +#### **JobIcon** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |7 |false | + +#### **Slash** +The icon used for slash. +This will separate directories. +By default ` ⹊ ` (Dotted Solidus [0x2e4a]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |8 |false | + +#### **SuccessStyle** +The style to use when things succeed + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |9 |false | + +#### **ErrorStyle** +The style to use when things fail + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |10 |false | + +#### **WarningStyle** +The style to use when warning + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |11 |false | + +#### **InfoStyle** +The style to use when cyan + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |12 |false | + +#### **UnknownStyle** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |13 |false | + +#### **BoldStyle** +The style to use when bolding text. + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |14 |false | + +#### **BoldStyleOff** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |15 |false | + +#### **ResetStyle** +The string used to reset styles. + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |16 |false | + +#### **ShowHistoryId** +If set, will display the history ID + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[Switch]`|false |named |false | + +--- + +### Syntax +```PowerShell +Get-GitPrompt [[-BranchIcon] ] [[-SuccessIcon] ] [[-ErrorIcon] ] [[-HomeIcon] ] [[-BranchOpenIcon] ] [[-BranchCloseIcon] ] [[-JobIcon] ] [[-Slash] ] [[-SuccessStyle] ] [[-ErrorStyle] ] [[-WarningStyle] ] [[-InfoStyle] ] [[-UnknownStyle] ] [[-BoldStyle] ] [[-BoldStyleOff] ] [[-ResetStyle] ] [-ShowHistoryId] [] +``` diff --git a/docs/Git.Status-Extension.md b/docs/Git.Status-Extension.md index 9ea1960d..c7b130a1 100644 --- a/docs/Git.Status-Extension.md +++ b/docs/Git.Status-Extension.md @@ -10,18 +10,53 @@ git status extension Returns git status as an object. +git status provides a lot of useful information. + --- ### Examples -> EXAMPLE 1 +Get the status of the current repository ```PowerShell git status ``` -> EXAMPLE 2 +Get the untracked files + +```PowerShell +git status | + Select-Object -ExpandProperty Untracked +``` +Get the status of the repo in the current directory + +```PowerShell +git status . +``` +Get untracked files in the current directory + +```PowerShell +git status . | + Select-Object -ExpandProperty Untracked +``` +See all git status can do + +```PowerShell +git status | + Get-Member +``` +We can also git status --short + +```PowerShell +git status --short +``` +We can also ask for short status with a branch summary + +```PowerShell +git status --short --branch +``` +We can use the shortform `-sb` ```PowerShell -git status | Select-Object -ExpandProperty Untracked +git status -sb ``` --- diff --git a/docs/README.md b/docs/README.md index cf20c9e6..da5f1d8d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -450,6 +450,7 @@ You might want to try giving some of these a try. ~~~PowerShell + # Get the status of the current repository git status ~~~ @@ -457,7 +458,59 @@ You might want to try giving some of these a try. ~~~PowerShell - git status | Select-Object -ExpandProperty Untracked + # Get the untracked files + git status | + Select-Object -ExpandProperty Untracked +~~~ + +### Git.Status Example 3 + + +~~~PowerShell + # Get the status of the repo in the current directory + git status . +~~~ + +### Git.Status Example 4 + + +~~~PowerShell + # Get untracked files in the current directory + git status . | + Select-Object -ExpandProperty Untracked +~~~ + +### Git.Status Example 5 + + +~~~PowerShell + # See all git status can do + git status | + Get-Member +~~~ + +### Git.Status Example 6 + + +~~~PowerShell + # We can also git status --short + git status --short +~~~ + +### Git.Status Example 7 + + +~~~PowerShell + # We can also ask for short status with a branch summary + git status --short --branch +~~~ + +### Git.Status Example 8 + + +~~~PowerShell + # We can use the shortform `-sb` + git status -sb ~~~ ### Git.SubModule.Status Example 1 diff --git a/docs/_data/Help/Get-GitOctal.json b/docs/_data/Help/Get-GitOctal.json new file mode 100644 index 00000000..8cd47e07 --- /dev/null +++ b/docs/_data/Help/Get-GitOctal.json @@ -0,0 +1,33 @@ +{ + "Synopsis": "Gets Git Octal strings", + "Description": "Gets Git Octal strings as the current output encoding.", + "Parameters": [ + { + "Name": null, + "Type": null, + "Description": "", + "Required": false, + "Position": 0, + "Aliases": null, + "DefaultValue": null, + "Globbing": false, + "PipelineInput": null, + "variableLength": false + } + ], + "Notes": [ + null + ], + "CommandType": "Function", + "Component": [ + null + ], + "Inputs": [ + null + ], + "Outputs": [ + null + ], + "Links": [], + "Examples": [] +} \ No newline at end of file diff --git a/docs/_data/Help/Get-GitPrompt.json b/docs/_data/Help/Get-GitPrompt.json new file mode 100644 index 00000000..2ce1d79c --- /dev/null +++ b/docs/_data/Help/Get-GitPrompt.json @@ -0,0 +1,33 @@ +{ + "Synopsis": "Git Prompt", + "Description": "Git friendly PowerShell prompt.\n\nShows current branch, modified, untracked, added, and removed files.", + "Parameters": [ + { + "Name": null, + "Type": null, + "Description": "", + "Required": false, + "Position": 0, + "Aliases": null, + "DefaultValue": null, + "Globbing": false, + "PipelineInput": null, + "variableLength": false + } + ], + "Notes": [ + null + ], + "CommandType": "Function", + "Component": [ + null + ], + "Inputs": [ + null + ], + "Outputs": [ + null + ], + "Links": [], + "Examples": [] +} \ No newline at end of file diff --git a/docs/_data/Help/git.octal.json b/docs/_data/Help/git.octal.json new file mode 100644 index 00000000..8cd47e07 --- /dev/null +++ b/docs/_data/Help/git.octal.json @@ -0,0 +1,33 @@ +{ + "Synopsis": "Gets Git Octal strings", + "Description": "Gets Git Octal strings as the current output encoding.", + "Parameters": [ + { + "Name": null, + "Type": null, + "Description": "", + "Required": false, + "Position": 0, + "Aliases": null, + "DefaultValue": null, + "Globbing": false, + "PipelineInput": null, + "variableLength": false + } + ], + "Notes": [ + null + ], + "CommandType": "Function", + "Component": [ + null + ], + "Inputs": [ + null + ], + "Outputs": [ + null + ], + "Links": [], + "Examples": [] +} \ No newline at end of file diff --git a/docs/_data/Help/git.prompt.json b/docs/_data/Help/git.prompt.json new file mode 100644 index 00000000..2ce1d79c --- /dev/null +++ b/docs/_data/Help/git.prompt.json @@ -0,0 +1,33 @@ +{ + "Synopsis": "Git Prompt", + "Description": "Git friendly PowerShell prompt.\n\nShows current branch, modified, untracked, added, and removed files.", + "Parameters": [ + { + "Name": null, + "Type": null, + "Description": "", + "Required": false, + "Position": 0, + "Aliases": null, + "DefaultValue": null, + "Globbing": false, + "PipelineInput": null, + "variableLength": false + } + ], + "Notes": [ + null + ], + "CommandType": "Function", + "Component": [ + null + ], + "Inputs": [ + null + ], + "Outputs": [ + null + ], + "Links": [], + "Examples": [] +} \ No newline at end of file diff --git a/docs/git.octal.md b/docs/git.octal.md new file mode 100644 index 00000000..dcb1789a --- /dev/null +++ b/docs/git.octal.md @@ -0,0 +1,28 @@ +Get-GitOctal +------------ + +### Synopsis +Gets Git Octal strings + +--- + +### Description + +Gets Git Octal strings as the current output encoding. + +--- + +### Parameters +#### **OctalString** +An octal string + +|Type |Required|Position|PipelineInput | +|----------|--------|--------|---------------------| +|`[String]`|false |1 |true (ByPropertyName)| + +--- + +### Syntax +```PowerShell +Get-GitOctal [[-OctalString] ] [] +``` diff --git a/docs/git.prompt.md b/docs/git.prompt.md new file mode 100644 index 00000000..c18cac6d --- /dev/null +++ b/docs/git.prompt.md @@ -0,0 +1,144 @@ +Get-GitPrompt +------------- + +### Synopsis +Git Prompt + +--- + +### Description + +Git friendly PowerShell prompt. + +Shows current branch, modified, untracked, added, and removed files. + +--- + +### Parameters +#### **BranchIcon** +The icon used for git branches. +By default `⑆` (OCR Branch Bank Identification [0x2446]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |1 |false | + +#### **SuccessIcon** +The prompt icon after a successful command. +By default, `≻` (Succeeds [0x227b]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |2 |false | + +#### **ErrorIcon** +By default, `≻` (Does not Succeed [0x2281]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |3 |false | + +#### **HomeIcon** +The home icon. +This is displayed when the path is +beneath the user home directory. +By default: `⌂` (House [0x2302]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |4 |false | + +#### **BranchOpenIcon** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |5 |false | + +#### **BranchCloseIcon** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |6 |false | + +#### **JobIcon** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |7 |false | + +#### **Slash** +The icon used for slash. +This will separate directories. +By default ` ⹊ ` (Dotted Solidus [0x2e4a]) + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |8 |false | + +#### **SuccessStyle** +The style to use when things succeed + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |9 |false | + +#### **ErrorStyle** +The style to use when things fail + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |10 |false | + +#### **WarningStyle** +The style to use when warning + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |11 |false | + +#### **InfoStyle** +The style to use when cyan + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |12 |false | + +#### **UnknownStyle** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |13 |false | + +#### **BoldStyle** +The style to use when bolding text. + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |14 |false | + +#### **BoldStyleOff** + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |15 |false | + +#### **ResetStyle** +The string used to reset styles. + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[String]`|false |16 |false | + +#### **ShowHistoryId** +If set, will display the history ID + +|Type |Required|Position|PipelineInput| +|----------|--------|--------|-------------| +|`[Switch]`|false |named |false | + +--- + +### Syntax +```PowerShell +Get-GitPrompt [[-BranchIcon] ] [[-SuccessIcon] ] [[-ErrorIcon] ] [[-HomeIcon] ] [[-BranchOpenIcon] ] [[-BranchCloseIcon] ] [[-JobIcon] ] [[-Slash] ] [[-SuccessStyle] ] [[-ErrorStyle] ] [[-WarningStyle] ] [[-InfoStyle] ] [[-UnknownStyle] ] [[-BoldStyle] ] [[-BoldStyleOff] ] [[-ResetStyle] ] [-ShowHistoryId] [] +``` diff --git a/docs/git/status/Diff.md b/docs/git/status/Diff.md new file mode 100644 index 00000000..fbd9c4d8 --- /dev/null +++ b/docs/git/status/Diff.md @@ -0,0 +1,15 @@ +git.status.Diff() +----------------- + +### Synopsis +Gets Status Difference + +--- + +### Description + +Gets the diff of a status. + +This will show any modifications to files. + +--- diff --git a/docs/git/status/README.md b/docs/git/status/README.md new file mode 100644 index 00000000..21d716da --- /dev/null +++ b/docs/git/status/README.md @@ -0,0 +1,18 @@ +## git.status + + +### Script Properties + + +* [get_Added](get_Added.md) +* [get_Ahead](get_Ahead.md) +* [get_Behind](get_Behind.md) +* [get_Deleted](get_Deleted.md) +* [get_Modified](get_Modified.md) +* [get_Removed](get_Removed.md) + + +### Script Methods + + +* [Diff()](Diff.md) diff --git a/docs/git/status/get_Added.md b/docs/git/status/get_Added.md new file mode 100644 index 00000000..d6aabf11 --- /dev/null +++ b/docs/git/status/get_Added.md @@ -0,0 +1,13 @@ +get_Added +--------- + +### Synopsis +Gets staged added files + +--- + +### Description + +Gets staged added files. These are all files that are new or modified. + +--- diff --git a/docs/git/status/get_Ahead.md b/docs/git/status/get_Ahead.md new file mode 100644 index 00000000..c2081f3d --- /dev/null +++ b/docs/git/status/get_Ahead.md @@ -0,0 +1,13 @@ +get_Ahead +--------- + +### Synopsis +Gets the commits ahead + +--- + +### Description + +Gets the number of commits ahead of the remote branch. + +--- diff --git a/docs/git/status/get_Behind.md b/docs/git/status/get_Behind.md new file mode 100644 index 00000000..2ec881ec --- /dev/null +++ b/docs/git/status/get_Behind.md @@ -0,0 +1,13 @@ +get_Behind +---------- + +### Synopsis +Gets the commits behind + +--- + +### Description + +Gets the number of commits behind the remote branch. + +--- diff --git a/docs/git/status/get_Deleted.md b/docs/git/status/get_Deleted.md new file mode 100644 index 00000000..c946ec21 --- /dev/null +++ b/docs/git/status/get_Deleted.md @@ -0,0 +1,13 @@ +get_Deleted +----------- + +### Synopsis +Gets unstaged deleted paths. + +--- + +### Description + +Gets unstaged deleted paths. + +--- diff --git a/docs/git/status/get_Modified.md b/docs/git/status/get_Modified.md new file mode 100644 index 00000000..6f51bff3 --- /dev/null +++ b/docs/git/status/get_Modified.md @@ -0,0 +1,13 @@ +get_Modified +------------ + +### Synopsis +Gets modified files + +--- + +### Description + +Gets unstaged modified files + +--- diff --git a/docs/git/status/get_Removed.md b/docs/git/status/get_Removed.md new file mode 100644 index 00000000..f63bbbe1 --- /dev/null +++ b/docs/git/status/get_Removed.md @@ -0,0 +1,15 @@ +get_Removed +----------- + +### Synopsis +Gets staged removed files + +--- + +### Description + +Gets staged removed files. + +These are all files that are staged for removal. + +--- diff --git a/ugit.format.ps1xml b/ugit.format.ps1xml index 88e77276..7f3b6723 100644 --- a/ugit.format.ps1xml +++ b/ugit.format.ps1xml @@ -2832,74 +2832,41 @@ $BackgroundColor On branch: - - $_.BranchName -notin 'main', 'master' - - $moduleName = 'ugit' - - do { - $lm = Get-Module -Name $moduleName -ErrorAction Ignore - if (-not $lm) { continue } - if ($lm.FormatPartsLoaded) { break } - $wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) { - foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") { - $ParentNode = $partNodeName.Node.ParentNode - "$($ParentNode.Name)={ - $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}" - } - }) -join [Environment]::NewLine - New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) | - Import-Module -Global - $onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'") - - if (-not $lm.OnRemove) { - $lm.OnRemove = $onRemove - } else { - $lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove) - } - $lm | Add-Member NoteProperty FormatPartsLoaded $true -Force - - } while ($false) - - -@(& ${ugit_Format-RichText} -ForegroundColor 'Verbose' -NoClear) -join '' - - - - $_.BranchName -notin 'main', 'master' - - $_.BranchName - - - - $_.BranchName -notin 'main', 'master' - - @(& ${ugit_Format-RichText} -ForegroundColor 'Verbose' ) -join '' - - - - $_.BranchName -in 'main', 'master' - - @(& ${ugit_Format-RichText} -ForegroundColor 'Warning' -NoClear) -join '' - - - - $_.BranchName -in 'main', 'master' - - $_.BranchName - - - - $_.BranchName -in 'main', 'master' - - @(& ${ugit_Format-RichText} -ForegroundColor 'Warning' ) -join '' + + $branchName = $_.BranchName + @( + if ($branchName -notin 'main', 'master', 'latest') { + $PSStyle.Foreground.Cyan + $PSStyle.Bold + } else { + $PSStyle.Formatting.Warning + } + $branchName + $PSStyle.Reset + ) -join '' + - - $_.Status -notlike 'Nothing*' - - $_.Status + [Environment]::NewLine + + $gitStatus = $_ + if ($gitStatus.Status -match 'nothing') { + return '' + } + @( + if ($gitStatus.Ahead -gt 0) { + $PSStyle.Foreground.Green + $PSStyle.Bold + } elseif ($gitStatus.Behind -gt 0 -or + $gitStatus.Status -match 'diverged') { + $PSStyle.Formatting.Error + } + else { + $PSStyle.Foreground.Cyan + } + $gitStatus.Status + [Environment]::NewLine + $PSStyle.Reset + ) -join '' + @@ -2910,89 +2877,131 @@ $BackgroundColor (use git commit -m to commit)" + [Environment]::NewLine - - - $_.Staged - - @(& ${ugit_Format-RichText} -ForegroundColor 'Green' -NoClear) -join '' - $_.Staged - (@(foreach ($line in $($_.Staged | Select-Object ChangeType, Path | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 8)) -split '(?>\r\n|\n)') { - (" " * 4) + $line - }) -join [Environment]::NewLine) + [Environment]::NewLine + (@( + foreach ($staged in $_.Staged) { + "`t$($staged.ChangeType):`t$($staged.Path)" + } + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) - - - $_.Staged - - @(& ${ugit_Format-RichText} -ForegroundColor 'Green' ) -join '' - $_.Unstaged - "Changes Not Staged For Commit: - (use git add <file> to add, git restore <file> to discard changes)" + [Environment]::NewLine + (@( + "Changes $($PSStyle.Foreground.Cyan + $PSStyle.Bold)Not Staged$($PSStyle.Reset) for commit:" + ' (use "git add <file>..." to update what will be committed)' + ' (use "git restore <file>..." to discard changes in working directory)' + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) - $_.Unstaged - - @(& ${ugit_Format-RichText} -ForegroundColor 'Red' -NoClear) -join '' - - - - $_.Unstaged + $_.Unstaged - (@(foreach ($line in $($_.Unstaged | Select-Object ChangeType, Path | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 8)) -split '(?>\r\n|\n)') { - (" " * 4) + $line - }) -join [Environment]::NewLine) + [Environment]::NewLine + (@( + foreach ($staged in $_.Unstaged) { + "`t$( + if ($staged.changetype -eq 'modified') { + $PSStyle.Foreground.Cyan + } + elseif ($PSStyle.changetype -eq 'newfile') { + $PSStyle.Foreground.Green + } + elseif ($staged.changetype -eq 'deleted') { + $PSStyle.Foreground.Red + } else { + $PSStyle.Foreground.White + } + $psStyle.Bold + )$($staged.ChangeType):`t$($staged.Path)$($PSStyle.Reset)" + } + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) - - - $_.Unstaged - - @(& ${ugit_Format-RichText} -ForegroundColor 'Red' ) -join '' - $_.Untracked - "Untracked Files: - (use git add <file> to include in commit)" + [Environment]::NewLine + (@( + "$($PSStyle.Foreground.Magenta + $PSStyle.Bold)Untracked$($PSStyle.Reset) Files:" + ' (use "git add <file>..." to include in what will be committed)' + ) -join [Environment]::NewLine) + ( + [Environment]::NewLine * 2 + ) $_.Untracked - @(& ${ugit_Format-RichText} -ForegroundColor 'Yellow' -NoClear) -join '' + $moduleName = 'ugit' + + do { + $lm = Get-Module -Name $moduleName -ErrorAction Ignore + if (-not $lm) { continue } + if ($lm.FormatPartsLoaded) { break } + $wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) { + foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") { + $ParentNode = $partNodeName.Node.ParentNode + "$($ParentNode.Name)={ + $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}" + } + }) -join [Environment]::NewLine + New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) | + Import-Module -Global + $onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'") + + if (-not $lm.OnRemove) { + $lm.OnRemove = $onRemove + } else { + $lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove) + } + $lm | Add-Member NoteProperty FormatPartsLoaded $true -Force + + } while ($false) + + +@(& ${ugit_Format-RichText} -ForegroundColor 'Magenta' -NoClear) -join '' $_.Untracked - @(foreach ($line in $($_.Untracked | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 8)) -split '(?>\r\n|\n)') { - (" " * 4) + $line - }) -join [Environment]::NewLine + (@( + $gitRoot = $_.GitRoot + foreach ($untracked in $_.Untracked) { + "`t$( + $untracked.FullName.Substring($gitRoot.Length + 1) -replace '[\\/]', '/' + )" + } + ) -join [Environment]::NewLine) + + [Environment]::NewLine + [Environment]::NewLine $_.Untracked - @(& ${ugit_Format-RichText} -ForegroundColor 'Yellow' ) -join '' + @(& ${ugit_Format-RichText} -ForegroundColor 'Magenta' ) -join '' + + + @(& ${ugit_Format-RichText} -ForegroundColor 'Success' -NoClear) -join '' @@ -3002,6 +3011,52 @@ $BackgroundColor } + + @(& ${ugit_Format-RichText} -ForegroundColor 'Success' ) -join '' + + + + + + + + Git.Status.Short + + Git.Status.Short + + + + + + + + $in = $_ + + @( + if ($in.Status) { + $in.Status + } + foreach ($short in $in.Short) { + @( + foreach ($letter in $short.Staged,$short.Unstaged) { + switch ($letter) { + M { $PSStyle.Foreground.Cyan; $_} + R { $PSStyle.Foreground.Red;$_} + N { $PSStyle.Foreground.Green;$_} + ? { $PSStyle.Foreground.Magenta;$_} + default { $_ } + } + } + + ' ' + $short.Path + $PSStyle.Reset + ) -join '' + } + ) -join [Environment]::NewLine + + + diff --git a/ugit.psd1 b/ugit.psd1 index 318623bd..4397ea4c 100644 --- a/ugit.psd1 +++ b/ugit.psd1 @@ -1,5 +1,5 @@ @{ - ModuleVersion = '0.4.6' + ModuleVersion = '0.4.7' RootModule = 'ugit.psm1' FormatsToProcess = 'ugit.format.ps1xml' TypesToProcess = 'ugit.types.ps1xml' @@ -10,14 +10,21 @@ Description = 'ugit: git, updated with PowerShell' FunctionsToExport = @( - 'Get-UGitExtension','Use-Git','Out-Git','Get-GitFunction' + 'Get-UGitExtension','Use-Git','Out-Git', + + 'Get-GitFunction' + + 'Get-GitOctal' + + 'Get-GitPrompt' ) AliasesToExport = @( 'git', 'gitreal', 'realgit', 'gitFunction', 'gitFunctions', - 'git.func', 'git.function', 'git.functions' + 'git.func', 'git.function', 'git.functions', + 'git.octal', 'git.prompt' ) PrivateData = @{ @@ -34,26 +41,29 @@ * [Discussions](https://github.com/StartAutomating/ugit/discussions/) * [Website](https://ugit.start-automating.com/) -## ugit 0.4.6: +## ugit 0.4.7 + +* `git status` improvement + * `git.status.ahead` (#358) + * `git.status.behind` (#359) + * `git.status.modified` (#356) + * `git.status.deleted` (#357) + * `git.status.addded` (#361) + * `git.status` formatting improvement (#363) + * `git.status.WorkingDirectory` (#365) + * `git.status.Unmerged` (#367) + * `git.status.Diff` (#364) + * `git status --short` support (#343) + * Performance and fault tolerance (#366) +* Removing IO.FileInfo extended properties (#369) +* Internationalization support (#177) +* New Functions: + * `Get-GitPrompt` (#368) + * `Get-GitOctal` (#370) + +To use the new prompt, you can simply: -* Git Functions Support! (#259) - * Any function or alias named git.* will be considered a git function - * git functions can be run in ugit implicitly (i.e. `git functions`) - * git functions can have parameters, accept arguments, and bind from the pipeline. - * We can get git functions with `Get-GitFunction` (#352) -* `git worktree list` support (#347) -* git status improvement (#341) - * Adding color ( Thanks @derekthecool !) - * Using Green for staged, Red for unstaged, Yellow for Untracked. -* Container Changes - * Fixing requirements (#348) - * Using DotNet SDK as base (#349) -* Workflow updates (#350) - * Updating upload-artifact - * Removing PSA from build -* Minor Updates - * `ugit.Piecemeal.ps1` now #requires Piecemeal (#344, Thanks @ninmonkey !) - * Community Guides (#329,#330,#331) +Set-Alias prompt git.prompt --- diff --git a/ugit.types.ps1xml b/ugit.types.ps1xml index e1a9598e..6d6ad2f1 100644 --- a/ugit.types.ps1xml +++ b/ugit.types.ps1xml @@ -706,6 +706,26 @@ Pop-Location git.status + + Diff + + Push + + Added + + <# +.SYNOPSIS + Gets staged added files +.DESCRIPTION + Gets staged added files. These are all files that are new or modified. +#> +@(foreach ($staged in $this.staged) { + if ($staged.ChangeType -in 'newfile','modified') { + $staged.File + } +}) + + + + Ahead + + <# +.SYNOPSIS + Gets the commits ahead +.DESCRIPTION + Gets the number of commits ahead of the remote branch. +#> +if ($this.Status -match 'ahead') { + $this.Status -replace '\D' -as [int] +} else { + 0 +} + + + + Behind + + <# +.SYNOPSIS + Gets the commits behind +.DESCRIPTION + Gets the number of commits behind the remote branch. +#> +if ($this.Status -match 'behind') { + @($this.Status -replace '[\D-[,]]','' -split ',',2)[0] -as [int] +} else { + 0 +} + + + + + Deleted + + <# +.SYNOPSIS + Gets unstaged deleted paths. +.DESCRIPTION + Gets unstaged deleted paths. +#> +@(foreach ($unstaged in $this.Unstaged) { + if ($unstaged.ChangeType -eq 'deleted') { + $unstaged.Path + } +}) + + + + Modified + + <# +.SYNOPSIS + Gets modified files +.DESCRIPTION + Gets unstaged modified files +#> +@(foreach ($unstaged in $this.Unstaged) { + if ($unstaged.ChangeType -eq 'modified') { + $unstaged.File + } +}) + + + + Removed + + <# +.SYNOPSIS + Gets staged removed files +.DESCRIPTION + Gets staged removed files. + + These are all files that are staged for removal. +#> +@(foreach ($staged in $this.staged) { + if ($staged.ChangeType -in 'deleted') { + $staged.Path + } +}) + + + + + + git.status.short + @@ -783,46 +907,6 @@ WorktreePath System.IO.FileInfo - - Changes - GitChanges - - - Diff - GitDiff - - - GitDifference - GitDiff - - - GitHistory - GitChanges - - - GitLog - GitLogs - - - HasChanged - GitDirty - - - HasChanges - GitDirty - - - History - GitChanges - - - IsUnstaged - GitDirty - - - Logs - GitLogs - GitChanges - - GitDiff - - Push-Location $this.Directory -(git log -n 1 $this.Name).Diff() -Pop-Location - - - - - GitDirty - - Push-Location $this.Directory -$(git status $this.Name '-s') -as [bool] -Pop-Location - - - - - GitLogs - - Push-Location $this.Directory -git log $this.Name -Pop-Location - - -