gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:11:52 25 4
gpt4 key购买 nike

我使用git_index_add_from_workdir添加,但是为什么git_index_entrycount返回0?还有一个问题,我从远程克隆一个git,git_index_entrycount也返回0?为什么?下面是我如何获取 IndexCount 的代码,我首先创建一个新的存储库,然后创建新的文件和文档

(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() 更新索引的内存实例。更改不会保留在文件系统上(为了实现这一点,必须调用git_index_write())。

调用 git_index_read() 使用文件系统上存储的内容更新内存索引,从而丢弃任何“未保存”的更改。这解释了为什么您最终的条目计数为零。

为了解决此问题,请删除对 git_index_read() 的调用,或者在调用 git_index_read()< 之前添加对 git_index_write() 的调用.

关于ios - 我使用git_index_add_from_workdir添加,但是为什么git_index_entrycount返回0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14291759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com