Watch Star Fork

oscstudio / git-securePowerShellMIT

pull.ps1 2.93 KB · 1 Lines
一键复制 编辑 原始数据 标准视图 历史
1 #### pull from remote
2
3
4 $PrefixDir = Split-Path -Parent $PSScriptRoot
5 Import-Module -Name "$PrefixDir/modules/AesProvider"
6 Import-Module -Name "$PrefixDir/modules/Git"
7 Import-Module -Name "$PrefixDir/modules/Process"
9 $gitfiles = @(
10 ".gitmodules",
11 ".gitignore",
12 ".gitattributes"
13 )
14
15 $workdir = Get-WorktreeDir
16 $gitdir = Get-CurrentGitDir -Worktree $workdir
19 Pop-Location
21 Set-Location "$gitdir/secure"
22 $op = Get-Content -ErrorAction Ignore -Path "$gitdir/pointer.json"|ConvertFrom-Json
24 $result = ProcessArgv -FilePath git -ArgumentList $args
25 if ($result -ne 0) {
26 exit $result
30 $aeskey = git --git-dir=`"$gitdir/secure/.git`" config "aes.key"
31
32 if ($null -eq $aeskey) {
33 $aeskey = Read-Host "Please input your aes key"
34 }
35
36
37 if ($null -eq $op) {
38 Write-Host "Resolve secure file"
39 git ls-tree -r HEAD|ForEach-Object {
40 $obj = -Split $_
41 $file = $obj[3]
42 $rawfile = "$workdir/$file"
43 if ($gitfiles.Contains($file)) {
44 Copy-Item "$gitdir/secure/$file" -Destination "$workdir/$file" -Force
45 return ## ForEach-Object need return
46 }
47 $sdir = Split-Path -Path $rawfile
48 $res = New-Item -ItemType Directory -Force -Path $sdir
49 if ($null -eq $res) {
50 Write-Host -ForegroundColor Red "create dir: $sdir"
51 return ## ForEach-Object need return
52 }
53 $ret = Restore-AesFile -File $file -Key $aeskey -Destination $rawfile
54 if ($ret -ne 0) {
55 Write-Host -ForegroundColor Red "AES Decrypt $file failed"
56 }
57 else {
58 Write-Host -ForegroundColor Green "Decrypt $file done"
59 }
60 }
61 }
62 else {
63 Write-Host "trace new secure file"
64 Get-Changeset -Commit $op.sid|ForEach-Object {
65 $file = $_["path"]
66 if ($_["status"] -eq "D") {
67 Write-Host "Delete $file"
68 Remove-Item -Path "$workdir/$file" -Force -ErrorAction Ignore
69 return ## ForEach-Object need return
70 }
71 if ($gitfiles.Contains($file)) {
72 Remove-Item -Path "$workdir/$file" -Force -ErrorAction Ignore
73 Copy-Item "$gitdir/secure/$file" -Destination "$workdir/$file" -Force
74 return ## ForEach-Object need return
76 $ret = Restore-AesFile -File "$gitdir/secure/$file" -Key $aeskey -Destination "$workdir/$file"
77 if ($ret -ne 0) {
78 Write-Host -ForegroundColor Red "AES decrypt file '$file' throw exception"
79 }
80 else {
81 Write-Host -ForegroundColor Green "Decrypt $file done"
82 }
83 }
84 }
85
86
87 $scommit = git rev-parse HEAD
88 $message = git log --pretty=format:'%s' -n 1 $scommit
89
90 Set-Location "$workdir"
91 git add -A
92 if ($LASTEXITCODE -ne 0) {
93 exit $LASTEXITCODE
94 }
95 &git commit -m "$message"
96 if ($LASTEXITCODE -ne 0) {
97 exit $LASTEXITCODE
98 }
99 $wcommit = git rev-parse HEAD
100
101
102 $pointer = @{}
103 $pointer["sid"] = $scommit
104 $pointer["wid"] = $wcommit
105
106 ConvertTo-Json -InputObject $pointer|Out-File "$gitdir/pointer.json"
107
108 Push-Location

搜索帮助