Skip to content

Commit 73cf163

Browse files
muhqutiennou
authored andcommitted
added stashes property to PBGitRepository.
As Objective-Git doesn't yet offer stash support use libgit2 right away. should be replaced by Objective-Git equivalent when available
1 parent 65ebec8 commit 73cf163

File tree

8 files changed

+157
-1
lines changed

8 files changed

+157
-1
lines changed

Classes/git/PBGitRef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ extern NSString * const kGitXTagType;
1313
extern NSString * const kGitXBranchType;
1414
extern NSString * const kGitXRemoteType;
1515
extern NSString * const kGitXRemoteBranchType;
16+
extern NSString * const kGitXStashType;
1617

1718
extern NSString * const kGitXTagRefPrefix;
1819
extern NSString * const kGitXBranchRefPrefix;
1920
extern NSString * const kGitXRemoteRefPrefix;
21+
extern NSString * const kGitXStashRefPrefix;
2022

2123

2224
@interface PBGitRef : NSObject <PBGitRefish>
@@ -36,6 +38,7 @@ extern NSString * const kGitXRemoteRefPrefix;
3638
- (BOOL) isTag;
3739
- (BOOL) isRemote;
3840
- (BOOL) isRemoteBranch;
41+
- (BOOL) isStash;
3942

4043
- (PBGitRef *) remoteRef;
4144

Classes/git/PBGitRef.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
NSString * const kGitXBranchType = @"branch";
1414
NSString * const kGitXRemoteType = @"remote";
1515
NSString * const kGitXRemoteBranchType = @"remote branch";
16+
NSString * const kGitXStashType = @"stash";
1617

1718
NSString * const kGitXTagRefPrefix = @"refs/tags/";
1819
NSString * const kGitXBranchRefPrefix = @"refs/heads/";
1920
NSString * const kGitXRemoteRefPrefix = @"refs/remotes/";
21+
NSString * const kGitXStashRefPrefix = @"refs/stash@";
2022

2123
@interface PBGitRef ()
2224

@@ -68,6 +70,8 @@ - (NSString *) type
6870
return @"tag";
6971
if ([self isRemote])
7072
return @"remote";
73+
if ([self isStash])
74+
return @"stash";
7175
return nil;
7276
}
7377

@@ -94,6 +98,11 @@ - (BOOL) isRemoteBranch
9498
return ([[ref componentsSeparatedByString:@"/"] count] > 3);
9599
}
96100

101+
- (BOOL) isStash
102+
{
103+
return [ref hasPrefix:kGitXStashRefPrefix];
104+
}
105+
97106
- (BOOL) isEqualToRef:(PBGitRef *)otherRef
98107
{
99108
return [ref isEqualToString:[otherRef ref]];
@@ -141,6 +150,8 @@ - (NSString *) refishName
141150

142151
- (NSString *) shortName
143152
{
153+
if ([self isStash])
154+
return [ref substringFromIndex:5];
144155
if ([self type])
145156
return [ref substringFromIndex:[[self type] length] + 7];
146157
return ref;
@@ -156,6 +167,8 @@ - (NSString *) refishType
156167
return kGitXRemoteBranchType;
157168
if ([self isRemote])
158169
return kGitXRemoteType;
170+
if ([self isStash])
171+
return kGitXStashType;
159172
return nil;
160173
}
161174

Classes/git/PBGitRepository.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@class PBGitRevSpecifier;
1313
@protocol PBGitRefish;
1414
@class PBGitRef;
15+
@class PBGitStash;
1516
@class GTRepository;
1617
@class GTConfiguration;
1718

@@ -61,6 +62,7 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
6162
@property (readonly, getter = getIndexURL) NSURL* indexURL;
6263

6364
@property (nonatomic, strong) PBGitHistoryList *revisionList;
65+
@property (nonatomic, readonly, strong) NSArray* stashes;
6466
@property (nonatomic, readonly, strong) NSArray* branches;
6567
@property (nonatomic, strong) NSMutableOrderedSet* branchesSet;
6668
@property (nonatomic, strong) PBGitRevSpecifier* currentBranch;
@@ -120,6 +122,7 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
120122
- (PBGitCommit *)commitForOID:(GTOID *)sha;
121123
- (BOOL)isOIDOnSameBranch:(GTOID *)baseOID asOID:(GTOID *)testOID;
122124
- (BOOL)isOIDOnHeadBranch:(GTOID *)testOID;
125+
- (PBGitStash *)stashForRef:(PBGitRef *)ref;
123126
- (BOOL)isRefOnHeadBranch:(PBGitRef *)testRef;
124127
- (BOOL)checkRefFormat:(NSString *)refName;
125128
- (BOOL)refExists:(PBGitRef *)ref;

Classes/git/PBGitRepository.m

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#import "PBGitRepositoryWatcher.h"
2424
#import "GitRepoFinder.h"
2525
#import "PBGitHistoryList.h"
26-
26+
#import "PBGitStash.h"
2727

2828
NSString *PBGitRepositoryDocumentType = @"Git Repository";
2929

@@ -616,6 +616,33 @@ - (NSString *)workingDirectory
616616
return [self.workingDirectoryURL path];
617617
}
618618

