我使用git_index_add_from_workdir添加,但为什么git_index_entrycount返回0?

时间:2013-01-12 08:46:40

标签: ios libgit2

我使用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);
}

1 个答案:

答案 0 :(得分:1)

git_index_add_from_workdir()更新索引的内存中实例。更改不会保留在文件系统上(为了实现此目的,必须调用git_index_write())。

调用git_index_read()使用存储在文件系统上的内容更新内存中索引,从而丢弃任何“未保存”的更改。这就解释了为什么你最终得到零入门的入门数。

要解决此问题,请在致电git_index_read()之前移除对git_index_write()的来电或致电git_index_read()