ruby – 如何以编程方式使用Rugged创建提交?

我正在尝试以编程方式使用 Rugged( libgit2的Ruby绑定)创建对现有存储库的提交.我试图遵循Rugged README中提供的文档,但我认为它与代码库的当前状态并不完全匹配.我尝试运行以下代码时不断收到错误
require 'rugged'
# Create an instance of the existing repository
repo = Rugged::Repository.new('/full/path/to/repo')
# grab the current Time object for Now
curr_time = Time.Now
# write a new blob to the repository,hang on to the object id
oid = repo.write("Some content for the this blob - #{curr_time}.",'blob')
# get the index for this repository
index = repo.index
# add the blob to the index
index.add(:path => 'newfile.txt',:oid => oid,:mode => 0100644)
curr_tree = index.write_tree(repo)
curr_ref = 'HEAD'
author = {:email=>'email@email.com',:time=>curr_time,:name=>'username'}
new_commit = Rugged::Commit.create(repo,:author => author,:message => "Some Commit Message at #{curr_time}.",:committer => author,:parents => [repo.head.target],:tree => curr_tree,:update_ref => curr_ref)

我得到的当前错误说index.add行有问题.它说TypeError:错误的参数类型为nil(预期的Fixnum).

任何帮助更好地理解如何创建一个坚固的新提交将非常感激.

更新

我刚刚通过运行gem install –prerelease rugged将Rugged 0.16.0更新为Rugged 0.18.0.gh.de28323.我上面详述的代码现在似乎有效.我不确定为什么它不适用于0.16.0.这个人似乎有同样的问题,他们在this answer详述.

解决方法

看起来你传递nil到index.add它不接受一个,并且该行中的错误只是之前未能检查错误的症状. repo.write的第二个参数应该是一个符号,而不是一个字符串,因此很可能返回nil来表示错误.传递:blob而不是’blob’应该修复它.

您可以查看我们用于生成libgit2自己的文档的https://github.com/libgit2/docurium/blob/master/lib/docurium.rb#L115-L116和周围代码.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...