Skip to content

Commit e22b66d

Browse files
committed
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]
1 parent ffe1b75 commit e22b66d

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
final class CommandLineOptions {
2727

2828
private final ImmutableList<String> files;
29+
private final boolean dryRun;
2930
private final boolean inPlace;
3031
private final ImmutableRangeSet<Integer> lines;
3132
private final ImmutableList<Integer> offsets;
@@ -41,6 +42,7 @@ final class CommandLineOptions {
4142

4243
CommandLineOptions(
4344
ImmutableList<String> files,
45+
boolean dryRun,
4446
boolean inPlace,
4547
ImmutableRangeSet<Integer> lines,
4648
ImmutableList<Integer> offsets,
@@ -54,6 +56,7 @@ final class CommandLineOptions {
5456
boolean sortImports,
5557
boolean removeUnusedImports) {
5658
this.files = files;
59+
this.dryRun = dryRun;
5760
this.inPlace = inPlace;
5861
this.lines = lines;
5962
this.offsets = offsets;
@@ -73,6 +76,11 @@ ImmutableList<String> files() {
7376
return files;
7477
}
7578

79+
/** Dry run. */
80+
boolean dryRun() {
81+
return dryRun;
82+
}
83+
7684
/** Format files in place. */
7785
boolean inPlace() {
7886
return inPlace;
@@ -151,6 +159,7 @@ static class Builder {
151159
private final ImmutableRangeSet.Builder<Integer> lines = ImmutableRangeSet.builder();
152160
private final ImmutableList.Builder<Integer> offsets = ImmutableList.builder();
153161
private final ImmutableList.Builder<Integer> lengths = ImmutableList.builder();
162+
private Boolean dryRun = false;
154163
private Boolean inPlace = false;
155164
private Boolean aosp = false;
156165
private Boolean version = false;
@@ -165,6 +174,11 @@ ImmutableList.Builder<String> filesBuilder() {
165174
return files;
166175
}
167176

177+
Builder dryRun(boolean dryRun) {
178+
this.dryRun = dryRun;
179+
return this;
180+
}
181+
168182
Builder inPlace(boolean inPlace) {
169183
this.inPlace = inPlace;
170184
return this;
@@ -227,6 +241,7 @@ Builder removeUnusedImports(boolean removeUnusedImports) {
227241
CommandLineOptions build() {
228242
return new CommandLineOptions(
229243
this.files.build(),
244+
this.dryRun,
230245
this.inPlace,
231246
this.lines.build(),
232247
this.offsets.build(),

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ static CommandLineOptions parse(Iterable<String> options) {
4848
}
4949
// NOTE: update usage information in UsageException when new flags are added
5050
switch (flag) {
51+
case "-n":
52+
case "--dry-run":
53+
optionsBuilder.dryRun(true);
54+
break;
5155
case "-i":
5256
case "-r":
5357
case "-replace":

core/src/main/java/com/google/googlejavaformat/java/Main.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Arrays;
3131
import java.util.LinkedHashMap;
3232
import java.util.Map;
33+
import java.util.Set;
34+
import java.util.TreeSet;
3335
import java.util.concurrent.ExecutionException;
3436
import java.util.concurrent.ExecutorService;
3537
import java.util.concurrent.Executors;
@@ -131,6 +133,7 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
131133
}
132134

133135
boolean allOk = true;
136+
Set<Path> needFormatting = new TreeSet<>();
134137
for (Map.Entry<Path, Future<String>> result : results.entrySet()) {
135138
String formatted;
136139
try {
@@ -151,7 +154,13 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
151154
allOk = false;
152155
continue;
153156
}
154-
if (parameters.inPlace()) {
157+
if (parameters.dryRun()) {
158+
if (formatted.equals(inputs.get(result.getKey()))) {
159+
continue;
160+
} else {
161+
needFormatting.add(result.getKey());
162+
}
163+
} else if (parameters.inPlace()) {
155164
if (formatted.equals(inputs.get(result.getKey()))) {
156165
continue; // preserve original file
157166
}
@@ -166,6 +175,13 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
166175
outWriter.write(formatted);
167176
}
168177
}
178+
179+
if (!needFormatting.isEmpty()) {
180+
errWriter.println("Need Formatting:");
181+
errWriter.println(needFormatting);
182+
return 1;
183+
}
184+
169185
return allOk ? 0 : 1;
170186
}
171187

core/src/main/java/com/google/googlejavaformat/java/UsageException.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public final class UsageException extends Exception {
3232
"Usage: google-java-format [options] file(s)",
3333
"",
3434
"Options:",
35+
" -n, --dry-run",
36+
" Don't change anything, only check; supposed to be used form the CI.",
3537
" -i, -r, -replace, --replace",
3638
" Send formatted output back to files, not stdout.",
3739
" -",

0 commit comments

Comments
 (0)