成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

使用 Github API 更新倉(cāng)庫(kù)

Jason_Geng / 2190人閱讀

摘要:原文鏈接總覽本文為大家提供一種使用更新倉(cāng)庫(kù)的方法。通常我們會(huì)使用客戶端然后到,但為我們提供了相關(guān),在某些情況下可以直接通過(guò)更新倉(cāng)庫(kù)。使用更新倉(cāng)庫(kù)只需六步。獲取用于獲取當(dāng)前的的。更新使的指針指到你最新提交的版本。

原文鏈接:https://ssshooter.com/2019-01...

0. 總覽

本文為大家提供一種使用?GitHub?API?更新 GitHub 倉(cāng)庫(kù)的方法。通常我們會(huì)使用 Git 客戶端 Commit 然后 Push 到 GitHub,但 GitHub 為我們提供了相關(guān) API,在某些情況下可以直接通過(guò) API 更新倉(cāng)庫(kù)。

在此之前有一些前置知識(shí)點(diǎn),你需要了解 git 的儲(chǔ)存原理。碰巧最近掘金有一篇很火的文章有提到相關(guān)知識(shí)。

使用 GitHub API 更新倉(cāng)庫(kù)只需六步。雖然對(duì)于第一次接觸的新手來(lái)說(shuō)可能會(huì)有點(diǎn)復(fù)雜,但是理清關(guān)系之后思路便會(huì)很清晰了。

獲取 Ref:Ref 指?Git 的引用。如果要發(fā)起 Commit,必須知道你的 Commit 要提交到什么地方去(Commit 是一個(gè)接一個(gè)的鏈狀關(guān)系,所以需要知道上一個(gè) Commit 的信息),這一步做的就是這件事。

獲取 Commit:用于獲取當(dāng)前 Commit 的 tree 的 sha。

生成 Blob:相當(dāng)于正常本地提交的 add?操作,可以參考這里的解釋。

生成 Tree:構(gòu)建一個(gè)新的 tree,把上一步生成的 Blob 放到合適的位置。這里需要用到的參數(shù) base_tree?就是來(lái)自第二步的 Commit 信息。

生成 Commit:提交你的 tree,這步跟正常 commit 很接近,不過(guò)要你手動(dòng)找到此次提交的上一次提交,借哈希值把新的 Commit 接起來(lái)。

更新 Ref:使 master 的指針指到你最新提交的版本。

注意:訪問(wèn)?POST 方法的接口必須帶 token,雖然 GET 可以不帶,但是會(huì)限制訪問(wèn)頻率,所以建議都帶上。相關(guān)文檔:https://developer.github.com/v3/#authentication

1. 獲取 Ref? 1.1 文檔地址

https://developer.github.com/v3/git/refs/#get-a-reference

1.2 請(qǐng)求地址

GET https://api.github.com/repos/ssshooter/test/git/refs/heads/master

1.3 返回?cái)?shù)據(jù)
{
  "ref": "refs/heads/master",
  "node_id": "MDM6UmVmMTY0Nzk4NDczOm1hc3Rlcg==",
  "url": "https://api.github.com/repos/ssshooter/test/git/refs/heads/master",
  "object": {
    "sha": "cda66de943082033f4b761639df77728d7bca4f0",
    "type": "commit",
    "url": "https://api.github.com/repos/ssshooter/test/git/commits/cda66de943082033f4b761639df77728d7bca4f0"
  }
}
2. 獲取 Commit 2.1 文檔地址

https://developer.github.com/v3/git/commits/#get-a-commit

2.2 請(qǐng)求地址

GET https://api.github.com/repos/ssshooter/test/git/commits/cda66de943082033f4b761639df77728d7bca4f0

