Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用GitHub API复制存储库?_Github_Github Api - Fatal编程技术网

如何使用GitHub API复制存储库?

如何使用GitHub API复制存储库?,github,github-api,Github,Github Api,我正在考虑使用GithubAPI创建一个新存储库的最佳方法,该存储库是GitHub上另一个存储库的精确副本。下载和上传每个文件似乎效率低下。您可以使用原始的回购协议:这将在您的帐户中的GitHub上直接创建一个新的回购协议 POST /repos/:owner/:repo/forks 假设原始回购协议尚未在您的帐户中声明。使用Ruby,您可以尝试以下操作: POST /repos/:owner/:repo/forks # Set Up class GithubCopy def sel

我正在考虑使用GithubAPI创建一个新存储库的最佳方法,该存储库是GitHub上另一个存储库的精确副本。下载和上传每个文件似乎效率低下。

您可以使用原始的回购协议:这将在您的帐户中的GitHub上直接创建一个新的回购协议

POST /repos/:owner/:repo/forks
假设原始回购协议尚未在您的帐户中声明。

使用Ruby,您可以尝试以下操作:

POST /repos/:owner/:repo/forks
# Set Up

class GithubCopy

  def self.copy_repo(args)
    from = args.fetch(:from)
    to   = args.fetch(:to)

    client = Octokit::Client.new(:access_token => 'your access token')

    base_items = client.contents from
    self.copy_items(base_items, from, to, client)
  end

  private

  def self.copy_items(base_items, from, to, client)
    base_items.each do |item|
      content = client.content from, :path => item.path
      if content.is_a?(Array)
        self.copy_items(content, from, to, client)
      else
        client.create_contents to, content.path, "Creating #{content.name}", Base64.decode64(content.content)
      end
    end
  end

end

# Usage
GithubCopy.copy_repo(from: 'repo1', to: 'repo2')
这假设repo1repo2都存在,并且您的访问令牌对这两个repo都有适当的权限。如果您需要先创建回购协议,请使用以下方法:

client = Octokit::Client.new(:access_token => settings.github_access_token)
client.create_repository 'some-repo', {organization: 'some-org'}

如@tmarwen所述,如果两份回购协议在同一账户下,您将需要单独的名称。一个缺点是,根据原始回购规模,这种方法可能需要一些时间

这将起作用,但是我可能希望多次复制同一个存储库,而这似乎不起作用。也许我做错了什么?@DekelAdler不,你没有做错什么:每次回购只能创建一个fork。