How to create github Repository and upload files from PHP?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 19:44:31
VonC

As mentioned in "GitHub API - write to repo", you would need to create a blob.

You can see an example in KnpLabs/php-github-api/test/Github/Tests/Api/GitData/BlobsTest.php#L53-L68

(I am using KnpLabs/php-github-api, since ornicar/php-github-api is deprecated and refers to it)

/**
 * @test
 */
public function shouldCreateBlob()
{
    $expectedValue = array('blob' => 'some data');
    $data = array('content' => 'some cotent', 'encoding' => 'utf8');

    $api = $this->getApiMock();
    $api->expects($this->once())
        ->method('post')
        ->with('repos/l3l0/l3l0repo/git/blobs', $data)
        ->will($this->returnValue($expectedValue));

    $this->assertEquals($expectedValue, $api->create('l3l0', 'l3l0repo', $data));
}

There is no filename yet: you upload content;

To create a file though, as shown in this example, you would still need to:

  • get the SHA the current master branch points to
  • fetch the tree this SHA belongs to
  • create a new tree object with the new blob, based on the old tree
  • create a new commit object using the new tree and point its parent to the current master
  • finally update the heads/master reference to point to the new commit
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!