4
4
import static org .nqm .command .Wrapper .forEachModuleWith ;
5
5
import static org .nqm .config .GisConfig .currentDir ;
6
6
import java .io .BufferedReader ;
7
+ import java .io .FileOutputStream ;
7
8
import java .io .IOException ;
8
9
import java .io .InputStreamReader ;
9
10
import java .nio .file .Files ;
@@ -33,6 +34,8 @@ public class GitCommand {
33
34
private static final String ORIGIN = "origin" ;
34
35
private static final Path TMP_FILE = Path .of ("/" , "tmp" , "gis_fetch" + currentDir ().replace ("/" , "_" ));
35
36
37
+ static final String GIS_AUTOCOMPLETE_FILE = "_gis" ;
38
+
36
39
public static final String GIT_STATUS = "status" ;
37
40
public static final String HOOKS_OPTION = "--hooks" ;
38
41
@@ -93,7 +96,8 @@ void checkoutNewBranch(
93
96
@ Parameters (index = "0" , paramLabel = "<new_branch_name>" ,
94
97
description = "branch name" ) String newBranch ,
95
98
@ Parameters (paramLabel = "<modules>" ,
96
- description = "Specified modules. If empty, will create for all submodules and root." ) String ... modules ) throws IOException {
99
+ description = "Specified modules. If empty, will create for all submodules and root." ) String ... modules )
100
+ throws IOException {
97
101
if (null == modules || modules .length < 1 ) {
98
102
forEachModuleDo (CHECKOUT , "-b" , newBranch );
99
103
return ;
@@ -148,7 +152,8 @@ void remotePruneOrigin() throws IOException {
148
152
}
149
153
150
154
@ Command (name = "local-prune" , aliases = "prune" )
151
- void localPrune (@ Parameters (index = "0" , paramLabel = "<default branch name>" ) String branch ) throws IOException {
155
+ void localPrune (@ Parameters (index = "0" , paramLabel = "<default branch name>" ) String branch )
156
+ throws IOException {
152
157
forEachModuleDo ("for-each-ref" ,
153
158
"--merged=%s" .formatted (branch ),
154
159
"--format=%(refname:short)" ,
@@ -160,14 +165,16 @@ void localPrune(@Parameters(index = "0", paramLabel = "<default branch name>") S
160
165
}
161
166
162
167
@ Command (name = "stash" )
163
- void stash (@ Option (names = "-pp" , description = "pop first stashed changes" ) boolean isPop ) throws IOException {
168
+ void stash (@ Option (names = "-pp" , description = "pop first stashed changes" ) boolean isPop )
169
+ throws IOException {
164
170
var args = isPop ? new String [] {"stash" , "pop" } : new String [] {"stash" };
165
171
forEachModuleDo (args );
166
172
}
167
173
168
174
@ Command (name = "branches" )
169
175
void listBranches (
170
- @ Option (names = "-nn" , description = "do not print module name" ) boolean noPrintModuleName ) throws IOException {
176
+ @ Option (names = "-nn" , description = "do not print module name" ) boolean noPrintModuleName )
177
+ throws IOException {
171
178
var sArgs = Stream .of ("for-each-ref" , "--format=%(refname:short)" , "refs/heads/" );
172
179
if (noPrintModuleName ) {
173
180
sArgs = Stream .concat (sArgs , Stream .of ("--gis-no-print-modules-name" ));
@@ -193,6 +200,30 @@ void files() throws IOException {
193
200
forEachModuleDo ("diff" , "--name-only" , "--gis-concat-modules-name" );
194
201
}
195
202
203
+ @ Command (name = "completion" , description = "generate an zsh auto completion script" )
204
+ void generateCompletion (
205
+ @ Option (names = "--directory" ,
206
+ description = "export completion zsh function to file at specified directory" ) Path dir )
207
+ throws IOException {
208
+ try (var stream = this .getClass ().getClassLoader ().getResourceAsStream (GIS_AUTOCOMPLETE_FILE )) {
209
+ var buffer = new BufferedReader (new InputStreamReader (stream ));
210
+ String line = null ;
211
+ if (dir != null ) {
212
+ var file = dir .resolve (GIS_AUTOCOMPLETE_FILE );
213
+ try (var out = new FileOutputStream (file .toFile ())) {
214
+ while ((line = buffer .readLine ()) != null ) {
215
+ out .write (line .getBytes ());
216
+ out .write ("%n" .formatted ().getBytes ());
217
+ }
218
+ }
219
+ return ;
220
+ }
221
+ while ((line = buffer .readLine ()) != null ) {
222
+ StdOutUtils .println (line );
223
+ }
224
+ }
225
+ }
226
+
196
227
private static Stream <String > streamOf (String [] input ) {
197
228
return Stream .of (input ).map (String ::trim ).distinct ();
198
229
}
0 commit comments