我使用 git_index_add_from_workdir 添加,但是为什么 git_index_entrycount 返回 0?还有一个问题,我从远程克隆了一个 git,而 git_index_entrycount 也返回 0?为什么?下面是我如何获得 IndexCount 的代码,我首先创建一个新的 repo,然后创建新的文件和文档
(IBAction)IndexInfo:(id)sender {
git_index *index = NULL;
int ret = 0 ;
char out[41];
out[40] = '\0';
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docPath = [array objectAtIndex:0];
NSString *dir = [docPath stringByAppendingPathComponent:@"efg/" ];
NSLog(@"dir:%@",dir);
git_repository *repo = NULL;
ret = git_repository_init(&repo, [dir UTF8String], 0);
NSLog(@"git_repository_init ret:%d", ret);
git_repository_index(&index, repo);
if(git_index_entrycount(index) == 0)
{
NSLog(@"initial ok");
}
NSString *testPath = [dir stringByAppendingPathComponent:@"test00.txt"];
NSString *string = @"write String";
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL bres = [fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
if(bres == NO)
{
NSLog(@"create file error.");
}
ret = git_index_add_from_workdir(index, "test00.txt");
NSLog(@"git_index_add_from_workdir ret:%d", ret);
ret = git_index_read(index);
NSLog(@"index_read ret:%d", ret);
int ecount = git_index_entrycount(index);
if (!ecount)
printf("Empty index\n");
NSLog(@"index ecount:%d",ecount);
for (int i = 0; i < ecount; ++i) {
const git_index_entry *e = git_index_get_byindex(index, i);
git_oid_fmt(out, &e->oid);
printf("File Path: %s\n", e->path);
printf(" Stage: %d\n", git_index_entry_stage(e));
printf(" Blob SHA: %s\n", out);
printf("File Mode: %07o\n", e->mode);
printf("File Size: %d bytes\n", (int)e->file_size);
printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
printf(" ctime: %d\n", (int)e->ctime.seconds);
printf(" mtime: %d\n", (int)e->mtime.seconds);
printf("\n");
}
git_index_free(index);
}