ObjectiveGit 是對 libgit2 的上層封裝。在項(xiàng)目中如果要使用 ObjectiveGit 的話需要生成對應(yīng)的 framework。具體的導(dǎo)入方法可以查看ObjectiveGit。
如果想要更好的理解 ObjectiveGit ,可以提前看一下Pro Git,會讓你對git有更加深入的理解。
clone
ObjectiveGit 中 clone 一個遠(yuǎn)程倉庫會用到下面這方法
// GTRepository.h
+ (instancetype _Nullable)cloneFromURL:(NSURL *)originURL toWorkingDirectory:(NSURL *)workdirURL options:(NSDictionary * _Nullable)options error:(NSError **)error transferProgressBlock:(void (^ _Nullable)(const git_transfer_progress *, BOOL *stop))transferProgressBlock;
其中 options 中的 key 如下
GTRepositoryCloneOptionsTransportFlags
GTRepositoryCloneOptionsBare
GTRepositoryCloneOptionsPerformCheckout
GTRepositoryCloneOptionsCheckoutOptions
GTRepositoryCloneOptionsCredentialProvider
GTRepositoryCloneOptionsCloneLocal
GTRepositoryCloneOptionsServerCertificateURL
這里說一下這個 GTRepositoryCloneOptionsCredentialProvider 。如果 clone 遠(yuǎn)程倉庫需要權(quán)限驗(yàn)證的話,比如賬號密碼,驗(yàn)證信息需要通過 GTRepositoryCloneOptionsCredentialProvider 配置, 對應(yīng)的 value 是一個 GTCredentialProvider。
GTCredentialProvider *provider = [GTCredentialProvider providerWithBlock:^GTCredential * _Nullable(GTCredentialType type, NSString * _Nonnull URL, NSString * _Nonnull userName) {
GTCredential *cre = [GTCredential credentialWithUserName:@"username" password:@"password" error:NULL];
return cre;
}];
NSDictionary *cloneOptions = @{GTRepositoryCloneOptionsCredentialProvider: provider };
options 其底層對應(yīng)的是 git_clone_options 其他變量的說明在 libgit2/libgit2 的 clone.h 中。
commit
生成一個 commit 需要以下幾個步驟:
- 把文件加入 index
- 把 index 寫入 tree
- 用 tree 和 提交信息生成 commit
每一步的具體含義可以查閱 Pro Git。這里主要說下在 ObjectiveGit 中是怎樣操作的:
先要獲取倉庫的 index
GTIndex *index = [_repo indexWithError:&error];
然后把要提交的文件添加到 index。這里的 filePath 為文件相對于倉庫根目錄的路徑
[index addFile:filePath error:&error];
這里 add 之后只是在內(nèi)存中操作,還需要將 add 操作寫入本地
[index write:&error];
將 index 寫入 tree
GTTree *tree = [index writeTree:&error];
最后將生成 commit
GTSignature *sign = [[GTSignature alloc] initWithName:@"Test" email:@"441473064@qq.com" time:[NSDate date]];
[_repo createCommitWithTree:tree message:string author:sign committer:sign parents:parents updatingReferenceNamed:@"HEAD" error:&error];
這里面會有一個參數(shù) parents,如果是第一次提交的話 parents 是空的,但是不是第一次提交的話就是必填的,不然生成 commit 會報錯。
GTReference *ref = [_repo headReferenceWithError:&error];
NSMutableArray *parents = [NSMutableArray array];
if (ref) {
[parents addObject:ref.resolvedTarget];
}
這樣一個完整的 commit 就完成了