2

I am trying to create GitHub Repository from PHP code, as well as i wanted to upload the files into the created GitHub Repository.

I been trying to use https://github.com/ornicar/php-github-api this PHP API, But i couldn't find anywhere that how can i upload Files into the created GitHub Repository.

So Please Help me to solve this issue, Thanks in Advance.

Ex:

  1. I want to create a Respo Named myProject1

  2. Then i wanted to add three files(papi.php, index.php, jquery.min.js).

So please help me.

rkaartikeyan
  • 1,977
  • 9
  • 29
  • 57
  • We expect questions here on Stack Overflow to show some effort. And be specific question to be able to help out. As it currently stands your question is too broad and lacks information. – PeeHaa Apr 18 '14 at 11:00
  • @PeeHaa Thanks for your comment, i showed the link that i used the php api, with that i can create repo, and others too. but i couldn't found anywhere how to upload file to the created repo from php. So i want people should guide me how to do that? or you can show me some blogs or links that explain how can upload/create files into the github repo from php. – rkaartikeyan Apr 18 '14 at 11:03
  • Possible duplicate of http://stackoverflow.com/questions/6668283/github-api-write-to-repo/7506554 – PeeHaa Apr 18 '14 at 11:47

1 Answers1

3

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your kind reply @VonC. in the above function where is i have to give the file name? so that the above function will create a file. For Example, I creating a respo from php api called myProject1 then i wanted to add three files(papi.php, index.php, jquery.min.js) in the Respo that i created. – rkaartikeyan Apr 18 '14 at 11:23
  • 1
    @rkaartikeyan you first create a blob: $data represents its content. *Then* you would need to create a new tree object and create a new commit. See an example (in perl, but the idea remains the same) at http://www.pqpq.de/2011/07/pithub-how-to-commit-new-file-via.html. – VonC Apr 18 '14 at 11:26
  • 1
    @rkaartikeyan Remember, Git is at its root a content management. That is why you upload first a *content* (no "filename" invoved there) – VonC Apr 18 '14 at 11:27