読者です 読者をやめる 読者になる 読者になる

git + S3

なにこれすごい。はてブが3ユーザってどういうことなの…


それはさておき、自分でも試してみた。
まずはJGitを探す。今はEclipseのとこでホスティングされているらしい。


Eclipse配下のサイトからダウンロード。
JGit - Download

0.9.3じゃないとfetchがうまくいかなかった…
http://ftp.daum.net/eclipse/jgit/maven/org/eclipse/jgit/org.eclipse.jgit.pgm/0.9.3/


~/work$ wget http://download.eclipse.org/jgit/maven/org/eclipse/jgit/org.eclipse.jgit.pgm/0.10.1/org.eclipse.jgit.pgm-0.10.1.sh
~/work$ chmod 755 org.eclipse.jgit.pgm-0.10.1.sh
~/work$ ./org.eclipse.jgit.pgm-0.10.1.sh
jgit --git-dir GIT_DIR --help (-h) --show-stack-trace command [ARG ...]

The most commonly used commands are:
add Add file contents to the index
branch List, create, or delete branches
clone Clone a repository into a new directory
commit Record changes to the repository
daemon Export repositories over git://
diff Show diffs
fetch Update remote refs from another repository
init Create an empty git repository
log View commit history
push Update remote repository from local refs
rm Stop tracking a file
tag Create a tag
version Display the version of jgit

なんか普通にバイナリ落とせた。


とりあえず /opt/local/bin に置いとく。


~/work$ sudo mv ./org.eclipse.jgit.pgm-0.10.1.sh /opt/local/bin/
~/work$ sudo ln -s /opt/local/bin/org.eclipse.jgit.pgm-0.10.1.sh /opt/local/bin/jgit
~/work$ jgit version
jgit version 0.10.1


ネイティブgitで適当なリポジトリを作成


~/work/myapp$ vi hello.c
~/work/myapp$ mv ~/myapp/hello.c .
~/work/myapp$ cat hello.c
#include

int main() {
printf("hello, world\n");
return 0;
}
~/work/myapp$ gcc hello.c -o hello && ./hello
hello, world
~/work/myapp$ git init
Initialized empty Git repository in /Users/sugawara/work/myapp/.git/
~/work/myapp$ git add hello.c
~/work/myapp$ git commit -m "first commit" -a
[master (root-commit) 6132e15] first commit
1 files changed, 6 insertions(+), 0 deletions(-)
create mode 100644 hello.c


ホームディレクトリに.jgit_s3ファイルを作成。*1


~/work/myapp$ vi ~/.jgit_s3
~/work/myapp$ cat ~/.jgit_s3
accesskey: XXXXX
secretkey: XXXXXXXXXX
acl: public


とりあえずシンガポールにバケット作成。


~/work/myapp$ s3cmd mb s3://winebarrel.git --bucket-location ap-southeast-1
Bucket 's3://winebarrel.git/' created


pushしてみる。


~/work/myapp$ export EC2_URL=http://ec2.ap-southeast-1.amazonaws.com # 必要かどうかわからない。。。
~/work/myapp$ git remote add s3 amazon-s3://.jgit_s3@winebarrel.git/projects/myapp.git/
~/work/myapp$ git config --add remote.s3.push refs/heads/master
~/work/myapp$ jgit push s3
Counting objects: 3
Finding sources: 100% (3/3)
Compressing objects: 100% (2/2)
Compressing objects: 100% (2/2)
Writing objects: 100% (3/3)
Put pack-9f06c54..pack: 0
Put pack-9f06c54..idx: 100% (1/1)
To amazon-s3://.jgit_s3@winebarrel.git/projects/myapp.git/
* [new branch] master -> master
ふっつーにpushできた。


cloneしてみる。


~/work/myapp$ cd ..
~/work$ git clone http://s3-ap-southeast-1.amazonaws.com/winebarrel.git/projects/myapp.git/ myapp2
Cloning into myapp2...
error: The requested URL returned error: 403 (curl_result = 22, http_code = 403, sha1 = 2e3643b53eea2980069edfe2e70de02f167692f2)

あれ?
myapp.gitをMake Publicしてから、もう一度やってみる。


