
Grit の調査のつづき。
フィルをコミットする方法とか調べた。
まず、リポジトリを指定して Repo を new
> require 'grit'
> include Grit
> repo = Repo.new('.')
test というファイルを作成して、
> system 'touch test'
=> true
インデックスに add
> repo.add('test')
=> ""
そしてそれを commit
> repo.commit_index('add test')
=> "[master (root-commit) 90de066] add test\n 0 files changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 test\n"
ちゃんとコミットされたか確認
> system 'git log'
commit 90de0663fc7d0f15ef685a9106580c7a389122d8
Author: jugyo <jugyo.org@gmail.com>
Date: Thu Oct 1 23:13:29 2009 +0900
add test
=> true
ファイルの内容を取得してみる
> repo.commits.first.tree.contents.first.data
=> ""
うん。できてる。
Grit::Index を使うやりかた
Grit::Index を使うと、ファイルをいちいち作らなくてもインデックスにコンテンツを直接 add して commit してしまえる。
インデックスオブジェクトを取得
> index = repo.index
=> #<Grit::Index:0x130bf8c @tree={}, @current_tree=nil, @repo=#<Grit::Repo "/xxx/grit_test/.git">>
インデックスに foo という名前のファイル(中身は ‘this is foo’)を add
> index.add('foo', 'this is foo')
=> "this is foo"
それをコミット
> index.commit('add foo', [repo.commits.first.id])
=> "bd8fb763d7c7fc5db067b1c5348dfa6bfe2acbc4"
コミットする際、親となるコミットを指定しないと親のいないコミットを作ってしまうことになる
コミットできてるか確認
> system 'git log'
commit bd8fb763d7c7fc5db067b1c5348dfa6bfe2acbc4
Author: jugyo <jugyo.org@gmail.com>
Date: Thu Oct 1 08:06:16 2009 -0700
add foo
commit 7e72c5fe12e03bbf25e9dcbe315b7f8fa63e6e79
Author: jugyo <jugyo.org@gmail.com>
Date: Fri Oct 2 00:04:51 2009 +0900
add test
=> true
できてるできてる。
