想请教个问题:为什么我在使用 rugged 进行 git push 操作时 报了 401 错误?

C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rugged-0.24.0/lib/rugged/repository.rb:22

4:in `push': Request failed with status code: 401 (Rugged::NetworkError)
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rugged-0.24.0/lib/rugged/rep
ository.rb:224:in `push'
        from mytest.rb:34:in `<main>' 

下面是我的代码 


require 'rugged'


git_email = '138XXXX@139.com'
git_name = 'XXXX'

repo_name = 'D://mytestcode//rubyOntest'


repo = Rugged::Repository.new('D://mytestcode//rubyOntest')
puts "1"
index = repo.index
puts "3"


oid = repo.write("This is a blob.", :blob)
#index.add(:path => "readme.txt", :oid => oid, :mode => 0100644)
index.add_all
puts "4"
options = {}
options[:tree] = index.write_tree(repo)
puts "5"
options[:author] = { :email => git_email, :name => git_name, :time => Time.now }
options[:committer] = { :email => git_email, :name => git_name, :time => Time.now }
puts "6"
options[:message] ||= "Making a commit via Rugged!--add  all"
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
options[:update_ref] = 'HEAD'
puts "7"

objuser=Rugged::Credentials::UserPassword.new(options)
puts "8"
Rugged::Commit.create(repo, options)
puts "9"
repo.push 'origin'# this is my error
puts "Done"


摇曳的蔷薇
浏览 70回答 2
2回答

波斯汪

401代表http未授权,你的repo.push方法调用未带授权信息&nbsp;repo.push/remote.push方法原型

Helenr

objuser=Rugged::Credentials::UserPassword.new({username: '用户名', password: '密码'})puts "add credentials"Rugged::Commit.create(repo, options)puts "Commit ..."remote = repo.remotes["origin"]puts remote.url#remote = Rugged::Remote.lookup(@repo&nbsp;, 'origin')&nbsp;#使用credentials进行授权即可。remote.push("refs/heads/master", {credentials: objuser})
打开App,查看更多内容
随时随地看视频慕课网APP