Skip to content

Commit 07d8c3f

Browse files
committed
Move analyse to GTRepository+Merging
1 parent 3821527 commit 07d8c3f

File tree

4 files changed

+52
-51
lines changed

4 files changed

+52
-51
lines changed

ObjectiveGit/GTRepository+Merging.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@
77
//
88

99
#import "GTRepository.h"
10+
#import "git2/merge.h"
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

14+
/// UserInfo key for conflicted files when pulling fails with a merge conflict
15+
extern NSString * const GTPullMergeConflictedFiles;
16+
17+
/// An enum describing the result of the merge analysis.
18+
/// See `git_merge_analysis_t`.
19+
typedef NS_OPTIONS(NSInteger, GTMergeAnalysis) {
20+
GTMergeAnalysisNone = GIT_MERGE_ANALYSIS_NONE,
21+
GTMergeAnalysisNormal = GIT_MERGE_ANALYSIS_NORMAL,
22+
GTMergeAnalysisUpToDate = GIT_MERGE_ANALYSIS_UP_TO_DATE,
23+
GTMergeAnalysisUnborn = GIT_MERGE_ANALYSIS_UNBORN,
24+
GTMergeAnalysisFastForward = GIT_MERGE_ANALYSIS_FASTFORWARD,
25+
};
26+
1327
@interface GTRepository (Merging)
1428

1529
/// Enumerate all available merge head entries.
@@ -29,6 +43,16 @@ NS_ASSUME_NONNULL_BEGIN
2943
/// Retruns a (possibly empty) array with GTCommit objects. Will not be nil.
3044
- (NSArray <GTCommit *>*)mergeHeadEntriesWithError:(NSError **)error;
3145

46+
/// Analyze which merge to perform.
47+
///
48+
/// analysis - The resulting analysis.
49+
/// fromBranch - The branch to merge from.
50+
/// error - The error if one occurred. Can be NULL.
51+
///
52+
/// Returns YES if the analysis was successful, NO otherwise (and `error`, if provided,
53+
/// will point to an error describing what happened).
54+
- (BOOL)analyzeMerge:(GTMergeAnalysis *)analysis fromBranch:(GTBranch *)fromBranch error:(NSError **)error;
55+
3256
@end
3357

3458
NS_ASSUME_NONNULL_END

ObjectiveGit/GTRepository+Merging.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,32 @@ - (NSArray *)mergeHeadEntriesWithError:(NSError **)error {
6767
return entries;
6868
}
6969

70+
- (BOOL)analyzeMerge:(GTMergeAnalysis *)analysis fromBranch:(GTBranch *)fromBranch error:(NSError **)error {
71+
NSParameterAssert(analysis != NULL);
72+
NSParameterAssert(fromBranch != nil);
73+
74+
GTCommit *fromCommit = [fromBranch targetCommitWithError:error];
75+
if (!fromCommit) {
76+
return NO;
77+
}
78+
79+
git_annotated_commit *annotatedCommit;
80+
[self annotatedCommit:&annotatedCommit fromCommit:fromCommit error:error];
81+
82+
// Allow fast-forward or normal merge
83+
git_merge_preference_t preference = GIT_MERGE_PREFERENCE_NONE;
84+
85+
// Merge analysis
86+
int gitError = git_merge_analysis((git_merge_analysis_t *)analysis, &preference, self.git_repository, (const git_annotated_commit **) &annotatedCommit, 1);
87+
if (gitError != GIT_OK) {
88+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to analyze merge"];
89+
return NO;
90+
}
91+
92+
// Cleanup
93+
git_annotated_commit_free(annotatedCommit);
94+
95+
return YES;
96+
}
97+
7098
@end

ObjectiveGit/GTRepository+Pull.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,9 @@
77
//
88

99
#import "GTRepository.h"
10-
#import "git2/merge.h"
1110

1211
NS_ASSUME_NONNULL_BEGIN
1312

14-
/// UserInfo key for conflicted files when pulling fails with a merge conflict
15-
extern NSString * const GTPullMergeConflictedFiles;
16-
17-
/// An enum describing the result of the merge analysis.
18-
/// See `git_merge_analysis_t`.
19-
typedef NS_OPTIONS(NSInteger, GTMergeAnalysis) {
20-
GTMergeAnalysisNone = GIT_MERGE_ANALYSIS_NONE,
21-
GTMergeAnalysisNormal = GIT_MERGE_ANALYSIS_NORMAL,
22-
GTMergeAnalysisUpToDate = GIT_MERGE_ANALYSIS_UP_TO_DATE,
23-
GTMergeAnalysisUnborn = GIT_MERGE_ANALYSIS_UNBORN,
24-
GTMergeAnalysisFastForward = GIT_MERGE_ANALYSIS_FASTFORWARD,
25-
};
26-
2713
typedef void (^GTRemoteFetchTransferProgressBlock)(const git_transfer_progress *progress, BOOL *stop);
2814

2915
@interface GTRepository (Pull)
@@ -44,16 +30,6 @@ typedef void (^GTRemoteFetchTransferProgressBlock)(const git_transfer_progress *
4430
/// will point to an error describing what happened).
4531
- (BOOL)pullBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:(nullable NSDictionary *)options error:(NSError **)error progress:(nullable GTRemoteFetchTransferProgressBlock)progressBlock;
4632

47-
/// Analyze which merge to perform.
48-
///
49-
/// analysis - The resulting analysis.
50-
/// fromBranch - The branch to merge from.
51-
/// error - The error if one occurred. Can be NULL.
52-
///
53-
/// Returns YES if the analysis was successful, NO otherwise (and `error`, if provided,
54-
/// will point to an error describing what happened).
55-
- (BOOL)analyzeMerge:(GTMergeAnalysis *)analysis fromBranch:(GTBranch *)fromBranch error:(NSError **)error;
56-
5733
@end
5834

5935
NS_ASSUME_NONNULL_END

ObjectiveGit/GTRepository+Pull.m

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,33 +163,6 @@ - (BOOL)annotatedCommit:(git_annotated_commit **)annotatedCommit fromCommit:(GTC
163163

164164
return YES;
165165
}
166-
167-
- (BOOL)analyzeMerge:(GTMergeAnalysis *)analysis fromBranch:(GTBranch *)fromBranch error:(NSError **)error {
168-
NSParameterAssert(analysis != NULL);
169-
NSParameterAssert(fromBranch != nil);
170-
171-
GTCommit *fromCommit = [fromBranch targetCommitWithError:error];
172-
if (!fromCommit) {
173-
return NO;
174-
}
175-
176-
git_annotated_commit *annotatedCommit;
177-
[self annotatedCommit:&annotatedCommit fromCommit:fromCommit error:error];
178-
179-
// Allow fast-forward or normal merge
180-
git_merge_preference_t preference = GIT_MERGE_PREFERENCE_NONE;
181-
182-
// Merge analysis
183-
int gitError = git_merge_analysis((git_merge_analysis_t *)analysis, &preference, self.git_repository, (const git_annotated_commit **) &annotatedCommit, 1);
184-
if (gitError != GIT_OK) {
185-
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to analyze merge"];
186-
return NO;
187-
}
188-
189-
// Cleanup
190-
git_annotated_commit_free(annotatedCommit);
191-
192-
return YES;
193166
}
194167

195168
@end

0 commit comments

Comments
 (0)