Skip to content

Commit c17215e

Browse files
committed
Create wrapper class around git_oid and use it
This is a convenience class to replace using NSStrings to store and compare SHAs. PBGitSHA has a much faster isEqual: function. It is <NSCopying> compliant and implements isEqual: and hash so it can be used as a key in dictionaries.
1 parent e067390 commit c17215e

20 files changed

+300
-132
lines changed

GitX.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
D8E105471157C18200FC28A4 /* PBQLTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = D8E105461157C18200FC28A4 /* PBQLTextView.m */; };
7474
D8E3B2B810DC9FB2001096A3 /* ScriptingBridge.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D8E3B2B710DC9FB2001096A3 /* ScriptingBridge.framework */; };
7575
D8E3B34D10DCA958001096A3 /* PBCreateTagSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = D8E3B34C10DCA958001096A3 /* PBCreateTagSheet.m */; };
76+
D8FBCF19115FA20C0098676A /* PBGitSHA.m in Sources */ = {isa = PBXBuildFile; fileRef = D8FBCF18115FA20C0098676A /* PBGitSHA.m */; };
7677
D8FDD9F711432A12005647F6 /* PBCloneRepositoryPanel.xib in Resources */ = {isa = PBXBuildFile; fileRef = D8FDD9F511432A12005647F6 /* PBCloneRepositoryPanel.xib */; };
7778
D8FDDA6A114335E8005647F6 /* PBGitSVBranchItem.m in Sources */ = {isa = PBXBuildFile; fileRef = D8FDDA5D114335E8005647F6 /* PBGitSVBranchItem.m */; };
7879
D8FDDA6B114335E8005647F6 /* PBGitSVFolderItem.m in Sources */ = {isa = PBXBuildFile; fileRef = D8FDDA5F114335E8005647F6 /* PBGitSVFolderItem.m */; };
@@ -293,6 +294,8 @@
293294
D8E3B34B10DCA958001096A3 /* PBCreateTagSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBCreateTagSheet.h; sourceTree = "<group>"; };
294295
D8E3B34C10DCA958001096A3 /* PBCreateTagSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBCreateTagSheet.m; sourceTree = "<group>"; };
295296
D8E3B38110DD4E2C001096A3 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PBCreateTagSheet.xib; sourceTree = "<group>"; };
297+
D8FBCF17115FA20C0098676A /* PBGitSHA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitSHA.h; sourceTree = "<group>"; };
298+
D8FBCF18115FA20C0098676A /* PBGitSHA.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitSHA.m; sourceTree = "<group>"; };
296299
D8FDD9F611432A12005647F6 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PBCloneRepositoryPanel.xib; sourceTree = "<group>"; };
297300
D8FDDA5C114335E8005647F6 /* PBGitSVBranchItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitSVBranchItem.h; sourceTree = "<group>"; };
298301
D8FDDA5D114335E8005647F6 /* PBGitSVBranchItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitSVBranchItem.m; sourceTree = "<group>"; };
@@ -828,6 +831,8 @@
828831
F56524EF0E02D45200F03B52 /* PBGitCommit.m */,
829832
F5C007730E731B48007B84B2 /* PBGitRef.h */,
830833
F5C007740E731B48007B84B2 /* PBGitRef.m */,
834+
D8FBCF17115FA20C0098676A /* PBGitSHA.h */,
835+
D8FBCF18115FA20C0098676A /* PBGitSHA.m */,
831836
D8295D281130A1DC00C838E8 /* PBGitHistoryList.h */,
832837
D8295D291130A1DC00C838E8 /* PBGitHistoryList.m */,
833838
F5FF4E160E0829C20006317A /* PBGitRevList.h */,
@@ -1175,6 +1180,7 @@
11751180
D8295D2A1130A1DC00C838E8 /* PBGitHistoryList.m in Sources */,
11761181
D8295DE01130E43900C838E8 /* PBGitHistoryGrapher.m in Sources */,
11771182
D8E105471157C18200FC28A4 /* PBQLTextView.m in Sources */,
1183+
D8FBCF19115FA20C0098676A /* PBGitSHA.m in Sources */,
11781184
);
11791185
runOnlyForDeploymentPostprocessing = 0;
11801186
};

