使用Rugged/libgit2创建提交时如何更新工作目录? [英] How to update the working directory when creating a commit with Rugged/libgit2?

查看:121
本文介绍了使用Rugged/libgit2创建提交时如何更新工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下测试脚本创建具有坚固性的提交:

I'm trying to create a commit with rugged using the following test script:

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 => "actor@bla"}
options = {
    :tree => tree,
    :parents => parents,
    :committer => actor,
    :message => "message",
    :update_ref => "HEAD"
}
puts Rugged::Commit.create(r, options)

创建提交,脚本输出773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7(新提交的SHA).生成的提交和树看起来应该是这样的:

The commit is created, and the script outputs 773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7 (the SHA of the new commit). The generated commit and tree look like they're supposed to:

ludwig$ git cat-file commit 773d97f453a6df6e8bb5099dc0b3fc8aba5ebaa7
tree 253d0a2b8419e1eb89fd462ef6e0b478c4388ca3
parent bb1593b0534c8a5b506c5c7f2952e245f1fe75f1
author Actor <actor@bla> 1417735899 +0100
committer Actor <actor@bla> 1417735899 +0100

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

但是,工作目录未更新:

However, the working directory is not updated:

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,以显示在工作目录中.我以为创建一个Rugged提交并设置:update_ref => "HEAD"应该可以自动更新工作目录,但是一定出错了,因为执行r.checkout_head也没有效果.但是,我认为我正在正确地遵循坚固的示例.我在这里想念什么?

I have to do a git reset --hard HEAD to get the missing file test.md to show up in the working directory. I thought creating a Rugged commit, and setting :update_ref => "HEAD", was supposed to update the working directory automatically, but something must be going wrong, because doing r.checkout_head also has no effect. However, I think I'm following the rugged examples correctly. What am I missing here?

ludwig$ gem list rugged

*** LOCAL GEMS ***

rugged (0.21.2)

推荐答案

您要采取的步骤是不希望影响workdir或当前分支的步骤.您不是在创建文件,也不是将修改后的索引写入磁盘.

The steps you're taking are those for when you do not want to affect the workdir or current branch. You are not creating the file and you are not writing the modified index to disk.

如果要在文件系统上放置文件,然后在新提交中对其进行跟踪,请先创建文件

If you want to put a file on the filesystem and then track it in a new commit, start by creating the file

# 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
...

,然后像现在一样进行提交.

and then commit as you are doing now.

这篇关于使用Rugged/libgit2创建提交时如何更新工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