BitBucket:如何通过API更新文件

时间:2019-01-29 22:32:55

标签: bitbucket 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,以便它在更新后不会覆盖内容?谢谢

1 个答案:

答案 0 :(得分:1)

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

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'