Skip to content

Commit c80f861

Browse files
author
Ben Chatelain
committed
Merge pull request #473 from libgit2/update-submodule-api
Update submodule ignore rule API
2 parents c6145b4 + 6d185a4 commit c80f861

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

ObjectiveGit/GTSubmodule.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ NS_ASSUME_NONNULL_BEGIN
6060
@property (nonatomic, strong, readonly) GTRepository *parentRepository;
6161

6262
/// The current ignore rule for this submodule.
63-
///
64-
/// Setting this property will only update the rule in memory, not on disk.
65-
@property (nonatomic, assign) GTSubmoduleIgnoreRule ignoreRule;
63+
@property (nonatomic, readonly, assign) GTSubmoduleIgnoreRule ignoreRule;
6664

6765
/// The OID that the submodule is pinned to in the parent repository's index.
6866
///
@@ -113,6 +111,16 @@ NS_ASSUME_NONNULL_BEGIN
113111
/// Returns whether reloading succeeded.
114112
- (BOOL)reload:(NSError **)error;
115113

114+
/// Write a new ignore rule to disk and get the resulting submodule. The
115+
/// receiver will not have the new ignore rule. To update the receiver, call
116+
/// `-reload:`.
117+
///
118+
/// ignoreRule - The ignore rule.
119+
/// error - The error if one occurred.
120+
///
121+
/// Returns the updated submodule or nil if an error occurred.
122+
- (nullable GTSubmodule *)submoduleByUpdatingIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error;
123+
116124
/// Synchronizes the submodule repository's configuration files with the settings
117125
/// from the parent repository.
118126
///
@@ -126,11 +134,17 @@ NS_ASSUME_NONNULL_BEGIN
126134
/// Returns the opened repository, or nil if an error occurs.
127135
- (nullable GTRepository *)submoduleRepository:(NSError **)error;
128136

129-
/// Determines the status for the submodule.
130-
///
131-
/// Returns the status, or `GTSubmoduleStatusUnknown` if an error occurs.
137+
/// Calls `-statusWithIgnoreRule:error:` with the submodule's ignore rule.
132138
- (GTSubmoduleStatus)status:(NSError **)error;
133139

140+
/// Determine the status for the submodule using the given ignore rule.
141+
///
142+
/// ignoreRule - The ignore rule to use in calculating status.
143+
/// error - The error if one occurred.
144+
///
145+
/// Returns the status or `GTSubmoduleStatusUnknown` if an error occurred.
146+
- (GTSubmoduleStatus)statusWithIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error;
147+
134148
/// Initializes the submodule by copying its information into the parent
135149
/// repository's `.git/config` file. This is equivalent to `git submodule init`
136150
/// on the command line.

ObjectiveGit/GTSubmodule.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ - (GTSubmoduleIgnoreRule)ignoreRule {
2525
return (GTSubmoduleIgnoreRule)git_submodule_ignore(self.git_submodule);
2626
}
2727

28-
- (void)setIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule {
29-
git_submodule_set_ignore(self.parentRepository.git_repository, git_submodule_name(self.git_submodule), (git_submodule_ignore_t)ignoreRule);
28+
- (GTSubmodule *)submoduleByUpdatingIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error {
29+
int result = git_submodule_set_ignore(self.parentRepository.git_repository, git_submodule_name(self.git_submodule), (git_submodule_ignore_t)ignoreRule);
30+
if (result != GIT_OK) {
31+
if (error != NULL) {
32+
*error = [NSError git_errorFor:result description:@"Couldn't set submodule ignore rule."];
33+
}
34+
return nil;
35+
}
3036

31-
// The docs for `git_submodule_set_ignore` note "This does not affect any
32-
// currently-loaded instances." So we need to reload.
33-
git_submodule_reload(self.git_submodule, 0);
37+
return [self.parentRepository submoduleWithName:self.name error:error];
3438
}
3539

3640
- (GTOID *)indexOID {
@@ -103,9 +107,9 @@ - (instancetype)initWithGitSubmodule:(git_submodule *)submodule parentRepository
103107

104108
#pragma mark Inspection
105109

106-
- (GTSubmoduleStatus)status:(NSError **)error {
110+
- (GTSubmoduleStatus)statusWithIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error {
107111
unsigned status;
108-
int gitError = git_submodule_status(&status, self.parentRepository.git_repository, git_submodule_name(self.git_submodule), git_submodule_ignore(self.git_submodule));
112+
int gitError = git_submodule_status(&status, self.parentRepository.git_repository, git_submodule_name(self.git_submodule), (git_submodule_ignore_t)ignoreRule);
109113
if (gitError != GIT_OK) {
110114
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get submodule %@ status.", self.name];
111115
return GTSubmoduleStatusUnknown;
@@ -114,6 +118,10 @@ - (GTSubmoduleStatus)status:(NSError **)error {
114118
return status;
115119
}
116120

121+
- (GTSubmoduleStatus)status:(NSError **)error {
122+
return [self statusWithIgnoreRule:self.ignoreRule error:error];
123+
}
124+
117125
#pragma mark Manipulation
118126

119127
- (BOOL)reload:(NSError **)error {
@@ -136,7 +144,7 @@ - (BOOL)sync:(NSError **)error {
136144
return YES;
137145
}
138146

139-
- (GTRepository *)submoduleRepository:(NSError **)error {
147+
- (nullable GTRepository *)submoduleRepository:(NSError **)error {
140148
git_repository *repo;
141149
int gitError = git_submodule_open(&repo, self.git_submodule);
142150
if (gitError != GIT_OK) {

ObjectiveGitTests/GTSubmoduleSpec.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@
9393
expect(@(success)).to(beTruthy());
9494
});
9595

96+
it(@"should update the ignore rule", ^{
97+
GTSubmodule *submodule = [repo submoduleWithName:@"Test_App" error:NULL];
98+
expect(submodule).notTo(beNil());
99+
expect(@(submodule.ignoreRule)).to(equal(@(GTSubmoduleIgnoreNone)));
100+
101+
GTSubmodule *updatedSubmodule = [submodule submoduleByUpdatingIgnoreRule:GTSubmoduleIgnoreAll error:NULL];
102+
expect(@(updatedSubmodule.ignoreRule)).to(equal(@(GTSubmoduleIgnoreAll)));
103+
expect(@(submodule.ignoreRule)).to(equal(@(GTSubmoduleIgnoreNone)));
104+
});
105+
96106
describe(@"clean, checked out submodule", ^{
97107
__block GTSubmodule *submodule;
98108

@@ -204,13 +214,11 @@
204214
});
205215

206216
it(@"should honor the ignore rule", ^{
207-
submodule.ignoreRule = GTSubmoduleIgnoreDirty;
208-
209217
GTSubmoduleStatus expectedStatus =
210218
GTSubmoduleStatusExistsInHEAD | GTSubmoduleStatusExistsInIndex | GTSubmoduleStatusExistsInConfig | GTSubmoduleStatusExistsInWorkingDirectory |
211219
GTSubmoduleStatusModifiedInIndex | GTSubmoduleStatusModifiedInWorkingDirectory;
212220

213-
expect(@([submodule status:NULL])).to(equal(@(expectedStatus)));
221+
expect(@([submodule statusWithIgnoreRule:GTSubmoduleIgnoreDirty error:NULL])).to(equal(@(expectedStatus)));
214222
});
215223

216224
it(@"should open a repository" ,^{

0 commit comments

Comments
 (0)