简体   繁体   English

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

git init
echo "blah blah blah" > blah.txt
git add blah.txt
git commit -m "write blah.txt"

which leaves git status printing 留下git status打印

On branch master
nothing to commit, working directory clean

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)

this seems to leave the repository in the right state, but the working dir in a weird place, with git status printing 这似乎使存储库处于正确的状态,但是工作目录位于一个奇怪的地方,带有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

i don't understand what git thinks is happening at this point ( blah.txt is staged for delete?). 我不明白git认为此时正在发生什么( blah.txt已准备好删除)? executing git reset --hard puts the working dir back in the desired state from what i can tell. 执行git reset --hard可以根据我的判断将工作目录恢复到所需状态。

thanks in advance for the help. 先谢谢您的帮助。

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 . 您已经更新了引用以指向新的提交,但是索引没有更改(因为您从未调用过Index#write ),因此就git而言,由于文件不在索引中,因此删除已进行,就像如果您已经运行git rm blah.txt

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

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