619+
#pragma mark Stashes
620+
621+
- (NSArray *) stashes
622+
{
623+
NSMutableArray *stashes = [NSMutableArray array];
624+
[self.gtRepo enumerateStashesUsingBlock:^(NSUInteger index, NSString *message, GTOID *oid, BOOL *stop) {
625+
PBGitStash *stash = [[PBGitStash alloc] initWithRepository:self stashOID:oid index:index message:message];
626+
[stashes addObject:stash];
627+
}];
628+
return [NSArray arrayWithArray:stashes];
629+
}
630+
631+
- (PBGitStash *)stashForRef:(PBGitRef *)ref {
632+
__block PBGitStash * found = nil;
633+
634+
[self.gtRepo enumerateStashesUsingBlock:^(NSUInteger index, NSString *message, GTOID *oid, BOOL *stop) {
635+
PBGitStash *stash = [[PBGitStash alloc] initWithRepository:self stashOID:oid index:index message:message];
636+
if ([stash.ref isEqualToRef:ref]) {
637+
found = stash;
638+
stop = YES;
639+
}
640+
}];
641+
return found;
642+
}
643+
644+
645+
619646
#pragma mark Remotes
620647

621648
- (NSArray *) remotes

Classes/git/PBGitSVStashItem.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// PBSourceViewStash.m
3+
// GitX
4+
//
5+
// Created by Mathias Leppich on 8/1/13.
6+
//
7+
//
8+
9+
#import "PBGitSVStashItem.h"
10+
#import "PBGitRevSpecifier.h"
11+
12+
13+
@implementation PBGitSVStashItem
14+
15+
+ (id)itemWithStash:(PBGitStash *)stash
16+
{
17+
NSString * title = [NSString stringWithFormat:@"@{%zd}: %@", stash.index, stash.message];
18+
PBGitSVStashItem * item = [self itemWithTitle:title];
19+
item.stash = stash;
20+
item.revSpecifier = [[PBGitRevSpecifier alloc] initWithRef:stash.ref];
21+
return item;
22+
}
23+
24+
-(PBGitRef *)ref {
25+
return self.stash.ref;
26+
}
27+
28+
@end

Classes/git/PBGitStash.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// PBGitStash.h
3+
// GitX
4+
//
5+
// Created by Mathias Leppich on 8/1/13.
6+
//
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <ObjectiveGit/ObjectiveGit.h>
11+
12+
@class PBGitCommit;
13+
@class PBGitRef;
14+
@class PBGitRepository;
15+
16+
@interface PBGitStash : NSObject
17+
@property (nonatomic, readonly) size_t index;
18+
@property (nonatomic, readonly) PBGitCommit * commit;
19+
@property (nonatomic, readonly) NSString* message;
20+
@property (nonatomic, readonly) PBGitRef* ref;
21+
22+
@property (nonatomic, readonly) PBGitCommit * indexCommit;
23+
@property (nonatomic, readonly) PBGitCommit * ancestorCommit;
24+
25+
- (id) initWithRepository:(PBGitRepository *)repo stashOID:(GTOID *)stashOID index:(NSInteger)index message:(NSString *)message;
26+
27+
@end

Classes/git/PBGitStash.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// PBGitStash.m
3+
// GitX
4+
//
5+
// Created by Mathias Leppich on 8/1/13.
6+
//
7+
//
8+
9+
#import "PBGitStash.h"
10+
#import "PBGitRef.h"
11+
#import "PBGitCommit.h"
12+
#import "PBGitRepository.h"
13+
14+
@implementation PBGitStash
15+
16+
-(id)initWithRepository:(PBGitRepository *)repo stashOID:(GTOID *)stashOID index:(NSInteger)index message:(NSString *)message
17+
{
18+
_index = index;
19+
_message = message;
20+
21+
GTRepository * gtRepo = repo.gtRepo;
22+
NSError * error = nil;
23+
GTCommit * gtCommit = (GTCommit *)[gtRepo lookUpObjectByOID:stashOID objectType:GTObjectTypeCommit error:&error];
24+
NSArray * parents = [gtCommit parents];
25+
GTCommit * gtIndexCommit = [parents objectAtIndex:1];
26+
GTCommit * gtAncestorCommit = [parents objectAtIndex:0];
27+
28+
_commit = [[PBGitCommit alloc] initWithRepository:repo andCommit:gtCommit];
29+
_indexCommit = [[PBGitCommit alloc] initWithRepository:repo andCommit:gtIndexCommit];
30+
_ancestorCommit = [[PBGitCommit alloc] initWithRepository:repo andCommit:gtAncestorCommit];
31+
32+
NSLog(@" stash: %zd, %@, %@, %@",_index,[_commit shortName], [_ancestorCommit shortName], [_indexCommit shortName]);
33+
return self;
34+
}
35+
36+
-(NSString *)description
37+
{
38+
return [NSString stringWithFormat:@"stash@{%zd}: %@", _index, _message];
39+
}
40+
41+
-(PBGitRef *)ref
42+
{
43+
NSString * refStr = [NSString stringWithFormat:@"refs/stash@{%zd}", _index];
44+
return [[PBGitRef alloc] initWithString:refStr];
45+
}
46+
47+
@end

