Skip to content

Commit 7f94b86

Browse files
authored
Merge pull request #49 from nqminhuit/gis-status-oneline
add new option to 'status' which supports printing one-line each submodule
2 parents 791cc7a + 6c1dedd commit 7f94b86

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/main/java/org/nqm/Gis.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ public void setVerbose(boolean verbose) {
2020
}
2121

2222
public static void main(String[] args) {
23+
var gis = new CommandLine(new Gis());
2324
if (args.length == 0) {
24-
new CommandLine(new Gis()).execute("status");
25+
gis.execute("status");
2526
return;
2627
}
27-
new CommandLine(new Gis()).execute(args);
28+
gis.execute(args);
2829
}
2930

3031
}

src/main/java/org/nqm/command/GitCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ void pull() {
3333
}
3434

3535
@Command(name = "status", aliases = "st")
36-
void status() {
36+
void status(@Option(names = "--one-line") boolean oneLineOpt) {
37+
if (oneLineOpt) {
38+
forEachModuleDo(path -> deployVertx(path, "status", "-sb", "--ignore-submodules", "--porcelain=v2", "--gis-one-line"));
39+
return;
40+
}
3741
forEachModuleDo(path -> deployVertx(path, "status", "-sb", "--ignore-submodules", "--porcelain=v2"));
3842
}
3943

src/main/java/org/nqm/utils/StdOutUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public static String coloringWord(Character c, String color) {
5050
}
5151

5252
public static String buildStaging(char[] chars) {
53+
if (chars.length == 0) {
54+
return "";
55+
}
5356
return Optional.of(chars[0])
5457
.map(s -> s != '.' ? coloringWord(s, CL_GREEN) : s + "")
5558
.orElse("") +
@@ -93,6 +96,25 @@ public static String gitStatus(String line) {
9396
};
9497
}
9598

99+
public static String gitStatusOneLine(String line) {
100+
var lineSplit = line.split("\s");
101+
return switch (lineSplit[0] + lineSplit[1]) {
102+
case "#branch.oid" -> "";
103+
case "#branch.head" -> " " + coloringWord(lineSplit[2], CL_BLUE);
104+
case "#branch.upstream" -> "";
105+
case "#branch.ab" -> Optional.of(lineSplit)
106+
.map(StdOutUtils::buildAheadBehind)
107+
.filter(GisStringUtils::isNotBlank)
108+
.map("[%s]"::formatted)
109+
.orElse("");
110+
default -> Optional.of(lineSplit)
111+
.map(StdOutUtils::preProcessUntrackFile)
112+
.map(splitS -> " "
113+
+ Optional.of(splitS[splitS.length - 1]).map(getFiles(line)).orElse(""))
114+
.orElse("");
115+
};
116+
}
117+
96118
private static String[] preProcessUntrackFile(String[] fileStats) {
97119
var length = fileStats.length;
98120
if (length < 1) {

src/main/java/org/nqm/vertx/CommandVerticle.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import static org.nqm.utils.GisStringUtils.isNotBlank;
55
import static org.nqm.utils.StdOutUtils.errln;
66
import static org.nqm.utils.StdOutUtils.gitStatus;
7+
import static org.nqm.utils.StdOutUtils.gitStatusOneLine;
78
import static org.nqm.utils.StdOutUtils.infof;
89
import static org.nqm.utils.StdOutUtils.warnln;
910
import java.io.BufferedReader;
1011
import java.io.IOException;
1112
import java.io.InputStreamReader;
1213
import java.nio.file.Path;
14+
import java.util.Objects;
1315
import java.util.Optional;
16+
import java.util.stream.Stream;
1417
import org.nqm.config.GisConfig;
1518
import org.nqm.config.GisLog;
1619
import io.vertx.core.AbstractVerticle;
@@ -19,15 +22,12 @@
1922
public class CommandVerticle extends AbstractVerticle {
2023

2124
private final String[] commandWithArgs;
25+
private String gisOption;
2226
private final Path path;
2327

2428
public CommandVerticle(Path path, String... args) {
2529
this.path = path;
26-
this.commandWithArgs = new String[args.length + 1];
27-
this.commandWithArgs[0] = GisConfig.GIT_HOME_DIR;
28-
for (int i = 0; i < args.length; i++) {
29-
this.commandWithArgs[i + 1] = args[i];
30-
}
30+
this.commandWithArgs = buildCommandWithArgs(args);
3131
GisLog.debug("executing command '%s' under module '%s'", commandWithArgs, path.getFileName());
3232
GisVertx.eventAddDir(path);
3333
}
@@ -38,6 +38,24 @@ public CommandVerticle() {
3838
GisVertx.eventAddDir(Path.of("."));
3939
}
4040

41+
private String[] buildCommandWithArgs(String... args) {
42+
var cmdWithArgs = new String[args.length + 1];
43+
cmdWithArgs[0] = GisConfig.GIT_HOME_DIR;
44+
var n = args.length;
45+
for (int i = 0; i < n - 1; i++) {
46+
cmdWithArgs[i + 1] = args[i];
47+
}
48+
// for better performance it is to required all '--gis' options to be at the end of cmd
49+
var lastArg = args[n - 1];
50+
if (args[n - 1].startsWith("--gis")) {
51+
this.gisOption = lastArg;
52+
}
53+
else {
54+
cmdWithArgs[n] = lastArg;
55+
}
56+
return Stream.of(cmdWithArgs).filter(Objects::nonNull).toArray(String[]::new);
57+
}
58+
4159
@Override
4260
public void start() {
4361
if (path == null) {
@@ -67,7 +85,9 @@ private void safelyPrint(Process pr) {
6785
var sb = new StringBuilder(infof("%s", "" + path.getFileName()));
6886
try {
6987
while (isNotBlank(line = input.readLine())) {
70-
sb.append(commandWithArgs[1].equals("status") ? gitStatus(line) : "%n %s".formatted(line));
88+
sb.append(commandWithArgs[1].equals("status")
89+
? "--gis-one-line".equals(gisOption) ? gitStatusOneLine(line) : gitStatus(line)
90+
: "%n %s".formatted(line));
7191
}
7292
out.println(sb.toString());
7393
Optional.of(pr.waitFor())

0 commit comments

Comments
 (0)