PBGitCommit.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@
1010
#import "PBGitRepository.h"
1111
#import "PBGitTree.h"
1212
#import "PBGitRefish.h"
13-
#include "git/oid.h"
13+
#import "PBGitSHA.h"
1414

1515

1616
extern NSString * const kGitXCommitType;
1717

1818

1919
@interface PBGitCommit : NSObject <PBGitRefish> {
20-
git_oid sha;
21-
git_oid *parentShas;
22-
int nParents;
20+
PBGitSHA *sha;
2321

2422
NSString* subject;
2523
NSString* author;
2624
NSString *committer;
2725
NSString* details;
2826
NSString *_patch;
29-
NSArray* parents;
27+
NSArray *parents;
3028
NSString *realSHA;
3129

3230
int timestamp;
@@ -35,8 +33,8 @@ extern NSString * const kGitXCommitType;
3533
PBGitRepository* repository;
3634
}
3735

38-
+ (id) commitWithRepository:(PBGitRepository*)repo andSha:(git_oid)newSha;
39-
- (id) initWithRepository:(PBGitRepository *)repo andSha:(git_oid)sha;
36+
+ (PBGitCommit *)commitWithRepository:(PBGitRepository*)repo andSha:(PBGitSHA *)newSha;
37+
- (id)initWithRepository:(PBGitRepository *)repo andSha:(PBGitSHA *)newSha;
4038

4139
- (void) addRef:(PBGitRef *)ref;
4240
- (void) removeRef:(id)ref;
@@ -51,14 +49,13 @@ extern NSString * const kGitXCommitType;
5149
- (NSString *) shortName;
5250
- (NSString *) refishType;
5351

54-
@property (readonly) git_oid *sha;
52+
@property (readonly) PBGitSHA *sha;
5553
@property (copy) NSString* subject;
5654
@property (copy) NSString* author;
5755
@property (copy) NSString *committer;
58-
@property (readonly) NSArray* parents; // TODO: remove this and its uses
56+
@property (retain) NSArray *parents;
5957

60-
@property (assign) git_oid *parentShas;
61-
@property (assign) int nParents, timestamp;
58+
@property (assign) int timestamp;
6259

6360
@property (retain) NSMutableArray* refs;
6461
@property (readonly) NSDate *date;

PBGitCommit.m

+27-41
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "PBGitCommit.h"
10+
#import "PBGitSHA.h"
1011
#import "PBGitDefaults.h"
1112

1213

@@ -15,24 +16,11 @@
1516

1617
@implementation PBGitCommit
1718

18-
@synthesize repository, subject, timestamp, author, parentShas, nParents, sign, lineInfo;
19+
@synthesize repository, subject, timestamp, author, sign, lineInfo;
20+
@synthesize sha;
21+
@synthesize parents;
1922
@synthesize committer;
2023

21-
- (NSArray *) parents
22-
{
23-
if (nParents == 0)
24-
return NULL;
25-
26-
int i;
27-
NSMutableArray *p = [NSMutableArray arrayWithCapacity:nParents];
28-
for (i = 0; i < nParents; ++i)
29-
{
30-
char *s = git_oid_mkhex(parentShas + i);
31-
[p addObject:[NSString stringWithUTF8String:s]];
32-
free(s);
33-
}
34-
return p;
35-
}
3624

3725
- (NSDate *)date
3826
{
@@ -50,17 +38,12 @@ - (NSArray*) treeContents
5038
return self.tree.children;
5139
}
5240

