@@ -123,8 +123,8 @@ static ArgStatus process_args(int argc, const char **argv, JsonnetConfig *config
123123 } else if (arg == " --" ) {
124124 // All subsequent args are not options.
125125 while ((++i) < args.size ())
126- remaining_args.push_back (args[i]);
127- break ;
126+ remaining_args.push_back (args[i]);
127+ break ;
128128 } else if (arg == " -i" || arg == " --in-place" ) {
129129 config->fmtInPlace = true ;
130130 } else if (arg == " --test" ) {
@@ -234,10 +234,11 @@ int main(int argc, const char **argv)
234234 bool changed_files = false ;
235235 assert (config.inputFiles .size () >= 1 );
236236 for (std::string &inputFile : config.inputFiles ) {
237+ const bool input_stdin = inputFile == " -" ;
237238 if (config.fmtInPlace ) {
238239 output_file = inputFile;
239240
240- if (inputFile == " - " ) {
241+ if (input_stdin ) {
241242 std::cerr << " ERROR: cannot use --in-place with stdin" << std::endl;
242243 jsonnet_destroy (vm);
243244 return EXIT_FAILURE;
@@ -265,31 +266,25 @@ int main(int argc, const char **argv)
265266 return EXIT_FAILURE;
266267 }
267268
268- if (config.fmtTest ) {
269- // Check the output matches the input.
270- bool ok = output == input;
269+ // Write output Jsonnet only if there is a difference between input and output
270+ bool different = output != input;
271+ if (config.fmtInPlace && different) {
272+ bool successful = write_output_file (output, output_file);
271273 jsonnet_realloc (vm, output, 0 );
272- if (!ok) {
273- if (inputFile != " -" && !config.filenameIsCode ) {
274- std::cout << inputFile << std::endl;
275- }
276- changed_files = true ;
274+ if (!successful) {
275+ jsonnet_destroy (vm);
276+ return EXIT_FAILURE;
277277 }
278278 } else {
279- // Write output Jsonnet only if there is a difference between input and output
280- bool different = output != input;
281- if (different) {
282- bool successful = write_output_file (output, output_file);
283- jsonnet_realloc (vm, output, 0 );
284- if (!successful) {
285- jsonnet_destroy (vm);
286- return EXIT_FAILURE;
287- }
288- if (inputFile != " -" && !config.filenameIsCode ) {
289- std::cout << inputFile << std::endl;
290- }
291- } else {
292- jsonnet_realloc (vm, output, 0 );
279+ jsonnet_realloc (vm, output, 0 );
280+ }
281+
282+ // If we're not processing stdin or some other unnamed thing, then
283+ // print the names of any changed or unclean files.
284+ if (different) {
285+ changed_files = true ;
286+ if (!input_stdin && !config.filenameIsCode ) {
287+ std::cout << inputFile << std::endl;
293288 }
294289 }
295290 }
0 commit comments