1
+ # !/usr/bin/env pwsh
2
+
3
+ <#
4
+ . SYNOPSIS
5
+ PowerShell version of Git submodule management, replicating `git-submodule.sh`.
6
+
7
+ . DESCRIPTION
8
+ Uses `git submodule--helper` to execute submodule operations like the original shell script.
9
+
10
+ . PARAMETER Command
11
+ The submodule command to execute (e.g., add, init, update, status, deinit).
12
+
13
+ . PARAMETER Path
14
+ The path of the submodule.
15
+
16
+ . PARAMETER Repository
17
+ The repository URL for adding a submodule.
18
+
19
+ . PARAMETER Branch
20
+ The branch to use when adding a submodule.
21
+
22
+ . PARAMETER Reference
23
+ A reference repository.
24
+
25
+ . PARAMETER Force
26
+ Enables force options.
27
+
28
+ . PARAMETER Recursive
29
+ Enables recursive options.
30
+
31
+ . PARAMETER Quiet
32
+ Suppresses output.
33
+
34
+ . PARAMETER Init
35
+ Initializes submodules.
36
+
37
+ . PARAMETER Remote
38
+ Fetches changes from the remote repository.
39
+
40
+ . PARAMETER NoFetch
41
+ Prevents fetching new commits.
42
+
43
+ . PARAMETER Checkout
44
+ Uses "checkout" mode in updates.
45
+
46
+ . PARAMETER Merge
47
+ Uses "merge" mode in updates.
48
+
49
+ . PARAMETER Rebase
50
+ Uses "rebase" mode in updates.
51
+
52
+ . PARAMETER SingleBranch
53
+ Fetches only one branch.
54
+
55
+ . EXAMPLE
56
+ ./git-submodule.ps1 add --Repository "https://github.com/example/repo.git" --Path "submodules/my-submodule"
57
+ #>
58
+
59
+ # system wide setup:
60
+ # git config --global alias.submodule '!pwsh <PATH_TO_THIS_SCRIPT>/git-submodule.ps1'
61
+ # git submodule update --init --recursive
62
+
63
+ param (
64
+ [string ]$Command ,
65
+ [string ]$Path ,
66
+ [string ]$Repository ,
67
+ [string ]$Branch ,
68
+ [string ]$Reference ,
69
+ [switch ]$Quiet ,
70
+ [switch ]$Force ,
71
+ [switch ]$Recursive ,
72
+ [switch ]$Init ,
73
+ [switch ]$Remote ,
74
+ [switch ]$NoFetch ,
75
+ [switch ]$Checkout ,
76
+ [switch ]$Merge ,
77
+ [switch ]$Rebase ,
78
+ [switch ]$SingleBranch
79
+ )
80
+
81
+ $insideGitRepo = git rev- parse - -is - inside- work- tree 2> $null
82
+ if ($insideGitRepo -ne " true" ) {
83
+ Write-Host " Error: Not inside a Git repository." - ForegroundColor Red
84
+ exit 1
85
+ }
86
+
87
+ $gitRoot = git rev- parse -- show-toplevel
88
+ Set-Location $gitRoot
89
+
90
+ function Execute-GitSubmoduleHelper {
91
+ param ([string ]$Subcommand , [string ]$Arguments )
92
+ $cmd = " git submodule--helper $Subcommand $Arguments "
93
+
94
+ if ($Quiet ) { $cmd += " --quiet" }
95
+ if ($Force ) { $cmd += " --force" }
96
+ if ($Recursive ) { $cmd += " --recursive" }
97
+
98
+ Write-Host " Executing: $cmd "
99
+ Invoke-Expression $cmd
100
+ }
101
+
102
+ switch ($Command ) {
103
+ " add" {
104
+ if (-not $Repository -or -not $Path ) {
105
+ Write-Host " Usage: git-submodule.ps1 add --Repository <URL> --Path <Path>" - ForegroundColor Yellow
106
+ exit 1
107
+ }
108
+ $args = " --name '$Path '"
109
+ if ($Branch ) { $args += " --branch '$Branch '" }
110
+ if ($Reference ) { $args += " --reference '$Reference '" }
111
+ Execute- GitSubmoduleHelper " add" " $args '$Repository ' '$Path '"
112
+ }
113
+
114
+ " init" {
115
+ Execute- GitSubmoduleHelper " init" " "
116
+ }
117
+
118
+ " deinit" {
119
+ if (-not $Path ) {
120
+ Write-Host " Usage: git-submodule.ps1 deinit --Path <Path>" - ForegroundColor Yellow
121
+ exit 1
122
+ }
123
+ Execute- GitSubmoduleHelper " deinit" " --force '$Path '"
124
+ }
125
+
126
+ " update" {
127
+ $args = " "
128
+ if ($Init ) { $args += " --init " }
129
+ if ($Remote ) { $args += " --remote " }
130
+ if ($NoFetch ) { $args += " -N " }
131
+ if ($Rebase ) { $args += " --rebase " }
132
+ if ($Merge ) { $args += " --merge " }
133
+ if ($Checkout ) { $args += " --checkout " }
134
+ if ($SingleBranch ) { $args += " --single-branch " }
135
+ Execute- GitSubmoduleHelper " update" " $args '$Path '"
136
+ }
137
+
138
+ " status" {
139
+ Execute- GitSubmoduleHelper " status" " "
140
+ }
141
+
142
+ " set-branch" {
143
+ if (-not $Path -or -not $Branch ) {
144
+ Write-Host " Usage: git-submodule.ps1 set-branch --Path <Path> --Branch <Branch>" - ForegroundColor Yellow
145
+ exit 1
146
+ }
147
+ Execute- GitSubmoduleHelper " set-branch" " --branch '$Branch ' '$Path '"
148
+ }
149
+
150
+ " set-url" {
151
+ if (-not $Path -or -not $NewUrl ) {
152
+ Write-Host " Usage: git-submodule.ps1 set-url --Path <Path> --NewUrl <URL>" - ForegroundColor Yellow
153
+ exit 1
154
+ }
155
+ Execute- GitSubmoduleHelper " set-url" " '$Path ' '$NewUrl '"
156
+ }
157
+
158
+ " summary" {
159
+ Execute- GitSubmoduleHelper " summary" " "
160
+ }
161
+
162
+ " foreach" {
163
+ if (-not $Path ) {
164
+ Write-Host " Usage: git-submodule.ps1 foreach --Path <command>" - ForegroundColor Yellow
165
+ exit 1
166
+ }
167
+ Execute- GitSubmoduleHelper " foreach" " '$Path '"
168
+ }
169
+
170
+ " sync" {
171
+ Execute- GitSubmoduleHelper " sync" " "
172
+ }
173
+
174
+ " absorbgitdirs" {
175
+ Execute- GitSubmoduleHelper " absorbgitdirs" " "
176
+ }
177
+
178
+ default {
179
+ Write-Host " Invalid command. Use: add, init, deinit, update, status, set-branch, set-url, summary, foreach, sync, absorbgitdirs" - ForegroundColor Red
180
+ exit 1
181
+ }
182
+ }
0 commit comments