ObjectiveGit(一)clone & commit

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 需要以下幾個步驟:

  1. 把文件加入 index
  2. 把 index 寫入 tree
  3. 用 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 就完成了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 輸入 y ,創(chuàng)建 git_hug 目錄No githug directory found, do you wish...
    風(fēng)花花閱讀 2,116評論 0 4
  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git,從https:/...
    落魂灬閱讀 12,813評論 4 54
  • 前些天,有個朋友這樣問我 倩:明明,晚上值夜班的時候值班室里你開幾個燈? 我:基本全開,我在更衣室睡覺的時...
    趙瑤岑閱讀 3,505評論 1 6
  • 不知道是這個世界本就無趣,還是我這個人枯燥乏味,愈發(fā)覺得體內(nèi)的快樂基因已經(jīng)被壓縮榨干,逐漸轉(zhuǎn)化成多愁善感分子滲入骨...
    聽見魚說閱讀 346評論 0 2

友情鏈接更多精彩內(nèi)容