0

I have a task on my Android build.gradle that is supposed to download the source code from a git repository of mine to a specific directory if it is not present there, and checkout to a specific branch after cloning (if the repo is already cloned, do a fetch and then a checkout). I am using GrGit version 1.6.0.

Here is the code for my task:

task downloadRepoSrc << {
    def branch = "development"
    def pathToClone = "path/to/clone/"
    def uriToRepo = "git@restofurihere"
    try {
        // Try to open repository if it exists
        def grgit = Grgit.open(dir: pathToClone)
        grgit.fetch()
        if (grgit.branch.list().find {it.name == branch}) {
            grgit.checkout(branch: branch)
        } else {
            grgit.checkout(branch: branch, startPoint: "origin/$branch", createBranch: true)
        }
    } catch (RepositoryNotFoundException ignored) {
        // Repository not found. Try to clone it.
        def grgit = Grgit.clone(dir: pathToClone, uri: "uriToRepo")
        grgit.checkout(branch: branch, startPoint: "origin/$branch", createBranch: true)
    } catch (Exception e) {
        e.printStackTrace()
    }
}

I also bind the task to the preBuild task so that it will be run every time you build the project, like this:

preBuild.dependsOn downloadRepoSrc

Whenever I run the task manually on Android studio's interface, it runs flawlessly. However, whenever I try to build the project, I get the following error message:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':native-myproject:downloadRepoSrc'.
> java.lang.StackOverflowError (no error message)

I am clueless as to why the task would fail when building the project, but not when being run manually. My goal with this is to make the build process for the dependency project automatic, provided I feed it the correct branch/tag to checkout on git. Currently, I'd need to remember to manually run the task every time I want to use a different branch/tag, which works, but is not optimal.

Any help or tips on what could be wrong would be greatly appreciated. If you need more details, please let me know.

EDIT: Here is the stacktrace outputted when running gradle with the --stacktrace option. I'll link it in a pastebin since it is quite long. It seems that it enters some infinite recursion on the resolveBranch function, but only when building the project, not when running the task manually.

Thurler
  • 1
  • 1
  • Have you tried running with the `--stacktrace` option to see what specific method (or series of methods) it's overflowing on? – ajoberstar Jan 21 '17 at 04:31
  • I updated the question with the stacktrace output. Using the debugger it seems that the problem happens on this line: `if (grgit.branch.list().find {it.name == branch})` – Thurler Jan 23 '17 at 16:57

0 Answers0