我删除文件:用户git_remove_bypath和git_index_write
我使用索引来提交,而不是通过删除的文件来提交文件,但是通过删除的文件也可以提交成功。
int commit(void *repository, const char *file_name[], const unsigned int file, const char *message, void *payload)
{
int error = -1;
git_signature *signature = NULL;
/*git git; identify of any object*/
/*git commit_id; identify of any object*/
void* index = NULL;
void* tree = NULL;
do /* try catch */
{
const char *path = git_repository_path((git_repository*)repository);
/*get signature*/
error = git_signature_default(&signature, (git_repository*)repository);
/*get repository head*/
error = git_repository_head(&ref_head,(git_repository*)repository);
if (error == GIT_OK)
{
error = git_commit_lookup(&parent_commit, (git_repository*)repository, git_reference_target(ref_head));
}
else if (error != -9)
{
break;
}
/*get repository index*/
error = git_repository_index((git_index**)&index, (git_repository*)repository);
for(int i = 0; i < Count; ++i)
{
/*get statue*/
error = git_status_file(&flags, (git_repository *)repository, file_name[i]);
if(-3 == error || -5 == error)
{
continue;
}
if( GIT_STATUS_INDEX_DELETED & flags || GIT_STATUS_WT_DELETED & flags )
{
/*remove file*/
error = git_index_remove_bypath((git_index *)index, file_name[i]);
}
else
{
/*add file*/
error = git_index_add_bypath((git_index *)index, file_name[i]);
}
/*write disk*/
error = git_index_write((git_index*)index);
}
/* Get the index and write it to a tree */
error = git_index_write_tree(&git, (git_index*)index);
/* lookup tree */
error = git_tree_lookup((git_tree**)&tree, (git_repository *)repository, &git);
/* Do the commit*/
error = git_commit_create(&commit_id, (git_repository*)repository, "HEAD", signature, signature, NULL,
message, (git_tree *)tree, 1, parents);
}while(0);
}
答案 0 :(得分:0)
1. Delete file 1.txt
git_remove_bypath(index, "1.txt")
git_index_write(index)
2. git status
On branch master
Changes to be committed:
deleted: 1.txt
Changes not staged for commit:
modified: 2.txt
3.only want to commit modified file 2.txt
git_index_add_bypath(index, "2.txt"); // just commit 2.txt,don't selected 1.txt.
git_index_write(index);
git_index_write_tree(&git_oid, index);
git_tree_lookup(&git_tree, repo, &gitoid);
git_commit_create(&commit_oid, repo, "HEAD",signature, signature, NULL, message, git_tree, 1, parents_commit);
4. git status
on branch master
nothing to commit, working tree clean
Question:
Why the deleted file is also committed? How can I only commit "2.txt" without commit "1.txt"?
Just like tortoise git, delete the file by menu command, after that user can only commit the modified file("2.txt")