GitX.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
97CF01F918A6C5BB00E30F2B /* deleted_file.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 97CF01F618A6C5BB00E30F2B /* deleted_file.pdf */; };
167167
97CF01FA18A6C5BB00E30F2B /* empty_file.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 97CF01F718A6C5BB00E30F2B /* empty_file.pdf */; };
168168
97CF01FB18A6C5BB00E30F2B /* new_file.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 97CF01F818A6C5BB00E30F2B /* new_file.pdf */; };
169+
A2F8D0DF17AAB32500580B84 /* PBGitStash.m in Sources */ = {isa = PBXBuildFile; fileRef = A2F8D0DE17AAB32500580B84 /* PBGitStash.m */; };
169170
D87127011229A21C00012334 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D87127001229A21C00012334 /* QuartzCore.framework */; };
170171
D89E9B141218BA260097A90B /* ScriptingBridge.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D89E9AB21218A9DA0097A90B /* ScriptingBridge.framework */; };
171172
D8E3B2B810DC9FB2001096A3 /* ScriptingBridge.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D8E3B2B710DC9FB2001096A3 /* ScriptingBridge.framework */; };
@@ -563,6 +564,8 @@
563564
97CF01F618A6C5BB00E30F2B /* deleted_file.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = deleted_file.pdf; sourceTree = "<group>"; };
564565
97CF01F718A6C5BB00E30F2B /* empty_file.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = empty_file.pdf; sourceTree = "<group>"; };
565566
97CF01F818A6C5BB00E30F2B /* new_file.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = new_file.pdf; sourceTree = "<group>"; };
567+
A2F8D0DD17AAB32500580B84 /* PBGitStash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitStash.h; sourceTree = "<group>"; };
568+
A2F8D0DE17AAB32500580B84 /* PBGitStash.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitStash.m; sourceTree = "<group>"; };
566569
D87127001229A21C00012334 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
567570
D89E9AB21218A9DA0097A90B /* ScriptingBridge.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ScriptingBridge.framework; path = System/Library/Frameworks/ScriptingBridge.framework; sourceTree = SDKROOT; };
568571
D8E3B2B710DC9FB2001096A3 /* ScriptingBridge.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ScriptingBridge.framework; path = /System/Library/Frameworks/ScriptingBridge.framework; sourceTree = "<absolute>"; };
@@ -903,6 +906,10 @@
903906
4A5D768214A9A9CC00DF6C68 /* PBGitXErrors.m */,
904907
4A5D768314A9A9CC00DF6C68 /* PBGitXProtocol.h */,
905908
4A5D768414A9A9CC00DF6C68 /* PBGitXProtocol.m */,
909+
643952721603E9BC00BB7AFF /* PBGitSubmodule.h */,
910+
643952731603E9BC00BB7AFF /* PBGitSubmodule.m */,
911+
A2F8D0DD17AAB32500580B84 /* PBGitStash.h */,
912+
A2F8D0DE17AAB32500580B84 /* PBGitStash.m */,
906913
643952751603EF9B00BB7AFF /* PBGitSVSubmoduleItem.h */,
907914
643952761603EF9B00BB7AFF /* PBGitSVSubmoduleItem.m */,
908915
4AB057E11652652000DE751D /* GitRepoFinder.h */,
@@ -1397,6 +1404,7 @@
13971404
643952771603EF9B00BB7AFF /* PBGitSVSubmoduleItem.m in Sources */,
13981405
4AB057E31652652000DE751D /* GitRepoFinder.m in Sources */,
13991406
4A2125A417C0C78A00B5B582 /* NSColor+RGB.m in Sources */,
1407+
A2F8D0DF17AAB32500580B84 /* PBGitStash.m in Sources */,
14001408
);
14011409
runOnlyForDeploymentPostprocessing = 0;
14021410
};

0 commit comments

Comments
 (0)