Skip to content

Commit db3a254

Browse files
committed
Merge pull request #224 from libgit2/fetch
Fetch support.
2 parents f522cab + 7160ff1 commit db3a254

18 files changed

+823
-65
lines changed

Classes/Categories/NSError+Git.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,22 @@ extern NSString * const GTGitErrorDomain;
6868
/// Returns a non-nil NSError.
6969
+ (NSError *)git_errorFor:(int)code description:(NSString *)desc, ... NS_FORMAT_FUNCTION(2, 3);
7070

71+
72+
/// Describes the given libgit2 error code, using `desc` as the error's
73+
/// description, and a failure reason from `reason` and the arguments that
74+
/// follow.
75+
///
76+
/// The created error will also have an `NSUnderlyingErrorKey` that contains the
77+
/// result of +git_errorFor: on the same error code.
78+
///
79+
/// code - The error code returned from libgit2.
80+
/// desc - The description to use in the created NSError. This may be nil.
81+
/// userInfo - A dictionary of additional values to insert in the NSError userInfo.
82+
/// This may be nil.
83+
/// reason - A format string to use for the created NSError's failure reason.
84+
/// This may be nil.
85+
/// ... - Format arguments to insert into `reason`.
86+
///
87+
/// Returns a non-nil NSError.
88+
+ (NSError *)git_errorFor:(int)code description:(NSString *)desc userInfo:(NSDictionary *)userInfo failureReason:(NSString *)reason, ... NS_FORMAT_FUNCTION(4, 5);
7189
@end

Classes/Categories/NSError+Git.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ + (NSError *)git_errorFor:(int)code description:(NSString *)desc failureReason:(
6767
return [NSError errorWithDomain:GTGitErrorDomain code:code userInfo:userInfo];
6868
}
6969

