Skip to content

Commit bf24a8d

Browse files
committed
Merge pull request #392 from libgit2/attributes
Attributes
2 parents b017543 + 99f3a7f commit bf24a8d

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

Classes/GTRepository+Attributes.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// GTRepository+Attributes.h
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Josh Abernathy on 7/25/14.
6+
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import "GTRepository.h"
10+
11+
@interface GTRepository (Attributes)
12+
13+
/// Look up the value for the attribute of the given name for the given path.
14+
///
15+
/// name - The name of the attribute to look up. Cannot be nil.
16+
/// path - The path to use for the lookup. Cannot be nil.
17+
///
18+
/// Returns the value of the attribute or nil.
19+
- (NSString *)attributeWithName:(NSString *)name path:(NSString *)path;
20+
21+
@end

Classes/GTRepository+Attributes.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// GTRepository+Attributes.m
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Josh Abernathy on 7/25/14.
6+
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import "GTRepository+Attributes.h"
10+
#import "NSError+Git.h"
11+
12+
@implementation GTRepository (Attributes)
13+
14+
- (NSString *)attributeWithName:(NSString *)name path:(NSString *)path {
15+
NSParameterAssert(name != nil);
16+
NSParameterAssert(path != nil);
17+
18+
const char *val = NULL;
19+
git_attr_get(&val, self.git_repository, GIT_ATTR_CHECK_FILE_THEN_INDEX, path.UTF8String, name.UTF8String);
20+
if (val == NULL) return nil;
21+
22+
return @(val);
23+
}
24+
25+
@end

Classes/ObjectiveGit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "git2.h"
2727

2828
#import <ObjectiveGit/GTRepository.h>
29+
#import <ObjectiveGit/GTRepository+Attributes.h>
2930
#import <ObjectiveGit/GTRepository+Stashing.h>
3031
#import <ObjectiveGit/GTRepository+Committing.h>
3132
#import <ObjectiveGit/GTRepository+Status.h>

ObjectiveGitFramework.xcodeproj/project.pbxproj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@
217217
88BC0E5218EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */; };
218218
88BC0E5318EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */; };
219219
88C0BC5917038CF3009E99AA /* GTConfigurationSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */; };
220+
88E353001982E9160051001F /* GTRepository+Attributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 88E352FE1982E9160051001F /* GTRepository+Attributes.h */; settings = {ATTRIBUTES = (Public, ); }; };
221+
88E353011982E9160051001F /* GTRepository+Attributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 88E352FE1982E9160051001F /* GTRepository+Attributes.h */; settings = {ATTRIBUTES = (Public, ); }; };
222+
88E353021982E9160051001F /* GTRepository+Attributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E352FF1982E9160051001F /* GTRepository+Attributes.m */; };
223+
88E353031982E9160051001F /* GTRepository+Attributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E352FF1982E9160051001F /* GTRepository+Attributes.m */; };
224+
88E353061982EA6B0051001F /* GTRepositoryAttributesSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E353051982EA6B0051001F /* GTRepositoryAttributesSpec.m */; };
220225
88EB7E4D14AEBA600046FEA4 /* GTConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
221226
88EB7E4E14AEBA600046FEA4 /* GTConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */; };
222227
88F05A9E16011F6E00B7AD1D /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* ObjectiveGit.framework */; };
@@ -303,7 +308,6 @@
303308
DD3D9520182AB3C4004AF532 /* GTBlameHunk.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3D951B182AB25C004AF532 /* GTBlameHunk.m */; };
304309
E9FFC6BF1577CC8300A9E736 /* GTConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */; };
305310
E9FFC6C01577CC8A00A9E736 /* GTConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
306-
F60A4A0D197FA491007CC0B0 /* NSData+Git.m in Sources */ = {isa = PBXBuildFile; fileRef = BD6C2267131459E700992935 /* NSData+Git.m */; };
307311
F6ED8DA1180E713200A32D40 /* GTRemoteSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = F6ED8DA0180E713200A32D40 /* GTRemoteSpec.m */; };
308312
/* End PBXBuildFile section */
309313

