Skip to content

Commit 7ed8410

Browse files
committed
feat(git): 添加 git 模块及相关测试文件
添加 git.psm1 模块包含 Get-GitIgnorePatterns 和 New-7ZipExcludeArgs 函数 添加对应的测试文件 git.Tests.ps1 验证功能 更新 psd1 文件以包含新模块和导出函数
1 parent addaa49 commit 7ed8410

3 files changed

Lines changed: 166 additions & 2 deletions

File tree

psutils/modules/git.psm1

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
function Get-GitIgnorePatterns {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter()]
5+
[string]$GitIgnorePath = (Join-Path -Path (Get-Location) -ChildPath '.gitignore')
6+
)
7+
8+
$patterns = @()
9+
try {
10+
if (-not (Test-Path -LiteralPath $GitIgnorePath)) {
11+
Write-Verbose "GitIgnore file not found: $GitIgnorePath"
12+
return @()
13+
}
14+
15+
$lines = Get-Content -LiteralPath $GitIgnorePath -ErrorAction Stop
16+
foreach ($line in $lines) {
17+
$t = $line.Trim()
18+
if ([string]::IsNullOrWhiteSpace($t)) { continue }
19+
20+
if ($t.StartsWith('\#')) {
21+
$t = $t.Substring(1)
22+
}
23+
elseif ($t.StartsWith('#')) {
24+
continue
25+
}
26+
27+
if ($t.StartsWith('!')) { continue }
28+
29+
while ($t.StartsWith('/')) { $t = $t.Substring(1) }
30+
if ($t.EndsWith('/')) { $t = $t.TrimEnd('/') }
31+
32+
if (-not [string]::IsNullOrWhiteSpace($t)) { $patterns += $t }
33+
}
34+
}
35+
catch {
36+
Write-Error $_.Exception.Message
37+
return @()
38+
}
39+
return (@($patterns | Select-Object -Unique))
40+
}
41+
42+
function New-7ZipExcludeArgs {
43+
[CmdletBinding()]
44+
param(
45+
[Parameter()]
46+
[string]$GitIgnorePath = (Join-Path -Path (Get-Location) -ChildPath '.gitignore'),
47+
[Parameter()]
48+
[string[]]$AdditionalExcludes = @()
49+
)
50+
51+
$baseExcludes = @(
52+
'node_modules',
53+
'prisma/*.db',
54+
'public/oss',
55+
'public/upload',
56+
'log/',
57+
'.next/',
58+
'.git/',
59+
'*.log',
60+
'*.tmp'
61+
)
62+
63+
$gitPatterns = Get-GitIgnorePatterns -GitIgnorePath $GitIgnorePath
64+
65+
$normalizedBase = $baseExcludes | ForEach-Object {
66+
$p = $_.Trim()
67+
while ($p.StartsWith('/')) { $p = $p.Substring(1) }
68+
if ($p.EndsWith('/')) { $p = $p.TrimEnd('/') }
69+
$p
70+
}
71+
72+
$normalizedAdditional = @()
73+
foreach ($a in $AdditionalExcludes) {
74+
if ([string]::IsNullOrWhiteSpace($a)) { continue }
75+
$p = $a.Trim()
76+
while ($p.StartsWith('/')) { $p = $p.Substring(1) }
77+
if ($p.EndsWith('/')) { $p = $p.TrimEnd('/') }
78+
$normalizedAdditional += $p
79+
}
80+
81+
$all = @($normalizedBase + $gitPatterns + $normalizedAdditional) |
82+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
83+
Select-Object -Unique
84+
85+
$excludeArgs = $all | ForEach-Object { "-xr!$_" }
86+
return $excludeArgs
87+
}
88+
89+
Export-ModuleMember -Function Get-GitIgnorePatterns, New-7ZipExcludeArgs

