Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy git tree from one Github repo to another using Trees API

Tags:

git

github

tree

Say I have two repos, repo1 and repo2.

Let's say repo1 has the following directory structure:

repo1
|--someFolder
   |-- file1.txt
   |-- file2.jpg

And let's say repo2 is empty. My goal is to use the GitHub Trees API to copy all of someFolder into repo2. But I've run into the following problems:

  1. When I use the following parameters in my HTTP POST request to the url https://api.github.com/repos/my-username/repo2/git/trees with the following parameters

    {'base_tree': repo2_base_tree_sha, 'tree': [ //Specially formatted tree from repo1 {'mode': u'100644', 'path': u'.travis.yml', 'sha': u'559d004c3af59b3bd8dcc486d5d2b5f91ab4f85d', 'type': u'blob'}, {'mode': u'100644', 'path': u'README.md', 'sha': u'11e4f5fa0b929ff6bccd8f5bcd99cbdbd845d69a', 'type': u'blob'} ] } I get a 422 response with a message saying tree.sha 559d004c3af59b3bd8dcc486d5d2b5f91ab4f85d is not a valid blob. This leads me to believe that one cannot simply construct a new tree for repo2 using a tree from repo1, so how is one to take a tree from another repository and add it to another?

  2. Secondly I can successfully modify repo1's base tree using the same parameters above except that base_tree is repo1's base_tree SHA. However, when I visit repo1 those changes aren't reflected, not sure as to why.

like image 685
Flabby Womark Avatar asked Oct 28 '25 06:10

Flabby Womark


1 Answers

I've been able to determine an answer to 2. myself.

The reason why creating a new tree doesn't automatically update the github page is because the most recent commit on github is not pointing to our newly created tree. We have to fix this by creating a commit that points to the new tree.

Currently working on 1.

--- UPDATE 7/12/17 ---

I have also determined the answer to number 1. For each dictionary in the 'tree' list above, the 'sha' field should only be included if the file that the dictionary represents has been committed to the destination repository, otherwise you need to have a 'content' field in the dictionary that stores the content that the file represents.

like image 52
Flabby Womark Avatar answered Oct 29 '25 20:10

Flabby Womark