53-
- (git_oid *)sha
54-
{
55-
return &sha;
56-
}
57-
58-
+ commitWithRepository:(PBGitRepository*)repo andSha:(git_oid)newSha
41+
+ (PBGitCommit *)commitWithRepository:(PBGitRepository*)repo andSha:(PBGitSHA *)newSha
5942
{
60-
return [[[self alloc] initWithRepository:repo andSha:newSha] autorelease];
43+
return [[self alloc] initWithRepository:repo andSha:newSha];
6144
}
6245

63-
- initWithRepository:(PBGitRepository*) repo andSha:(git_oid)newSha
46+
- (id)initWithRepository:(PBGitRepository*) repo andSha:(PBGitSHA *)newSha
6447
{
6548
details = nil;
6649
repository = repo;
@@ -70,34 +53,38 @@ - (git_oid *)sha
7053

7154
- (NSString *)realSha
7255
{
73-
if (!realSHA) {
74-
char *hex = git_oid_mkhex(&sha);
75-
realSHA = [NSString stringWithUTF8String:hex];
76-
free(hex);
77-
}
78-
79-
return realSHA;
56+
return sha.string;
8057
}
8158

82-
- (BOOL) isOnSameBranchAs:(PBGitCommit *)other
59+
- (BOOL) isOnSameBranchAs:(PBGitCommit *)otherCommit
8360
{
84-
if (!other)
61+
if (!otherCommit)
8562
return NO;
8663

87-
NSString *mySHA = [self realSha];
88-
NSString *otherSHA = [other realSha];
89-
90-
if ([otherSHA isEqualToString:mySHA])
64+
if ([self isEqual:otherCommit])
9165
return YES;
9266

93-
return [repository isOnSameBranch:otherSHA asSHA:mySHA];
67+
return [repository isOnSameBranch:otherCommit.sha asSHA:self.sha];
9468
}
9569

9670
- (BOOL) isOnHeadBranch
9771
{
9872
return [self isOnSameBranchAs:[repository headCommit]];
9973
}
10074

75+
- (BOOL)isEqual:(id)otherCommit
76+
{
77+
if (![otherCommit isMemberOfClass:[PBGitCommit class]])
78+
return NO;
79+
80+
return [self.sha isEqual:[(PBGitCommit *)otherCommit sha]];
81+
}
82+
83+
- (NSUInteger)hash
84+
{
85+
return [self.sha hash];
86+
}
87+
10188
// FIXME: Remove this method once it's unused.
10289
- (NSString*) details
10390
{
@@ -150,17 +137,16 @@ - (BOOL) hasRef:(PBGitRef *)ref
150137

151138
- (NSMutableArray *)refs
152139
{
153-
return [[repository refs] objectForKey:[self realSha]];
140+
return [[repository refs] objectForKey:[self sha]];
154141
}
155142

156143
- (void) setRefs:(NSMutableArray *)refs
157144
{
158-
[[repository refs] setObject:refs forKey:[self realSha]];
145+
[[repository refs] setObject:refs forKey:[self sha]];
159146
}
160147

161148
- (void)finalize
162149
{
163-
free(parentShas);
164150
[super finalize];
165151
}
166152

PBGitGrapher.mm

+15-12
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ - (void) decorateCommit: (PBGitCommit *) commit
3939
int i = 0, newPos = -1;
4040
std::list<PBGitLane *> *currentLanes = new std::list<PBGitLane *>;
4141
std::list<PBGitLane *> *previousLanes = (std::list<PBGitLane *> *)pl;
42+
NSArray *parents = [commit parents];
43+
int nParents = [parents count];
4244

43-
int maxLines = (previousLanes->size() + commit.nParents + 2) * 2;
45+
int maxLines = (previousLanes->size() + nParents + 2) * 2;
4446
struct PBGitGraphLine *lines = (struct PBGitGraphLine *)malloc(sizeof(struct PBGitGraphLine) * maxLines);
4547
int currentLine = 0;
4648

@@ -55,14 +57,14 @@ - (void) decorateCommit: (PBGitCommit *) commit
5557
i++;
5658
// This is our commit! We should do a "merge": move the line from
5759
// our upperMapping to their lowerMapping
58-
if ((*it)->isCommit([commit sha])) {
60+
if ((*it)->isCommit([[commit sha] oid])) {
5961
if (!didFirst) {
6062
didFirst = YES;
6163
currentLanes->push_back(*it);
6264
currentLane = currentLanes->back();
6365
newPos = currentLanes->size();
6466
add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
65-
if (commit.nParents)
67+
if (nParents)
6668
add_line(lines, &currentLine, 0, newPos, newPos,(*it)->index());
6769
}
6870
else {
@@ -84,8 +86,9 @@ - (void) decorateCommit: (PBGitCommit *) commit
8486
//Add your own parents
8587

8688
// If we already did the first parent, don't do so again
87-
if (!didFirst && currentLanes->size() < MAX_LANES && commit.nParents) {
88-
PBGitLane *newLane = new PBGitLane(commit.parentShas);
89+
if (!didFirst && currentLanes->size() < MAX_LANES && nParents) {
90+
git_oid parentOID = [[parents objectAtIndex:0] oid];
91+
PBGitLane *newLane = new PBGitLane(&parentOID);
8992
currentLanes->push_back(newLane);
9093
newPos = currentLanes->size();
9194
add_line(lines, &currentLine, 0, newPos, newPos, newLane->index());
@@ -97,15 +100,15 @@ - (void) decorateCommit: (PBGitCommit *) commit
97100
// This boolean will tell us if that happened
98101
BOOL addedParent = NO;
99102

100-
int parentIndex;
101-
for (parentIndex = 1; parentIndex < commit.nParents; ++parentIndex) {
102-
git_oid *parent = commit.parentShas + parentIndex;
103+
int parentIndex = 0;
104+
for (parentIndex = 1; parentIndex < nParents; ++parentIndex) {
105+
git_oid parentOID = [[parents objectAtIndex:parentIndex] oid];
103106
int i = 0;
104107
BOOL was_displayed = NO;
105108
std::list<PBGitLane *>::iterator it = currentLanes->begin();
106109
for (; it != currentLanes->end(); ++it) {
107110
i++;
108-
if ((*it)->isCommit(parent)) {
111+
if ((*it)->isCommit(parentOID)) {
109112
add_line(lines, &currentLine, 0, i, newPos,(*it)->index());
110113
was_displayed = YES;
111114
break;
@@ -119,7 +122,7 @@ - (void) decorateCommit: (PBGitCommit *) commit
119122

120123
// Really add this parent
121124
addedParent = YES;
122-
PBGitLane *newLane = new PBGitLane(parent);
125+
PBGitLane *newLane = new PBGitLane(&parentOID);
123126
currentLanes->push_back(newLane);
124127
add_line(lines, &currentLine, 0, currentLanes->size(), newPos, newLane->index());
125128
}
@@ -138,8 +141,8 @@ - (void) decorateCommit: (PBGitCommit *) commit
138141
previous.numColumns = currentLanes->size();
139142

140143
// Update the current lane to point to the new parent
141-
if (currentLane && commit.nParents > 0)
142-
currentLane->setSha(commit.parentShas[0]);
144+
if (currentLane && nParents > 0)
145+
currentLane->setSha([[parents objectAtIndex:0] oid]);
143146
else
144147
currentLanes->remove(currentLane);
145148

PBGitHistoryController.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@class PBRefController;
1818
@class QLPreviewPanel;
1919
@class PBCommitList;
20+
@class PBGitSHA;
2021

2122
@interface PBGitHistoryController : PBViewController {
2223
IBOutlet PBRefController *refController;
@@ -58,7 +59,7 @@
5859
- (IBAction) setTreeView:(id)sender;
5960
- (IBAction) setBranchFilter:(id)sender;
6061

61-
- (void) selectCommit: (NSString*) commit;
62+
- (void)selectCommit:(PBGitSHA *)commit;
6263
- (IBAction) refresh:(id)sender;
6364
- (IBAction) toggleQLPreviewPanel:(id)sender;
6465
- (IBAction) openSelectedFile:(id)sender;

PBGitHistoryController.m

+6-6
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(
236236
if ([repository.currentBranch isSimpleRef])
237237
[self selectCommit:[repository shaForRef:[repository.currentBranch ref]]];
238238
else
239-
[self selectCommit:[[self firstCommit] realSha]];
239+
[self selectCommit:[[self firstCommit] sha]];
240240
return;
241241
}
242242

@@ -385,9 +385,9 @@ - (void) scrollSelectionToTopOfViewFrom:(NSInteger)oldIndex
385385
commitList.useAdjustScroll = NO;
386386
}
387387

388-
- (NSArray *) selectedObjectsForSHA:(NSString *)commitSHA
388+
- (NSArray *) selectedObjectsForSHA:(PBGitSHA *)commitSHA
389389
{
390-
NSPredicate *selection = [NSPredicate predicateWithFormat:@"realSha == %@", commitSHA];
390+
NSPredicate *selection = [NSPredicate predicateWithFormat:@"sha == %@", commitSHA];
391391
NSArray *selectedCommits = [[commitController content] filteredArrayUsingPredicate:selection];
392392

393393
if (([selectedCommits count] == 0) && [self firstCommit])
@@ -396,9 +396,9 @@ - (NSArray *) selectedObjectsForSHA:(NSString *)commitSHA
396396
return selectedCommits;
397397
}
398398

399-
- (void) selectCommit:(NSString *)commitSHA
399+
- (void)selectCommit:(PBGitSHA *)commitSHA
400400
{
401-
if (!forceSelectionUpdate && [[selectedCommit realSha] isEqualToString:commitSHA])
401+
if (!forceSelectionUpdate && [[selectedCommit sha] isEqual:commitSHA])
402402
return;
403403

404404
NSInteger oldIndex = [[commitController selectionIndexes] firstIndex];
@@ -521,7 +521,7 @@ - (NSArray *)menuItemsForPaths:(NSArray *)paths
521521
PBGitRef *headRef = [[repository headRef] ref];
522522
NSString *headRefName = [headRef shortName];
523523
NSString *diffTitle = [NSString stringWithFormat:@"Diff %@ with %@", multiple ? @"files" : @"file", headRefName];
524-
BOOL isHead = [[selectedCommit realSha] isEqualToString:[repository headSHA]];
524+
BOOL isHead = [[selectedCommit sha] isEqual:[repository headSHA]];
525525
NSMenuItem *diffItem = [[NSMenuItem alloc] initWithTitle:diffTitle
526526
action:isHead ? nil : @selector(diffFilesAction:)
527527
keyEquivalent:@""];

PBGitHistoryGrapher.m

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "PBGitHistoryGrapher.h"
1010
#import "PBGitGrapher.h"
11+
#import "PBGitSHA.h"
1112

1213

1314
@implementation PBGitHistoryGrapher
@@ -41,13 +42,13 @@ - (void) graphCommits:(NSArray *)revList
4142
NSInteger counter = 0;
4243

4344
for (PBGitCommit *commit in revList) {
44-
NSString *commitSHA = [commit realSha];
45+
PBGitSHA *commitSHA = [commit sha];
4546
if (viewAllBranches || [searchSHAs containsObject:commitSHA]) {
4647
[grapher decorateCommit:commit];
4748
[commits addObject:commit];
4849
if (!viewAllBranches) {
4950
[searchSHAs removeObject:commitSHA];
50-
[searchSHAs addObjectsFromArray:commit.parents];
51+
[searchSHAs addObjectsFromArray:[commit parents]];
5152
}
5253
}
5354
if (++counter % 2000 == 0) {

0 commit comments

Comments
 (0)