6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gitコマンドがない環境でjgitの力でgitを使う方法

Last updated at Posted at 2018-04-03

モチベーション

とある環境で「sudo権限がない」とか、「makeでコケる」とかの理由でgitがインストールできず、
困っていて、それの一つの対処としてやってみて、うまく行ったので紹介します

(頑張ってクロスコンパイルに挑戦したけど駄目だったって人の助けになれば、嬉しいです)

アイデア

gitのJavaの実装であるjgitを使えばJVMがある環境ならどこでも動くんじゃないか

やり方

全体の流れとしては、
eclipse/jgitを取ってきて、ビルドする感じです。mvnコマンドが使える環境ならビルドできると思います。
(gitmakeしたりするよりも上手くいきやすいと思います)

# リポジトリを取ってきます
# (適当なgitのある環境でやりました)
git clone git@github.com:eclipse/jgit.git
# (またはwget https://github.com/eclipse/jgit/tree/stable-4.11 でzipをダウンロードしてもいいと思います)

# ディレクトリを移動します
cd jgit/

# 現在の最新の安定版にcheckoutします
git checkout stable-4.11

# インストールします
mvn clean install

# ビルドできたjgitを動かしてみる
./org.eclipse.jgit.pgm/target/jgit --version

./org.eclipse.jgit.pgm/target/がポータブルで、このtargetディレクトリをgitコマンドが欲しい環境に設置すればOKです。

jgitをビルドする環境とjgitを設置したい環境は別でも大丈夫だと思います(Write once, run anywhereというJavaのスローガンを信じて...)。

あとは、./org.eclipse.jgit.pgm/target/jgitに向かって"git"という名前で以下のコマンドでシンボリックリンクを作成します

# gitという名前でシンボリックを作成する($PWDに作成される)
ln -s ./org.eclipse.jgit.pgm/target/jgit git

ここにパスを通せば、gitコマンドのように扱えます。

JGitって何?信頼性あるの?

JGitはJavaでgitの実装です。

公式では以下のように言っているので、裏でgitとかを叩いている感じのものではないです。

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

主な使われ方はJava(もしくはJVM言語)からgitcommitとかcheckoutとかの命令たちをプログラムから扱うことだと思います。以下がその例です。

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

system()みたいなので、gitコマンドを叩いたりせずに操るのが主な目的だと思います。
今回のようにCLIとして使い方法も公式はドキュメント用意していて、この使い方も全然ありなはずです。

信頼性に関してですが、
Eclipseが開発しているようなので、Eclipseでも使われてでしょうし、
Scalaで書かれているGitbucket(オープンソース版のGitHubみたいな)でも使われているみたいなので、かなり信頼できると思っています。

jgitの動作確認

(注意:以下のgitjgitへのシンボリックリンクです)

$ 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

参考

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?