Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mark a file as deleted in a tree using the GitHub API?

Using the GitHub API, I can create a tree containing all the modified files for a commit, specify a base_tree that will be updated with this tree of changes and then commit it.

But, as the documentation says, the new tree can contain only paths for files with the modes

100644 for file (blob), 100755 for executable (blob), 040000 for subdirectory (tree), 160000 for submodule (commit), or 120000 for a blob that specifies the path of a symlink.

It doesn't say what should I do if I want to mark some path as deleted, as with git rm <path>.

like image 719
fiatjaf Avatar asked May 13 '14 17:05

fiatjaf


2 Answers

Currently, the only way to remove an entry from the tree (either a blob or another tree) is to construct a new tree and not list that entry. After you have that tree, construct a new commit that links to this new tree, and don't forget to bump the ref to point to this new commit.

like image 107
Ivan Zuzak Avatar answered Oct 19 '22 19:10

Ivan Zuzak


@ivan-zuzak 's answer is obsolete now that the GitHub API supports deleting files by passing null sha's.

To delete a file you would create a tree in Javascript with octokit like so:

await octokit.request('POST /repos/{owner}/{repo}/git/trees', {
  owner: 'octocat',
  repo: 'hello-world',
  tree: [
    {
      path: 'path/to/file-that-will-be-deleted.py',
      mode: '100664',
      type: 'blob',
      sha: null,
    }
  ],
  base_tree: 'sha'
})

The base_tree should contain the SHA1 of an existing Git tree object you want to delete the file from.

After that everything Ivan said still holds true, so you should construct a new commit that links to this new tree, and bump the ref to point to this new commit.

like image 32
gijswijs Avatar answered Oct 19 '22 20:10

gijswijs