From ae6e1d17dd41b856a687e6ab30a92e29a9559045 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 25 Jul 2014 15:51:27 -0400 Subject: [PATCH 1/4] Added -attributeWithName:path:. --- Classes/GTRepository+Attributes.h | 21 ++++++++++++++++++ Classes/GTRepository+Attributes.m | 22 +++++++++++++++++++ Classes/ObjectiveGit.h | 1 + .../project.pbxproj | 13 ++++++++++- 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Classes/GTRepository+Attributes.h create mode 100644 Classes/GTRepository+Attributes.m diff --git a/Classes/GTRepository+Attributes.h b/Classes/GTRepository+Attributes.h new file mode 100644 index 000000000..8f214505f --- /dev/null +++ b/Classes/GTRepository+Attributes.h @@ -0,0 +1,21 @@ +// +// GTRepository+Attributes.h +// ObjectiveGitFramework +// +// Created by Josh Abernathy on 7/25/14. +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// + +#import "GTRepository.h" + +@interface GTRepository (Attributes) + +/// Look up the value for the attribute of the given name for the given path. +/// +/// name - The name of the attribute to look up. Cannot be nil. +/// path - The path to use for the lookup. Cannot be nil. +/// +/// Returns the value of the attribute or nil. +- (NSString *)attributeWithName:(NSString *)name path:(NSString *)path; + +@end diff --git a/Classes/GTRepository+Attributes.m b/Classes/GTRepository+Attributes.m new file mode 100644 index 000000000..d77acb288 --- /dev/null +++ b/Classes/GTRepository+Attributes.m @@ -0,0 +1,22 @@ +// +// GTRepository+Attributes.m +// ObjectiveGitFramework +// +// Created by Josh Abernathy on 7/25/14. +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// + +#import "GTRepository+Attributes.h" +#import "NSError+Git.h" + +@implementation GTRepository (Attributes) + +- (NSString *)attributeWithName:(NSString *)name path:(NSString *)path { + const char *val = NULL; + git_attr_get(&val, self.git_repository, 0, path.UTF8String, name.UTF8String); + if (val == NULL) return nil; + + return @(val); +} + +@end diff --git a/Classes/ObjectiveGit.h b/Classes/ObjectiveGit.h index c5ffec585..f2bca21a7 100644 --- a/Classes/ObjectiveGit.h +++ b/Classes/ObjectiveGit.h @@ -26,6 +26,7 @@ #import "git2.h" #import +#import #import #import #import diff --git a/ObjectiveGitFramework.xcodeproj/project.pbxproj b/ObjectiveGitFramework.xcodeproj/project.pbxproj index 63c74fd8d..062a02fdb 100644 --- a/ObjectiveGitFramework.xcodeproj/project.pbxproj +++ b/ObjectiveGitFramework.xcodeproj/project.pbxproj @@ -217,6 +217,10 @@ 88BC0E5218EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */; }; 88BC0E5318EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */; }; 88C0BC5917038CF3009E99AA /* GTConfigurationSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */; }; + 88E353001982E9160051001F /* GTRepository+Attributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 88E352FE1982E9160051001F /* GTRepository+Attributes.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 88E353011982E9160051001F /* GTRepository+Attributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 88E352FE1982E9160051001F /* GTRepository+Attributes.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 88E353021982E9160051001F /* GTRepository+Attributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E352FF1982E9160051001F /* GTRepository+Attributes.m */; }; + 88E353031982E9160051001F /* GTRepository+Attributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E352FF1982E9160051001F /* GTRepository+Attributes.m */; }; 88EB7E4D14AEBA600046FEA4 /* GTConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 88EB7E4E14AEBA600046FEA4 /* GTConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */; }; 88F05A9E16011F6E00B7AD1D /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* ObjectiveGit.framework */; }; @@ -303,7 +307,6 @@ DD3D9520182AB3C4004AF532 /* GTBlameHunk.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3D951B182AB25C004AF532 /* GTBlameHunk.m */; }; E9FFC6BF1577CC8300A9E736 /* GTConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */; }; E9FFC6C01577CC8A00A9E736 /* GTConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; - F60A4A0D197FA491007CC0B0 /* NSData+Git.m in Sources */ = {isa = PBXBuildFile; fileRef = BD6C2267131459E700992935 /* NSData+Git.m */; }; F6ED8DA1180E713200A32D40 /* GTRemoteSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = F6ED8DA0180E713200A32D40 /* GTRemoteSpec.m */; }; /* End PBXBuildFile section */ @@ -457,6 +460,8 @@ 88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Reset.h"; sourceTree = ""; }; 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Reset.m"; sourceTree = ""; }; 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfigurationSpec.m; sourceTree = ""; }; + 88E352FE1982E9160051001F /* GTRepository+Attributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Attributes.h"; sourceTree = ""; }; + 88E352FF1982E9160051001F /* GTRepository+Attributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Attributes.m"; sourceTree = ""; }; 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTConfiguration.h; sourceTree = ""; }; 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfiguration.m; sourceTree = ""; }; 88F05A6B16011E5400B7AD1D /* ObjectiveGitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ObjectiveGitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -845,6 +850,8 @@ 886E622918AEBF75000611A0 /* GTFilterSource.m */, D0CE551E18B6C58F008EB8E0 /* GTFilterList.h */, D0CE551F18B6C58F008EB8E0 /* GTFilterList.m */, + 88E352FE1982E9160051001F /* GTRepository+Attributes.h */, + 88E352FF1982E9160051001F /* GTRepository+Attributes.m */, ); path = Classes; sourceTree = ""; @@ -957,6 +964,7 @@ 04DB464F133AB5E200D9C624 /* GTRepository.h in Headers */, 306123AD17EA5261006591D4 /* EXTScope.h in Headers */, 04DB4652133AB5E200D9C624 /* GTEnumerator.h in Headers */, + 88E353011982E9160051001F /* GTRepository+Attributes.h in Headers */, 04DB4653133AB5E200D9C624 /* GTTree.h in Headers */, 5BE612891745EE3400266D8C /* GTTreeBuilder.h in Headers */, D021DF501806899200934E32 /* NSArray+StringArray.h in Headers */, @@ -1032,6 +1040,7 @@ 55C8057D13875C11004DCB0F /* NSData+Git.h in Headers */, D03B57A118BFFF07007124F4 /* GTDiffPatch.h in Headers */, 883CD6AB1600EBC600F57354 /* GTRemote.h in Headers */, + 88E353001982E9160051001F /* GTRepository+Attributes.h in Headers */, 55C8057E13875C1B004DCB0F /* NSString+Git.h in Headers */, 30A3D6541667F11C00C49A39 /* GTDiff.h in Headers */, 3011D86B1668E48500CE3409 /* GTDiffFile.h in Headers */, @@ -1236,6 +1245,7 @@ buildActionMask = 2147483647; files = ( 6A74CA3816A9432A00E1A3C5 /* ObjectiveGit.m in Sources */, + 88E353031982E9160051001F /* GTRepository+Attributes.m in Sources */, 886E622D18AEBF75000611A0 /* GTFilterSource.m in Sources */, 6A74CA3416A942AA00E1A3C5 /* NSData+Git.m in Sources */, 6A74CA3216A9429700E1A3C5 /* GTRemote.m in Sources */, @@ -1335,6 +1345,7 @@ buildActionMask = 2147483647; files = ( BDE4C065130EFE2C00851650 /* NSError+Git.m in Sources */, + 88E353021982E9160051001F /* GTRepository+Attributes.m in Sources */, BDE4C067130EFE2C00851650 /* GTRepository.m in Sources */, BDD8AE7013131B8800CB5D40 /* GTEnumerator.m in Sources */, BD6C235313146E6600992935 /* GTCommit.m in Sources */, From 74eb2d496f10629e68c46724a31afc4c23dc1663 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 25 Jul 2014 15:51:34 -0400 Subject: [PATCH 2/4] Test it. --- .../project.pbxproj | 4 +++ .../GTRepositoryAttributesSpec.m | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 ObjectiveGitTests/GTRepositoryAttributesSpec.m diff --git a/ObjectiveGitFramework.xcodeproj/project.pbxproj b/ObjectiveGitFramework.xcodeproj/project.pbxproj index 062a02fdb..b4fabca27 100644 --- a/ObjectiveGitFramework.xcodeproj/project.pbxproj +++ b/ObjectiveGitFramework.xcodeproj/project.pbxproj @@ -221,6 +221,7 @@ 88E353011982E9160051001F /* GTRepository+Attributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 88E352FE1982E9160051001F /* GTRepository+Attributes.h */; settings = {ATTRIBUTES = (Public, ); }; }; 88E353021982E9160051001F /* GTRepository+Attributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E352FF1982E9160051001F /* GTRepository+Attributes.m */; }; 88E353031982E9160051001F /* GTRepository+Attributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E352FF1982E9160051001F /* GTRepository+Attributes.m */; }; + 88E353061982EA6B0051001F /* GTRepositoryAttributesSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88E353051982EA6B0051001F /* GTRepositoryAttributesSpec.m */; }; 88EB7E4D14AEBA600046FEA4 /* GTConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 88EB7E4E14AEBA600046FEA4 /* GTConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */; }; 88F05A9E16011F6E00B7AD1D /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* ObjectiveGit.framework */; }; @@ -462,6 +463,7 @@ 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfigurationSpec.m; sourceTree = ""; }; 88E352FE1982E9160051001F /* GTRepository+Attributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Attributes.h"; sourceTree = ""; }; 88E352FF1982E9160051001F /* GTRepository+Attributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Attributes.m"; sourceTree = ""; }; + 88E353051982EA6B0051001F /* GTRepositoryAttributesSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTRepositoryAttributesSpec.m; sourceTree = ""; }; 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTConfiguration.h; sourceTree = ""; }; 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfiguration.m; sourceTree = ""; }; 88F05A6B16011E5400B7AD1D /* ObjectiveGitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ObjectiveGitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -752,6 +754,7 @@ D0751CD818BE520400134314 /* GTFilterListSpec.m */, 88234B2518F2FE260039972E /* GTRepositoryResetSpec.m */, 8870390A1975E3F2004118D7 /* GTDiffDeltaSpec.m */, + 88E353051982EA6B0051001F /* GTRepositoryAttributesSpec.m */, ); path = ObjectiveGitTests; sourceTree = ""; @@ -1328,6 +1331,7 @@ 8832811F173D8816006D7DCF /* GTIndexSpec.m in Sources */, D0F4E28A17C7F24200BBDE30 /* NSErrorGitSpec.m in Sources */, 88328128173D8A64006D7DCF /* GTTreeSpec.m in Sources */, + 88E353061982EA6B0051001F /* GTRepositoryAttributesSpec.m in Sources */, 88234B2618F2FE260039972E /* GTRepositoryResetSpec.m in Sources */, 5BE612931745EEBC00266D8C /* GTTreeBuilderSpec.m in Sources */, D06D9E011755D10000558C17 /* GTEnumeratorSpec.m in Sources */, diff --git a/ObjectiveGitTests/GTRepositoryAttributesSpec.m b/ObjectiveGitTests/GTRepositoryAttributesSpec.m new file mode 100644 index 000000000..dc4539e35 --- /dev/null +++ b/ObjectiveGitTests/GTRepositoryAttributesSpec.m @@ -0,0 +1,35 @@ +// +// GTRepositoryAttributesSpec.m +// ObjectiveGitFramework +// +// Created by Josh Abernathy on 7/25/14. +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// + +#import "GTRepository+Attributes.h" + +SpecBegin(GTRepositoryAttributes) + +__block GTRepository *repository; + +beforeEach(^{ + repository = [self blankFixtureRepository]; +}); + +it(@"should be able to look up attributes", ^{ + static NSString * const testAttributes = @"*.txt filter=reverse"; + NSURL *attributesURL = [repository.fileURL URLByAppendingPathComponent:@".gitattributes"]; + BOOL success = [testAttributes writeToURL:attributesURL atomically:YES encoding:NSUTF8StringEncoding error:NULL]; + expect(success).to.beTruthy(); + + NSString *value = [repository attributeWithName:@"filter" path:@"*.txt"]; + expect(value).to.equal(@"reverse"); + + value = [repository attributeWithName:@"filter" path:@"thing.txt"]; + expect(value).to.equal(@"reverse"); + + value = [repository attributeWithName:@"filter" path:@"thing.jpg"]; + expect(value).to.beNil(); +}); + +SpecEnd From da90d15312150a9f352e9e500d3f8fe406b93b50 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 25 Jul 2014 15:52:34 -0400 Subject: [PATCH 3/4] Actually assert. --- Classes/GTRepository+Attributes.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Classes/GTRepository+Attributes.m b/Classes/GTRepository+Attributes.m index d77acb288..c8d63f9a3 100644 --- a/Classes/GTRepository+Attributes.m +++ b/Classes/GTRepository+Attributes.m @@ -12,6 +12,9 @@ @implementation GTRepository (Attributes) - (NSString *)attributeWithName:(NSString *)name path:(NSString *)path { + NSParameterAssert(name != nil); + NSParameterAssert(path != nil); + const char *val = NULL; git_attr_get(&val, self.git_repository, 0, path.UTF8String, name.UTF8String); if (val == NULL) return nil; From 99f3a7f1f9b6e8f7cea84998c49703bdb1d8b0da Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 25 Jul 2014 16:04:35 -0400 Subject: [PATCH 4/4] Use GIT_ATTR_CHECK_FILE_THEN_INDEX. --- Classes/GTRepository+Attributes.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/GTRepository+Attributes.m b/Classes/GTRepository+Attributes.m index c8d63f9a3..4abdc10da 100644 --- a/Classes/GTRepository+Attributes.m +++ b/Classes/GTRepository+Attributes.m @@ -16,7 +16,7 @@ - (NSString *)attributeWithName:(NSString *)name path:(NSString *)path { NSParameterAssert(path != nil); const char *val = NULL; - git_attr_get(&val, self.git_repository, 0, path.UTF8String, name.UTF8String); + git_attr_get(&val, self.git_repository, GIT_ATTR_CHECK_FILE_THEN_INDEX, path.UTF8String, name.UTF8String); if (val == NULL) return nil; return @(val);