forked from MicrosoftDocs/Azure-RMSDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateSubmodule.ps1
70 lines (62 loc) · 2.28 KB
/
UpdateSubmodule.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#This tool will only update submodules in local workspace.
param(
[string]$credential,
[switch]$remote=$False
)
$WorkingFolder = (Split-Path $script:MyInvocation.MyCommand.Path -Parent)
$submoduleMetaFile = (Join-Path -Path $WorkingFolder -ChildPath ".gitmodules")
function Add-Credential {
param ([string]$gitUrl, [string]$credential)
$blocks = $gitUrl -split "/|\\"
$protocol = "https:"
$outItems = New-Object System.Collections.Generic.List[System.Object]
if (($blocks[0] -eq "https:") -Or ($blocks[0] -eq "http:") -Or ($blocks[0] -eq "git:")) {
$protocol = $blocks[0]
}
for ($i=1; $i -lt $blocks.Length; $i++) {
if ($blocks[$i]) {
$outItems.Add($blocks[$i])
}
}
return ("{0}{1}{2}" -f $protocol,"//$credential@",($outItems -join "/"))
}
if (!$credential) {
Write-Host "Please specify a credential info (-credential username:password or -credential username:token) to access git."
Write-Host "Terminated."
Exit 1
}
#Update Repository
Write-Host "************************************************"
#Set current working director
Set-Location $WorkingFolder
if (!(Test-Path $submoduleMetaFile)) {
Write-Host "Error: The submodule metadata file $submoduleMetaFile doesn't exists, please add submodule first."
Write-Host "Terminated."
Exit 1
}
Write-Host "git submodule init"
git submodule init
$content = [IO.File]::ReadAllText("$submoduleMetaFile")
$contents = $content -split "\[|\]"
for ($i=1; $i -lt $contents.Length; $i=$i+2) {
$submoduleName = ($contents[$i] -split '"')[1]
$properties = ($contents[$i+1].Trim() -split '["\r\n?"|"\n\r?"]')
$items = New-Object System.Collections.Generic.List[System.Object]
foreach ($property in $properties) {
$property = $property.Trim()
if ($property) {
$items.Add($property)
}
}
$items = $items.toArray()
$submoduleUrl = ($items[1] -split "=")[1].Trim()
$submoduleUrl = Add-Credential -gitUrl $submoduleUrl -credential $credential
git config submodule."$submoduleName".url $submoduleUrl
}
Write-Host "git submodule update"
git submodule update
if ($remote) {
Write-Host "git submodule update --remote"
git submodule update --remote
}
Write-Host "************************************************"