From aabbc8a1ff67b27236202c18998d8af48803e091 Mon Sep 17 00:00:00 2001 From: Felix Lapalme Date: Mon, 22 Apr 2024 21:27:43 -0400 Subject: [PATCH] Specify `initial_head` when initializing a repository for tests so the global git config doesn't make it fail (#988) --- GitUpKit/Core/GCRepository.h | 1 + GitUpKit/Core/GCRepository.m | 9 +++++++++ GitUpKit/Core/GCTestCase.m | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/GitUpKit/Core/GCRepository.h b/GitUpKit/Core/GCRepository.h index 00914c3d..b5f6855c 100644 --- a/GitUpKit/Core/GCRepository.h +++ b/GitUpKit/Core/GCRepository.h @@ -62,6 +62,7 @@ typedef NS_ENUM(NSUInteger, GCFileMode) { @property(nonatomic, readonly) GCRepositoryState state; // Do NOT use on a bare repository - (instancetype)initWithExistingLocalRepository:(NSString*)path error:(NSError**)error; - (instancetype)initWithNewLocalRepository:(NSString*)path bare:(BOOL)bare error:(NSError**)error; // git init {path} +- (instancetype)initWithNewLocalRepository:(NSString*)path bare:(BOOL)bare defaultBranchName:(NSString*)defaultBranchName error:(NSError**)error; - (BOOL)cleanupState:(NSError**)error; // Do NOT use on a bare repository diff --git a/GitUpKit/Core/GCRepository.m b/GitUpKit/Core/GCRepository.m index f66684a3..5f8f8c47 100644 --- a/GitUpKit/Core/GCRepository.m +++ b/GitUpKit/Core/GCRepository.m @@ -159,11 +159,20 @@ - (instancetype)initWithExistingLocalRepository:(NSString*)path error:(NSError** } - (instancetype)initWithNewLocalRepository:(NSString*)path bare:(BOOL)bare error:(NSError**)error { + return [self initWithNewLocalRepository:path bare:bare defaultBranchName:nil error:error]; +} + +- (instancetype)initWithNewLocalRepository:(NSString*)path bare:(BOOL)bare defaultBranchName:(NSString*)defaultBranchName error:(NSError**)error { git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT; options.flags = GIT_REPOSITORY_INIT_NO_REINIT | GIT_REPOSITORY_INIT_MKPATH; if (bare) { options.flags |= GIT_REPOSITORY_INIT_BARE; } + + if (defaultBranchName) { + options.initial_head = defaultBranchName.UTF8String; + } + git_repository* repository; CALL_LIBGIT2_FUNCTION_RETURN(nil, git_repository_init_ext, &repository, path.fileSystemRepresentation, &options); return [self initWithRepository:repository error:error]; diff --git a/GitUpKit/Core/GCTestCase.m b/GitUpKit/Core/GCTestCase.m index 332d98a2..4da70590 100644 --- a/GitUpKit/Core/GCTestCase.m +++ b/GitUpKit/Core/GCTestCase.m @@ -36,7 +36,7 @@ - (void)setUp { } - (GCRepository*)createLocalRepositoryAtPath:(NSString*)path bare:(BOOL)bare { - GCRepository* repo = [[GCRepository alloc] initWithNewLocalRepository:path bare:bare error:NULL]; + GCRepository* repo = [[GCRepository alloc] initWithNewLocalRepository:path bare:bare defaultBranchName:@"master" error:NULL]; XCTAssertNotNil(repo); NSString* configDirectory = [[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]] stringByAppendingPathComponent:@"git"];