Skip to content

Commit

Permalink
Merge pull request #49 from nqminhuit/gis-status-oneline
Browse files Browse the repository at this point in the history
add new option to 'status' which supports printing one-line each submodule
  • Loading branch information
nqminhuit authored Oct 9, 2022
2 parents 791cc7a + 6c1dedd commit 7f94b86
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/nqm/Gis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
6 changes: 5 additions & 1 deletion src/main/java/org/nqm/command/GitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/nqm/utils/StdOutUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("") +
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/org/nqm/vertx/CommandVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 7f94b86

Please sign in to comment.