提交创建但不添加要提交的文件。
第6步使用的方法
void GitWizard::commit(const QString &textCommit)
{
int error;
git_oid commit_id, parentCommitId;
git_tree *tree;
git_commit *parent;
git_signature *sig = nullptr;
char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
qDebug("\n*Commit Writing*\n");
error = git_signature_default(&sig, repo);
checkForError(error, "Get default signature");
error = git_reference_name_to_id(&parentCommitId, repo, "HEAD" );
checkForError(error, "Reference to id");
error = git_commit_lookup(&parent, repo, &parentCommitId );
checkForError(error, "Commit lookup");
error = git_commit_create_v(
&commit_id, /* out id */
repo,
"HEAD", /* do not update the HEAD */
sig,
sig,
"UTF-8", /* use default message encoding */
"example commit",
tree,
1,
parent);
/**
* Now we can take a look at the commit SHA we've generated.
*/
if (!checkForError(error, "Add commit")) {
git_oid_fmt(oid_hex, &commit_id);
qDebug("New Commit: %s\n", oid_hex);
}
/**
* Free all objects used in the meanwhile.
*/
git_tree_free(tree);
git_commit_free(parent);
git_signature_free(sig);
}
void GitWizard::initNewRepo(const QString &dir)
{
int error = git_repository_init(&repo, dir.toUtf8().constData(), false);
if (!checkForError(error, QString("Init new repo in directory %1").arg(dir))) {
mRepoPath = dir;
error = git_repository_index(&idx, repo);
checkForError(error, QString("Init index"));
}
}
bool GitWizard::checkForError(int errorCode, QString action) const
{
qDebug() << action;
const git_error *error = giterr_last();
if (!errorCode)
return false;
qCritical("Git Error %d <%s>: %s", errorCode, action.toUtf8().constData(),
(error && error->message) ? error->message : "?");
return true;
}
void GitWizard::addToIndex(const QList<QString> &files) {
int filesSize = files.size();
for (int i = 0; i < filesSize; i++) {
addToIndex(files.at(i));
}
writeIndex();
}
void GitWizard::writeIndex()
{
int error = git_index_write(idx);
checkForError(error, QString("Write index to disk"));
}
void GitWizard::createInitialCommit()
{
git_signature *sig = nullptr;
git_oid tree_id, commit_id;
git_tree *tree = nullptr;
int error = git_signature_default(&sig, repo);
if (!checkForError(error, "Get default signature")) {
error = git_repository_index(&idx, repo);
}
if (!checkForError(error, "Open repository index")) {
error = git_index_write_tree(&tree_id, idx);
}
if (!checkForError(error, "Look up initial tree")) {
error = git_tree_lookup(&tree, repo, &tree_id);
}
if (!checkForError(error, "Write initial tree from index")) {
error = git_commit_create_v(
&commit_id, repo, "HEAD", sig, sig,
nullptr, "Initial commit", tree, 0);
if (!checkForError(error, "Create initail commit")) {
getCurrentBranch();
}
}
git_tree_free(tree);
git_signature_free(sig);
}
“ git log”结果新提交
但是已更改的文件未添加到新提交中,并且“ git status”显示已添加到索引中的文件为新文件。
据我所知,需要正确找到分支的HEAD,它将是所创建提交的父提交。此代码有效:
void GitWizard::commit(const QString &textCommit)
{
int error;
git_oid tree_id, commit_id, parentCommitId;
git_tree *tree;
git_commit *parent;
git_signature *sig = nullptr;
char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
qDebug("\n*Commit Writing*\n");
error = git_signature_default(&sig, repo);
checkForError(error, "Get default signature");
error = git_index_write_tree(&tree_id, idx);
checkForError(error, "Write tree");
error = git_tree_lookup(&tree, repo, &tree_id);
checkForError(error, "Tree lookup");
error = git_reference_name_to_id(&parentCommitId, repo, "HEAD" );
checkForError(error, "Reference to id");
error = git_commit_lookup(&parent, repo, &parentCommitId );
checkForError(error, "Commit lookup");
error = git_commit_create_v(
&commit_id, /* out id */
repo,
"HEAD", /* do not update the HEAD */
sig,
sig,
"UTF-8", /* use default message encoding */
textCommit.toUtf8().constData(),
tree,
1,
parent);
/**
* Now we can take a look at the commit SHA we've generated.
*/
if (!checkForError(error, "Add commit")) {
git_oid_fmt(oid_hex, &commit_id);
qDebug("New Commit: %s\n", oid_hex);
}
/**
* Free all objects used in the meanwhile.
*/
git_tree_free(tree);
git_commit_free(parent);
git_signature_free(sig);
}