0
1. create file a.txt
2. commit file a.txt
3. a.txt is tracked
我能做到這一點的libgit2容易。 但是,如果我修改A.TXT,並希望將其添加到索引我會做 git add a.txt
可惜我不能效仿與libgit2。我幾乎嘗試了互聯網提供的一切,但沒有任何工作。所以我覺得我錯過了一些基本的東西。請注意,我可以添加沒有跟蹤索引而沒有問題的新文件,只有我找不到添加它們的方式。
這是我的代碼。
void add_file(char *file)
{
git_index *index;
int error;
const git_index_entry *entry;
git_index_entry new_entry;
error = git_repository_index(&index, m_repo);
entry = git_index_get_bypath(index,file, 0);
if(entry)
{
memcpy(&new_entry, entry, sizeof(git_index_entry));
new_entry.path = file;
new_entry.mode = GIT_FILEMODE_BLOB;
error = git_index_add(index, &entry);
}
else
error = git_index_add_bypath(index, file);
error = git_index_write(index);
git_index_free(index);
}
編輯:
埃德的帖子後,我更新我的代碼,但它仍然是添加了不跟蹤唯一的新文件。
git_index *index;
git_oid tree;
int error;
error = git_repository_index(&index, m_repo);
error = git_index_add_bypath(index, file);
error = git_index_write(index);
error = git_index_write_tree(&tree, index);
git_index_free(index);
你能解釋一下在API調用這方面有點: 「你需要將文件與得到的OID添加到對象數據庫,然後更新索引」 其API方法我應該叫什麼呢? –
沒關係,我找到了。它是'git_index_write_tree'。 –
我想我誤解了你說的話。你能否解釋第三段可能是一個例子(不一定是全部工作)。我想我明白了,但我想我完全誤解了。查看更新後的帖子 –