Last active
October 31, 2020 17:06
-
-
Save JohnLBevan/2706dc9e2e965c5d88c43f462465d327 to your computer and use it in GitHub Desktop.
Test if a file in a git repo is executable (i.e. has `chmod+x` set). Works on Windows as well as *nix systems.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Thanks to Torek for the git command | |
| # https://stackoverflow.com/questions/64607795/query-git-index-for-chmod-values-on-windows/64617017#64617017 | |
| # https://git-scm.com/docs/git-ls-files | |
| function Test-IsExecutable { | |
| [CmdletBinding()] | |
| Param ( | |
| # Side note: Not only should Path point to a file/files within a git repo | |
| # but also you should call this command from within that same repo | |
| [Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
| [string[]]$Path | |
| , | |
| [Parameter()] | |
| [Switch]$OutputBooleanOnly | |
| ) | |
| Begin { | |
| [int]$mode = 0 | |
| } | |
| Process { | |
| foreach ($file in $Path) { | |
| try { | |
| [string]$output = git ls-files --stage "$file" | |
| $result = [PSCustomObject]@{ | |
| FileName = $file | |
| IsExecutable = [int]::TryParse(($output -replace '^(\d+).*', '$1'), [ref]$mode) -and ($mode -eq 100755) | |
| Output = $output | |
| } | |
| if ($OutputBooleanOnly.IsPresent) { | |
| $result.IsExecutable | |
| } else { | |
| $result | |
| } | |
| } catch { | |
| write-error "$file - $($_.Exception.ToString())" | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment