Skip to content

Commit 5bf9d5f

Browse files
committed
Corrections per @tiennou feedback
1 parent 2fea0a5 commit 5bf9d5f

File tree

5 files changed

+55
-75
lines changed

5 files changed

+55
-75
lines changed

ObjectiveGit/GTNote.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ NS_ASSUME_NONNULL_BEGIN
6969
/// oid - git_oid of the target to attach to
7070
/// repository - Repository containing the target OID refers to
7171
/// referenceName - Name for the notes reference in the repo, or NULL for default ("refs/notes/commits")
72-
/// error - Will be filled with a git error code in case of error.
73-
/// May be NULL.
7472
///
75-
/// Returns initialized GTNote instance or nil on failure (error will be populated, if passed).
76-
- (nullable instancetype)initWithTargetGitOID:(git_oid *)oid repository:(git_repository *)repository referenceName:(const char * _Nullable)referenceName error:(int * _Nullable)error;
73+
/// Returns initialized GTNote instance or nil on failure.
74+
- (nullable instancetype)initWithTargetGitOID:(git_oid *)oid repository:(git_repository *)repository referenceName:(const char * _Nullable)referenceName;
7775

7876
- (instancetype)init NS_UNAVAILABLE;
7977

ObjectiveGit/GTNote.m

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,25 @@ - (GTOID *)targetOID {
5858
}
5959

6060
- (instancetype)initWithTargetOID:(GTOID *)oid repository:(GTRepository *)repository referenceName:(NSString *)referenceName error:(NSError **)error {
61-
int err = GIT_OK;
61+
self = [super init];
62+
if (self == nil) return nil;
6263

63-
id object = [self initWithTargetGitOID:(git_oid *)oid.git_oid repository:repository.git_repository referenceName:referenceName.UTF8String error:&err];
64+
int gitErr = git_note_read(&_note, repository.git_repository, referenceName.UTF8String, oid.git_oid);
6465

65-
if (err != GIT_OK && error != NULL) {
66-
*error = [NSError git_errorFor:err description:@"Failed to create a note."];
67-
}
66+
if (gitErr != GIT_OK && error != NULL) *error = [NSError git_errorFor:gitErr description:@"Failed to read a note."];
6867

69-
return object;
68+
if (gitErr != GIT_OK) return nil;
69+
70+
return self;
7071
}
7172

