push
生成一個commit之后,接下來就可以推送到遠(yuǎn)端了。push之前需要獲取到遠(yuǎn)端倉庫信息。由于一個本地倉庫可以設(shè)置多個遠(yuǎn)端倉庫,這里默認(rèn)就取第一個遠(yuǎn)端倉庫。
GTConfiguration *conf = [_repo configurationWithError:NULL];
NSError *error = nil;
GTRemote *remote = [conf.remotes firstObject];
由于push操作是需要權(quán)限的,所以要配置用戶名和密碼。注意這里的key值和clone的時候不一樣。
GTCredentialProvider *provider = [GTCredentialProvider providerWithBlock:^GTCredential * _Nullable(GTCredentialType type, NSString * _Nonnull URL, NSString * _Nonnull userName) {
GTCredential *cre = [GTCredential credentialWithUserName:@"xporter" password:@"12345678" error:NULL];
return cre;
}];
NSDictionary *remoteOptions = @{GTRepositoryRemoteOptionsCredentialProvider:provider};
獲取分支信息
GTBranch *curretntBarch = [_repo currentBranchWithError:&error];
開始向遠(yuǎn)端推送
BOOL r = [_repo pushBranch:curretntBarch toRemote:remote withOptions:pushOptions error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL * _Nonnull stop) {
NSLog(@"%d/%d",current,total);
}];
如果沒有報錯的話就是推送成功了,可以到遠(yuǎn)端倉庫進(jìn)行驗證。
pull
要同步遠(yuǎn)端的指定分支的信息,就需要用到pull方法。先獲取需要更新的分支
GTBranch *brach = [_repo currentBranchWithError:NULL];
pull 也是需要權(quán)限信息的,要設(shè)置用戶信息
GTCredentialProvider *provider = [GTCredentialProvider providerWithBlock:^GTCredential * _Nullable(GTCredentialType type, NSString * _Nonnull URL, NSString * _Nonnull userName) {
GTCredential *cre = [GTCredential credentialWithUserName:@"xporter" password:@"12345678" error:NULL];
return cre;
}];
NSDictionary *remoteOptions = @{GTRepositoryRemoteOptionsCredentialProvider:provider};
獲取遠(yuǎn)端分支信息
GTConfiguration *conf = [_repo configurationWithError:NULL];
NSError *error = nil;
GTRemote *remote = [conf.remotes firstObject];
拉取遠(yuǎn)端信息
BOOL r = [_repo pullBranch:brach fromRemote:remote withOptions:pullOptions error:&error progress:^(const git_transfer_progress * _Nonnull progress, BOOL * _Nonnull stop) {
}];