I use git_index_add_from_workdir to add, but why git_index_entrycount return 0?
and another question, I clone a git from remote, and git_index_entrycount also return 0?
Why?
below is the code that how I got the IndexCount , I first create a new repo, and create new file and document
(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);
}
git_index_add_from_workdir()updates the in-memory instance of the index. The change isn’t persisted on the filesystem (in order to make this happen, one would have to callgit_index_write()).Calling
git_index_read()updates the in-memory index with what is being stored on the filesystem and thus discards any “unsaved” change. This explains why you end up with an entrycount of zero entry.In order to fix this, either remove the call to
git_index_read()or add a call togit_index_write()before the call togit_index_read().