Skip to content

Commit 8fcc04e

Browse files
committed
Fix checkout errors blaming the working directory (issue gitx#629)
git says why it refused, and GitX said something else. Checking out a branch that is checked out in another worktree fails with "fatal: 'x' is already used by worktree at ...", and the sheet answered "Perhaps your working directory is not clean?", which sends the reader looking for changes that are not there. The same guess stood in for every failure of the two checkouts and of cherry pick. * Report the failed task's own output as the failure reason, keeping the guess for a failure that produced none * Cover the worktree refusal, and both fallbacks, with tests that need no git on the machine
1 parent 3a4a41d commit 8fcc04e

3 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎Classes/git/PBGitRepository.m‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,16 @@ - (BOOL)pushBranch:(PBGitRef *)branchRef toRemote:(PBGitRef *)remoteRef error:(N
922922
return success;
923923
}
924924

925+
- (NSString *)failureReasonFromTaskError:(NSError *)taskError orFallback:(NSString *)fallback
926+
{
927+
if (![taskError.domain isEqualToString:PBTaskErrorDomain])
928+
return fallback;
929+
930+
NSString *output = [taskError.userInfo[PBTaskTerminationOutputKey] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
931+
932+
return output.length ? output : fallback;
933+
}
934+
925935
- (BOOL)checkoutRefish:(id<PBGitRefish>)ref error:(NSError **)error
926936
{
927937
NSString *refName = nil;
@@ -935,7 +945,8 @@ - (BOOL)checkoutRefish:(id<PBGitRefish>)ref error:(NSError **)error
935945
NSString *output = [self outputOfTaskWithArguments:arguments error:&gitError];
936946
if (!output) {
937947
NSString *title = @"Checkout failed";
938-
NSString *message = [NSString stringWithFormat:@"There was an error checking out the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
948+
NSString *guess = [NSString stringWithFormat:@"There was an error checking out the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
949+
NSString *message = [self failureReasonFromTaskError:gitError orFallback:guess];
939950

940951
return PBReturnError(error, title, message, gitError);
941952
}
@@ -963,7 +974,8 @@ - (BOOL)checkoutFiles:(NSArray *)files fromRefish:(id<PBGitRefish>)ref error:(NS
963974
NSString *output = [self outputOfTaskWithArguments:arguments error:&gitError];
964975
if (!output) {
965976
NSString *title = @"Checkout failed";
966-
NSString *message = [NSString stringWithFormat:@"There was an error checking out the file(s) from the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
977+
NSString *guess = [NSString stringWithFormat:@"There was an error checking out the file(s) from the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
978+
NSString *message = [self failureReasonFromTaskError:gitError orFallback:guess];
967979

968980
return PBReturnError(error, title, message, gitError);
969981
}
@@ -1004,7 +1016,8 @@ - (BOOL)cherryPickRefish:(id<PBGitRefish>)ref error:(NSError **)error
10041016
NSString *output = [self outputOfTaskWithArguments:arguments error:&gitError];
10051017
if (!output) {
10061018
NSString *title = @"Cherry pick failed!";
1007-
NSString *message = [NSString stringWithFormat:@"There was an error cherry picking the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
1019+
NSString *guess = [NSString stringWithFormat:@"There was an error cherry picking the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
1020+
NSString *message = [self failureReasonFromTaskError:gitError orFallback:guess];
10081021

10091022
return PBReturnError(error, title, message, gitError);
10101023
}

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
5E1B93C7D04A47F2A8C61B30 /* PBGitCommitControllerRefreshTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E1B93C7D04A47F2A8C61B31 /* PBGitCommitControllerRefreshTests.m */; };
168168
7C41A9E2B5D340E1A7F26C08 /* PBGitHistorySelectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */; };
169169
53E95A725F5849DA845F5010 /* PBGitHistoryMenuValidationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AD4A0BA06594816ACB9327B /* PBGitHistoryMenuValidationTests.m */; };
170+
9120A847CBE34BD0AEEA32CD /* PBGitRepositoryCheckoutErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 751132C10E5249A5A42FDC71 /* PBGitRepositoryCheckoutErrorTests.m */; };
170171
1D8C4F70A93E42B6BE7C0D95 /* PBGitWindowWiringTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4F2E8A13C7D9406B85A31E27 /* PBGitWindowWiringTests.m */; };
171172
7A1C3E5D2B8F49A0C6D14E82 /* PBRepositoryFinderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E9B0D4A18C7452FB3A0D95E /* PBRepositoryFinderTests.m */; };
172173
4C7F2A9E5D1B48A3E602F7C1 /* PBGitCommitFileSelectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B8E1D6C0A9F42B7D51C8E30 /* PBGitCommitFileSelectionTests.m */; };
@@ -719,6 +720,7 @@
719720
5E1B93C7D04A47F2A8C61B31 /* PBGitCommitControllerRefreshTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitCommitControllerRefreshTests.m; path = GitXTests/PBGitCommitControllerRefreshTests.m; sourceTree = "<group>"; };
720721
5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistorySelectionTests.m; path = GitXTests/PBGitHistorySelectionTests.m; sourceTree = "<group>"; };
721722
9AD4A0BA06594816ACB9327B /* PBGitHistoryMenuValidationTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistoryMenuValidationTests.m; path = GitXTests/PBGitHistoryMenuValidationTests.m; sourceTree = "<group>"; };
723+
751132C10E5249A5A42FDC71 /* PBGitRepositoryCheckoutErrorTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryCheckoutErrorTests.m; path = GitXTests/PBGitRepositoryCheckoutErrorTests.m; sourceTree = "<group>"; };
722724
4F2E8A13C7D9406B85A31E27 /* PBGitWindowWiringTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitWindowWiringTests.m; path = GitXTests/PBGitWindowWiringTests.m; sourceTree = "<group>"; };
723725
6E9B0D4A18C7452FB3A0D95E /* PBRepositoryFinderTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBRepositoryFinderTests.m; path = GitXTests/PBRepositoryFinderTests.m; sourceTree = "<group>"; };
724726
3B8E1D6C0A9F42B7D51C8E30 /* PBGitCommitFileSelectionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitCommitFileSelectionTests.m; path = GitXTests/PBGitCommitFileSelectionTests.m; sourceTree = "<group>"; };
@@ -857,6 +859,7 @@
857859
5E1B93C7D04A47F2A8C61B31 /* PBGitCommitControllerRefreshTests.m */,
858860
5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */,
859861
9AD4A0BA06594816ACB9327B /* PBGitHistoryMenuValidationTests.m */,
862+
751132C10E5249A5A42FDC71 /* PBGitRepositoryCheckoutErrorTests.m */,
860863
4F2E8A13C7D9406B85A31E27 /* PBGitWindowWiringTests.m */,
861864
6E9B0D4A18C7452FB3A0D95E /* PBRepositoryFinderTests.m */,
862865
3B8E1D6C0A9F42B7D51C8E30 /* PBGitCommitFileSelectionTests.m */,
@@ -1810,6 +1813,7 @@
18101813
5E1B93C7D04A47F2A8C61B30 /* PBGitCommitControllerRefreshTests.m in Sources */,
18111814
7C41A9E2B5D340E1A7F26C08 /* PBGitHistorySelectionTests.m in Sources */,
18121815
53E95A725F5849DA845F5010 /* PBGitHistoryMenuValidationTests.m in Sources */,
1816+
9120A847CBE34BD0AEEA32CD /* PBGitRepositoryCheckoutErrorTests.m in Sources */,
18131817
1D8C4F70A93E42B6BE7C0D95 /* PBGitWindowWiringTests.m in Sources */,
18141818
7A1C3E5D2B8F49A0C6D14E82 /* PBRepositoryFinderTests.m in Sources */,
18151819
4C7F2A9E5D1B48A3E602F7C1 /* PBGitCommitFileSelectionTests.m in Sources */,
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// PBGitRepositoryCheckoutErrorTests.m
3+
// GitXTests
4+
//
5+
6+
#import <XCTest/XCTest.h>
7+
#import "PBGitRepository.h"
8+
#import "PBTask.h"
9+
10+
// The reason a failed git task is reported with is private to the repository,
11+
// so the test names it the way the other suites name the seams they drive.
12+
@interface PBGitRepository (CheckoutErrorTesting)
13+
- (NSString *)failureReasonFromTaskError:(NSError *)taskError orFallback:(NSString *)fallback;
14+
@end
15+
16+
static NSString *const kWorktreeRefusal = @"fatal: 'feature' is already used by worktree at '/tmp/other-worktree'";
17+
static NSString *const kGuess = @"There was an error checking out the branch 'feature'.\n\nPerhaps your working directory is not clean?";
18+
19+
@interface PBGitRepositoryCheckoutErrorTests : XCTestCase
20+
@property (nonatomic, strong) PBGitRepository *repository;
21+
@end
22+
23+
@implementation PBGitRepositoryCheckoutErrorTests
24+
25+
- (void)setUp
26+
{
27+
[super setUp];
28+
29+
// The reason does not depend on the repository it is reported for, and
30+
// -[PBGitRepository init] reaches for neither libgit2 nor git.
31+
self.repository = [[PBGitRepository alloc] init];
32+
}
33+
34+
- (NSError *)taskErrorWithOutput:(NSString *)output
35+
{
36+
return [NSError errorWithDomain:PBTaskErrorDomain
37+
code:PBTaskNonZeroExitCodeError
38+
userInfo:@{PBTaskTerminationStatusKey : @128,
39+
PBTaskTerminationOutputKey : output}];
40+
}
41+
42+
// The bug (#629): git refuses the checkout because the branch is checked out in
43+
// another worktree, and GitX reported a guess about the working directory
44+
// instead, which sends the reader looking for changes that are not there.
45+
- (void)testGitsOwnRefusalIsReportedInsteadOfTheGuess
46+
{
47+
NSError *taskError = [self taskErrorWithOutput:kWorktreeRefusal];
48+
49+
XCTAssertEqualObjects([self.repository failureReasonFromTaskError:taskError orFallback:kGuess], kWorktreeRefusal);
50+
}
51+
52+
- (void)testGitsOutputIsTrimmed
53+
{
54+
NSError *taskError = [self taskErrorWithOutput:[kWorktreeRefusal stringByAppendingString:@"\n"]];
55+
56+
XCTAssertEqualObjects([self.repository failureReasonFromTaskError:taskError orFallback:kGuess], kWorktreeRefusal);
57+
}
58+
59+
- (void)testTheFallbackIsKeptWhenGitSaidNothing
60+
{
61+
NSError *taskError = [self taskErrorWithOutput:@" \n "];
62+
63+
XCTAssertEqualObjects([self.repository failureReasonFromTaskError:taskError orFallback:kGuess], kGuess);
64+
}
65+
66+
- (void)testTheFallbackIsKeptForAFailureThatIsNotATaskError
67+
{
68+
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
69+
70+
XCTAssertEqualObjects([self.repository failureReasonFromTaskError:error orFallback:kGuess], kGuess);
71+
}
72+
73+
@end

0 commit comments

Comments
 (0)