繁体   English   中英

我如何使用rugged创建和提交文件,例如从命令行?

[英]how can i use rugged to create and commit a file like from the command line?

我正在尝试使用rugged做一些非常简单的事情:创建并提交文件,使存储库处于与执行操作相同的状态:

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

我正在使用从坚固的仓库的自述文件改编的代码,其归结为:

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打印

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可以根据我的判断将工作目录恢复到所需状态。

先谢谢您的帮助。

您忘记了将更改保存到索引。 您已经更新了引用以指向新的提交,但是索引没有更改(因为您从未调用过Index#write ),因此就git而言,由于文件不在索引中,因此删除已进行,就像如果您已经运行git rm blah.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM