gitgroovyhookgitblit

Get content versionned file in groovy GitBlit hook


I have a custom pre-receive hook GitBlit made in Groovy. I would like to read a versionned file in the current repository and extract its content in a string.

I already saw {repository} and {user} vars to get the absolute path of the repository, but nothing found.

Do you know any parameter or tool function to get path (or content) of this file ? Thanks


Solution

  • I found the solution consulting this page. There is tools functions in com.gitblit.utils.JGitUtils and com.gitblit.GitBlit who allow to get file content as a string.

    Documentation is available here

    import com.gitblit.GitBlit
    import com.gitblit.Keys
    import com.gitblit.models.RepositoryModel
    import com.gitblit.models.UserModel
    import com.gitblit.utils.JGitUtils
    import org.eclipse.jgit.lib.Repository
    import org.eclipse.jgit.revwalk.RevCommit
    import org.eclipse.jgit.transport.ReceiveCommand
    import org.eclipse.jgit.transport.ReceiveCommand.Result
    import org.slf4j.Logger
    
    // Get repository
    def repo = gitblit.getRepository(repository.name)
    //logger.info(repo.dump()) // See a dump of the var in the logs
    
    // Get string file content
    def str = JGitUtils.getStringContent(repo, null, "myFile.txt", null)
    

    NB : repository var is naturally injected by GitBlit at the execution of the groovy hook script.