簡體   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