how can i use rugged to create and commit a file l

2020-07-23 08:49发布

问题:

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"

which leaves git status printing

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

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?). executing git reset --hard puts the working dir back in the desired state from what i can tell.

thanks in advance for the help.

回答1:

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.