2014-12-04 49 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

相关问题