|
| 1 | +// |
| 2 | +// GTRepository+Worktree.m |
| 3 | +// ObjectiveGitFramework |
| 4 | +// |
| 5 | +// Created by Etienne on 25/07/2017. |
| 6 | +// Copyright © 2017 GitHub, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "GTRepository+Worktree.h" |
| 10 | + |
| 11 | +@implementation GTRepository (Worktree) |
| 12 | + |
| 13 | ++ (instancetype)repositoryWithWorktree:(GTWorktree *)worktree error:(NSError **)error { |
| 14 | + return [[self alloc] initWithWorktree:worktree error:error]; |
| 15 | +} |
| 16 | + |
| 17 | +- (instancetype)initWithWorktree:(GTWorktree *)worktree error:(NSError **)error { |
| 18 | + NSParameterAssert(worktree != nil); |
| 19 | + |
| 20 | + git_repository *repo; |
| 21 | + int gitError = git_repository_open_from_worktree(&repo, worktree.git_worktree); |
| 22 | + if (gitError != GIT_OK) { |
| 23 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to open worktree"]; |
| 24 | + return nil; |
| 25 | + } |
| 26 | + return [self initWithGitRepository:repo]; |
| 27 | +} |
| 28 | + |
| 29 | +- (BOOL)isWorktree { |
| 30 | + return (BOOL)git_repository_is_worktree(self.git_repository); |
| 31 | +} |
| 32 | + |
| 33 | +- (NSURL *)commonGitDirectoryURL { |
| 34 | + return [NSURL fileURLWithPath:@(git_repository_commondir(self.git_repository)) isDirectory:YES]; |
| 35 | +} |
| 36 | + |
| 37 | +- (GTReference *)HEADReferenceInWorktreeWithName:(NSString *)name error:(NSError **)error { |
| 38 | + NSParameterAssert(name != nil); |
| 39 | + |
| 40 | + git_reference *ref; |
| 41 | + int gitError = git_repository_head_for_worktree(&ref, self.git_repository, name.UTF8String); |
| 42 | + if (gitError != GIT_OK) { |
| 43 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to resolve HEAD in worktree"]; |
| 44 | + return nil; |
| 45 | + } |
| 46 | + |
| 47 | + return [[GTReference alloc] initWithGitReference:ref repository:self]; |
| 48 | +} |
| 49 | + |
| 50 | +- (BOOL)isHEADDetached:(BOOL *)detached inWorktreeWithName:(NSString *)name error:(NSError **)error { |
| 51 | + NSParameterAssert(detached != nil); |
| 52 | + NSParameterAssert(name != nil); |
| 53 | + |
| 54 | + int gitError = git_repository_head_detached_for_worktree(self.git_repository, name.UTF8String); |
| 55 | + if (gitError < 0) { |
| 56 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to resolve HEAD in worktree"]; |
| 57 | + return NO; |
| 58 | + } |
| 59 | + |
| 60 | + *detached = (gitError == 1); |
| 61 | + |
| 62 | + return YES; |
| 63 | +} |
| 64 | + |
| 65 | +- (BOOL)setWorkingDirectoryURL:(NSURL *)URL updateGitLink:(BOOL)update error:(NSError **)error { |
| 66 | + NSParameterAssert(URL != nil); |
| 67 | + |
| 68 | + int gitError = git_repository_set_workdir(self.git_repository, URL.fileSystemRepresentation, update); |
| 69 | + if (gitError != GIT_OK) { |
| 70 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to set workdir"]; |
| 71 | + return NO; |
| 72 | + } |
| 73 | + |
| 74 | + return YES; |
| 75 | +} |
| 76 | + |
| 77 | +- (NSArray<NSString *> *)worktreeNamesWithError:(NSError **)error { |
| 78 | + git_strarray names; |
| 79 | + int gitError = git_worktree_list(&names, self.git_repository); |
| 80 | + if (gitError != GIT_OK) { |
| 81 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to load worktree names"]; |
| 82 | + return nil; |
| 83 | + } |
| 84 | + |
| 85 | + return [NSArray git_arrayWithStrarray:names]; |
| 86 | +} |
| 87 | + |
| 88 | +- (GTWorktree *)lookupWorktreeWithName:(NSString *)name error:(NSError **)error { |
| 89 | + NSParameterAssert(name != nil); |
| 90 | + |
| 91 | + git_worktree *worktree; |
| 92 | + int gitError = git_worktree_lookup(&worktree, self.git_repository, name.UTF8String); |
| 93 | + if (gitError != GIT_OK) { |
| 94 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to lookup worktree"]; |
| 95 | + return nil; |
| 96 | + } |
| 97 | + |
| 98 | + return [[GTWorktree alloc] initWithGitWorktree:worktree]; |
| 99 | +} |
| 100 | + |
| 101 | +- (GTWorktree *)openWorktree:(NSError **)error { |
| 102 | + git_worktree *worktree; |
| 103 | + int gitError = git_worktree_open_from_repository(&worktree, self.git_repository); |
| 104 | + if (gitError != GIT_OK) { |
| 105 | + if (error) *error = [NSError git_errorFor:gitError description:@"Failed to open worktree"]; |
| 106 | + return nil; |
| 107 | + } |
| 108 | + |
| 109 | + return [[GTWorktree alloc] initWithGitWorktree:worktree]; |
| 110 | +} |
| 111 | + |
| 112 | +@end |
0 commit comments