S3をgitのリモートリポジトリにする

3年前の話題が自分的に再燃したので、ブログを書く。

要約

  • jgit(パッチバージョン)を使うとS3をリモートリポジトリにできます
  • gitのカスタムコマンドを作るとさらにシームレスに連携できます

jgit

jgitJavaで実装されたgitでEclipseプラグインなどで使われているが、S3をリポジトリにできるというおもしろ機能がある。 ただし、本家のバージョンではus-east-1しか使えなくて「うーむ…」と思っていたのだが、ソース見たら案外簡単に直せそうだったので、直してプルリクを投げた

修正版jgitの作り方

brew install maven
git clone https://github.com/winebarrel/jgit.git
cd jgit
git checkout origin/support_for_other_region -b support_for_other_region
mvn package
cp org.eclipse.jgit.pgm/target/jgit /usr/local/bin/

gitのカスタムコマンドを作る

jgitとgitを使い分けるのはめんどくさいのでgitのカスタムコマンドを作る。

  • /usr/local/bin/git-s3-clone
#!/bin/sh
jgit clone "$@"
  • /usr/local/bin/git-s3-fetch
#!/bin/sh
jgit fetch "$@"
  • /usr/local/bin/git-s3-push
#!/bin/sh
jgit push "$@"

S3をリモートリポジトリとして使う

まず、設定ファイルを用意する。

  • ~/.jgit_s3
accesskey: ...
secretkey: ...
acl: private
endpoint: s3-ap-northeast-1.amazonaws.com

※2014/11/28 注) domainendpoint に変えました

S3にバケットを用意する。

aws s3 mb s3://winebarrel-repo --region ap-northeast-1

プロジェクトをpush

mkdir myapp
cd myapp/
git init
echo test > test.txt
git add .
git commit -m 'first commit'
git remote add origin amazon-s3://.jgit_s3@winebarrel-repo/projects/myapp.git/
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git s3-push

プロジェクトをclone/fetch

git s3-clone amazon-s3://.jgit_s3@winebarrel-repo/projects/myapp.git
cd myapp
git s3-fetch
git merge origin/master

かなり違和感なく使える。