~/work$ rm -rf myapp2
~/work$ git clone http://s3-ap-southeast-1.amazonaws.com/winebarrel.git/projects/myapp.git/ myapp2
Cloning into myapp2...
error: The requested URL returned error: 403 (curl_result = 22, http_code = 403, sha1 = 2e3643b53eea2980069edfe2e70de02f167692f2)

あれれ?


いろいろ試した結果、projects/でMake Publicして、バケットでEveryone -> Allow listしたらcloneできた。


~/work$ rm -rf myapp2
~/work$ git clone http://s3-ap-southeast-1.amazonaws.com/winebarrel.git/projects/myapp.git/ myapp2
Cloning into myapp2...
~/work$ ls -la myapp2/
total 8
drwxr-xr-x 4 sugawara staff 136 2 6 03:53 .
drwxr-xr-x 19 sugawara staff 646 2 6 03:53 ..
drwxr-xr-x 12 sugawara staff 408 2 6 03:53 .git
-rw-r--r-- 1 sugawara staff 75 2 6 03:53 hello.c


jgitでもcloneしてみる。


~/work$ jgit clone amazon-s3://.jgit_s3@winebarrel.git/projects/myapp.git myapp3
Initialized empty Git repository in /Users/sugawara/work/myapp3/.git
Get pack-9f06c54..idx: 100% (1/1)
From {0}
* [new branch] master -> origin/master
~/work$ cd myapp3
~/work/myapp3$ ls
hello.c
~/work/myapp3$ git log
commit 2e3643b53eea2980069edfe2e70de02f167692f2
Author: Genki Sugawara
Date: Sun Feb 6 03:40:01 2011 +0900

test commit
~/work/myapp3$ jgit pull
fatal: pull is not a jgit command

pullできないのか…

fetch/margeしてみる…うまくいかず。


~/work/myapp/myapp2$ jgit fetch
java.lang.NullPointerException
at org.eclipse.jgit.pgm.Fetch.run(Fetch.java:96)
at org.eclipse.jgit.pgm.TextBuiltin.execute(TextBuiltin.java:139)
at org.eclipse.jgit.pgm.Main.execute(Main.java:191)
at org.eclipse.jgit.pgm.Main.run(Main.java:120)
at org.eclipse.jgit.pgm.Main.main(Main.java:94)


version 0.9.3でうまくいった!


~/work/myapp$ jgit version
jgit version 0.9.3
~/work/myapp$ jgit clone amazon-s3://.jgit_s3@winebarrel.git/projects/myapp.git/ myapp-a
Initialized empty Git repository in /Users/sugawara/work/myapp/myapp-a/.git
Get pack-24b5174..idx: 100% (1/1)
From {0}
* [new branch] master -> origin/master
~/work/myapp$ jgit clone amazon-s3://.jgit_s3@winebarrel.git/projects/myapp.git/ myapp-b
Initialized empty Git repository in /Users/sugawara/work/myapp/myapp-b/.git
Get pack-24b5174..idx: 100% (1/1)
From {0}
* [new branch] master -> origin/master
~/work/myapp$ cd myapp-a/
~/work/myapp/myapp-a$ echo foo > bar
~/work/myapp/myapp-a$ git add bar
~/work/myapp/myapp-a$ git commit -m 'test commit' -a
[master ecbba00] test commit 1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 bar
~/work/myapp/myapp-a$ jgit push
fatal: Nothing to push.
~/work/myapp/myapp-a$ jgit push origin master
Counting objects: 3
Finding sources: 100% (3/3)
Compressing objects: 100% (2/2)
Compressing objects: 100% (1/1)
Writing objects: 100% (3/3)
Put pack-12b39d5..pack: 0
Put pack-12b39d5..idx: 100% (1/1)
To amazon-s3://.jgit_s3@winebarrel.git/projects/myapp.git/
a83fbe6..ecbba00 master -> master
~/work/myapp/myapp-a$ cd ../myapp-b
~/work/myapp/myapp-b$ jgit fetch
Get pack-12b39d5..idx: 100% (1/1)
From {0}
a83fbe6..ecbba00 master -> origin/master
~/work/myapp/myapp-b$ ls
hello.c
~/work/myapp/myapp-b$ git merge origin/master
Updating a83fbe6..ecbba00
Fast-forward
bar | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 bar
~/work/myapp/myapp-b$ ls
bar hello.c

*1:ホームディレクトじゃないとだめだった