Skip to content

Commit 72a4986

Browse files
authored
Add-ToModuleGitCommit (#94)
* Add-ToModuleGitCommit * Final Cleanup
1 parent d006e15 commit 72a4986

7 files changed

Lines changed: 299 additions & 122 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Git Repository
2+
function Assert-AddGitRepository{
3+
param(
4+
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
5+
[Alias("PSPath")][ValidateNotNullOrEmpty()]
6+
[string] $Path
7+
)
8+
process{
9+
$Path = $Path | Convert-Path
10+
11+
Assert-ItemExist -Path ($Path | Join-Path -ChildPath ".git") -Comment ".git"
12+
}
13+
}
14+
15+
function Assert-AddGitCommit{
16+
param(
17+
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
18+
[Alias("PSPath")][ValidateNotNullOrEmpty()]
19+
[string] $Path,
20+
[Parameter(Mandatory)][string]$MessageExpected
21+
)
22+
process{
23+
$Path = $Path | Convert-Path
24+
25+
# Extract last commit message from log to check body message
26+
$body= (git -C $Path log -1 --pretty=%B | out-string).Trim()
27+
Assert-AreEqual -Expected $MessageExpected -Presented $body -Comment "Git commit message"
28+
29+
# Extarct last commit message from log to check author
30+
$author = git -C $Path log -1 --pretty='[%an][%ae]'
31+
Assert-AreEqual -Expected "[TestingHelper Agent][tha@sample.com]" -Presented $author -Comment "Git commit author"
32+
}
33+
}

TestingHelperTest/public/Add-ToModule.Tests.ps1

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -68,61 +68,3 @@ function TestingHelperTest_AddToModule_FULL_PipeCalls_Module{
6868
Remove-Module -Name "MyModule"
6969
}
7070

71-
function TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder{
72-
73-
New-TestingFolder -Path "folderName"
74-
75-
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
76-
$result | Assert-AddGitRepository
77-
78-
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru @WarningParameters
79-
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar
80-
81-
}
82-
83-
function TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder_Force{
84-
85-
New-TestingFolder -Path "folderName"
86-
87-
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
88-
$result | Assert-AddGitRepository
89-
90-
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force -PassThru @WarningParameters
91-
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
92-
}
93-
94-
function TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder_WhatIf_DoubleCall{
95-
96-
$folder = New-TestingFolder -Path "folderName" -PassThru
97-
98-
# WhatIf
99-
$result = $folder | Add-TT_ToModuleGitRepository -Whatif @WarningParameters
100-
Assert-IsNull -Object $result
101-
Assert-Count -Expected 0 -Presented $warningVar
102-
103-
# First call
104-
$result = $folder | Add-TT_ToModuleGitRepository @WarningParameters
105-
Assert-IsNull -Object $result
106-
Assert-Count -Expected 0 -Presented $warningVar
107-
108-
# Second call
109-
$result = $folder | Assert-AddGitRepository
110-
Assert-IsNull -Object $result
111-
Assert-Count -Expected 0 -Presented $warningVar
112-
113-
# Second call Whatif
114-
$result = $folder | Add-TT_ToModuleGitRepository -whatif @WarningParameters
115-
Assert-IsNull -Object $result
116-
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar
117-
118-
# Second call -force -whatif
119-
$result = $folder | Add-TT_ToModuleGitRepository -whatif -force @WarningParameters
120-
Assert-IsNull -Object $result
121-
Assert-Count -Expected 0 -Presented $warningVar
122-
123-
# Second call -force
124-
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force @WarningParameters
125-
Assert-IsNull -Object $result
126-
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
127-
}
128-
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
function TestingHelperTest_AddToModuleGitRepository_Init_PipeCalls_Folder{
2+
3+
New-TestingFolder -Path "folderName"
4+
5+
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
6+
$result | Assert-AddGitRepository
7+
8+
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru @WarningParameters
9+
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar
10+
11+
}
12+
13+
function TestingHelperTest_AddToModuleGitRepository_Init_PipeCalls_Folder_Force{
14+
15+
New-TestingFolder -Path "folderName"
16+
17+
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
18+
$result | Assert-AddGitRepository
19+
20+
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force -PassThru @WarningParameters
21+
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
22+
}
23+
24+
function TestingHelperTest_AddToModuleGitRepository_Init_PipeCalls_Folder_WhatIf_DoubleCall{
25+
26+
$folder = New-TestingFolder -Path "folderName" -PassThru
27+
28+
# WhatIf
29+
$result = $folder | Add-TT_ToModuleGitRepository -Whatif @WarningParameters
30+
Assert-IsNull -Object $result
31+
Assert-Count -Expected 0 -Presented $warningVar
32+
33+
# First call
34+
$result = $folder | Add-TT_ToModuleGitRepository @WarningParameters
35+
Assert-IsNull -Object $result
36+
Assert-Count -Expected 0 -Presented $warningVar
37+
38+
# Second call
39+
$result = $folder | Assert-AddGitRepository
40+
Assert-IsNull -Object $result
41+
Assert-Count -Expected 0 -Presented $warningVar
42+
43+
# Second call Whatif
44+
$result = $folder | Add-TT_ToModuleGitRepository -whatif @WarningParameters
45+
Assert-IsNull -Object $result
46+
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar
47+
48+
# Second call -force -whatif
49+
$result = $folder | Add-TT_ToModuleGitRepository -whatif -force @WarningParameters
50+
Assert-IsNull -Object $result
51+
Assert-Count -Expected 0 -Presented $warningVar
52+
53+
# Second call -force
54+
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force @WarningParameters
55+
Assert-IsNull -Object $result
56+
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
57+
}
58+
59+
function TestingHelperTest_AddToModuleGitCommit_PipeCalls_Folder{
60+
61+
$folder = New-TestingFolder -Path "folderName" -PassThru
62+
63+
$result = $folder | Add-TT_ToModuleGitCommit @ErrorParameters
64+
Assert-IsNull -Object $result
65+
Assert-Contains -Expected "Git repository does not exist. Use -Force or Add-ToModuleGitRepository to create it." -Presented $errorVar
66+
67+
# -Force
68+
$result = $folder | Add-TT_ToModuleGitCommit -Force -Passthru @ErrorParameters
69+
$result | Assert-AddGitRepository
70+
$result | Assert-AddGitCommit -MessageExpected "TH Init commit"
71+
72+
# No Message
73+
$result | Add-TT_ToModuleGitCommit -PassThru @ErrorParameters
74+
$result | Assert-AddGitCommit -MessageExpected "TH Commit"
75+
76+
# With Message
77+
$result | Add-TT_ToModuleGitCommit -Message "Some message to the commit" -PassThru @ErrorParameters
78+
$result | Assert-AddGitCommit -MessageExpected "Some message to the commit"
79+
80+
}
81+

private/Git.Dependency.ps1

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,43 @@
44

55
$GITLASTERROR = $null
66

7+
# Reset git configuration
8+
function Reset-GitRepoConfiguration {
9+
[CmdletBinding(SupportsShouldProcess)]
10+
param(
11+
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
12+
[Alias("PSPath")][ValidateNotNullOrEmpty()]
13+
[string] $Path
14+
)
15+
16+
begin{
17+
$userName = "TestingHelper Agent"
18+
$userEmail = "tha@sample.com"
19+
}
20+
21+
process{
22+
#check if its null or empty
23+
if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) {
24+
$result1 = git -C $Path config user.email $userEmail
25+
if($LASTEXITCODE -ne 0){
26+
$GITLASTERROR = "Git config user.email failed - $result1"
27+
return $null
28+
}
29+
}
30+
31+
#check if its null or empty
32+
if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) {
33+
$result2 = git -C $Path config user.name $userName
34+
if($LASTEXITCODE -ne 0){
35+
$GITLASTERROR = "Git config user.name failed - $result2"
36+
return $null
37+
}
38+
}
39+
40+
return $true
41+
}
42+
}
43+
744
# Initializae git repository
845
function script:Invoke-GitRepositoryInit{
946
[CmdletBinding()]
@@ -13,33 +50,72 @@ function script:Invoke-GitRepositoryInit{
1350

1451
# check if git is installed
1552
$gitPath = Get-Command -Name git -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
16-
1753
if(!$gitPath){
1854
$GITLASTERROR = "Git is not installed"
1955
return $null
2056
}
2157

22-
$result = git init $Path
58+
# Initialize git repository
59+
# Silence warnings from git STDERR stream. 2>$null
60+
$result = git -C $Path init --initial-branch="main" 2>$null
2361

2462
# check the result of git call
2563
if($LASTEXITCODE -ne 0){
26-
$GITLASTERROR = "Git init failed"
64+
$GITLASTERROR = "Git init failed."
2765
return $null
2866
}
2967

3068
$GITLASTERROR = $null
3169
return $result
3270
}
3371

34-
function script:Test-GitRepository{
72+
# Create a commit with actual changes
73+
function script:Invoke-GitRepositoryCommit{
3574
[CmdletBinding()]
3675
param(
37-
[Parameter(Mandatory)][string]$Path
76+
[Parameter(Mandatory)][string]$Path,
77+
[Parameter(Mandatory)][string]$Message
3878
)
3979

40-
$gitPath = $Path | Join-Path -ChildPath ".git"
80+
# Reset git configuration.
81+
$gitReset = Reset-GitRepoConfiguration -Path $Path
82+
if(!$gitReset){
83+
$GITLASTERROR = "Git Resetting configuration failed - $GITLASTERROR"
84+
return $null
85+
}
4186

42-
$ret = Test-Path -Path $gitPath
87+
# check if git is installed
88+
$gitPath = Get-Command -Name git -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
89+
if(!$gitPath){
90+
$GITLASTERROR = "Git is not installed"
91+
return $null
92+
}
93+
94+
# Stage all changes
95+
$result = git -C $Path add .
96+
# check the result of git call
97+
if($LASTEXITCODE -ne 0){
98+
$GITLASTERROR = "Git staginig failed - $result"
99+
return $null
100+
}
101+
102+
# Commit
103+
$result = git -C $Path commit --allow-empty -m $Message
104+
if($LASTEXITCODE -ne 0){
105+
$GITLASTERROR = "Git commit failed - $result"
106+
return $null
107+
}
43108

44-
return $ret
109+
$GITLASTERROR = $null
110+
return $result
111+
}
112+
113+
# Check if the folder is a git repository
114+
function script:Test-GitRepository{
115+
[CmdletBinding()]
116+
param(
117+
[Parameter(Mandatory)][string]$Path
118+
)
119+
# check if we are on a git folder
120+
return ((git -C $Path rev-parse --is-inside-work-tree 2>$null) -eq "true")
45121
}

public/Add-ToModule.ps1

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,6 @@ function Add-ToModuleDevContainerJson{
5151
}
5252
} Export-ModuleMember -Function Add-ToModuleDevContainerJson
5353

54-
# Adds git repository to the module
55-
function Add-ToModuleGitRepository{
56-
[CmdletBinding(SupportsShouldProcess)]
57-
param(
58-
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
59-
[Alias("PSPath")][ValidateNotNullOrEmpty()]
60-
[string] $Path,
61-
[Parameter(ValueFromPipelineByPropertyName)][switch]$Force,
62-
[Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru
63-
)
64-
65-
process{
66-
$Path = NormalizePath -Path:$Path ?? return $null
67-
$ret = ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru
68-
69-
# check if git was initialized before on this folder
70-
71-
if((Test-GitRepository -Path $Path) -and (!$Force)){
72-
Write-Warning "Git repository already exists."
73-
return $ret
74-
}
75-
76-
if ($PSCmdlet.ShouldProcess($Path, "Git init")) {
77-
78-
$result = Invoke-GitRepositoryInit -Path $Path
79-
} else {
80-
# Fake a success run
81-
$result = "Initialized empty Git repository in"
82-
}
83-
84-
if(!$result){
85-
Write-Error "Git init failed. $GITLASTERROR"
86-
return $ret
87-
}
88-
89-
# Write warning of the execution if needed
90-
# SUCCESS "Initialized empty Git repository in $Path/.git/"
91-
# ALREADY "Reinitialized existing Git repository in $Path/.git/"
92-
if (!($result.StartsWith("Initialized empty Git repository in"))) {
93-
94-
if($result.StartsWith("Reinitialized existing Git repository in") -and $Force){
95-
Write-Warning "Reinitialized existing Git repository."
96-
97-
} else {
98-
Write-Warning "Git init may have failed. Please check the output"
99-
}
100-
}
101-
102-
return $ret
103-
104-
}
105-
} Export-ModuleMember -Function Add-ToModuleGitRepository
106-
10754
# Add License file
10855
function Add-ToModuleLicense{
10956
[CmdletBinding(SupportsShouldProcess)]

0 commit comments

Comments
 (0)