Skip to content

Commit 8e2e25b

Browse files
committed
Rewrite GitRepoFinder a little.
Renamed to PBRepositoryFinder and cleaned up its methods.
1 parent 199e44d commit 8e2e25b

6 files changed

Lines changed: 99 additions & 17 deletions

File tree

‎Classes/Controllers/PBGitRepositoryDocument.m‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#import "PBGitRevSpecifier.h"
1313
#import "PBGitBinary.h"
1414
#import "GitXScriptingConstants.h"
15-
#import "GitRepoFinder.h"
15+
#import "PBRepositoryFinder.h"
1616
#import "PBGitDefaults.h"
1717

1818
NSString *PBGitRepositoryDocumentType = @"Git Repository";
@@ -123,7 +123,7 @@ - (void)showWindows
123123
NSString *path = [[eventRecord paramDescriptorForKeyword:typeFileURL] stringValue];
124124
if (path) {
125125
NSURL *workingDirectory = [NSURL URLWithString:path];
126-
if ([[GitRepoFinder gitDirForURL:workingDirectory] isEqual:[self fileURL]]) {
126+
if ([[PBRepositoryFinder gitDirForURL:workingDirectory] isEqual:[self fileURL]]) {
127127
NSAppleEventDescriptor *argumentsList = [eventRecord paramDescriptorForKeyword:kGitXAEKeyArgumentsList];
128128
[self handleGitXScriptingArguments:argumentsList inWorkingDirectory:workingDirectory];
129129

‎Classes/Controllers/PBRepositoryDocumentController.m‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#import "PBGitRevList.h"
1212
#import "PBEasyPipe.h"
1313
#import "PBGitBinary.h"
14-
#import "GitRepoFinder.h"
1514

1615
#import <ObjectiveGit/GTRepository.h>
1716

‎Classes/git/PBGitRepository.m‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import "GitXScriptingConstants.h"
2222
#import "PBHistorySearchController.h"
2323
#import "PBGitRepositoryWatcher.h"
24-
#import "GitRepoFinder.h"
24+
#import "PBRepositoryFinder.h"
2525
#import "PBGitHistoryList.h"
2626
#import "PBGitStash.h"
2727

@@ -56,8 +56,8 @@ - (id)initWithURL:(NSURL *)repositoryURL error:(NSError **)error
5656
if (!self) return nil;
5757

5858
NSError *gtError = nil;
59-
NSURL *repoURL = [GitRepoFinder gitDirForURL:repositoryURL];
60-
_gtRepo = [GTRepository repositoryWithURL:repoURL error:&gtError];
59+
NSURL *repoURL = [PBRepositoryFinder gitDirForURL:repositoryURL];
60+
_gtRepo = [GTRepository repositoryWithURL:repoURL error:&gtError];
6161
if (!_gtRepo) {
6262
if (error) {
6363
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@@ -85,7 +85,6 @@ - (void) dealloc
8585
[watcher stop];
8686
}
8787

88-
8988
#pragma mark -
9089
#pragma mark Properties/General methods
9190

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// GitRepoFinder.h
2+
// PBRepositoryFinder.h
33
// GitX
44
//
55
// Created by Rowan James on 13/11/2012.
@@ -8,8 +8,9 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
@interface GitRepoFinder : NSObject
11+
@interface PBRepositoryFinder : NSObject
1212

13+
+ (NSURL *)fileURLForURL:(NSURL *)inputURL;
1314
+ (NSURL*)workDirForURL:(NSURL*)fileURL;
1415
+ (NSURL*)gitDirForURL:(NSURL*)fileURL;
1516

‎Classes/git/PBRepositoryFinder.m‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// PBRepositoryFinder.m
3+
// GitX
4+
//
5+
// Created by Rowan James on 13/11/2012.
6+
//
7+
//
8+
9+
#import "PBRepositoryFinder.h"
10+
11+
@implementation PBRepositoryFinder
12+
13+
+ (NSURL *)workDirForURL:(NSURL *)fileURL;
14+
{
15+
if (!fileURL.isFileURL) {
16+
return nil;
17+
}
18+
19+
git_repository *repo = NULL;
20+
git_repository_open_ext(&repo, fileURL.path.UTF8String, GIT_REPOSITORY_OPEN_CROSS_FS, NULL);
21+
if (!repo) {
22+
return NULL;
23+
}
24+
25+
const char *workdir = git_repository_workdir(repo);
26+
NSURL *result = nil;
27+
if (workdir) {
28+
result = [NSURL fileURLWithPath:[NSString stringWithUTF8String:workdir]];
29+
}
30+
31+
git_repository_free(repo); repo = nil;
32+
return result;
33+
}
34+
35+
+ (NSURL *)gitDirForURL:(NSURL *)fileURL
36+
{
37+
if (!fileURL.isFileURL)
38+
{
39+
return nil;
40+
}
41+
git_buf path_buffer = {NULL, 0, 0};
42+
int gitResult = git_repository_discover(&path_buffer,
43+
[fileURL.path UTF8String],
44+
GIT_REPOSITORY_OPEN_CROSS_FS,
45+
nil);
46+
47+
NSData *repoPathBuffer = nil;
48+
if (path_buffer.ptr) {
49+
repoPathBuffer = [NSData dataWithBytes:path_buffer.ptr length:path_buffer.asize];
50+
git_buf_free(&path_buffer);
51+
}
52+
53+
if (gitResult == GIT_OK && repoPathBuffer.length)
54+
{
55+
NSString* repoPath = [NSString stringWithUTF8String:repoPathBuffer.bytes];
56+
BOOL isDirectory;
57+
if ([[NSFileManager defaultManager] fileExistsAtPath:repoPath
58+
isDirectory:&isDirectory] && isDirectory)
59+
{
60+
NSURL* result = [NSURL fileURLWithPath:repoPath
61+
isDirectory:isDirectory];
62+
return result;
63+
}
64+
}
65+
return nil;
66+
}
67+
68+
+ (NSURL *)fileURLForURL:(NSURL *)inputURL
69+
{
70+
NSURL* gitDir = [self gitDirForURL:inputURL];
71+
if (!gitDir) {
72+
return nil; // not a Git directory at all
73+
}
74+
75+
NSURL *workDir = [self workDirForURL:inputURL];
76+
if (workDir) {
77+
return workDir; // root of this working copy or deepest submodule
78+
}
79+
80+
return gitDir; // bare repo
81+
}
82+
83+
@end

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@
150150
4A8F6B4C14A9B6C90002F4D7 /* MGScopeBar.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4A8F6B4714A9B6110002F4D7 /* MGScopeBar.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
151151
4A90A73514A9D24300D0DA02 /* GitX.sdef in Resources */ = {isa = PBXBuildFile; fileRef = 4A90A73414A9D24300D0DA02 /* GitX.sdef */; };
152152
4AAAFDDA14A010DD008FC9B5 /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4A68AD6714A0050F006DE321 /* Sparkle.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
153-
4AB057E31652652000DE751D /* GitRepoFinder.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB057E21652652000DE751D /* GitRepoFinder.m */; };
154-
4AB057E41652652000DE751D /* GitRepoFinder.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB057E21652652000DE751D /* GitRepoFinder.m */; };
153+
4AB057E31652652000DE751D /* PBRepositoryFinder.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB057E21652652000DE751D /* PBRepositoryFinder.m */; };
154+
4AB057E41652652000DE751D /* PBRepositoryFinder.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB057E21652652000DE751D /* PBRepositoryFinder.m */; };
155155
4AB71FF814B7EDD400F1DFFC /* RJModalRepoSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB71FF714B7EDD400F1DFFC /* RJModalRepoSheet.m */; };
156156
4AC42F7D16BFBADA007CCA3A /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A40159814067B7A00DB9C07 /* AppKit.framework */; };
157157
4D3FB3E7198AEAAF000B4A58 /* PBGitRepositoryDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D3FB3E6198AEAAF000B4A58 /* PBGitRepositoryDocument.m */; };
@@ -570,8 +570,8 @@
570570
4A90A72C14A9C12F00D0DA02 /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = SOURCE_ROOT; };
571571
4A90A73314A9D1E800D0DA02 /* GitX.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GitX.h; sourceTree = SOURCE_ROOT; };
572572
4A90A73414A9D24300D0DA02 /* GitX.sdef */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = GitX.sdef; sourceTree = SOURCE_ROOT; };
573-
4AB057E11652652000DE751D /* GitRepoFinder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GitRepoFinder.h; sourceTree = "<group>"; };
574-
4AB057E21652652000DE751D /* GitRepoFinder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GitRepoFinder.m; sourceTree = "<group>"; };
573+
4AB057E11652652000DE751D /* PBRepositoryFinder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBRepositoryFinder.h; sourceTree = "<group>"; };
574+
4AB057E21652652000DE751D /* PBRepositoryFinder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBRepositoryFinder.m; sourceTree = "<group>"; usesTabs = 0; };
575575
4AB71FF614B7EDD400F1DFFC /* RJModalRepoSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RJModalRepoSheet.h; sourceTree = "<group>"; };
576576
4AB71FF714B7EDD400F1DFFC /* RJModalRepoSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RJModalRepoSheet.m; sourceTree = "<group>"; };
577577
4D3FB3E5198AEAAF000B4A58 /* PBGitRepositoryDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRepositoryDocument.h; sourceTree = "<group>"; };
@@ -959,8 +959,8 @@
959959
A2F8D0DE17AAB32500580B84 /* PBGitStash.m */,
960960
643952751603EF9B00BB7AFF /* PBGitSVSubmoduleItem.h */,
961961
643952761603EF9B00BB7AFF /* PBGitSVSubmoduleItem.m */,
962-
4AB057E11652652000DE751D /* GitRepoFinder.h */,
963-
4AB057E21652652000DE751D /* GitRepoFinder.m */,
962+
4AB057E11652652000DE751D /* PBRepositoryFinder.h */,
963+
4AB057E21652652000DE751D /* PBRepositoryFinder.m */,
964964
2682AAB91929140E00271A4D /* GTOID+JavaScript.h */,
965965
2682AABA1929140E00271A4D /* GTOID+JavaScript.m */,
966966
);
@@ -1461,7 +1461,7 @@
14611461
4A5D777514A9AEB000DF6C68 /* PBSourceViewRemote.m in Sources */,
14621462
4AB71FF814B7EDD400F1DFFC /* RJModalRepoSheet.m in Sources */,
14631463
643952771603EF9B00BB7AFF /* PBGitSVSubmoduleItem.m in Sources */,
1464-
4AB057E31652652000DE751D /* GitRepoFinder.m in Sources */,
1464+
4AB057E31652652000DE751D /* PBRepositoryFinder.m in Sources */,
14651465
4A2125A417C0C78A00B5B582 /* NSColor+RGB.m in Sources */,
14661466
A2F8D0DF17AAB32500580B84 /* PBGitStash.m in Sources */,
14671467
A2F8D0EB17AAB95E00580B84 /* PBGitSVStashItem.m in Sources */,
@@ -1475,7 +1475,7 @@
14751475
4A5D773A14A9A9F600DF6C68 /* gitx.m in Sources */,
14761476
4A5D773C14A9AA2F00DF6C68 /* PBGitBinary.m in Sources */,
14771477
4A5D773D14A9AA3700DF6C68 /* PBEasyPipe.m in Sources */,
1478-
4AB057E41652652000DE751D /* GitRepoFinder.m in Sources */,
1478+
4AB057E41652652000DE751D /* PBRepositoryFinder.m in Sources */,
14791479
);
14801480
runOnlyForDeploymentPostprocessing = 0;
14811481
};

0 commit comments

Comments
 (0)