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
845function 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}
0 commit comments