jgit-cookbook

repository·master·Indexed 23 days ago

https://github.com/centic9/jgit-cookbook

A collection of ready-to-run Java code snippets and examples for the JGit library. It demonstrates how to implement common Git operations using both high-level porcelain commands and low-level APIs, and includes a sample HTTP Git server implementation using GitServlet.

Tokens
708
Snippets
3
Records
6
Agent score
33%

What's inside jgit-cookbook

  1. Overview of JGit API layers

    master

    JGit is organized into two primary layers:

    1. Low-level API: Provides granular access to Git objects (commits, trees, blobs, tags) and internal structures. Use this for complex operations or when you need fine-grained control.
    2. Porcelain commands: Higher-level, user-facing commands that mimic standard Git CLI behavior (e.g., add, commit, branch, merge). These are generally easier to use for common tasks.
  2. Install and build jgit-cookbook

    master

    To use the snippets in this project, you need JDK 17 or newer. You can clone the repository and build it using Maven or Gradle to generate project files for your IDE.

    Using Maven

    mvn dependency:sources eclipse:eclipse package

    Using Gradle

    ./gradlew eclipse check
    git clone https://github.com/centic9/jgit-cookbook.git
  3. Run the GitServlet HTTP server

    master

    The httpserver sub-project provides a simple HTTP Git server based on JGit's GitServlet. It serves a demo repository on any URL beneath /repo.

    To start the server, navigate to the httpserver directory and run:

    ./gradlew run

    Once running, you can interact with the sample repository using standard Git commands:

    git clone http://localhost:8080/repo/some_repo
  4. Run the JGit HTTP Server

    master

    The org.dstadler.jgit.server.Main class provides a minimal implementation of a Git server using Jetty and JGit's GitServlet. Once the server is running, you can clone the hosted repository using the Git CLI.

    Usage

    After starting the application, use the following command to clone the repository:

    git clone http://localhost:8080/TestRepo

    Important Notes

    • Browser Access: Visiting http://localhost:8080/<reponame> in a web browser will result in an HTTP 404 error. The server is designed to handle the Git protocol, not web browsing.
    • Repository Logic: In this simple implementation, the server returns the same repository for any requested name.
  5. Configure the GitServlet Repository Resolver

    master

    To use the GitServlet in a custom server, you must provide a RepositoryResolver. This resolver is responsible for mapping incoming requests to a specific JGit Repository instance.

    In the provided example, the resolver is configured to return a single repository for every request and increments the repository's open count to manage its lifecycle:

    GitServlet gs = new GitServlet();
    gs.setRepositoryResolver((req, name) -> {
        repository.incrementOpen();
        return repository;
    });