[JAVA] How to use git with the power of jgit in an environment without git commands

motivation

In a certain environment, git cannot be installed due to reasons such as" you do not have sudo privileges "or" moss with make". I was in trouble, I tried it as one of the countermeasures, and it worked, so I will introduce it

(I tried my best to cross-compile, but I hope it helps people even if it doesn't work.)

idea

"If you use jgit, which is a Java implementation of git, it will work anywhere with a JVM **."

manner

As a whole flow, It's like fetching eclipse / jgit and building it. I think you can build it if you can use the mvn command. (I think it works better than make git)

#Get the repository
#(I did it in an environment with a suitable git)
git clone [email protected]:eclipse/jgit.git
# (Or wget https://github.com/eclipse/jgit/tree/stable-4.I think you can download the zip at 11)

#Move directory
cd jgit/

#Check out to the current latest stable version
git checkout stable-4.11

#Install
mvn clean install

#Try running the built jgit
./org.eclipse.jgit.pgm/target/jgit --version

./org.eclipse.jgit.pgm/target/ is portable, and you can set this target directory in the environment where you want the git command.

** I think it's okay to build jgit and the environment where you want to install jgit ** (believe in the Java slogan ** Write once, run anywhere ** ...).

After that, create a symbolic link with the following command with the name " git " toward ./org.eclipse.jgit.pgm/target/jgit.

#Create a symbolic with the name git ($Created in PWD)
ln -s ./org.eclipse.jgit.pgm/target/jgit git

If you pass the path here, you can treat it like a git command.

What is JGit? Is it reliable?

** JGit is an implementation of git in Java. ** **

The official statement is as follows, so it's not like hitting git behind the scenes.

An implementation of the Git version control system in pure Java.

I think that the main usage is to handle instructions such as commit and checkout of git from Java (or JVM language) programmatically. The following is an example.

//git commit example
Git git = new Git(db);
CommitCommand commit = git.commit();
commit.setMessage("initial commit").call();
//git log example
Git git = new Git(db);
Iterable<RevCommit> log = git.log().call();

It's like system (), so I think the main purpose is to operate it without hitting the git command. There is an official document on how to use it as a CLI like this time, and there should be no way to use it.

Regarding reliability, It seems that Eclipse is developing it, so it will be used in Eclipse as well, It seems to be used in Gitbucket (like the open source version of GitHub) written in Scala, so I think it's quite reliable.

Operation check of jgit

(Note: The git below is a symbolic link to jgit)

$ git --version
jgit version 5.0.0-SNAPSHOT
$ git
jgit --git-dir GIT_DIR --help (-h) --show-stack-trace --version command [ARG ...]

The most commonly used commands are:
 add              Add file contents to the index
 archive          Zip up files from the named tree
 branch           List, create, or delete branches
 checkout         Checkout a branch to the working tree
 clean            Remove untracked files from the working tree
 clone            Clone a repository into a new directory
 commit           Record changes to the repository
 config           Get and set repository or global options
 daemon           Export repositories over git://
 debug-lfs-store  Run LFS Store in a given directory
 describe         Show the most recent tag that is reachable from a commit
 diff             Show diffs
 fetch            Update remote refs from another repository
 gc               Cleanup unnecessary files and optimize the local repository
 init             Create an empty git repository
 log              View commit history
 ls-remote        List references in a remote repository
 ls-tree          List the contents of a tree object
 merge            Merges two development histories
 push             Update remote repository from local refs
 reflog           Manage reflog information
 repo             Parse a repo manifest file and add submodules
 reset            Reset current HEAD to the specified state
 rm               Stop tracking a file
 show             Display one commit
 status           Show the working tree status
 tag              Create a tag
 version          Display the version of jgit

reference

Recommended Posts

How to use git with the power of jgit in an environment without git commands
How to manage the difference in each environment with yml without increasing the number of RAILS_ENV
How to execute with commands of normal development language in Docker development environment
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Summary of how to use the proxy set in IE when connecting with Java
How to get the length of an audio file in java
How to use environment variables in RubyOnRails
How to convert an array of Strings to an array of objects with the Stream API
[Swift] How to set an image in the background without using UIImageView.
Output of how to use the slice method
How to use JQuery in js.erb of Rails6
[Rails] How to build an environment with Docker
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
[Swift] How to get the number of elements in an array (super basic)
Use hashes well in Ruby to calculate the total amount of an order
How to get the ID of a user authenticated with Firebase in Swift
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to set environment variables in the properties file of Spring boot application
How to use Z3 library in Scala with Eclipse
Organized how to interact with the JDK in stages
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use JDD library in Scala with Eclipse
How to boot by environment with Spring Boot of Maven
[Rails] How to display an image in the view
If you use SQLite with VSCode, use the extension (how to see the binary file of sqlite3)
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
`bind': Address already in use --bind (2) for 127.0.0.1:3000 (Errno :: EADDRINUSE) How to deal with the error
How to request by passing an array to the query with HTTP Client of Ruby
Use the --build-arg option of docker-compose to pass environment variables to the container built with Dockerfile
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
How to use the getter / setter method (in object orientation)
How to use trained model of tensorflow2.0 with Kotlin / Java
How to pass an object to Mapper in MyBatis without arguments
How to create a placeholder part to use in the IN clause
How to retrieve the hash value in an array in Ruby
Uppercase only the specified range with substring. (How to use substring)
What happened in "Java 8 to Java 11" and how to build an environment
How to add elements without specifying the length of the array
How to derive the last day of the month in Java
How to change the contents of the jar file without decompressing
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
The story of migrating a stray batch without an owner from EC2 to a Docker environment
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
I want to recreate the contents of assets from scratch in the environment built with capistrano
How to use the link_to method
How to use Lombok in Spring
How to use the include? method
[Beginner] Procedure to log in to the virtual environment built with Vagrant
[Sprint Boot] How to use 3 types of SqlParameterSource defined in org.springframework.jdbc.core.namedparam
[Swift5] How to get an array and the complement of arrays
How to make an application with ruby on rails (assuming that the environment has been built)
How to use the wrapper class
I tried to build the environment of PlantUML Server with Docker
How to use setDefaultCloseOperation () of JFrame
How to output the value when there is an array in the array
How to build an environment with Docker, which is the minimum required to start a Rails application
How to use mssql-tools with alpine
[Java] How to search for a value in an array (or list) with the contains method
The story of toString () starting with passing an array to System.out.println
How to use InjectorHolder in OpenAM