Skip to content

Commit

Permalink
Merge pull request #272 from StartAutomating/ugit-docker
Browse files Browse the repository at this point in the history
ugit 0.4.4
  • Loading branch information
StartAutomating authored Apr 16, 2024
2 parents e61bd78 + 7f29524 commit 613110b
Show file tree
Hide file tree
Showing 23 changed files with 814 additions and 39 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/TestAndPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,43 @@ jobs:
- name: PSA
uses: StartAutomating/PSA@main
id: PSA
- name: Log in to the Container registry (on branch)
uses: docker/login-action@master
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker Metadata (for branch)
if: ${{github.ref_name != 'main' && github.ref_name != 'master' && github.ref_name != 'latest'}}
id: meta
uses: docker/metadata-action@master
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Extract Docker Metadata (for main)
if: ${{github.ref_name == 'main' || github.ref_name == 'master' || github.ref_name == 'latest'}}
id: metaMain
uses: docker/metadata-action@master
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: latest=true
- name: Build and push Docker image (from main)
if: ${{github.ref_name == 'main' || github.ref_name == 'master' || github.ref_name == 'latest'}}
uses: docker/build-push-action@master
with:
context: .
push: true
tags: ${{ steps.metaMain.outputs.tags }}
labels: ${{ steps.metaMain.outputs.labels }}
- name: Build and push Docker image (from branch)
if: ${{github.ref_name != 'main' && github.ref_name != 'master' && github.ref_name != 'latest'}}
uses: docker/build-push-action@master
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
env:
AT_PROTOCOL_HANDLE: mrpowershell.bsky.social
AT_PROTOCOL_APP_PASSWORD: ${{ secrets.AT_PROTOCOL_APP_PASSWORD }}
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
50 changes: 50 additions & 0 deletions Build/GitHub/Jobs/buildugit.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,56 @@
name = 'PSA'
uses = 'StartAutomating/PSA@main'
id = 'PSA'
},
@{
'name'='Log in to the Container registry (on branch)'
'uses'='docker/login-action@master'
'with'=@{
'registry'='${{ env.REGISTRY }}'
'username'='${{ github.actor }}'
'password'='${{ secrets.GITHUB_TOKEN }}'
}
},
@{
name = 'Extract Docker Metadata (for branch)'
if = '${{github.ref_name != ''main'' && github.ref_name != ''master'' && github.ref_name != ''latest''}}'
'id'='meta'
'uses'='docker/metadata-action@master'
'with'=@{
'images'='${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}'
}
},
@{
name = 'Extract Docker Metadata (for main)'
if = '${{github.ref_name == ''main'' || github.ref_name == ''master'' || github.ref_name == ''latest''}}'
'id'='metaMain'
'uses'='docker/metadata-action@master'
'with'=@{
'images'='${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}'
'flavor'='latest=true'
}
},
@{
name = 'Build and push Docker image (from main)'
if = '${{github.ref_name == ''main'' || github.ref_name == ''master'' || github.ref_name == ''latest''}}'
uses = 'docker/build-push-action@master'
'with'=@{
'context'='.'
'push'='true'
'tags'='${{ steps.metaMain.outputs.tags }}'
'labels'='${{ steps.metaMain.outputs.labels }}'
}
},
@{
name = 'Build and push Docker image (from branch)'
if = '${{github.ref_name != ''main'' && github.ref_name != ''master'' && github.ref_name != ''latest''}}'
uses = 'docker/build-push-action@master'
with = @{
'context'='.'
'push'='true'
'tags'='${{ steps.meta.outputs.tags }}'
'labels'='${{ steps.meta.outputs.labels }}'
}
}
)
}
6 changes: 4 additions & 2 deletions Build/ugit.GitHubWorkflow.PSDevOps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Push-Location ($PSScriptRoot | Split-Path)
New-GitHubWorkflow -Name "Analyze, Test, Tag, and Publish" -On Push, PullRequest, Demand -Job PowerShellStaticAnalysis,
TestPowerShellOnLinux,
TagReleaseAndPublish,
buildugit -OutputPath .\.github\workflows\TestAndPublish.yml -Env @{
buildugit -OutputPath .\.github\workflows\TestAndPublish.yml -Env ([Ordered]@{
"AT_PROTOCOL_HANDLE" = "mrpowershell.bsky.social"
"AT_PROTOCOL_APP_PASSWORD" = '${{ secrets.AT_PROTOCOL_APP_PASSWORD }}'
}
"REGISTRY" = "ghcr.io"
"IMAGE_NAME" = '${{ github.repository }}'
})

