2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GitHub APIで空コミットをコミットする

Posted at

背景

空コミットしてプルリク作成をスクリプトで自動化したい
gitリポジトリをcloneしてコミットしてpushするのは面倒
GitHubのAPIでcloneなして空コミットができそうだったのでやってみた

マニュアル

やり方

  • ブランチ(分岐元)のtreeを取得しコミットのshaを取得
GET /repos/:owner/:repo/git/trees/:branch?recursive=1
  -> "sha": "<commit-sha>" ...(*1)
  • 取得したコミットのshaを指定してtreeのshaを取得
GET /repos/:owner/:repo/git/commits/:sha <---(*1)
   -> "tree":{ "sha": "<tree-sha>"} ...(*2)
  • 取得treeを指定してcommitする(これが空コミットになる)
POST /repos/:owner/:repo/git/commits -d
  '{
    "message": "create new branch",
    "author": {
      "name": "<commiter-name>",
      "email": "<commiter-email>"
    },
    "parents": [
      "<commit-sha>" <---(*1)
    ],
    "tree": "<tree-sha>" <---(*2)
  }'

  -> "sha": "<new-commit-sha>" ...(*3)
  • ブランチを新しく作成する
POST /repos/:owner/:repo/git/refs -d
  '{
    "ref": "refs/heads/<branch-name>",
    "sha": "<new-commit-sha>" <---(*3)
  }'

さいごに

APIでもできた。gitリポジトリによっては(サイズが大きくて重たいものだと)APIでやったほうが早い。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?