]> source.dussan.org Git - jgit.git/commitdiff
ReceivePack: Use try-with-resources for PostReceiveHook 59/152359/2
authorMasaya Suzuki <masayasuzuki@google.com>
Fri, 8 Nov 2019 03:07:57 +0000 (19:07 -0800)
committerMasaya Suzuki <masayasuzuki@google.com>
Mon, 2 Dec 2019 22:01:49 +0000 (14:01 -0800)
Instead of dropping a second exception, use try-with-resources so that
exception thrown by a hook is attached as a suppressed exception.

This removes a need of holding an exception that is thrown while
unpacking an incoming packfile.

Change-Id: I74d8b944931abbf53d9d1ae5db2e889639fba027
Signed-off-by: Masaya Suzuki <masayasuzuki@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java

index 00ea6ac94f438c9b990337d77a74953a5d29c067..32c9abc1acb79956a5270e9fbd492316b690601f 100644 (file)
@@ -2213,18 +2213,19 @@ public class ReceivePack {
                }
 
                if (hasCommands()) {
-                       Throwable unpackError = null;
-                       if (needPack()) {
-                               try {
-                                       receivePackAndCheckConnectivity();
-                               } catch (IOException | RuntimeException
-                                               | SubmoduleValidationException | Error err) {
-                                       unpackError = err;
+                       try (PostReceiveExecutor e = new PostReceiveExecutor()) {
+                               if (needPack()) {
+                                       try {
+                                               receivePackAndCheckConnectivity();
+                                       } catch (IOException | RuntimeException
+                                                       | SubmoduleValidationException | Error err) {
+                                               unlockPack();
+                                               sendStatusReport(err);
+                                               throw new UnpackException(err);
+                                       }
                                }
-                       }
 
-                       try {
-                               if (unpackError == null) {
+                               try {
                                        setAtomic(isCapabilityEnabled(CAPABILITY_ATOMIC));
 
                                        validateCommands();
@@ -2238,24 +2239,12 @@ public class ReceivePack {
                                                failPendingCommands();
                                        }
                                        executeCommands();
+                               } finally {
+                                       unlockPack();
                                }
-                       } finally {
-                               unlockPack();
-                       }
 
-                       sendStatusReport(unpackError);
-
-                       if (unpackError != null) {
-                               // we already know which exception to throw. Ignore
-                               // potential additional exceptions raised in postReceiveHooks
-                               try {
-                                       postReceive.onPostReceive(this, filterCommands(Result.OK));
-                               } catch (Throwable e) {
-                                       // empty
-                               }
-                               throw new UnpackException(unpackError);
+                               sendStatusReport(null);
                        }
-                       postReceive.onPostReceive(this, filterCommands(Result.OK));
                        autoGc();
                }
        }
@@ -2292,4 +2281,12 @@ public class ReceivePack {
                }
                return new ReceiveCommand(oldId, newId, name);
        }
+
+       private class PostReceiveExecutor implements AutoCloseable {
+               @Override
+               public void close() {
+                       postReceive.onPostReceive(ReceivePack.this,
+                                       filterCommands(Result.OK));
+               }
+       }
 }