70+
+ (NSError *)git_errorFor:(int)code description:(NSString *)desc userInfo:(NSDictionary *)additionalUserInfo failureReason:(NSString *)reason, ... {
71+
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:additionalUserInfo];
72+
73+
if (desc != nil) userInfo[NSLocalizedDescriptionKey] = desc;
74+
if (reason != nil) {
75+
va_list args;
76+
va_start(args, reason);
77+
78+
NSString *formattedReason = [[NSString alloc] initWithFormat:reason arguments:args];
79+
va_end(args);
80+
81+
userInfo[NSLocalizedFailureReasonErrorKey] = formattedReason;
82+
}
83+
84+
NSError *underError = [self git_errorFor:code];
85+
if (underError != nil) userInfo[NSUnderlyingErrorKey] = underError;
86+
87+
return [NSError errorWithDomain:GTGitErrorDomain code:code userInfo:userInfo];
88+
}
89+
7090
+ (NSError *)git_errorFor:(int)code {
7191
NSDictionary *userInfo = nil;
7292

Classes/GTBranch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
@class GTCommit;
2929
@class GTReference;
30+
@class GTRemote;
3031
@class GTRepository;
3132

3233
typedef NS_ENUM(NSInteger, GTBranchType) {

Classes/GTBranch.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "GTEnumerator.h"
2929
#import "GTRepository.h"
3030
#import "GTCommit.h"
31+
#import "GTRemote.h"
3132
#import "NSError+Git.h"
3233

3334
@implementation GTBranch

Classes/GTConfiguration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ - (NSArray *)remotes {
128128
git_remote *remote = NULL;
129129

130130
if (git_remote_load(&remote, repository.git_repository, name) == 0) {
131-
[remotes addObject:[[GTRemote alloc] initWithGitRemote:remote]];
131+
[remotes addObject:[[GTRemote alloc] initWithGitRemote:remote inRepository:repository]];
132132
}
133133
}
134134

Classes/GTFetchHeadEntry.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// GTFetchHeadEntry.h
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Pablo Bendersky on 8/14/14.
6+
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@class GTRepository;
12+
@class GTOID;
13+
@class GTReference;
14+
15+
/// A class representing an entry on the FETCH_HEAD file, as returned by the callback of git_repository_fetchhead_foreach.
16+
@interface GTFetchHeadEntry : NSObject
17+
18+
/// The reference of this fetch entry.
19+
@property (nonatomic, readonly, strong) GTReference *reference;
20+
21+
/// The remote URL where this entry was originally fetched from.
22+
@property (nonatomic, readonly, copy) NSString *remoteURLString;
23+
24+
/// The target OID of this fetch entry (what we need to merge with)
25+
@property (nonatomic, readonly, copy) GTOID *targetOID;
26+
27+
/// Flag indicating if we need to merge this entry or not.
28+
@property (nonatomic, getter = isMerge, readonly) BOOL merge;
29+
30+
/// Initializes a GTFetchHeadEntry.
31+
///
32+
/// reference - Reference on the repository. Cannot be nil.
33+
/// remoteURLString - URL String where this was originally fetched from. Cannot be nil.
34+
/// targetOID - Target OID. Cannot be nil.
35+
/// merge - Indicates if this is pending a merge.
36+
- (instancetype)initWithReference:(GTReference *)reference remoteURLString:(NSString *)remoteURLString targetOID:(GTOID *)targetOID isMerge:(BOOL)merge;
37+
38+
@end

Classes/GTFetchHeadEntry.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// GTFetchHeadEntry.m
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Pablo Bendersky on 8/14/14.
6+
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import "GTFetchHeadEntry.h"
10+
#import "GTOID.h"
11+
12+
@implementation GTFetchHeadEntry
13+
14+
- (instancetype)initWithReference:(GTReference *)reference remoteURLString:(NSString *)remoteURLString targetOID:(GTOID *)targetOID isMerge:(BOOL)merge {
15+
NSParameterAssert(reference != nil);
16+
NSParameterAssert(remoteURLString != nil);
17+
NSParameterAssert(targetOID != nil);
18+
19+
self = [super init];
20+
if (self == nil) return nil;
21+
22+
_reference = reference;
23+
_remoteURLString = [remoteURLString copy];
24+
_targetOID = [targetOID copy];
25+
_merge = merge;
26+
27+
return self;
28+
}
29+
30+
#pragma mark NSObject
31+
32+
- (NSString *)description {
33+
return [NSString stringWithFormat:@"<%@: %p>{ reference: %@, remoteURL: %@, targetOID: %@, merge: %i }", self.class, self, self.reference, self.remoteURLString, self.targetOID, (int)self.merge];
34+
}
35+
36+
@end

Classes/GTRemote.h

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,106 @@
88

99
#import "git2.h"
1010

11+
@class GTRepository;
12+
@class GTOID;
13+
@class GTReference;
14+
@class GTCredentialProvider;
15+
16+
extern NSString * const GTRemoteRenameProblematicRefSpecs;
17+
18+
// Auto Tag settings. See `git_remote_autotag_option_t`.
19+
typedef enum {
20+
GTRemoteDownloadTagsAuto = GIT_REMOTE_DOWNLOAD_TAGS_AUTO,
21+
GTRemoteDownloadTagsNone = GIT_REMOTE_DOWNLOAD_TAGS_NONE,
22+
GTRemoteDownloadTagsAll = GIT_REMOTE_DOWNLOAD_TAGS_ALL,
23+
} GTRemoteAutoTagOption;
24+
1125
/// A class representing a remote for a git repository.
1226
///
13-
/// Analagous to `git_remote` in libgit2.
27+
/// Analogous to `git_remote` in libgit2.
1428
@interface GTRemote : NSObject
1529

16-
/// Initializes a new GTRemote to represent an underlying `git_remote`.
17-
///
18-
/// remote - The underlying `git_remote` object.
19-
- (id)initWithGitRemote:(git_remote *)remote;
20-
21-
/// The underlying `git_remote` object.
22-
- (git_remote *)git_remote __attribute__((objc_returns_inner_pointer));
30+
/// The repository owning this remote.
31+
@property (nonatomic, readonly, strong) GTRepository *repository;
2332

2433
/// The name of the remote.
2534
@property (nonatomic, readonly, copy) NSString *name;
2635

2736
/// The URL string for the remote.
2837
@property (nonatomic, readonly, copy) NSString *URLString;
2938

39+
/// The push URL for the remote, if provided.
40+
@property (nonatomic, copy) NSString *pushURLString;
41+
42+
/// Whether the remote is connected or not.
43+
@property (nonatomic, readonly, getter=isConnected) BOOL connected;
44+
45+
/// Whether the remote updates FETCH_HEAD when fetched.
46+
/// Defaults to YES.
47+
@property (nonatomic) BOOL updatesFetchHead;
48+
49+
/// The auto-tag setting for the remote.
50+
@property (nonatomic) GTRemoteAutoTagOption autoTag;
51+
3052
/// The fetch refspecs for this remote.
3153
///
3254
/// This array will contain NSStrings of the form
3355
/// `+refs/heads/*:refs/remotes/REMOTE/*`.
3456
@property (nonatomic, readonly, copy) NSArray *fetchRefspecs;
3557

58+
/// The push refspecs for this remote.
59+
///
60+
/// This array will contain NSStrings of the form
61+
/// `+refs/heads/*:refs/remotes/REMOTE/*`.
62+
@property (nonatomic, readonly, copy) NSArray *pushRefspecs;
63+
64+
/// Tests if a URL is supported (e.g. it's a supported URL scheme)
65+
+ (BOOL)isSupportedURLString:(NSString *)URLString;
66+
67+
/// Tests if a URL is valid (e.g. it actually makes sense as a URL)
68+
+ (BOOL)isValidURLString:(NSString *)URLString;
69+
70+
/// Tests if a name is valid
71+
+ (BOOL)isValidRemoteName:(NSString *)name;
72+
73+
/// Create a new remote in a repository.
74+
///
75+
/// name - The name for the new remote. Cannot be nil.
76+
/// URLString - The origin URL for the remote. Cannot be nil.
77+
/// repo - The repository the remote should be created in. Cannot be nil.
78+
/// error - Will be set if an error occurs.
79+
///
80+
/// Returns a new remote, or nil if an error occurred
81+
+ (instancetype)createRemoteWithName:(NSString *)name URLString:(NSString *)URLString inRepository:(GTRepository *)repo error:(NSError **)error;
82+
83+
/// Load a remote from a repository.
84+
///
85+
/// name - The name for the new remote. Cannot be nil.
86+
/// repo - The repository the remote should be looked up in. Cannot be nil.
87+
/// error - Will be set if an error occurs.
88+
///
89+
/// Returns the loaded remote, or nil if an error occurred.
90+
+ (instancetype)remoteWithName:(NSString *)name inRepository:(GTRepository *)repo error:(NSError **)error;
91+
92+
/// Initialize a remote from a `git_remote`.
93+
///
94+
/// remote - The underlying `git_remote` object. Cannot be nil.
95+
/// repo - The repository the remote belongs to. Cannot be nil.
96+
- (instancetype)initWithGitRemote:(git_remote *)remote inRepository:(GTRepository *)repo;
97+
98+
/// The underlying `git_remote` object.
99+
- (git_remote *)git_remote __attribute__((objc_returns_inner_pointer));
100+
101+
/// Rename the remote.
102+
///
103+
/// name - The new name for the remote. Cannot be nil.
104+
/// error - Will be set if an error occurs. If there was an error renaming some
105+
/// refspecs, their names will be available as an arry under the
106+
/// `GTRemoteRenameProblematicRefSpecs` key.
107+
///
108+
/// Return YES if successful, NO otherwise.
109+
- (BOOL)rename:(NSString *)name error:(NSError **)error;
110+
36111
/// Updates the URL string for this remote.
37112
///
38113
/// URLString - The URLString to update to. May not be nil.

0 commit comments

Comments
 (0)