2.3 返回?cái)?shù)據(jù)
{
  "sha": "cda66de943082033f4b761639df77728d7bca4f0",
  "node_id": "MDY6Q29tbWl0MTY0Nzk4NDczOmNkYTY2ZGU5NDMwODIwMzNmNGI3NjE2MzlkZjc3NzI4ZDdiY2E0ZjA=",
  "url": "https://api.github.com/repos/ssshooter/test/git/commits/cda66de943082033f4b761639df77728d7bca4f0",
  "html_url": "https://github.com/ssshooter/test/commit/cda66de943082033f4b761639df77728d7bca4f0",
  "author": {
    "name": "DeJavuJo",
    "email": "ssshooterx@gmail.com",
    "date": "2019-01-09T06:02:07Z"
  },
  "committer": {
    "name": "GitHub",
    "email": "noreply@github.com",
    "date": "2019-01-09T06:02:07Z"
  },
  "tree": {
    "sha": "d7a57d28ace0db12773c7d70675f94a48da6421f",
    "url": "https://api.github.com/repos/ssshooter/test/git/trees/d7a57d28ace0db12773c7d70675f94a48da6421f"
  },
  "message": "Create readme.md",
  "parents": [
  ],
  "verification": {
    "verified": true,
    "reason": "valid",
    "signature": "-----BEGIN PGP SIGNATURE-----

wsBcBAABCAAQBQJcNY5fCRBK7hj4Ov3rIwAAdHIIABh8I89hATqg1mSYtpx1aY
Jt/woDobMO7FGE5qXO0NNrCMqwF6mdPJOMMMZvVWF1ULTm8ZJ52GAh5xAnPMZEnQ
8tTjj/Qc0eZCm5B1xO66rgx+7eKkeGUPJj5bX7Lf0i9Se70Ff0jaCdH94RgiSL2d
clDLhNyM4u6AH//k7S3Ud1O6ezXd4+99381Xa331PDLhJttUAbRFRCThszbcxRUT
PvyhPNvXB5ug4J+tAMaZJvZEaED0c1k2Yx1+TYkLvPjOlqXvqaDVpMZieluD5l+f
u13NAoCnLfBe0Skak/8MxRDbMcJYNW78ll60HAa6jJtElD8Vkek8WoVAZ8p3iIE=
=xI87
-----END PGP SIGNATURE-----
",
    "payload": "tree d7a57d28ace0db12773c7d70675f94a48da6421f
author DeJavuJo  1547013727 +0800
committer GitHub  1547013727 +0800

Create readme.md"
  }
}
3. 生成 Blob 3.1 文檔地址

https://developer.github.com/v3/git/blobs/#create-a-blob

3.2 請(qǐng)求地址

POST https://api.github.com/repos/ssshooter/test/git/blobs

3.3 請(qǐng)求參數(shù)
{
  "content": "Content of the blob",
  "encoding": "utf-8"
}
3.4 返回?cái)?shù)據(jù)
{
    "sha": "929246f65aab4d636cb229c790f966afc332c124",
    "url": "https://api.github.com/repos/ssshooter/test/git/blobs/929246f65aab4d636cb229c790f966afc332c124"
}
4. 生成 tree 4.1 文檔地址

https://developer.github.com/v3/git/trees/#create-a-tree

4.2 請(qǐng)求地址

POST https://api.github.com/repos/ssshooter/test/git/trees

4.3 請(qǐng)求參數(shù)

注意:tree.path 可以寫深層目錄,如?deep/deep/newFile.md(前面不用寫斜杠)

{
  "base_tree": "d7a57d28ace0db12773c7d70675f94a48da6421f", // commit tree 的 sha
  "tree": [
    {
      "path": "apiCommitFile.md", // 文件路徑
      "mode": "100644", // 類型,詳情看文檔
      "type": "blob",
      "sha": "929246f65aab4d636cb229c790f966afc332c124" // 剛才生成的 blob 的 sha
    }
  ]
}
4.4 返回?cái)?shù)據(jù)
{
    "sha": "2b3a65095ceb7cf4425f52d59ca5974d826cff80",
    "url": "https://api.github.com/repos/ssshooter/test/git/trees/2b3a65095ceb7cf4425f52d59ca5974d826cff80",
    "tree": [
        {
            "path": "readme.md",
            "mode": "100644",
            "type": "blob",
            "sha": "b1b716105590454bfc4c0247f193a04088f39c7f",
            "size": 5,
            "url": "https://api.github.com/repos/ssshooter/test/git/blobs/b1b716105590454bfc4c0247f193a04088f39c7f"
        },
        {
            "path": "tree",
            "mode": "040000",
            "type": "tree",
            "sha": "91d8d59147e395effaeacd01c9d8553b37cf77c5",
            "url": "https://api.github.com/repos/ssshooter/test/git/trees/91d8d59147e395effaeacd01c9d8553b37cf77c5"
        }
    ],
    "truncated": false
}
5. 生成 Commit 5.1 文檔地址

https://developer.github.com/v3/git/commits/#create-a-commit

5.2 請(qǐng)求地址

POST https://api.github.com/repos/ssshooter/test/git/commits

