]> source.dussan.org Git - jgit.git/commitdiff
[errorprone] MyersDiff#main: fix CatchAndPrintStackTrace 64/204564/6
authorMatthias Sohn <matthias.sohn@sap.com>
Thu, 21 Sep 2023 10:22:24 +0000 (12:22 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 25 Sep 2023 14:15:35 +0000 (16:15 +0200)
Use a PrintWriter similarly like Main#run of org.eclipse.jgit.pgm does.

See https://errorprone.info/bugpattern/CatchAndPrintStackTrace

Change-Id: I8ee34da53d94ddb72b73ae86bfebc3b7f0a3dc04

org.eclipse.jgit/src/org/eclipse/jgit/diff/MyersDiff.java

index cf6826ab923af15f8a55dd48a172458b5fc482bb..45c7b4a4755815aa46861e01a418a43b24a5016e 100644 (file)
 
 package org.eclipse.jgit.diff;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.text.MessageFormat;
 
 import org.eclipse.jgit.errors.DiffInterruptedException;
@@ -531,7 +535,7 @@ if (k < beginK || k > endK)
         */
        public static void main(String[] args) {
                if (args.length != 2) {
-                       System.err.println(JGitText.get().need2Arguments);
+                       err().println(JGitText.get().need2Arguments);
                        System.exit(1);
                }
                try {
@@ -540,7 +544,13 @@ if (k < beginK || k > endK)
                        EditList r = INSTANCE.diff(RawTextComparator.DEFAULT, a, b);
                        System.out.println(r.toString());
                } catch (Exception e) {
-                       e.printStackTrace();
+                       PrintWriter err = err();
+                       err.println(e.getMessage());
+                       e.printStackTrace(err);
                }
        }
+
+       private static PrintWriter err() {
+               return new PrintWriter(new OutputStreamWriter(System.err, UTF_8));
+       }
 }