psutils/psutils.psd1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
'modules\os.psm1',
8686
'modules\proxy.psm1',
8787
'modules\pwsh.psm1',
88+
'modules\git.psm1',
8889
'modules\string.psm1',
8990
'modules\test.psm1',
9091
'modules\win.psm1',
@@ -96,7 +97,7 @@
9697
# 缓存管理模块 (cache.psm1)
9798
'Invoke-WithCache', 'Clear-ExpiredCache', 'Get-CacheStats',
9899
# 环境管理模块 (env.psm1)
99-
'Get-Dotenv', 'Install-Dotenv', 'Import-EnvPath', 'Set-EnvPath', 'Add-EnvPath', 'Get-EnvParam', 'Remove-FromEnvPath','Sync-PathFromBash'
100+
'Get-Dotenv', 'Install-Dotenv', 'Import-EnvPath', 'Set-EnvPath', 'Add-EnvPath', 'Get-EnvParam', 'Remove-FromEnvPath', 'Sync-PathFromBash'
100101
# 错误处理模块 (error.psm1)
101102
'Debug-CommandExecution',
102103
# 文件系统模块 (filesystem.psm1)
@@ -128,7 +129,9 @@
128129
# Windows 系统模块 (win.psm1)
129130
'Add-Startup', 'New-Shortcut',
130131
# 包装器模块 (wrapper.psm1)
131-
'Set-CustomAlias', 'Get-CustomAlias'
132+
'Set-CustomAlias', 'Get-CustomAlias',
133+
# Git 工具模块 (git.psm1)
134+
'Get-GitIgnorePatterns', 'New-7ZipExcludeArgs'
132135
)
133136

134137
# 要从此模块导出的 Cmdlet,为获得最佳性能,请不要使用通配符,如果没有要导出的 Cmdlet,请使用空数组

psutils/tests/git.Tests.ps1

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Import-Module (Join-Path $PSScriptRoot '..' 'psutils.psd1')
2+
3+
Describe 'Git 模块函数' {
4+
Context 'Get-GitIgnorePatterns 解析有效行' {
5+
BeforeAll {
6+
$testRoot = Join-Path $env:TEMP ("psutils_git_tests_" + [Guid]::NewGuid().ToString())
7+
New-Item -ItemType Directory -Path $testRoot | Out-Null
8+
$gitIgnore = Join-Path $testRoot '.gitignore'
9+
@(
10+
'# comment',
11+
'\#headline',
12+
'!important',
13+
'/build/',
14+
'dist/',
15+
'logs/',
16+
'/logs',
17+
'node_modules',
18+
'*.log',
19+
'*.tmp'
20+
) | Set-Content -LiteralPath $gitIgnore -Encoding UTF8
21+
}
22+
23+
It '应返回解析后的有效模式集合' {
24+
$patterns = Get-GitIgnorePatterns -GitIgnorePath $gitIgnore
25+
($patterns) | Should -Not -Be $null
26+
$patterns | Should -Contain 'build'
27+
$patterns | Should -Contain 'dist'
28+
$patterns | Should -Contain 'logs'
29+
$patterns | Should -Contain 'node_modules'
30+
$patterns | Should -Contain '*.log'
31+
$patterns | Should -Contain '*.tmp'
32+
$patterns | Should -Contain '#headline'
33+
$patterns | Should -Not -Contain '!important'
34+
((@($patterns | Where-Object { $_ -eq 'logs' }).Count)) | Should -Be 1
35+
}
36+
37+
AfterAll {
38+
Remove-Item -LiteralPath $testRoot -Recurse -Force
39+
}
40+
}
41+
42+
Context 'New-7ZipExcludeArgs 生成排除参数' {
43+
BeforeAll {
44+
$testRoot = Join-Path $env:TEMP ("psutils_git_tests_" + [Guid]::NewGuid().ToString())
45+
New-Item -ItemType Directory -Path $testRoot | Out-Null
46+
$gitIgnore = Join-Path $testRoot '.gitignore'
47+
@(
48+
'/build/',
49+
'logs/',
50+
'node_modules',
51+
'*.log'
52+
) | Set-Content -LiteralPath $gitIgnore -Encoding UTF8
53+
}
54+
55+
It '应合并基础、gitignore 和额外排除并去重' {
56+
$args = New-7ZipExcludeArgs -GitIgnorePath $gitIgnore -AdditionalExcludes @('dist','build/*.tmp','logs')
57+
($args) | Should -Not -Be $null
58+
foreach ($a in $args) { $a | Should -Match '^-xr!.+' }
59+
$args | Should -Contain '-xr!node_modules'
60+
$args | Should -Contain '-xr!build'
61+
$args | Should -Contain '-xr!dist'
62+
$args | Should -Contain '-xr!logs'
63+
$args | Should -Contain '-xr!*.log'
64+
((@($args | Where-Object { $_ -eq '-xr!logs' }).Count)) | Should -Be 1
65+
}
66+
67+
AfterAll {
68+
Remove-Item -LiteralPath $testRoot -Recurse -Force
69+
}
70+
}
71+
}
72+

0 commit comments

Comments
 (0)