From 6c1deddbb571a7714bfec421ceed680e662692dd Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Sun, 9 Oct 2022 12:35:51 +0700 Subject: [PATCH] add new option to 'status' which supports printing one-line each submodule --- src/main/java/org/nqm/Gis.java | 5 +-- src/main/java/org/nqm/command/GitCommand.java | 6 +++- src/main/java/org/nqm/utils/StdOutUtils.java | 22 +++++++++++++ .../java/org/nqm/vertx/CommandVerticle.java | 32 +++++++++++++++---- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/nqm/Gis.java b/src/main/java/org/nqm/Gis.java index 8a981af..2f4577f 100644 --- a/src/main/java/org/nqm/Gis.java +++ b/src/main/java/org/nqm/Gis.java @@ -20,11 +20,12 @@ public void setVerbose(boolean verbose) { } public static void main(String[] args) { + var gis = new CommandLine(new Gis()); if (args.length == 0) { - new CommandLine(new Gis()).execute("status"); + gis.execute("status"); return; } - new CommandLine(new Gis()).execute(args); + gis.execute(args); } } diff --git a/src/main/java/org/nqm/command/GitCommand.java b/src/main/java/org/nqm/command/GitCommand.java index 515887e..0dc7599 100644 --- a/src/main/java/org/nqm/command/GitCommand.java +++ b/src/main/java/org/nqm/command/GitCommand.java @@ -33,7 +33,11 @@ void pull() { } @Command(name = "status", aliases = "st") - void status() { + void status(@Option(names = "--one-line") boolean oneLineOpt) { + if (oneLineOpt) { + forEachModuleDo(path -> deployVertx(path, "status", "-sb", "--ignore-submodules", "--porcelain=v2", "--gis-one-line")); + return; + } forEachModuleDo(path -> deployVertx(path, "status", "-sb", "--ignore-submodules", "--porcelain=v2")); } diff --git a/src/main/java/org/nqm/utils/StdOutUtils.java b/src/main/java/org/nqm/utils/StdOutUtils.java index 6763029..c1bab77 100644 --- a/src/main/java/org/nqm/utils/StdOutUtils.java +++ b/src/main/java/org/nqm/utils/StdOutUtils.java @@ -50,6 +50,9 @@ public static String coloringWord(Character c, String color) { } public static String buildStaging(char[] chars) { + if (chars.length == 0) { + return ""; + } return Optional.of(chars[0]) .map(s -> s != '.' ? coloringWord(s, CL_GREEN) : s + "") .orElse("") + @@ -93,6 +96,25 @@ public static String gitStatus(String line) { }; } + public static String gitStatusOneLine(String line) { + var lineSplit = line.split("\s"); + return switch (lineSplit[0] + lineSplit[1]) { + case "#branch.oid" -> ""; + case "#branch.head" -> " " + coloringWord(lineSplit[2], CL_BLUE); + case "#branch.upstream" -> ""; + case "#branch.ab" -> Optional.of(lineSplit) + .map(StdOutUtils::buildAheadBehind) + .filter(GisStringUtils::isNotBlank) + .map("[%s]"::formatted) + .orElse(""); + default -> Optional.of(lineSplit) + .map(StdOutUtils::preProcessUntrackFile) + .map(splitS -> " " + + Optional.of(splitS[splitS.length - 1]).map(getFiles(line)).orElse("")) + .orElse(""); + }; + } + private static String[] preProcessUntrackFile(String[] fileStats) { var length = fileStats.length; if (length < 1) { diff --git a/src/main/java/org/nqm/vertx/CommandVerticle.java b/src/main/java/org/nqm/vertx/CommandVerticle.java index ffc554f..3185131 100644 --- a/src/main/java/org/nqm/vertx/CommandVerticle.java +++ b/src/main/java/org/nqm/vertx/CommandVerticle.java @@ -4,13 +4,16 @@ import static org.nqm.utils.GisStringUtils.isNotBlank; import static org.nqm.utils.StdOutUtils.errln; import static org.nqm.utils.StdOutUtils.gitStatus; +import static org.nqm.utils.StdOutUtils.gitStatusOneLine; import static org.nqm.utils.StdOutUtils.infof; import static org.nqm.utils.StdOutUtils.warnln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Path; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Stream; import org.nqm.config.GisConfig; import org.nqm.config.GisLog; import io.vertx.core.AbstractVerticle; @@ -19,15 +22,12 @@ public class CommandVerticle extends AbstractVerticle { private final String[] commandWithArgs; + private String gisOption; private final Path path; public CommandVerticle(Path path, String... args) { this.path = path; - this.commandWithArgs = new String[args.length + 1]; - this.commandWithArgs[0] = GisConfig.GIT_HOME_DIR; - for (int i = 0; i < args.length; i++) { - this.commandWithArgs[i + 1] = args[i]; - } + this.commandWithArgs = buildCommandWithArgs(args); GisLog.debug("executing command '%s' under module '%s'", commandWithArgs, path.getFileName()); GisVertx.eventAddDir(path); } @@ -38,6 +38,24 @@ public CommandVerticle() { GisVertx.eventAddDir(Path.of(".")); } + private String[] buildCommandWithArgs(String... args) { + var cmdWithArgs = new String[args.length + 1]; + cmdWithArgs[0] = GisConfig.GIT_HOME_DIR; + var n = args.length; + for (int i = 0; i < n - 1; i++) { + cmdWithArgs[i + 1] = args[i]; + } + // for better performance it is to required all '--gis' options to be at the end of cmd + var lastArg = args[n - 1]; + if (args[n - 1].startsWith("--gis")) { + this.gisOption = lastArg; + } + else { + cmdWithArgs[n] = lastArg; + } + return Stream.of(cmdWithArgs).filter(Objects::nonNull).toArray(String[]::new); + } + @Override public void start() { if (path == null) { @@ -67,7 +85,9 @@ private void safelyPrint(Process pr) { var sb = new StringBuilder(infof("%s", "" + path.getFileName())); try { while (isNotBlank(line = input.readLine())) { - sb.append(commandWithArgs[1].equals("status") ? gitStatus(line) : "%n %s".formatted(line)); + sb.append(commandWithArgs[1].equals("status") + ? "--gis-one-line".equals(gisOption) ? gitStatusOneLine(line) : gitStatus(line) + : "%n %s".formatted(line)); } out.println(sb.toString()); Optional.of(pr.waitFor())