ruby 重命名文件使用Github API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 重命名文件使用Github API相关的知识,希望对你有一定的参考价值。

master_ref  = client.ref(repo, 'heads/master')
last_commit = client.commit(repo, master_ref[:object][:sha])
last_tree   = client.tree(repo, last_commit[:sha], recursive: true)
changed_tree = last_tree[:tree].map(&:to_hash).reject { |blob| blob[:type] == 'tree' }
changed_tree.each { |blob| blob[:path] = 'History.markdown-2' if blob[:path] == 'History.markdown' }
changed_tree.each { |blob| blob.delete(:url) && blob.delete(:size) }
new_tree   = client.create_tree(repo, changed_tree)
new_commit = client.create_commit(repo, 'move file test', new_tree[:sha], last_commit[:sha])
client.update_ref(repo, 'heads/master', new_commit.sha)

# References:
#
# http://www.levibotelho.com/development/commit-a-file-with-the-github-api/
# https://github.com/michael/github/blob/fac19118d73193e8dd1a66f8d8d11af4eda2fd71/github.js
master_ref  = client.ref(repo, 'heads/master')
last_commit = client.commit(repo, master_ref[:object][:sha])
last_tree   = client.tree(repo, last_commit[:sha], recursive: true)
changed_tree = last_tree[:tree].map(&:to_hash).reject { |blob| blob[:type] == 'tree' }
changed_tree.each { |blob| blob[:path] = 'History.markdown-5' if blob[:path] == 'History.markdown-4' }
changed_tree.each { |blob| blob.delete(:url) && blob.delete(:size) }
tree_for_rename = client.create_tree(repo, changed_tree)

blob_sha = client.create_blob(repo, Base64.encode64('## HEAD'), 'base64')
mod_tree = client.create_tree(repo, [ { :path => 'History.markdown-5', :mode => "100644", :type => "blob", :sha => blob_sha } ], {:base_tree => tree_for_rename[:sha] })

new_commit = client.create_commit(repo, 'move file test', mod_tree[:sha], last_commit[:sha])
client.update_ref(repo, 'heads/master', new_commit.sha)

以上是关于ruby 重命名文件使用Github API的主要内容,如果未能解决你的问题,请参考以下文章

ruby复制文件夹到新的重命名的文件夹

如何在 ruby​​ 中使用 MongoMapper 重命名数据库?

在github中移动和重命名文件

如何重命名 GitHub 网站上的目录/文件夹?

BIM 360 重命名文件夹 API

ruby 重命名参数