From c13901f3454945439ca62926413b4363e479ae46 Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Thu, 15 Dec 2016 20:37:38 -0600 Subject: [PATCH] More cleanup of resource closing --- .../org/ajoberstar/grgit/operation/CloneOp.groovy | 6 ++++-- .../org/ajoberstar/grgit/operation/InitOp.groovy | 6 ++++-- .../org/ajoberstar/grgit/operation/CloneOpSpec.groovy | 2 +- .../org/ajoberstar/grgit/operation/InitOpSpec.groovy | 9 +++++++++ .../org/ajoberstar/grgit/operation/OpenOpSpec.groovy | 11 +++++++++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/org/ajoberstar/grgit/operation/CloneOp.groovy b/src/main/groovy/org/ajoberstar/grgit/operation/CloneOp.groovy index bee5975e..1aa3ad14 100644 --- a/src/main/groovy/org/ajoberstar/grgit/operation/CloneOp.groovy +++ b/src/main/groovy/org/ajoberstar/grgit/operation/CloneOp.groovy @@ -19,6 +19,7 @@ import java.util.concurrent.Callable import org.ajoberstar.grgit.Credentials import org.ajoberstar.grgit.Grgit +import org.ajoberstar.grgit.Repository import org.ajoberstar.grgit.auth.TransportOpUtil import org.ajoberstar.grgit.exception.GrgitException import org.ajoberstar.grgit.util.CoercionUtil @@ -103,8 +104,9 @@ class CloneOp implements Callable { if (refToCheckout) { cmd.branch = refToCheckout } try { - cmd.call().close() - return Grgit.open(dir: dir, creds: credentials) + Git jgit = cmd.call() + Repository repo = new Repository(CoercionUtil.toFile(dir), jgit, credentials) + return new Grgit(repo) } catch (GitAPIException e) { throw new GrgitException('Problem cloning repository.', e) } diff --git a/src/main/groovy/org/ajoberstar/grgit/operation/InitOp.groovy b/src/main/groovy/org/ajoberstar/grgit/operation/InitOp.groovy index f8dc93b1..34098431 100644 --- a/src/main/groovy/org/ajoberstar/grgit/operation/InitOp.groovy +++ b/src/main/groovy/org/ajoberstar/grgit/operation/InitOp.groovy @@ -18,6 +18,7 @@ package org.ajoberstar.grgit.operation import java.util.concurrent.Callable import org.ajoberstar.grgit.Grgit +import org.ajoberstar.grgit.Repository import org.ajoberstar.grgit.exception.GrgitException import org.ajoberstar.grgit.util.CoercionUtil @@ -65,8 +66,9 @@ class InitOp implements Callable { cmd.bare = bare cmd.directory = CoercionUtil.toFile(dir) try { - cmd.call() - return Grgit.open(dir: dir) + Git jgit = cmd.call() + Repository repo = new Repository(CoercionUtil.toFile(dir), jgit, null) + return new Grgit(repo) } catch (GitAPIException e) { throw new GrgitException('Problem initializing repository.', e) } diff --git a/src/test/groovy/org/ajoberstar/grgit/operation/CloneOpSpec.groovy b/src/test/groovy/org/ajoberstar/grgit/operation/CloneOpSpec.groovy index e231580d..a0af8d82 100644 --- a/src/test/groovy/org/ajoberstar/grgit/operation/CloneOpSpec.groovy +++ b/src/test/groovy/org/ajoberstar/grgit/operation/CloneOpSpec.groovy @@ -138,6 +138,6 @@ class CloneOpSpec extends MultiGitOpSpec { when: grgit.close() then: - assert repoDir.deleteDir() + repoDir.deleteDir() } } diff --git a/src/test/groovy/org/ajoberstar/grgit/operation/InitOpSpec.groovy b/src/test/groovy/org/ajoberstar/grgit/operation/InitOpSpec.groovy index 795d30bf..ca683d26 100644 --- a/src/test/groovy/org/ajoberstar/grgit/operation/InitOpSpec.groovy +++ b/src/test/groovy/org/ajoberstar/grgit/operation/InitOpSpec.groovy @@ -45,4 +45,13 @@ class InitOpSpec extends Specification { then: GitTestUtil.repoFile(grgit, '.', false).listFiles().collect { it.name } == ['.git'] } + + def 'init repo can be deleted after being closed'() { + given: + def grgit = Grgit.init(dir: repoDir, bare: false) + when: + grgit.close() + then: + repoDir.deleteDir() + } } diff --git a/src/test/groovy/org/ajoberstar/grgit/operation/OpenOpSpec.groovy b/src/test/groovy/org/ajoberstar/grgit/operation/OpenOpSpec.groovy index bfa0dc4a..a6ab3157 100644 --- a/src/test/groovy/org/ajoberstar/grgit/operation/OpenOpSpec.groovy +++ b/src/test/groovy/org/ajoberstar/grgit/operation/OpenOpSpec.groovy @@ -15,6 +15,8 @@ */ package org.ajoberstar.grgit.operation +import java.nio.file.Files + import org.ajoberstar.grgit.Commit import org.ajoberstar.grgit.Grgit import org.ajoberstar.grgit.Status @@ -139,4 +141,13 @@ class OpenOpSpec extends SimpleGitOpSpec { opened.head() == commit opened.status() == new Status(unstaged: [modified: [FILE_PATH]]) } + + def 'opened repo can be deleted after being closed'() { + given: + Grgit opened = Grgit.open(dir: repoDir('.').canonicalFile) + when: + opened.close() + then: + opened.repository.rootDir.deleteDir() + } }