我有一个 Git 存储库,其中包含以下git log信息master:
commit 23c5565d156266f3e26943d0811abaa88f7957a1 (HEAD -> master)
Fourth commit.
commit 92c0d491c3fd537a06876d1c0fc69c27ebf3e935
Third commit.
commit 67d604623f1b7fd8492f42edd6aebda83b075173
Second commit.
commit 11ef1cc228c27c87771dbc1a6a3fa98b7a605fa5
First commit.
commit 946f4fb2a02042f450090414eee6324799eb35df
Initial commit
Run Code Online (Sandbox Code Playgroud)
使用libgit2我想执行变基,将前四个提交压缩在一起(23c556- 11ef1)并保持初始提交完好无损。我的目标是通过将master分支重新设置为 commit 946f4,选择第一个提交,然后压缩其他三个来解决这个问题。git rebase -i HEAD~4这与最近三个提交上的 squash 命令类似。
这是我为尝试解决此问题而编写的伪代码:
git_rebase_init( &rebase, m_repository, branch, nullptr, initialCommit, &opts );
bool shouldSquash = false;
git_oid rebaseOID = {{0}};
while ( git_rebase_next( &op, rebase ) == GIT_OK )
{
if ( shouldSquash )
{
op->type = GIT_REBASE_OPERATION_SQUASH;
}
else
{
op->type = GIT_REBASE_OPERATION_PICK;
shouldSquash = true;
}
git_signature * author = generateSignature();
if ( !author ) return false;
git_rebase_commit( &rebaseOID, rebase, author, author, nullptr, nullptr );
}
git_rebase_finish( rebase, nullptr );
Run Code Online (Sandbox Code Playgroud)
不幸的是,当变基完成时,没有任何提交被压缩。它们的显示方式与上面日志中的一样,只是它们具有不同的提交哈希值。