Skip to content

Commit d9965ae

Browse files
committed
Merge pull request libgit2#339 from libgit2/submodule-index-add
Submodule index add
2 parents 7cde267 + 7494ae1 commit d9965ae

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Classes/GTIndex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111

112112
// Add an entry to the index.
113113
//
114+
// Note that this *cannot* add submodules. See -[GTSubmodule addToIndex:].
115+
//
114116
// entry - The entry to add.
115117
// error - The error if one occurred.
116118
//
@@ -120,6 +122,8 @@
120122
// Add an entry (by relative path) to the index.
121123
// Will fail if the receiver's repository is nil.
122124
//
125+
// Note that this *cannot* add submodules. See -[GTSubmodule addToIndex:].
126+
//
123127
// file - The path (relative to the root of the repository) of the file to add.
124128
// error - The error if one occurred.
125129
//

Classes/GTSubmodule.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,13 @@ typedef enum {
135135
// Returns whether the initialization succeeded.
136136
- (BOOL)writeToParentConfigurationDestructively:(BOOL)overwrite error:(NSError **)error;
137137

138+
/// Add the current HEAD to the parent repository's index.
139+
///
140+
/// Note that it does *not* write the index.
141+
///
142+
/// error - The error if one occurred.
143+
///
144+
/// Returns whether the add was successful.
145+
- (BOOL)addToIndex:(NSError **)error;
146+
138147
@end

Classes/GTSubmodule.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ - (BOOL)writeToParentConfigurationDestructively:(BOOL)overwrite error:(NSError *
140140
return YES;
141141
}
142142

143+
- (BOOL)addToIndex:(NSError **)error {
144+
int gitError = git_submodule_add_to_index(self.git_submodule, 0);
145+
if (gitError != GIT_OK) {
146+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to add submodule %@ to its parent's index.", self.name];
147+
return NO;
148+
}
149+
150+
return YES;
151+
}
152+
143153
#pragma mark NSObject
144154

145155
- (NSString *)description {

ObjectiveGitTests/GTSubmoduleSpec.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@
9999
expect(submodule.path).to.equal(@"new_submodule_path");
100100
});
101101

102+
it(@"should add its HEAD to its parent's index", ^{
103+
GTSubmodule *submodule = [repo submoduleWithName:@"Test_App" error:NULL];
104+
expect(submodule).notTo.beNil();
105+
106+
GTRepository *submoduleRepository = [[GTRepository alloc] initWithURL:[repo.fileURL URLByAppendingPathComponent:submodule.path] error:NULL];
107+
expect(submoduleRepository).notTo.beNil();
108+
109+
GTCommit *commit = [submoduleRepository lookUpObjectByRevParse:@"HEAD^" error:NULL];
110+
BOOL success = [submoduleRepository checkoutCommit:commit strategy:GTCheckoutStrategyForce error:NULL progressBlock:nil];
111+
expect(success).to.beTruthy();
112+
113+
success = [submodule addToIndex:NULL];
114+
expect(success).to.beTruthy();
115+
});
116+
102117
describe(@"clean, checked out submodule", ^{
103118
__block GTSubmodule *submodule;
104119

0 commit comments

Comments
 (0)