New-GitHubWorkflow -On Issue,
Demand -Job RunGitPub -Name OnIssueChanged -OutputPath .\.github\workflows\OnIssue.yml
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
## ugit 0.4.4:

> Like It? [Star It](https://github.com/StartAutomating/ugit)
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
* ugit a container! (#262, #263, #264)
* `docker run --interactive --tty ghcr.io/startautomating/ugit`
* `git checkout` improvements
* `git checkout -PullRequest [int]` (#178)
* `git checkout -NewBranchName [string]` (#266)
* `git checkout -ResetBranchName [string]` (#267)
* `git checkout -Detach [switch]` (#268)
* `git checkout -ResetPath [string]` (#269)
* `git checkout -FromBranch [string]` (#270)
* `git checkout -RevisionNumber/-ParentNumber [int]` (#271)
* `git sparse-checkout` improvements
* `git sparse-checkout -FileFilter [string[]]` ( Fixes #257 )
* `git sparse-checkout -DirectoryFilter [string[]]` ( Fixes #258 )
* `git branch -Remote [switch]` (#185)
* `git config --list` outputs as objects (#265)
* `git --format json` outputs as objects (#239)

---

## ugit 0.4.3:

* Cloning Improvements:
Expand Down
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
FROM mcr.microsoft.com/powershell

RUN apt-get update && apt-get install -y git curl ca-certificates libc6 libgcc1

ENV PSModulePath ./Modules

COPY . ./Modules/ugit
RUN pwsh -c "New-Item -Path /root/.config/powershell/Microsoft.PowerShell_profile.ps1 -Value 'Import-Module ugit' -Force"
RUN apt-get update && apt-get install -y git curl ca-certificates libc6 libgcc1

RUN pwsh -c "New-Item -ItemType File -Path \$Profile -Force -Value 'Import-Module ugit'"
22 changes: 22 additions & 0 deletions Extensions/Git.Branch.Input.Ugit.Extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<#
.SYNOPSIS
git branch input extension
.DESCRIPTION
Extends the parameters for git branch, making it easier to use from PowerShell.
.EXAMPLE
git branch -Remote
#>
[ValidatePattern('^git branch')]
[Management.Automation.Cmdlet("Use","Git")]
[CmdletBinding(PositionalBinding=$false)]
param(
# If set, will add the --remote flag to the command.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Remotes')]
[switch]
$Remote
)

if ($Remote) {
"--remote"
}
115 changes: 115 additions & 0 deletions Extensions/Git.Checkout.Input.Ugit.Extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<#
.SYNOPSIS
git checkout input extension
.DESCRIPTION
Extends the parameters for git checkout, making it easier to use from PowerShell.
If the -PullRequest parameter is provided, the branch will be fetched from the remote.
If the -BranchName parameter is provided, the branch will be checked out.
#>
[ValidatePattern('^git checkout')]
[Management.Automation.Cmdlet("Use","Git")]
[CmdletBinding(PositionalBinding=$false)]
param(
# The branch name.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$BranchName,

# The number of the pull request.
# If no Branch Name is provided, the branch will be `PR-$PullRequest`.
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$PullRequest,

# The name of a new branch
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('New','NewBranch')]
[string]
$NewBranchName,

# The name of a branch to reset.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('ResetBranch')]
[string]
$ResetBranchName,

# One or more specific paths to reset.
# This will overwrite the contents of the files with the contents of the index.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Reset')]
[string[]]
$ResetPath,

# The name of the branch to checkout from.
# This is only used when the -ResetPath parameter is provided.
# It defaults to `HEAD`.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$FromBranch = 'HEAD',

# The revision number to checkout.
# This is only used when the -ResetPath parameter is provided.
# If provided, this will checkout the Nth most recent parent.
# Eg. HEAD~2
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$RevisionNumber,

# The pattern number to checkout.
# This is only used when the -ResetPath parameter is provided.
# If provided, this will checkout the Nth most recent parent.
# Eg. HEAD^2
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$ParentNumber,

# If set, will checkout a branch in a detached state.
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$Detach
)

if ($Detach) {
"--detach"
}

if ($PullRequest) {
$remoteName = git remote | Select-Object -ExpandProperty RemoteName | Select-Object -First 1
if (-not $BranchName) {
$BranchName = "PR-$PullRequest"
}
$fetchedBranch = git fetch $remoteName "pull/$PullRequest/head:$BranchName"
if (-not $fetchedBranch) {
return $BranchName
}
$BranchName
}
elseif ($NewBranchName) {
"-b"
$NewBranchName
}
elseif ($ResetBranchName) {
if ($PSCmdlet -and $PSCmdlet.ShouldProcess("Reset branch $ResetBranchName")) {
"-B" # Beware of capital B in git
$ResetBranchName
}
}
elseif ($BranchName) {
$BranchName
}

if ($ResetPath) {
if ($ParentNumber) {
"$FromBranch^$ParentNumber"
} elseif ($RevisionNumber) {
"$FromBranch~$RevisionNumber"
} else {
"$FromBranch"
}

foreach ($pathToReset in $ResetPath) {
$pathToReset
}
}
61 changes: 61 additions & 0 deletions Extensions/Git.Config.List.ugit.extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<#
.SYNOPSIS
git config list extension
.DESCRIPTION
Parses the output of git config --list int a PowerShell object.
.EXAMPLE
git config --list
.EXAMPLE
git config --global --list
.EXAMPLE
git config --list --local
.EXAMPLE
git config --list --show-origin
#>
[Management.Automation.Cmdlet("Out","Git")] # It extends Out-Git
[ValidatePattern('^[\S]{0,}git config[\s\S]{1,}--list')]
param()

begin {
$configLines = @()
}

process {
$configLines += "$gitOut"
}

end {
$configEntries = [Ordered]@{}

# Only lines containing an = are considered configuration entries
foreach ($configLine in $configLines -match '=') {
# Split the line into key and value
$key, $value = $configLine -split '=', 2
# If the key starts with file:, replace spaces with ? and convert to URI
if ($key -match '^file:') {
$key = $key -replace '\s', '?' -as [uri]
}
# If there are no entries, set the value
if (-not $configEntries[$key]) {
$configEntries[$key] = $value
} else {
# If there are multiple entries, convert to array
$configEntries[$key] = @($configEntries[$key]) + $value
}
}

# If there were any entries,
if ($configEntries.Count) {
# create a custom object
$configObject = [PSCustomObject]$configEntries
# decorate it as a 'git.config.list' object
$configObject.PSTypeNames.insert(0,'git.config.list')
# and output it.
$configObject
} else {
# If there were no entries, output the original lines
$configLines
}
}


28 changes: 28 additions & 0 deletions Extensions/Git.Format.Json.ugit.extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<#
.SYNOPSIS
git json format
.DESCRIPTION
Parses the output of git format, if the results are a series of json objects
.EXAMPLE
git branch --format "{'ref':'%(refname:short)','parent':'%(parent)'}"
#>
[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git
[ValidatePattern("\s-{2}format.+?[\[\{].+?[\]\}]", Options = 'IgnoreCase,IgnorePatternWhitespace'
)]
param()


process {
$gitOutJson = try {
if ($gitOut) { $gitOut | ConvertFrom-Json}
} catch {
$null
}
if ($gitOutJson) {
$gitOutJson
return
}
else {
return $gitOut
}
}
Loading

0 comments on commit 613110b

Please sign in to comment.