@@ -457,6 +461,9 @@
457461
88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Reset.h"; sourceTree = "<group>"; };
458462
88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Reset.m"; sourceTree = "<group>"; };
459463
88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfigurationSpec.m; sourceTree = "<group>"; };
464+
88E352FE1982E9160051001F /* GTRepository+Attributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Attributes.h"; sourceTree = "<group>"; };
465+
88E352FF1982E9160051001F /* GTRepository+Attributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Attributes.m"; sourceTree = "<group>"; };
466+
88E353051982EA6B0051001F /* GTRepositoryAttributesSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTRepositoryAttributesSpec.m; sourceTree = "<group>"; };
460467
88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTConfiguration.h; sourceTree = "<group>"; };
461468
88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfiguration.m; sourceTree = "<group>"; };
462469
88F05A6B16011E5400B7AD1D /* ObjectiveGitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ObjectiveGitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -747,6 +754,7 @@
747754
D0751CD818BE520400134314 /* GTFilterListSpec.m */,
748755
88234B2518F2FE260039972E /* GTRepositoryResetSpec.m */,
749756
8870390A1975E3F2004118D7 /* GTDiffDeltaSpec.m */,
757+
88E353051982EA6B0051001F /* GTRepositoryAttributesSpec.m */,
750758
);
751759
path = ObjectiveGitTests;
752760
sourceTree = "<group>";
@@ -845,6 +853,8 @@
845853
886E622918AEBF75000611A0 /* GTFilterSource.m */,
846854
D0CE551E18B6C58F008EB8E0 /* GTFilterList.h */,
847855
D0CE551F18B6C58F008EB8E0 /* GTFilterList.m */,
856+
88E352FE1982E9160051001F /* GTRepository+Attributes.h */,
857+
88E352FF1982E9160051001F /* GTRepository+Attributes.m */,
848858
);
849859
path = Classes;
850860
sourceTree = "<group>";
@@ -957,6 +967,7 @@
957967
04DB464F133AB5E200D9C624 /* GTRepository.h in Headers */,
958968
306123AD17EA5261006591D4 /* EXTScope.h in Headers */,
959969
04DB4652133AB5E200D9C624 /* GTEnumerator.h in Headers */,
970+
88E353011982E9160051001F /* GTRepository+Attributes.h in Headers */,
960971
04DB4653133AB5E200D9C624 /* GTTree.h in Headers */,
961972
5BE612891745EE3400266D8C /* GTTreeBuilder.h in Headers */,
962973
D021DF501806899200934E32 /* NSArray+StringArray.h in Headers */,
@@ -1032,6 +1043,7 @@
10321043
55C8057D13875C11004DCB0F /* NSData+Git.h in Headers */,
10331044
D03B57A118BFFF07007124F4 /* GTDiffPatch.h in Headers */,
10341045
883CD6AB1600EBC600F57354 /* GTRemote.h in Headers */,
1046+
88E353001982E9160051001F /* GTRepository+Attributes.h in Headers */,
10351047
55C8057E13875C1B004DCB0F /* NSString+Git.h in Headers */,
10361048
30A3D6541667F11C00C49A39 /* GTDiff.h in Headers */,
10371049
3011D86B1668E48500CE3409 /* GTDiffFile.h in Headers */,
@@ -1236,6 +1248,7 @@
12361248
buildActionMask = 2147483647;
12371249
files = (
12381250
6A74CA3816A9432A00E1A3C5 /* ObjectiveGit.m in Sources */,
1251+
88E353031982E9160051001F /* GTRepository+Attributes.m in Sources */,
12391252
886E622D18AEBF75000611A0 /* GTFilterSource.m in Sources */,
12401253
6A74CA3416A942AA00E1A3C5 /* NSData+Git.m in Sources */,
12411254
6A74CA3216A9429700E1A3C5 /* GTRemote.m in Sources */,
@@ -1318,6 +1331,7 @@
13181331
8832811F173D8816006D7DCF /* GTIndexSpec.m in Sources */,
13191332
D0F4E28A17C7F24200BBDE30 /* NSErrorGitSpec.m in Sources */,
13201333
88328128173D8A64006D7DCF /* GTTreeSpec.m in Sources */,
1334+
88E353061982EA6B0051001F /* GTRepositoryAttributesSpec.m in Sources */,
13211335
88234B2618F2FE260039972E /* GTRepositoryResetSpec.m in Sources */,
13221336
5BE612931745EEBC00266D8C /* GTTreeBuilderSpec.m in Sources */,
13231337
D06D9E011755D10000558C17 /* GTEnumeratorSpec.m in Sources */,
@@ -1335,6 +1349,7 @@
13351349
buildActionMask = 2147483647;
13361350
files = (
13371351
BDE4C065130EFE2C00851650 /* NSError+Git.m in Sources */,
1352+
88E353021982E9160051001F /* GTRepository+Attributes.m in Sources */,
13381353
BDE4C067130EFE2C00851650 /* GTRepository.m in Sources */,
13391354
BDD8AE7013131B8800CB5D40 /* GTEnumerator.m in Sources */,
13401355
BD6C235313146E6600992935 /* GTCommit.m in Sources */,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// GTRepositoryAttributesSpec.m
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Josh Abernathy on 7/25/14.
6+
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import "GTRepository+Attributes.h"
10+
11+
SpecBegin(GTRepositoryAttributes)
12+
13+
__block GTRepository *repository;
14+
15+
beforeEach(^{
16+
repository = [self blankFixtureRepository];
17+
});
18+
19+
it(@"should be able to look up attributes", ^{
20+
static NSString * const testAttributes = @"*.txt filter=reverse";
21+
NSURL *attributesURL = [repository.fileURL URLByAppendingPathComponent:@".gitattributes"];
22+
BOOL success = [testAttributes writeToURL:attributesURL atomically:YES encoding:NSUTF8StringEncoding error:NULL];
23+
expect(success).to.beTruthy();
24+
25+
NSString *value = [repository attributeWithName:@"filter" path:@"*.txt"];
26+
expect(value).to.equal(@"reverse");
27+
28+
value = [repository attributeWithName:@"filter" path:@"thing.txt"];
29+
expect(value).to.equal(@"reverse");
30+
31+
value = [repository attributeWithName:@"filter" path:@"thing.jpg"];
32+
expect(value).to.beNil();
33+
});
34+
35+
SpecEnd

0 commit comments

Comments
 (0)