首页 > 解决方案 > BitBucket:如何通过 API 更新文件

问题描述

我正在努力了解如何实现以下 API 来更新存储库中的文件:

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/src#post

GitLab 和 GitHub 有一个简单的 api - 传递文件内容 + 提交 SHA,如果提交 SHA 仍然是最新的,它将更新。

我试过这个,当然它很高兴地覆盖了已经存在的任何东西:

curl -X POST \
  'https://api.bitbucket.org/2.0/repositories/%7B%7D/{uuid}/src' \
  -H 'Authorization: Bearer ***' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'cache-control: no-cache' \
  -d 'src%2Flocale%2Fen.js=test&message=Test%20commit'

如何指定文件提交 SHA,以便在更新时不会覆盖内容?谢谢

标签: bitbucketbitbucket-api

解决方案


在您的问题中已有的文档中指出:

parents (string):
A comma-separated list of SHA1s of the commits that should be the parents of the newly created commit.
When omitted, the new commit will inherit from and become a child of the main branch's tip/HEAD commit.
When more than one SHA1 is provided, the first SHA1 identifies the commit from which the content will be inherited.
When more than 2 parents are provided on a Mercurial repo, a 400 is returned as Mercurial does not support "octopus merges".

这并没有真正说明预防,但在branch参数的描述中指出:

When a branch name is not specified, but a parent SHA1 is provided, then Bitbucket asserts that it represents the main branch's current HEAD/tip, or a 409 is returned.

因此,听起来您只需要将该参数添加到您的数据有效负载中,并且如果提供的哈希不是当前提交,它应该可以防止更改。然后,您将收到带有 http 状态 409 的响应。

-d 'src%2Flocale%2Fen.js=test&message=Test%20commit&parents=sha-hash'

推荐阅读