72-
- (instancetype)initWithTargetGitOID:(git_oid *)oid repository:(git_repository *)repository referenceName:(const char *)referenceName error:(int *)error {
73+
- (instancetype)initWithTargetGitOID:(git_oid *)oid repository:(git_repository *)repository referenceName:(const char *)referenceName {
7374
self = [super init];
7475
if (self == nil) return nil;
7576

7677
int gitErr = git_note_read(&_note, repository, referenceName, oid);
7778

78-
if (gitErr != GIT_OK) {
79-
if (error != NULL) *error = gitErr;
80-
81-
return nil;
82-
}
79+
if (gitErr != GIT_OK) return nil;
8380

8481
return self;
8582
}
@@ -95,21 +92,15 @@ + (NSString *)defaultReferenceNameForRepository:(GTRepository *)repository error
9592
git_buf default_ref_name = { 0 };
9693
int gitErr = git_note_default_ref(&default_ref_name, repository.git_repository);
9794
if (gitErr != GIT_OK) {
98-
git_buf_free(&default_ref_name);
99-
100-
if (error != NULL) {
101-
*error = [NSError git_errorFor:gitErr description:@"Unable to get default git notes reference name"];
102-
}
95+
if (error != NULL) *error = [NSError git_errorFor:gitErr description:@"Unable to get default git notes reference name"];
10396

10497
return nil;
10598
}
10699

107100
if (default_ref_name.ptr != NULL) {
108101
noteRef = @(default_ref_name.ptr);
109102
} else {
110-
if (error != NULL) {
111-
*error = [NSError git_errorFor:GIT_ERROR description:@"Unable to get default git notes reference name"];
112-
}
103+
if (error != NULL) *error = [NSError git_errorFor:GIT_ERROR description:@"Unable to get default git notes reference name"];
113104
}
114105

115106
git_buf_free(&default_ref_name);

ObjectiveGit/GTRepository+RemoteOperations.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ extern NSString *const GTRepositoryRemoteOptionsFetchPrune;
2222
/// A `GTRemoteAutoTagOption`, that will be used to determine how the fetch should handle tags.
2323
extern NSString *const GTRepositoryRemoteOptionsDownloadTags;
2424

25+
/// A `@(BOOL)`, indicating git notes should also be pushed to the default notes reference name (if `@(YES)`), or an `NSArray(NSString)` containing reference names to be pushed through.
26+
extern NSString *const GTRepositoryRemoteOptionsPushNotes;
27+
2528
/// An enum describing the data needed for pruning.
2629
/// See `git_fetch_prune_t`.
2730
typedef NS_ENUM(NSInteger, GTFetchPruneOption) {
@@ -76,6 +79,7 @@ typedef NS_ENUM(NSInteger, GTFetchPruneOption) {
7679
/// options - Options applied to the push operation. Can be NULL.
7780
/// Recognized options are:
7881
/// `GTRepositoryRemoteOptionsCredentialProvider`
82+
/// `GTRepositoryRemoteOptionsPushNotes` (to push together with notes in one push)
7983
/// error - The error if one occurred. Can be NULL.
8084
/// progressBlock - An optional callback for monitoring progress. May be NULL.
8185
///
@@ -89,34 +93,15 @@ typedef NS_ENUM(NSInteger, GTFetchPruneOption) {
8993
/// remote - The remote to push to. Must not be nil.
9094
/// options - Options applied to the push operation. Can be NULL.
9195
/// Recognized options are:
92-
/// `GTRepositoryRemoteOptionsCredentialProvider`
96+
/// `GTRepositoryRemoteOptionsCredentialProvider`,
97+
/// `GTRepositoryRemoteOptionsPushNotes` (to push together with notes in one push)
9398
/// error - The error if one occurred. Can be NULL.
9499
/// progressBlock - An optional callback for monitoring progress. May be NULL.
95100
///
96101
/// Returns YES if the push was successful, NO otherwise (and `error`, if provided,
97102
/// will point to an error describing what happened).
98103
- (BOOL)pushBranches:(NSArray<GTBranch *> *)branches toRemote:(GTRemote *)remote withOptions:(nullable NSDictionary *)options error:(NSError **)error progress:(nullable void (^)(unsigned int current, unsigned int total, size_t bytes, BOOL *stop))progressBlock;
99104

100-
/// Push an array of branches to a remote, together with notes (in one push).
101-
/// Normally, if you use Git notes functionality, to push the notes to remote, you would have to
102-
/// do two operations: pushBranches followed by pushNotes. This is unrational, so we offer you a handy
103-
/// shortcut that allows to push the branches together with the notes.
104-
///
105-
/// branches - An array of branches to push. Must not be nil.
106-
/// remote - The remote to push to. Must not be nil.
107-
/// options - Options applied to the push operation. Can be NULL.
108-
/// Recognized options are:
109-
/// `GTRepositoryRemoteOptionsCredentialProvider`
110-
/// referenceName - Reference name for notes, if they should be pushed together with the branches.
111-
/// Use +[GTNote defaultReferenceNameWithError:] to push the default note reference.
112-
/// Passing NULL here will make notes NOT to be pushed.
113-
/// error - The error if one occurred. Can be NULL.
114-
/// progressBlock - An optional callback for monitoring progress. May be NULL.
115-
///
116-
/// Returns YES if the push was successful, NO otherwise (and `error`, if provided,
117-
/// will point to an error describing what happened).
118-
- (BOOL)pushBranches:(NSArray<GTBranch *> *)branches toRemote:(GTRemote *)remote withOptions:(nullable NSDictionary *)options withNotesReferenceName:(nullable NSString *)notesReferenceName error:(NSError **)error progress:(nullable void (^)(unsigned int current, unsigned int total, size_t bytes, BOOL *stop))progressBlock;
119-
120105
/// Push a given Git notes reference name to a remote.
121106
///
122107
/// noteReferenceName - Name of the notes reference. If NULL, will default to whatever the default is (e.g. "refs/notes/commits")

ObjectiveGit/GTRepository+RemoteOperations.m

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
NSString *const GTRepositoryRemoteOptionsCredentialProvider = @"GTRepositoryRemoteOptionsCredentialProvider";
2929
NSString *const GTRepositoryRemoteOptionsFetchPrune = @"GTRepositoryRemoteOptionsFetchPrune";
3030
NSString *const GTRepositoryRemoteOptionsDownloadTags = @"GTRepositoryRemoteOptionsDownloadTags";
31+
NSString *const GTRepositoryRemoteOptionsPushNotes = @"GTRepositoryRemoteOptionsPushNotes";
3132

3233
typedef void (^GTRemoteFetchTransferProgressBlock)(const git_transfer_progress *stats, BOOL *stop);
3334
typedef void (^GTRemotePushTransferProgressBlock)(unsigned int current, unsigned int total, size_t bytes, BOOL *stop);
@@ -175,10 +176,6 @@ - (BOOL)pushBranch:(GTBranch *)branch toRemote:(GTRemote *)remote withOptions:(N
175176
}
176177

177178
- (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
178-
return [self pushBranches:branches toRemote:remote withOptions:options withNotesReferenceName:nil error:error progress:progressBlock];
179-
}
180-
181-
- (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options withNotesReferenceName:(NSString *)referenceName error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
182179
NSParameterAssert(branches != nil);
183180
NSParameterAssert(branches.count != 0);
184181
NSParameterAssert(remote != nil);
@@ -202,13 +199,27 @@ - (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions
202199
[refspecs addObject:[NSString stringWithFormat:@"refs/heads/%@:%@", branch.shortName, remoteBranchReference]];
203200
}
204201

205-
// Also push the notes reference, if needed.
206-
if (referenceName != nil) {
207-
// but check whether the reference exists for the repo, otherwise, our push will fail
208-
GTReference *notesRef = [self lookUpReferenceWithName:referenceName error:nil];
209-
210-
if (notesRef != nil) {
211-
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", referenceName, referenceName]];
202+
// Also push the notes reference(s), if needed.
203+
id pushNotesOption = options[GTRepositoryRemoteOptionsPushNotes];
204+
if (pushNotesOption != nil) {
205+
if ([pushNotesOption isKindOfClass:[NSNumber class]]) { // Push notes is a bool, only push the default reference name if it's YES
206+
if ([(NSNumber *)pushNotesOption boolValue]) {
207+
NSString *notesReferenceName = [GTNote defaultReferenceNameForRepository:self error:nil];
208+
209+
// Check whether the reference name exists for the repo, or our push will fail
210+
if (notesReferenceName != nil && [self lookUpReferenceWithName:notesReferenceName error:nil] != nil) {
211+
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", notesReferenceName, notesReferenceName]];
212+
}
213+
}
214+
} else if ([pushNotesOption isKindOfClass:[NSArray class]]) {
215+
for (NSString *notesReferenceName in (NSArray *)pushNotesOption) {
216+
if ([notesReferenceName isKindOfClass:[NSString class]]) { // Just a sanity check, we only accept NSStrings in the array
217+
// Check whether the reference name exists for the repo, or our push will fail
218+
if (notesReferenceName != nil && [self lookUpReferenceWithName:notesReferenceName error:nil] != nil) {
219+
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", notesReferenceName, notesReferenceName]];
220+
}
221+
}
222+
}
212223
}
213224
}
214225

ObjectiveGit/GTRepository.m

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#import "GTRepository+References.h"
5555
#import "GTNote.h"
5656

57+
#import "EXTScope.h"
58+
5759
#import "git2.h"
5860

5961
NSString * const GTRepositoryCloneOptionsBare = @"GTRepositoryCloneOptionsBare";
@@ -974,38 +976,36 @@ - (BOOL)removeNoteFromObject:(GTObject *)parentObject referenceName:(NSString *)
974976
return YES;
975977
}
976978

977-
- (BOOL)enumerateNotesWithReferenceName:(NSString *)referenceName error:(NSError **)error usingBlock:(void (^)(GTNote *note, GTObject *object, BOOL *stop))block
978-
{
979+
- (BOOL)enumerateNotesWithReferenceName:(NSString *)referenceName error:(NSError **)error usingBlock:(void (^)(GTNote *note, GTObject *object, BOOL *stop))block {
979980
git_note_iterator *iter = NULL;
980981

981982
int gitError = git_note_iterator_new(&iter, self.git_repository, referenceName.UTF8String);
982983

983984
if (gitError != GIT_OK)
984985
{
985-
if(error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to enumerate notes"];
986+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to enumerate notes"];
986987
return NO;
987988
}
988989

990+
@onExit {
991+
git_note_iterator_free(iter);
992+
};
993+
989994
git_oid note_id;
990995
git_oid object_id;
991996
BOOL success = YES;
992997
int iterError = GIT_OK;
993998

994999
while ((iterError = git_note_next(&note_id, &object_id, iter)) == GIT_OK) {
995-
NSError* lookupErr = nil;
1000+
NSError *lookupErr = nil;
9961001

997-
GTNote* note = [[GTNote alloc] initWithTargetGitOID:&object_id repository:self.git_repository referenceName:referenceName.UTF8String error:&gitError];
998-
if (note == nil) {
999-
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Cannot find a note"];
1000-
success = NO;
1001-
break;
1002-
}
1002+
GTNote *note = [[GTNote alloc] initWithTargetOID:[GTOID oidWithGitOid:&object_id] repository:self referenceName:referenceName error:error];
1003+
if (note == nil) return NO;
10031004

1004-
GTObject* obj = [self lookUpObjectByGitOid:&object_id error:&lookupErr];
1005+
GTObject *obj = [self lookUpObjectByGitOid:&object_id error:&lookupErr];
10051006
if (obj == nil && lookupErr != nil) {
10061007
if (error != NULL) *error = lookupErr;
1007-
success = NO;
1008-
break;
1008+
return NO;
10091009
}
10101010

10111011
BOOL stop = NO;
@@ -1015,12 +1015,7 @@ - (BOOL)enumerateNotesWithReferenceName:(NSString *)referenceName error:(NSError
10151015
}
10161016
}
10171017

1018-
git_note_iterator_free(iter);
1019-
1020-
if (iterError != GIT_OK && iterError != GIT_ITEROVER) {
1021-
if (error != NULL)
1022-
*error = [NSError git_errorFor:iterError description:@"Iterator error"];
1023-
}
1018+
if (iterError != GIT_OK && iterError != GIT_ITEROVER && error != NULL) *error = [NSError git_errorFor:iterError description:@"Iterator error"];
10241019

10251020
return success;
10261021
}

0 commit comments

Comments
 (0)