Skip to content

Update submodule ignore rule API #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions ObjectiveGit/GTSubmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong, readonly) GTRepository *parentRepository;

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

/// The OID that the submodule is pinned to in the parent repository's index.
///
Expand Down Expand Up @@ -111,6 +109,16 @@ NS_ASSUME_NONNULL_BEGIN
/// Returns whether reloading succeeded.
- (BOOL)reload:(NSError **)error;

/// Write a new ignore rule to disk and get the resulting submodule. The
/// receiver will not have the new ignore rule. To update the receiver, call
/// `-reload:`.
///
/// ignoreRule - The ignore rule.
/// error - The error if one occurred.
///
/// Returns the updated submodule or nil if an error occurred.
- (nullable GTSubmodule *)submoduleByUpdatingIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error;

/// Synchronizes the submodule repository's configuration files with the settings
/// from the parent repository.
///
Expand All @@ -124,11 +132,17 @@ NS_ASSUME_NONNULL_BEGIN
/// Returns the opened repository, or nil if an error occurs.
- (nullable GTRepository *)submoduleRepository:(NSError **)error;

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

/// Determine the status for the submodule using the given ignore rule.
///
/// ignoreRule - The ignore rule to use in calculating status.
/// error - The error if one occurred.
///
/// Returns the status or `GTSubmoduleStatusUnknown` if an error occurred.
- (GTSubmoduleStatus)statusWithIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error;

/// Initializes the submodule by copying its information into the parent
/// repository's `.git/config` file. This is equivalent to `git submodule init`
/// on the command line.
Expand Down
24 changes: 16 additions & 8 deletions ObjectiveGit/GTSubmodule.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ - (GTSubmoduleIgnoreRule)ignoreRule {
return (GTSubmoduleIgnoreRule)git_submodule_ignore(self.git_submodule);
}

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

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

- (GTOID *)indexOID {
Expand Down Expand Up @@ -98,9 +102,9 @@ - (instancetype)initWithGitSubmodule:(git_submodule *)submodule parentRepository

#pragma mark Inspection

- (GTSubmoduleStatus)status:(NSError **)error {
- (GTSubmoduleStatus)statusWithIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error {
unsigned status;
int gitError = git_submodule_status(&status, self.parentRepository.git_repository, git_submodule_name(self.git_submodule), git_submodule_ignore(self.git_submodule));
int gitError = git_submodule_status(&status, self.parentRepository.git_repository, git_submodule_name(self.git_submodule), (git_submodule_ignore_t)ignoreRule);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get submodule %@ status.", self.name];
return GTSubmoduleStatusUnknown;
Expand All @@ -109,6 +113,10 @@ - (GTSubmoduleStatus)status:(NSError **)error {
return status;
}

- (GTSubmoduleStatus)status:(NSError **)error {
return [self statusWithIgnoreRule:self.ignoreRule error:error];
}

#pragma mark Manipulation

- (BOOL)reload:(NSError **)error {
Expand All @@ -131,7 +139,7 @@ - (BOOL)sync:(NSError **)error {
return YES;
}

- (GTRepository *)submoduleRepository:(NSError **)error {
- (nullable GTRepository *)submoduleRepository:(NSError **)error {
git_repository *repo;
int gitError = git_submodule_open(&repo, self.git_submodule);
if (gitError != GIT_OK) {
Expand Down
14 changes: 11 additions & 3 deletions ObjectiveGitTests/GTSubmoduleSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@
expect(@(success)).to(beTruthy());
});

it(@"should update the ignore rule", ^{
GTSubmodule *submodule = [repo submoduleWithName:@"Test_App" error:NULL];
expect(submodule).notTo(beNil());
expect(@(submodule.ignoreRule)).to(equal(@(GTSubmoduleIgnoreNone)));

GTSubmodule *updatedSubmodule = [submodule submoduleByUpdatingIgnoreRule:GTSubmoduleIgnoreAll error:NULL];
expect(@(updatedSubmodule.ignoreRule)).to(equal(@(GTSubmoduleIgnoreAll)));
expect(@(submodule.ignoreRule)).to(equal(@(GTSubmoduleIgnoreNone)));
});

describe(@"clean, checked out submodule", ^{
__block GTSubmodule *submodule;

Expand Down Expand Up @@ -204,13 +214,11 @@
});

it(@"should honor the ignore rule", ^{
submodule.ignoreRule = GTSubmoduleIgnoreDirty;

GTSubmoduleStatus expectedStatus =
GTSubmoduleStatusExistsInHEAD | GTSubmoduleStatusExistsInIndex | GTSubmoduleStatusExistsInConfig | GTSubmoduleStatusExistsInWorkingDirectory |
GTSubmoduleStatusModifiedInIndex | GTSubmoduleStatusModifiedInWorkingDirectory;

expect(@([submodule status:NULL])).to(equal(@(expectedStatus)));
expect(@([submodule statusWithIgnoreRule:GTSubmoduleIgnoreDirty error:NULL])).to(equal(@(expectedStatus)));
});

it(@"should open a repository" ,^{
Expand Down