Skip to content

Commit

Permalink
More cleanup of resource closing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoberstar committed Dec 16, 2016
1 parent e65e80c commit c13901f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/main/groovy/org/ajoberstar/grgit/operation/CloneOp.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,8 +104,9 @@ class CloneOp implements Callable<Grgit> {
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)
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/groovy/org/ajoberstar/grgit/operation/InitOp.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -65,8 +66,9 @@ class InitOp implements Callable<Grgit> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ class CloneOpSpec extends MultiGitOpSpec {
when:
grgit.close()
then:
assert repoDir.deleteDir()
repoDir.deleteDir()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
11 changes: 11 additions & 0 deletions src/test/groovy/org/ajoberstar/grgit/operation/OpenOpSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

0 comments on commit c13901f

Please sign in to comment.