2014-12-04 50 views
2

我試圖創建一個堅固的使用下面的測試腳本提交:如何在使用Rugged/libgit2創建提交時更新工作目錄?

require "rugged" 
r = Rugged::Repository.new(".") 
index = r.index 
index.read_tree(r.references["refs/heads/master"].target.tree) 
blob = r.write("My test", :blob) 
index.add(:oid => blob, :path => "test.md", :mode => 0100644) 
tree = index.write_tree 
parents = [r.references["refs/heads/master"].target].compact 
actor = {:name => "Actor", :email => "[email protected]"} 
options = { 
    :tree => tree, 
    :parents => parents, 
    :committer => actor, 
    :message => "message", 
    :update_ref => "HEAD" 
} 
puts Rugged::Commit.create(r, options) 

提交將被創建,並且腳本輸出773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7(新提交的SHA)。所生成的提交和樹看起來像他們應該:

ludwig$ git cat-file commit 773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7 
tree 253d0a2b8419e1eb89fd462ef6e0b478c4388ca3 
parent bb1593b0534c8a5b506c5c7f2952e245f1fe75f1 
author Actor <[email protected]> 1417735899 +0100 
committer Actor <[email protected]> 1417735899 +0100 

message 
ludwig$ git ls-tree 253d0a2b8419e1eb89fd462ef6e0b478c4388ca3 
100644 blob a7f8d9e5dcf3a68fdd2bfb727cde12029875260b Initial file 
100644 blob 7a76116e416ef56a6335b1cde531f34c9947f6b2 test.md 

然而,工作目錄沒有更新:

ludwig$ ls 
Initial file rugged_test.rb 
ludwig$ git status 
On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

    deleted: test.md 

我要做一個git reset --hard HEAD獲得丟失的文件test.md展示在工作目錄中。我認爲創建一個堅固的提交,並設置:update_ref => "HEAD",應該自動更新工作目錄,但一定會出錯,因爲做r.checkout_head也沒有效果。不過,我認爲我正確地跟着the rugged examples。我在這裏錯過了什麼?

編輯:

ludwig$ gem list rugged 

*** LOCAL GEMS *** 

rugged (0.21.2) 

回答

0

你採取這些步驟,因爲當你不想影響工作目錄的或當前分支。您沒有創建該文件,也沒有將修改的索引寫入磁盤。

如果你想要把一個文件在文件系統,然後跟蹤它在一個新的承諾,通過創建文件

# Create the file and give it some content 
f = open("test.md", "w") 
f << "My test" 
f.close 

# The file from the workdir from to the index 
# and write the changes out to disk 
index = repo.index 
index.add("test.md") 
index.write 

# Get the tree for the commit 
tree = index.write_tree 
... 

開始,然後提交,你現在正在做的事情。

+0

啊,我期待堅固的工作目錄是最新的,因爲JGit這樣做,也通過「管道」API添加到索引。謝謝你澄清崎嶇不應該這樣工作。我很困惑,因爲'r.checkout_head'沒有工作,但看起來是因爲我忘了正確設置:strategy選項。 – dometto 2014-12-05 20:09:09

相關問題