Skip to content

Commit 5420cd2

Browse files
committed
Merge pull request libgit2#188 from libgit2/thread-safe
Thread safety
2 parents 03b017f + d379cbb commit 5420cd2

29 files changed

+396
-649
lines changed

Classes/GTBranch.m

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@
3030
#import "GTCommit.h"
3131
#import "NSError+Git.h"
3232

33-
34-
@interface GTBranch ()
35-
@property (nonatomic, strong) GTReference *reference;
36-
@property (nonatomic, strong) GTRepository *repository;
37-
@end
38-
3933
@implementation GTBranch
4034

4135
- (NSString *)description {
@@ -56,9 +50,6 @@ - (NSUInteger)hash {
5650

5751
#pragma mark API
5852

59-
@synthesize reference;
60-
@synthesize repository;
61-
6253
+ (NSString *)localNamePrefix {
6354
return @"refs/heads/";
6455
}
@@ -76,22 +67,25 @@ + (id)branchWithReference:(GTReference *)ref repository:(GTRepository *)repo {
7667
}
7768

7869
- (id)initWithName:(NSString *)branchName repository:(GTRepository *)repo error:(NSError **)error {
79-
if((self = [super init])) {
80-
self.reference = [GTReference referenceByLookingUpReferencedNamed:branchName inRepository:repo error:error];
81-
if(self.reference == nil) {
82-
return nil;
83-
}
84-
85-
self.repository = repo;
86-
}
87-
return self;
70+
NSParameterAssert(branchName != nil);
71+
NSParameterAssert(repo != nil);
72+
73+
GTReference *ref = [GTReference referenceByLookingUpReferencedNamed:branchName inRepository:repo error:error];
74+
if (ref == nil) return nil;
75+
76+
return [self initWithReference:ref repository:repo];
8877
}
8978

9079
- (id)initWithReference:(GTReference *)ref repository:(GTRepository *)repo {
91-
if((self = [super init])) {
92-
self.reference = ref;
93-
self.repository = repo;
94-
}
80+
NSParameterAssert(ref != nil);
81+
NSParameterAssert(repo != nil);
82+
83+
self = [super init];
84+
if (self == nil) return nil;
85+
86+
_repository = repo;
87+
_reference = ref;
88+
9589
return self;
9690
}
9791

@@ -100,8 +94,6 @@ - (NSString *)name {
10094
}
10195

10296
- (NSString *)shortName {
103-
if (![self.reference isValid]) return nil;
104-
10597
const char *name;
10698
int gitError = git_branch_name(&name, self.reference.git_reference);
10799
if (gitError != GIT_OK) return nil;
@@ -122,7 +114,7 @@ - (NSString *)sha {
122114
}
123115

124116
- (NSString *)remoteName {
125-
if (self.branchType == GTBranchTypeLocal || ![self.reference isValid]) return nil;
117+
if (self.branchType == GTBranchTypeLocal) return nil;
126118

127119
const char *name;
128120
int gitError = git_branch_name(&name, self.reference.git_reference);
@@ -145,7 +137,7 @@ - (GTCommit *)targetCommitAndReturnError:(NSError **)error {
145137
}
146138

147139
- (NSUInteger)numberOfCommitsWithError:(NSError **)error {
148-
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:repository error:error];
140+
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self.repository error:error];
149141
if (enumerator == nil) return NSNotFound;
150142

151143
if (![enumerator pushSHA:self.sha error:error]) return NSNotFound;
@@ -181,19 +173,12 @@ - (NSArray *)uniqueCommitsRelativeToBranch:(GTBranch *)otherBranch error:(NSErro
181173
}
182174

183175
- (BOOL)deleteWithError:(NSError **)error {
184-
if (!self.reference.valid) {
185-
if (error != NULL) *error = GTReference.invalidReferenceError;
186-
return NO;
187-
}
188-
189176
int gitError = git_branch_delete(self.reference.git_reference);
190-
if(gitError != GIT_OK) {
177+
if (gitError != GIT_OK) {
191178
if(error != NULL) *error = [NSError git_errorFor:gitError withAdditionalDescription:@"Failed to delete branch."];
192179
return NO;
193180
}
194-
195-
self.reference = nil;
196-
181+
197182
return YES;
198183
}
199184

@@ -203,12 +188,6 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success
203188
return self;
204189
}
205190

206-
if (!self.reference.valid) {
207-
if (success != NULL) *success = NO;
208-
if (error != NULL) *error = GTReference.invalidReferenceError;
209-
return NO;
210-
}
211-
212191
git_reference *trackingRef = NULL;
213192
int gitError = git_branch_upstream(&trackingRef, self.reference.git_reference);
214193

Classes/GTCommit.m

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@
3535
#import "NSString+Git.h"
3636
#import "NSDate+GTTimeAdditions.h"
3737

38-
@interface GTCommit ()
39-
@property (nonatomic, strong) GTSignature *author;
40-
@property (nonatomic, strong) GTSignature *committer;
41-
@property (nonatomic, copy) NSArray *parents;
42-
@end
43-
44-
4538
@implementation GTCommit
4639

4740
- (NSString *)description {
@@ -133,18 +126,11 @@ - (NSTimeZone *)commitTimeZone {
133126
}
134127

135128
- (GTSignature *)author {
136-
if (_author == nil) {
137-
_author = [[GTSignature alloc] initWithGitSignature:git_commit_author(self.git_commit)];
138-
}
139-
140-
return _author;
129+
return [[GTSignature alloc] initWithGitSignature:git_commit_author(self.git_commit)];
141130
}
142131

143132
- (GTSignature *)committer {
144-
if (_committer == nil) {
145-
_committer = [[GTSignature alloc] initWithGitSignature:git_commit_committer(self.git_commit)];
146-
}
147-
return _committer;
133+
return [[GTSignature alloc] initWithGitSignature:git_commit_committer(self.git_commit)];
148134
}
149135

150136
- (GTTree *)tree {
@@ -160,22 +146,18 @@ - (GTTree *)tree {
160146
}
161147

162148
- (NSArray *)parents {
163-
if(_parents == nil) {
164-
unsigned int numberOfParents = git_commit_parentcount(self.git_commit);
165-
NSMutableArray *parents = [NSMutableArray arrayWithCapacity:numberOfParents];
166-
167-
for (unsigned int i = 0; i < numberOfParents; i++) {
168-
git_commit *parent = NULL;
169-
int parentResult = git_commit_parent(&parent, self.git_commit, i);
170-
if (parentResult != GIT_OK) continue;
171-
172-
[parents addObject:(GTCommit *)[GTObject objectWithObj:(git_object *)parent inRepository:self.repository]];
173-
}
174-
175-
_parents = [parents copy];
176-
}
149+
unsigned numberOfParents = git_commit_parentcount(self.git_commit);
150+
NSMutableArray *parents = [NSMutableArray arrayWithCapacity:numberOfParents];
177151

178-
return _parents;
152+
for (unsigned i = 0; i < numberOfParents; i++) {
153+
git_commit *parent = NULL;
154+
int parentResult = git_commit_parent(&parent, self.git_commit, i);
155+
if (parentResult != GIT_OK) continue;
156+
157+
[parents addObject:(GTCommit *)[GTObject objectWithObj:(git_object *)parent inRepository:self.repository]];
158+
}
159+
160+
return parents;
179161
}
180162

181163
@end

Classes/GTConfiguration+Private.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
@interface GTConfiguration ()
1414

15-
@property (nonatomic, readwrite, weak) GTRepository *repository;
16-
1715
// Initializes the receiver.
1816
//
1917
// config - The libgit2 config. Cannot be NULL.

Classes/GTConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@interface GTConfiguration : NSObject
1515

1616
@property (nonatomic, readonly, assign) git_config *git_config;
17-
@property (nonatomic, readonly, weak) GTRepository *repository;
17+
@property (nonatomic, readonly, strong) GTRepository *repository;
1818
@property (nonatomic, readonly, copy) NSArray *configurationKeys;
1919

2020
// The GTRemotes in the config. If the configuration isn't associated with any

Classes/GTConfiguration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ @implementation GTConfiguration
1818
#pragma mark Lifecycle
1919

2020
- (void)dealloc {
21-
git_config_free(self.git_config);
21+
git_config_free(_git_config);
2222
}
2323

2424
- (id)initWithGitConfig:(git_config *)config repository:(GTRepository *)repository {

Classes/GTIndex.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@
3030
#include "git2.h"
3131

3232
@class GTIndexEntry;
33+
@class GTRepository;
3334

3435
@interface GTIndex : NSObject
3536

37+
// The repository in which the index resides. This may be nil if the index was
38+
// created with -initWithFileURL:error:.
39+
@property (nonatomic, readonly, strong) GTRepository *repository;
40+
3641
// The underlying libgit2 index.
3742
@property (nonatomic, readonly) git_index *git_index;
3843

@@ -55,11 +60,12 @@
5560

5661
// Initializes the receiver with the given libgit2 index.
5762
//
58-
// index - The libgit2 index from which the index should be created. Cannot be
59-
// NULL.
63+
// index - The libgit2 index from which the index should be created. Cannot
64+
// be NULL.
65+
// repository - The repository in which the index resides. Cannot be nil.
6066
//
6167
// Returns the initialized object.
62-
- (id)initWithGitIndex:(git_index *)index;
68+
- (id)initWithGitIndex:(git_index *)index repository:(GTRepository *)repository;
6369

6470
// Refresh the index from the datastore
6571
//

Classes/GTIndex.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ - (id)initWithFileURL:(NSURL *)fileURL error:(NSError **)error {
6464
return self;
6565
}
6666

67-
- (id)initWithGitIndex:(git_index *)index {
67+
- (id)initWithGitIndex:(git_index *)index repository:(GTRepository *)repository {
6868
NSParameterAssert(index != NULL);
69+
NSParameterAssert(repository != nil);
6970

7071
self = [super init];
7172
if (self == nil) return nil;
7273

7374
_git_index = index;
75+
_repository = repository;
7476

7577
return self;
7678
}

Classes/GTIndexEntry.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#import "NSError+Git.h"
3232
#import "NSString+Git.h"
3333

34-
3534
@implementation GTIndexEntry
3635

3736
#pragma mark NSObject

Classes/GTObject.m

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
#import "GTBlob.h"
3838
#import "GTTag.h"
3939

40-
@interface GTObject ()
41-
@property (nonatomic, assign) git_object *git_object;
42-
@end
43-
44-
4540
@implementation GTObject
4641

4742
- (NSString *)description {
@@ -65,13 +60,16 @@ - (BOOL)isEqual:(id)otherObject {
6560

6661
#pragma mark API
6762

68-
@synthesize git_object;
63+
- (id)initWithObj:(git_object *)object inRepository:(GTRepository *)repo {
64+
NSParameterAssert(object != NULL);
65+
NSParameterAssert(repo != nil);
66+
67+
self = [super init];
68+
if (self == nil) return nil;
69+
70+
_repository = repo;
71+
_git_object = object;
6972

70-
- (id)initWithObj:(git_object *)theObject inRepository:(GTRepository *)theRepo {
71-
if((self = [super init])) {
72-
_repository = theRepo;
73-
self.git_object = theObject;
74-
}
7573
return self;
7674
}
7775

@@ -112,7 +110,10 @@ - (NSString *)shortSha {
112110
}
113111

114112
- (GTOdbObject *)odbObjectWithError:(NSError **)error {
115-
return [self.repository.objectDatabase objectWithOid:git_object_id(self.git_object) error:error];
113+
GTObjectDatabase *database = [self.repository objectDatabaseWithError:error];
114+
if (database == nil) return nil;
115+
116+
return [database objectWithOid:git_object_id(self.git_object) error:error];
116117
}
117118

118119
@end

Classes/GTObjectDatabase.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@
2828

2929
@interface GTObjectDatabase : NSObject
3030

31-
@property (nonatomic, assign) git_odb *git_odb;
32-
@property (nonatomic, readonly, weak) GTRepository *repository;
31+
@property (nonatomic, readonly, assign) git_odb *git_odb;
32+
@property (nonatomic, readonly, strong) GTRepository *repository;
3333

34-
+ (id)objectDatabaseWithRepository:(GTRepository *)repository;
35-
- (id)initWithRepository:(GTRepository *)repository;
34+
// Initializes the object database with the given repository.
35+
//
36+
// repo - The repository from which the object database should be created.
37+
// Cannot be nil.
38+
// error - The error if one occurred.
39+
//
40+
// Returns the initialized object.
41+
- (id)initWithRepository:(GTRepository *)repo error:(NSError **)error;
3642

3743
- (GTOdbObject *)objectWithOid:(const git_oid *)oid error:(NSError **)error;
3844
- (GTOdbObject *)objectWithSha:(NSString *)sha error:(NSError **)error;

0 commit comments

Comments
 (0)