From: Nathan Kinsinger <[email protected]>

---
 PBGitCommit.h |    5 +++++
 PBGitCommit.m |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/PBGitCommit.h b/PBGitCommit.h
index 5849cc1..2d54190 100644
--- a/PBGitCommit.h
+++ b/PBGitCommit.h
@@ -26,6 +26,7 @@ extern NSString * const kGitXCommitType;
        NSString* details;
        NSString *_patch;
        NSArray* parents;
+       NSString *realSHA;
 
        int timestamp;
        char sign;
@@ -33,12 +34,16 @@ extern NSString * const kGitXCommitType;
        PBGitRepository* repository;
 }
 
++ (id)commitWithRepository:(PBGitRepository*)repo andSha:(git_oid)newSha;
 - (id)initWithRepository:(PBGitRepository *)repo andSha:(git_oid)sha;
 
 - (void)addRef:(PBGitRef *)ref;
 - (void)removeRef:(id)ref;
+- (BOOL)hasRef:(PBGitRef *)ref;
 
 - (NSString *)realSha;
+- (BOOL)isOnSameBranchAs:(PBGitCommit *)other;
+- (BOOL)isOnHeadBranch;
 
 // <PBGitRefish>
 - (NSString *)refishName;
diff --git a/PBGitCommit.m b/PBGitCommit.m
index 547cfe1..ae59ef6 100644
--- a/PBGitCommit.m
+++ b/PBGitCommit.m
@@ -54,6 +54,11 @@ - (git_oid *)sha
        return &sha;
 }
 
++ (id)commitWithRepository:(PBGitRepository*)repo andSha:(git_oid)newSha
+{
+       return [[[self alloc] initWithRepository:repo andSha:newSha] 
autorelease];
+}
+
 - (id)initWithRepository:(PBGitRepository*) repo andSha:(git_oid)newSha
 {
        details = nil;
@@ -64,10 +69,42 @@ - (id)initWithRepository:(PBGitRepository*) repo 
andSha:(git_oid)newSha
 
 - (NSString *)realSha
 {
-       char *hex = git_oid_mkhex(&sha);
-       NSString *str = [NSString stringWithUTF8String:hex];
-       free(hex);
-       return str;
+       if (!realSHA) {
+               char *hex = git_oid_mkhex(&sha);
+               realSHA = [NSString stringWithUTF8String:hex];
+               free(hex);
+       }
+
+       return realSHA;
+}
+
+- (BOOL)isOnSameBranchAs:(PBGitCommit *)other
+{
+       if (!other)
+               return NO;
+
+       NSString *mySHA = [self realSha];
+       NSString *otherSHA = [other realSha];
+
+       if ([otherSHA isEqualToString:mySHA])
+               return YES;
+
+       NSString *commitRange = [NSString stringWithFormat:@"%...@..%@", mySHA, 
otherSHA];
+       NSString *parentsOutput = [repository outputForArguments:[NSArray 
arrayWithObjects:@"rev-list", @"--parents", @"-1", commitRange, nil]];
+       if ([parentsOutput isEqualToString:@""]) {
+               return NO;
+       }
+
+       NSString *mergeSHA = [repository outputForArguments:[NSArray 
arrayWithObjects:@"merge-base", mySHA, otherSHA, nil]];
+       if ([mergeSHA isEqualToString:mySHA] || [mergeSHA 
isEqualToString:otherSHA])
+               return YES;
+
+       return NO;
+}
+
+- (BOOL)isOnHeadBranch
+{
+       return [self isOnSameBranchAs:[repository headCommit]];
 }
 
 // FIXME: Remove this method once it's unused.
@@ -108,6 +145,18 @@ - (void)removeRef:(id)ref
        [self.refs removeObject:ref];
 }
 
+- (BOOL)hasRef:(PBGitRef *)ref
+{
+       if (!self.refs)
+               return NO;
+
+       for (PBGitRef *existingRef in self.refs)
+               if ([existingRef isEqual:ref])
+                       return YES;
+
+       return NO;
+}
+
 - (NSMutableArray *)refs
 {
        return [[repository refs] objectForKey:[self realSha]];
-- 
1.7.0.3

To unsubscribe from this group, send email to gitx+unsubscribegooglegroups.com 
or reply to this email with the words "REMOVE ME" as the subject.

Reply via email to