我在通过 GitHub API 执行简单挑选时遇到问题。这一定是可能的,但我不清楚如何......
我正在使用 C# 为 Slack 开发一个聊天机器人,以帮助管理 Kubernetes 环境和 GitHub 版本,并希望通过修补程序功能对其进行扩展。给定一个环境,它应该创建一个与当前版本匹配的分支,并在其中挑选一个或多个提交 SHA,由请求的作者通过 Slack 提供。
所有管道都已就位。使用 POST/repos/:owner/:repo/git/refs
我能够创建与特定版本匹配的分支。这意味着我有一个branch name
、commit SHA
和tree SHA
准备好进行下一步;挑选一个或多个提交 SHA 到这个分支。使用 POST/repos/:owner/:repo/git/commits
我可以创建一个提交,但我不确定要使用哪个树和/或父级 - 这可能会导致我遇到的问题遇到调用 POST/repos/:owner/:repo/merges
,因为它在本地返回状态 409( merge 冲突),当然,它没有。
我似乎找到的唯一真实例子是 https://github.com/tibdex/github-cherry-pick .然而,它并不真正符合我的场景,我很难理解 Git 的内部工作原理。
我的场景(从最新到最旧);
* commit E (current state of `master`)
* commit D
* commit C (deployed to environment)
* commit B
* commit A
在这种情况下,我想将提交 E 挑选到提交 C 的新分支中,创建一个我可以发布的集合(A、B、C、E)。
* commit E (current state of `master`)
* commit D
|
| * commit E (new branch, to be deployed)
|/
* commit C (deployed to environment)
* commit B
* commit A
基本上我需要的是这个 bash 的 GitHub API 版本;
git checkout -b {new-branch-name} {sha}
git cherry-pick {sha}
git push main {new-branch-name}
感谢任何帮助!
最佳答案
下面是我如何在 Github API 上用伪代码实现 cherry-pick:
// here is a commit, for example, from a pull request:
listOfCommits = GET /repos/$owner/$repo/pulls/$number/commits
commit = listOfCommits.head // the first one in the list
// Here is the branch we want to cherry-pick to:
branch = GET /repos/$owner/$repo/branches/$branchName
branchSha = branch.commit.sha
branchTree = branch.commit.commit.tree.sha
// Create a temporary commit on the branch, which extends as a sibling of
// the commit we want but contains the current tree of the target branch:
parentSha = commit.parents.head // first parent -- there should only be one
tempCommit = POST /repos/$owner/$repo/git/commits { "message": "temp",
"tree": branchTree,
"parents": [parentSha] }
// Now temporarily force the branch over to that commit
PATCH /repos/$owner/$repo/git/refs/heads/$refName { sha = tempCommit.sha,
force = true }
// Merge the commit we want into this mess:
merge = POST /repos/$owner/$repo/merges { "base": branchName
"head": commit.sha }
// and get that tree!
mergeTree = merge.commit.tree.sha
// Now that we know what the tree should be, create the cherry-pick commit.
// Note that branchSha is the original from up at the top.
cherry = POST /repos/$owner/$repo/git/commits { "message": "looks good!",
"tree": mergeTree,
"parents": [branchSha] }
// Replace the temp commit with the real commit:
PATCH /repos/$owner/$repo/git/refs/heads/$refName { sha = cherry.sha,
force = true }
// Done!
Git高手请指教,不过我相信这样可以做到:
git checkout -b {new-branch-name} {sha}
git cherry-pick {sha}
git push main {new-branch-name}
https://stackoverflow.com/questions/53859199/