5.3 請(qǐng)求參數(shù)
{
  "message": "a Commit with GitHub api",
  "parents": [
    "cda66de943082033f4b761639df77728d7bca4f0" // 上次 commit 的sha
  ],
  "tree": "2b3a65095ceb7cf4425f52d59ca5974d826cff80"
}
5.4 返回?cái)?shù)據(jù)
{
    "sha": "45c58a9358b67fc81e4034cb36c5196d791686ef",
    "node_id": "MDY6Q29tbWl0MTY0Nzk4NDczOjQ1YzU4YTkzNThiNjdmYzgxZTQwMzRjYjM2YzUxOTZkNzkxNjg2ZWY=",
    "url": "https://api.github.com/repos/ssshooter/test/git/commits/45c58a9358b67fc81e4034cb36c5196d791686ef",
    "html_url": "https://github.com/ssshooter/test/commit/45c58a9358b67fc81e4034cb36c5196d791686ef",
    "author": {
        "name": "ssshooter",
        "email": "ssshooterx@gmail.com",
        "date": "2019-01-09T10:24:10Z"
    },
    "committer": {
        "name": "ssshooter",
        "email": "ssshooterx@gmail.com",
        "date": "2019-01-09T10:24:10Z"
    },
    "tree": {
        "sha": "2b3a65095ceb7cf4425f52d59ca5974d826cff80",
        "url": "https://api.github.com/repos/ssshooter/test/git/trees/2b3a65095ceb7cf4425f52d59ca5974d826cff80"
    },
    "message": "a Commit with GitHub api",
    "parents": [
        {
            "sha": "cda66de943082033f4b761639df77728d7bca4f0",
            "url": "https://api.github.com/repos/ssshooter/test/git/commits/cda66de943082033f4b761639df77728d7bca4f0",
            "html_url": "https://github.com/ssshooter/test/commit/cda66de943082033f4b761639df77728d7bca4f0"
        }
    ],
    "verification": {
        "verified": false,
        "reason": "unsigned",
        "signature": null,
        "payload": null
    }
}
6. 更新 Ref 6.1 文檔地址

https://developer.github.com/v3/git/refs/#update-a-reference

6.2 請(qǐng)求地址

https://api.github.com/repos/ssshooter/test/git/refs/heads/master

6.3 請(qǐng)求參數(shù)
{
    "sha":"45c58a9358b67fc81e4034cb36c5196d791686ef",
    "force":true
}
6.4 返回?cái)?shù)據(jù)
{
    "ref": "refs/heads/master",
    "node_id": "MDM6UmVmMTY0Nzk4NDczOm1hc3Rlcg==",
    "url": "https://api.github.com/repos/ssshooter/test/git/refs/heads/master",
    "object": {
        "sha": "45c58a9358b67fc81e4034cb36c5196d791686ef",
        "type": "commit",
        "url": "https://api.github.com/repos/ssshooter/test/git/commits/45c58a9358b67fc81e4034cb36c5196d791686ef"
    }
}
7. node.js 實(shí)踐代碼
var updateGitHubRes = function(blob, path) {
  var commitSha
  var commitTreeSha
  return getRef()
    .then(({ data }) => {
      commitSha = data.object.sha
      return getCommit(commitSha)
    })
    .then(({ data }) => {
      commitTreeSha = data.tree.sha
      return createBlob(blob)
    })
    .then(({ data }) => {
      var blobSha = data.sha
      return createTree(commitTreeSha, path, blobSha)
    })
    .then(({ data }) => {
      var treeSha = data.sha
      return createCommit(commitSha, treeSha)
    })
    .then(({ data }) => {
      var newCommitSha = data.sha
      return updataRef(newCommitSha)
    })
    .catch(err => {
      console.log(err)
    })
}

var getRef = function() {
  return axios.get(`/${owner}/${repo}/git/refs/heads/master`)
}

var getCommit = function(commitSha) {
  return axios.get(`/${owner}/${repo}/git/commits/${commitSha}`)
}

var createBlob = function(content) {
  return axios.post(`/${owner}/${repo}/git/blobs`, {
    content,
    encoding: "utf-8"
  })
}

var createTree = function(base_tree, path, sha) {
  return axios.post(`/${owner}/${repo}/git/trees`, {
    base_tree, // commit tree 的 sha
    tree: [
      {
        path, // 文件路徑
        mode: "100644", // 類型,詳情看文檔
        type: "blob",
        sha // 剛才生成的 blob 的 sha
      }
    ]
  })
}

var createCommit = function(parentCommitSha, tree, message = "update post") {
  return axios.post(`/${owner}/${repo}/git/commits`, {
    message,
    parents: [parentCommitSha],// 上次 commit 的sha
    tree
  })
}

var updataRef = function(newCommitSha) {
  return axios.post(`/${owner}/${repo}/git/refs/heads/master`, {
    sha: newCommitSha,
    force: true
  })
}
參考文獻(xiàn)

