Skip to content

Commit 223a3f4

Browse files
committed
Fix Ignore doing nothing once a .gitignore is already there (issue gitx#597)
Ignoring a file worked the first time and never again, because appending to the .gitignore left behind measured the length of the string being built rather than the one read off disk, and indexing past the end of it raised an exception the menu item quietly swallowed. * Ask the file read off disk whether it already ends in a newline * Leave an empty .gitignore without a newline in front of the first path written into it
1 parent 5b021fc commit 223a3f4

3 files changed

Lines changed: 116 additions & 1 deletion

File tree

‎Classes/git/PBGitRepository.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ - (BOOL)ignoreFilePaths:(NSArray *)filePaths error:(NSError **)error
621621
if (!currentFile) return NO;
622622

623623
// Add a newline if not yet present
624-
if ([currentFile characterAtIndex:([ignoreFile length] - 1)] != '\n')
624+
if (currentFile.length > 0 && ![currentFile hasSuffix:@"\n"])
625625
[currentFile appendString:@"\n"];
626626
[currentFile appendString:filesAsString];
627627

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
551BF176112F3F4B00265053 /* gitx_askpasswd in Resources */ = {isa = PBXBuildFile; fileRef = 551BF111112F371800265053 /* gitx_askpasswd */; };
155155
63E155858B2CC783EE10F1D7 /* PBGitRevListRaceConditionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0044 /* PBGitRevListRaceConditionTests.m */; };
156156
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */; };
157+
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */; };
157158
63E155858B2CC783EE10F1D8 /* PBGitIndexReconcileTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */; };
158159
63E155858B2CC783EE10F1D9 /* PBGitIndexRefreshTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */; };
159160
7C41A9E2B5D340E1A7F26C08 /* PBGitHistorySelectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */; };
@@ -696,6 +697,7 @@
696697
643952761603EF9B00BB7AFF /* PBSourceViewGitSubmoduleItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBSourceViewGitSubmoduleItem.m; sourceTree = "<group>"; };
697698
689D57B4A78CEF3B32EB0044 /* PBGitRevListRaceConditionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRevListRaceConditionTests.m; path = GitXTests/PBGitRevListRaceConditionTests.m; sourceTree = "<group>"; };
698699
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryFetchArgumentsTests.m; path = GitXTests/PBGitRepositoryFetchArgumentsTests.m; sourceTree = "<group>"; };
700+
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryIgnoreTests.m; path = GitXTests/PBGitRepositoryIgnoreTests.m; sourceTree = "<group>"; };
699701
689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexReconcileTests.m; path = GitXTests/PBGitIndexReconcileTests.m; sourceTree = "<group>"; };
700702
689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexRefreshTests.m; path = GitXTests/PBGitIndexRefreshTests.m; sourceTree = "<group>"; };
701703
5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistorySelectionTests.m; path = GitXTests/PBGitHistorySelectionTests.m; sourceTree = "<group>"; };
@@ -824,6 +826,7 @@
824826
children = (
825827
689D57B4A78CEF3B32EB0044 /* PBGitRevListRaceConditionTests.m */,
826828
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */,
829+
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */,
827830
689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */,
828831
689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */,
829832
5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */,
@@ -1767,6 +1770,7 @@
17671770
files = (
17681771
63E155858B2CC783EE10F1D7 /* PBGitRevListRaceConditionTests.m in Sources */,
17691772
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */,
1773+
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */,
17701774
63E155858B2CC783EE10F1D8 /* PBGitIndexReconcileTests.m in Sources */,
17711775
63E155858B2CC783EE10F1D9 /* PBGitIndexRefreshTests.m in Sources */,
17721776
7C41A9E2B5D340E1A7F26C08 /* PBGitHistorySelectionTests.m in Sources */,
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// PBGitRepositoryIgnoreTests.m
3+
// GitXTests
4+
//
5+
6+
#import <XCTest/XCTest.h>
7+
#import "PBGitRepository.h"
8+
9+
// Names a .gitignore the test owns, so that ignoring a file needs no working
10+
// directory and no libgit2 behind it.
11+
@interface PBIgnoreStubRepository : PBGitRepository
12+
@property (nonatomic, strong) NSString *ignoreFilename;
13+
@end
14+
15+
@implementation PBIgnoreStubRepository
16+
17+
- (NSString *)gitIgnoreFilename
18+
{
19+
return self.ignoreFilename;
20+
}
21+
22+
@end
23+
24+
@interface PBGitRepositoryIgnoreTests : XCTestCase
25+
@property (nonatomic, strong) PBIgnoreStubRepository *repository;
26+
@property (nonatomic, strong) NSURL *directory;
27+
@end
28+
29+
@implementation PBGitRepositoryIgnoreTests
30+
31+
- (void)setUp
32+
{
33+
[super setUp];
34+
35+
self.directory = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]]];
36+
[[NSFileManager defaultManager] createDirectoryAtURL:self.directory
37+
withIntermediateDirectories:YES
38+
attributes:nil
39+
error:NULL];
40+
41+
self.repository = [[PBIgnoreStubRepository alloc] init];
42+
self.repository.ignoreFilename = [self.directory URLByAppendingPathComponent:@".gitignore"].path;
43+
}
44+
45+
- (void)tearDown
46+
{
47+
[[NSFileManager defaultManager] removeItemAtURL:self.directory error:NULL];
48+
49+
[super tearDown];
50+
}
51+
52+
- (void)writeIgnoreFile:(NSString *)contents
53+
{
54+
[contents writeToFile:self.repository.ignoreFilename atomically:YES encoding:NSUTF8StringEncoding error:NULL];
55+
}
56+
57+
- (NSString *)ignoreFileContents
58+
{
59+
return [NSString stringWithContentsOfFile:self.repository.ignoreFilename encoding:NSUTF8StringEncoding error:NULL];
60+
}
61+
62+
- (BOOL)ignore:(NSArray<NSString *> *)paths
63+
{
64+
NSError *error = nil;
65+
66+
return [self.repository ignoreFilePaths:paths error:&error];
67+
}
68+
69+
- (void)testTheFirstFileIgnoredWritesTheFile
70+
{
71+
XCTAssertTrue([self ignore:@[ @"f1" ]]);
72+
73+
XCTAssertEqualObjects([self ignoreFileContents], @"f1");
74+
}
75+
76+
- (void)testASecondFileIsAddedToTheFileTheFirstOneLeftBehind
77+
{
78+
XCTAssertTrue([self ignore:@[ @"f1" ]]);
79+
80+
XCTAssertTrue([self ignore:@[ @"f2" ]], @"the second file has to be ignored just like the first");
81+
XCTAssertEqualObjects([self ignoreFileContents], @"f1\nf2");
82+
}
83+
84+
- (void)testAFileAlreadyEndingInANewlineDoesNotGetASecondOne
85+
{
86+
[self writeIgnoreFile:@"build/\n"];
87+
88+
XCTAssertTrue([self ignore:@[ @"f1" ]]);
89+
90+
XCTAssertEqualObjects([self ignoreFileContents], @"build/\nf1");
91+
}
92+
93+
- (void)testAnEmptyFileIsWrittenWithoutALeadingNewline
94+
{
95+
[self writeIgnoreFile:@""];
96+
97+
XCTAssertTrue([self ignore:@[ @"f1" ]]);
98+
99+
XCTAssertEqualObjects([self ignoreFileContents], @"f1");
100+
}
101+
102+
- (void)testEverySelectedFileGetsItsOwnLine
103+
{
104+
[self writeIgnoreFile:@"build/"];
105+
106+
XCTAssertTrue([self ignore:(@[ @"f1", @"f2" ])]);
107+
108+
XCTAssertEqualObjects([self ignoreFileContents], @"build/\nf1\nf2");
109+
}
110+
111+
@end

0 commit comments

Comments
 (0)