Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bare Repository has neither a working tree, nor an index via execution of git hooks #87

Closed
lfernandofaraujo opened this issue Oct 28, 2020 · 12 comments

Comments

@lfernandofaraujo
Copy link
Contributor

When execute the assemble task via git hooks with the plugin androidGitVersion configured, the build was failed.

Task failed with an exception.

  • Where:
    Script '/home//AndroidStudioProjects//script-git-version.gradle' line: 17

  • What went wrong:
    A problem occurred evaluating script.

Bare Repository has neither a working tree, nor an index

Searching the web, i found this ask:
Jgit:Bare Repository has neither a working tree, nor an index

@mincong answer this:
Opening a Git repository using FileRepositoryBuilder is tricky. This is an internal class. Its method setGitDir(File) defines the location of the repository metadata (the .git folder). In other words, it is used to construct a Git bare repository.

Suggestions of implementation:
Before:

private Results scan() {
        validateSettings()
        Results results = new Results()

        Repository repo
        try {
            repo = new FileRepositoryBuilder()
                    .readEnvironment()
                    .findGitDir(project.projectDir)
                    .build()
        } catch (IllegalArgumentException ignore) {
            // No repo found
            return results
        }

        def git = Git.wrap(repo)
[...]
}

After:

private Results scan() {
        validateSettings()
        Results results = new Results()

        Git git
        Repository repo
        try {
            git = Git.open(project.projectDir)
            repo = git.repository
        } catch (IllegalArgumentException ignore) {
            // No repo found
            return results
        }
        def head = repo.findRef(Constants.HEAD).getTarget()
[...]
}

Link: https://stackoverflow.com/questions/49282593/jgitbare-repository-has-neither-a-working-tree-nor-an-index

@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

This is good but it break's everyone's favorite git feature: submodules. I'm looking into this.

@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

I think the real problem above may be that some aspect of your hooks is operating without access to the .git tree, causing RepositoryNotFoundException which is not caught.

In this case, androidGitVersion can print a warning message and continue.

@gladed gladed closed this as completed in 35adebc Oct 30, 2020
@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

@Ferrugemm this is now published in 0.4.14; if this doesn't work please let me know or reopen.

@lfernandofaraujo
Copy link
Contributor Author

Hey @gladed, thanks for the attention and the upgrades, but still don't working :(

The stacktrace from my machine (Linux 18.04 LTS)

~/AndroidStudioProjects/<project>   <branch>/gladed-plugin ✚  git commit -m "new version of gladed plugin v0.4.14"

1: Task failed with an exception.
-----------
* Where:
Script '/.../<file>.gradle' line: 10

* What went wrong:
A problem occurred evaluating script.
> Bare Repository has neither a working tree, nor an index

I made the modification above and generated the local .jar and it works to me.

@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

Can I reproduce this locally? What kind of a hook is installed?

@gladed gladed reopened this Oct 30, 2020
@lfernandofaraujo
Copy link
Contributor Author

Yes, you can!

It is pre-commit hooks and basically make auto check of my project.
This article drives the setup process: https://medium.com/@alistair.cerio/android-ktlint-and-pre-commit-git-hook-5dd606e230a9

Besides that, i've your plugin configured in app/build.gradle, with .name and the .code.

@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

Here's what I did, and it worked. Are you sure the repo you're working from is present and not bare? Or is this a result you're only getting from CI on a server?

$ cat >.git/hooks/pre-commit
#!/bin/bash

git stash -q --keep-index

./gradlew androidGitVersion

RESULT=$?

git stash pop -q

# return 1 exit code if running checks fails
[ $RESULT -ne 0 ] && exit 1
exit 0
^D

$ chmod a+x .git/hooks/pre-commit

$ git commit -m "temp" --allow-empty
...
> Task :...:androidGitVersion
androidGitVersion.name  1.2.3
androidGitVersion.code  1002003

BUILD SUCCESSFUL in 1s
5 actionable tasks: 5 executed
[master 98c6276f] temp

@lfernandofaraujo
Copy link
Contributor Author

@gladed yes, it is my local machine and repo :(

How can i verify the not bare repository?

@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

@lfernandofaraujo
Copy link
Contributor Author

From my .sh and terminal:

$ git rev-parse --is-bare-repository

The result is false

@gladed
Copy link
Collaborator

gladed commented Oct 30, 2020

Then there must be something else wrong on your local setup. jgit appears to be reporting that it cannot find your working file tree.

Note that the implementation in this project matches published jgit documentation and FileRepositoryBuilder is an ordinary, public, non-deprecated class.

@gladed gladed closed this as completed Oct 30, 2020
@lfernandofaraujo
Copy link
Contributor Author

Thanks @gladed 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant