我如何使用rugged来创建和提交文件,如从命令行? [英] how can i use rugged to create and commit a file like from the command line?
问题描述
我试图用坚固耐用的方法来做一些非常简单的事情:创建并提交一个文件,使存储库处于与执行相同的状态:
i'm trying to use rugged to do something pretty simple: create and commit a file, leaving the repository in the same state as doing:
git init
echo "blah blah blah" > blah.txt
git add blah.txt
git commit -m "write blah.txt"
其中包含 git status
打印
On branch master
nothing to commit, working directory clean
我使用的代码来自坚固的回购 readme ,归结为:
i'm using code adapted from the rugged repo's readme, which boils down to:
name = "blah.txt"
repo = Rugged::Repository.init_at dir
File.open File.join(dir, name), 'w' do |f|
f.write content
end
oid = Rugged::Blob.from_workdir repo, name
index = repo.index
index.add(:path => name, :oid => oid, :mode => 0100644)
options = {}
options[:tree] = index.write_tree(repo)
options[:author] = { :email => "testuser@github.com",
:name => 'Test Author',
:time => Time.now }
options[:committer] = { :email => "testuser@github.com",
:name => 'Test Author',
:time => Time.now }
options[:message] = "write #{ name }"
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
options[:update_ref] = 'HEAD'
commit = Rugged::Commit.create(repo, options)
这似乎使存储库处于正确的状态,但是工作目录 git status
打印
this seems to leave the repository in the right state, but the working dir in a weird place, with git status
printing
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: blah.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
blah.txt
我不明白git认为此时发生了什么( blah.txt
正在进行删除?)。执行 git reset --hard
将工作目录从我所知道的状态恢复到所需的状态。
i don't understand what git thinks is happening at this point (blah.txt
is staged for delete?). executing git reset --hard
puts the working dir back in the desired state from what i can tell.
谢谢提前寻求帮助。
thanks in advance for the help.
推荐答案
您忘记将更改保存到索引中。你已经更新了引用来指向新的提交,但是索引没有改变(因为你永远不会调用 Index#write
),所以就git而言,删除因为该文件不在索引中,就好像您运行 git rm blah.txt
。
You've forgotten to save your changes to the index. You've updated the reference to point to the new commit, but the index is unchanged (as you never call Index#write
), so as far as git is concerned the deletion is staged because the file is not in the index, just as if you had run git rm blah.txt
.
这篇关于我如何使用rugged来创建和提交文件,如从命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!