https://int128.hatenablog.com/entry/2017/09/05/161641
https://juejin.im/post/5c33f49de51d45523070f7bb
https://git-scm.com/book/zh/v2/Git-內(nèi)部原理-Git-引用

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/101029.html

相關(guān)文章

  • 使用 Gatsby.js 搭建靜態(tài)博客 EX 使用語(yǔ)雀發(fā)布到博客

    摘要:原文鏈接偶然看到通過(guò)語(yǔ)雀發(fā)布文章到靜態(tài)博客,很方便,實(shí)現(xiàn)過(guò)程也很有意思。我的配置接收推送此時(shí)在語(yǔ)雀發(fā)布文章,接口就會(huì)收到推送的文章信息。 原文鏈接:https://ssshooter.com/2019-01... 偶然看到通過(guò)語(yǔ)雀 webhook 發(fā)布文章到 Hexo 靜態(tài)博客,很方便,實(shí)現(xiàn)過(guò)程也很有意思。同樣的原理可以運(yùn)用到 Gatsby.js 博客上。 因?yàn)槭褂昧?netlify,...

    DoINsiSt 評(píng)論0 收藏0
  • 使用 Gatsby.js 搭建靜態(tài)博客 6 評(píng)論系統(tǒng)

    摘要:原文鏈接方案選擇大家都知道等第三方評(píng)論系統(tǒng)的存在。部署自己的的原理就是使用接口把評(píng)論更新到你靜態(tài)博客的倉(cāng)庫(kù),觸發(fā)博客重新部署,在頁(yè)面生成評(píng)論。這樣得到的博客頁(yè)面包括評(píng)論部分都是完全靜態(tài)的。配置完畢推送到或本地運(yùn)行。 原文鏈接:https://ssshooter.com/2019-01... 方案選擇 大家都知道 disqus 等第三方評(píng)論系統(tǒng)的存在。disqus 幾年前還是挺好使的,但...

    venmos 評(píng)論0 收藏0
  • Hexo 博客終極玩法:云端寫作,自動(dòng)部署

    摘要:配置觸發(fā)方式一般會(huì)得到這么個(gè)語(yǔ)雀配置配置一個(gè)倉(cāng)庫(kù)的可以選擇所有更新觸發(fā)或者主動(dòng)觸發(fā),主動(dòng)觸發(fā)的意思即發(fā)布需要勾選一個(gè)選項(xiàng)才會(huì)觸發(fā)。 Hexo + Github + 語(yǔ)雀 + yuque-hexo +travis-ci+severless 打造全自動(dòng)持續(xù)集成個(gè)人博客,云端寫作,自動(dòng)部署,完美體驗(yàn)~ 一、Hexo+Github 的痛點(diǎn) 1.為啥要用hexo+github? 作為一個(gè)程序猿,...

    AlphaWallet 評(píng)論0 收藏0
  • Hexo 博客終極玩法:云端寫作,自動(dòng)部署

    摘要:配置觸發(fā)方式一般會(huì)得到這么個(gè)語(yǔ)雀配置配置一個(gè)倉(cāng)庫(kù)的可以選擇所有更新觸發(fā)或者主動(dòng)觸發(fā),主動(dòng)觸發(fā)的意思即發(fā)布需要勾選一個(gè)選項(xiàng)才會(huì)觸發(fā)。 Hexo + Github + 語(yǔ)雀 + yuque-hexo +travis-ci+severless 打造全自動(dòng)持續(xù)集成個(gè)人博客,云端寫作,自動(dòng)部署,完美體驗(yàn)~ 一、Hexo+Github 的痛點(diǎn) 1.為啥要用hexo+github? 作為一個(gè)程序猿,...

    EsgynChina 評(píng)論0 收藏0
  • 使用 satis 搭建 composer 本地倉(cāng)庫(kù)

    摘要:如果需要定時(shí)更新,則需要配置定時(shí)任務(wù)去定時(shí)更新設(shè)置本地倉(cāng)庫(kù)設(shè)置虛擬主機(jī)使用本地倉(cāng)庫(kù)中的包文件中添加以下拉取,即可獲取本地庫(kù)了如果本地倉(cāng)庫(kù)不存在且有網(wǎng)絡(luò)會(huì)去網(wǎng)絡(luò)中獲取。 環(huán)境 windows nginx php composer 安裝 拉取 satis 項(xiàng)目包,并拉取項(xiàng)目依賴 composer create-project composer/satis --stability=de...

    張率功 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<