From ec5ce10758d39fedaf26eaef564a79d6763d76e7 Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Sun, 28 May 2017 14:02:26 +0200 Subject: [PATCH] Add --dry-run option to simplify CI system integrations Fixes: #105. Test Plan: $ git show --name-only HEAD | grep java$ | xargs -r gjf --dry-run Need Formatting: [gerrit-common/src/main/java/com/google/gerrit/common/IoUtil.java,\ gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java] --- .../java/CommandLineOptions.java | 15 +++++++++++++++ .../java/CommandLineOptionsParser.java | 4 ++++ .../com/google/googlejavaformat/java/Main.java | 18 +++++++++++++++++- .../googlejavaformat/java/UsageException.java | 2 ++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java index 47a6cfd21..0b10a2f3a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java @@ -26,6 +26,7 @@ final class CommandLineOptions { private final ImmutableList files; + private final boolean dryRun; private final boolean inPlace; private final ImmutableRangeSet lines; private final ImmutableList offsets; @@ -41,6 +42,7 @@ final class CommandLineOptions { CommandLineOptions( ImmutableList files, + boolean dryRun, boolean inPlace, ImmutableRangeSet lines, ImmutableList offsets, @@ -54,6 +56,7 @@ final class CommandLineOptions { boolean sortImports, boolean removeUnusedImports) { this.files = files; + this.dryRun = dryRun; this.inPlace = inPlace; this.lines = lines; this.offsets = offsets; @@ -73,6 +76,11 @@ ImmutableList files() { return files; } + /** Dry run. */ + boolean dryRun() { + return dryRun; + } + /** Format files in place. */ boolean inPlace() { return inPlace; @@ -151,6 +159,7 @@ static class Builder { private final ImmutableRangeSet.Builder lines = ImmutableRangeSet.builder(); private final ImmutableList.Builder offsets = ImmutableList.builder(); private final ImmutableList.Builder lengths = ImmutableList.builder(); + private Boolean dryRun = false; private Boolean inPlace = false; private Boolean aosp = false; private Boolean version = false; @@ -165,6 +174,11 @@ ImmutableList.Builder filesBuilder() { return files; } + Builder dryRun(boolean dryRun) { + this.dryRun = dryRun; + return this; + } + Builder inPlace(boolean inPlace) { this.inPlace = inPlace; return this; @@ -227,6 +241,7 @@ Builder removeUnusedImports(boolean removeUnusedImports) { CommandLineOptions build() { return new CommandLineOptions( this.files.build(), + this.dryRun, this.inPlace, this.lines.build(), this.offsets.build(), diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java index b12e2bfd2..aef8093ed 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java @@ -48,6 +48,10 @@ static CommandLineOptions parse(Iterable options) { } // NOTE: update usage information in UsageException when new flags are added switch (flag) { + case "-n": + case "--dry-run": + optionsBuilder.dryRun(true); + break; case "-i": case "-r": case "-replace": diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java index 7164d96bb..037cf8e99 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Main.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java @@ -30,6 +30,8 @@ import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -131,6 +133,7 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti } boolean allOk = true; + Set needFormatting = new TreeSet<>(); for (Map.Entry> result : results.entrySet()) { String formatted; try { @@ -151,7 +154,13 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti allOk = false; continue; } - if (parameters.inPlace()) { + if (parameters.dryRun()) { + if (formatted.equals(inputs.get(result.getKey()))) { + continue; + } else { + needFormatting.add(result.getKey()); + } + } else if (parameters.inPlace()) { if (formatted.equals(inputs.get(result.getKey()))) { continue; // preserve original file } @@ -166,6 +175,13 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti outWriter.write(formatted); } } + + if (!needFormatting.isEmpty()) { + errWriter.println("Need Formatting:"); + errWriter.println(needFormatting); + return 1; + } + return allOk ? 0 : 1; } diff --git a/core/src/main/java/com/google/googlejavaformat/java/UsageException.java b/core/src/main/java/com/google/googlejavaformat/java/UsageException.java index 6a2999133..9d2bc4e6d 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/UsageException.java +++ b/core/src/main/java/com/google/googlejavaformat/java/UsageException.java @@ -32,6 +32,8 @@ public final class UsageException extends Exception { "Usage: google-java-format [options] file(s)", "", "Options:", + " -n, --dry-run", + " Don't change anything, only check; supposed to be used form the CI.", " -i, -r, -replace, --replace", " Send formatted output back to files, not stdout.", " -",