forked from pieter/gitx
-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathPBGitRepository.m
1200 lines (987 loc) · 35 KB
/
PBGitRepository.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PBGitRepository.m
// GitTest
//
// Created by Pieter de Bie on 13-06-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitRepository.h"
#import "PBGitCommit.h"
#import "PBGitWindowController.h"
#import "PBGitBinary.h"
#import "NSFileHandleExt.h"
#import "PBEasyPipe.h"
#import "PBGitRef.h"
#import "PBGitRevSpecifier.h"
#import "PBRemoteProgressSheet.h"
#import "PBGitRevList.h"
#import "PBGitDefaults.h"
#import "GitXScriptingConstants.h"
#import "PBHistorySearchController.h"
#import "PBGitRepositoryWatcher.h"
#import "GitRepoFinder.h"
#import "PBGitSubmodule.h"
#import <ObjectiveGit/GTRepository.h>
#import <ObjectiveGit/GTIndex.h>
#import <ObjectiveGit/GTConfiguration.h>
NSString *PBGitRepositoryDocumentType = @"Git Repository";
@interface PBGitRepository ()
@property (nonatomic, strong) NSNumber *hasSVNRepoConfig;
@end
@implementation PBGitRepository
@synthesize revisionList, branchesSet, currentBranch, refs, hasChanged, submodules;
@synthesize currentBranchFilter;
- (BOOL) isBareRepository
{
return self.gtRepo.isBare;
}
- (BOOL) readHasSVNRemoteFromConfig
{
NSError *error = nil;
GTConfiguration *config = [self.gtRepo configurationWithError:&error];
NSArray *allKeys = config.configurationKeys;
for (NSString *key in allKeys) {
if ([key hasPrefix:@"svn-remote."]) {
return TRUE;
}
}
return false;
}
- (BOOL) hasSVNRemote
{
if (!self.hasSVNRepoConfig) {
self.hasSVNRepoConfig = @([self readHasSVNRemoteFromConfig]);
}
return [self.hasSVNRepoConfig boolValue];
}
// NSFileWrapper is broken and doesn't work when called on a directory containing a large number of directories and files.
//because of this it is safer to implement readFromURL than readFromFileWrapper.
//Because NSFileManager does not attempt to recursively open all directories and file when fileExistsAtPath is called
//this works much better.
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
if (![PBGitBinary path])
{
if (outError) {
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:[PBGitBinary notFoundError]
forKey:NSLocalizedRecoverySuggestionErrorKey];
*outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
}
return NO;
}
BOOL isDirectory = FALSE;
[[NSFileManager defaultManager] fileExistsAtPath:[absoluteURL path] isDirectory:&isDirectory];
if (!isDirectory) {
if (outError) {
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:@"Reading files is not supported."
forKey:NSLocalizedRecoverySuggestionErrorKey];
*outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
}
return NO;
}
NSError *error = nil;
_gtRepo = [GTRepository repositoryWithURL:absoluteURL error:&error];
if (!_gtRepo) {
if (outError) {
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%@ does not appear to be a git repository.", [[self fileURL] path]], NSLocalizedRecoverySuggestionErrorKey,
error, NSUnderlyingErrorKey,
nil];
*outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
}
return NO;
}
revisionList = [[PBGitHistoryList alloc] initWithRepository:self];
[self reloadRefs];
// Setup the FSEvents watcher to fire notifications when things change
watcher = [[PBGitRepositoryWatcher alloc] initWithRepository:self];
return YES;
}
- (NSURL *) gitURL {
return self.gtRepo.gitDirectoryURL;
}
- (id) init
{
self = [super init];
if (!self)
return nil;
self.branchesSet = [NSMutableOrderedSet orderedSet];
self.submodules = [NSMutableArray array];
currentBranchFilter = [PBGitDefaults branchFilter];
return self;
}
- (void)close
{
[revisionList cleanup];
[super close];
}
- (void) forceUpdateRevisions
{
[revisionList forceUpdate];
}
- (BOOL)isDocumentEdited
{
return NO;
}
// The fileURL the document keeps is to the working dir
- (NSString *) displayName
{
if (self.gtRepo.isHEADDetached)
return [NSString stringWithFormat:@"%@ (detached HEAD)", [self projectName]];
return [NSString stringWithFormat:@"%@ (branch: %@)", [self projectName], [[self headRef] description]];
}
- (NSString *) projectName
{
NSString* result = [self.workingDirectory lastPathComponent];
return result;
}
// Get the .gitignore file at the root of the repository
- (NSString*)gitIgnoreFilename
{
return [[self workingDirectory] stringByAppendingPathComponent:@".gitignore"];
}
// Overridden to create our custom window controller
- (void)makeWindowControllers
{
#ifndef CLI
[self addWindowController: [[PBGitWindowController alloc] initWithRepository:self displayDefault:YES]];
#endif
}
- (PBGitWindowController *)windowController
{
if ([[self windowControllers] count] == 0)
return NULL;
return [[self windowControllers] objectAtIndex:0];
}
- (void) addRef:(GTReference*)gtRef
{
GTObject *refTarget = gtRef.resolvedTarget;
if (![refTarget isKindOfClass:[GTObject class]]) {
NSLog(@"Tried to add invalid ref %@ -> %@", gtRef, refTarget);
return;
}
PBGitSHA *sha = [PBGitSHA shaWithOID:refTarget.OID.git_oid];
if (!sha) {
NSLog(@"Couldn't determine sha for ref %@ -> %@", gtRef, refTarget);
return;
}
PBGitRef* ref = [[PBGitRef alloc] initWithString:gtRef.name];
// NSLog(@"addRef %@ %@ at %@", ref.type, gtRef.name, [sha string]);
NSMutableArray* curRefs = refs[sha];
if ( curRefs != nil ) {
if ([curRefs containsObject:ref]) {
NSLog(@"Duplicate ref shouldn't be added: %@", ref);
return;
}
[curRefs addObject:ref];
} else {
refs[sha] = [NSMutableArray arrayWithObject:ref];
}
}
int addSubmoduleName(git_submodule *module, const char* name, void * context)
{
PBGitRepository *me = (__bridge PBGitRepository *)context;
PBGitSubmodule *sub = [[PBGitSubmodule alloc] init];
[sub setWorkingDirectory:me.workingDirectory];
[sub setSubmodule:module];
[me.submodules addObject:sub];
return 0;
}
- (void) loadSubmodules
{
self.submodules = [NSMutableArray array];
git_repository* theRepo = self.gtRepo.git_repository;
if (!theRepo)
{
return;
}
git_submodule_foreach(theRepo, addSubmoduleName, (__bridge void *)self);
}
- (void) reloadRefs
{
// clear out ref caches
_headRef = nil;
_headSha = nil;
self->refs = [NSMutableDictionary dictionary];
NSError* error = nil;
NSArray* allRefs = [self.gtRepo referenceNamesWithError:&error];
// load all named refs
NSMutableOrderedSet *oldBranches = [self.branchesSet mutableCopy];
for (NSString* referenceName in allRefs)
{
GTReference* gtRef =
[[GTReference alloc] initByLookingUpReferenceNamed:referenceName
inRepository:self.gtRepo
error:&error];
if (gtRef == nil)
{
NSLog(@"Reference \"%@\" could not be found in the repository", referenceName);
if (error)
{
NSLog(@"Error loading reference was: %@", error);
}
continue;
}
PBGitRef* gitRef = [PBGitRef refFromString:referenceName];
PBGitRevSpecifier* revSpec = [[PBGitRevSpecifier alloc] initWithRef:gitRef];
[self addBranch:revSpec];
[self addRef:gtRef];
[oldBranches removeObject:revSpec];
}
for (PBGitRevSpecifier *branch in oldBranches)
if ([branch isSimpleRef] && ![branch isEqual:[self headRef]])
[self removeBranch:branch];
[self loadSubmodules];
[self willChangeValueForKey:@"refs"];
[self didChangeValueForKey:@"refs"];
[[[self windowController] window] setTitle:[self displayName]];
}
- (void) lazyReload
{
if (!hasChanged)
return;
[self.revisionList updateHistory];
hasChanged = NO;
}
- (PBGitRevSpecifier *)headRef
{
if (_headRef)
return _headRef;
NSString* branch = [self parseSymbolicReference: @"HEAD"];
if (branch && [branch hasPrefix:@"refs/heads/"])
_headRef = [[PBGitRevSpecifier alloc] initWithRef:[PBGitRef refFromString:branch]];
else
_headRef = [[PBGitRevSpecifier alloc] initWithRef:[PBGitRef refFromString:@"HEAD"]];
_headSha = [self shaForRef:[_headRef ref]];
return _headRef;
}
- (PBGitSHA *)headSHA
{
if (! _headSha)
[self headRef];
return _headSha;
}
- (PBGitCommit *)headCommit
{
return [self commitForSHA:[self headSHA]];
}
- (PBGitSHA *)shaForRef:(PBGitRef *)ref
{
if (!ref)
return nil;
for (PBGitSHA *sha in refs.allKeys)
{
NSMutableSet *refsForSha = [refs objectForKey:sha];
for (PBGitRef *existingRef in refsForSha)
{
if ([existingRef isEqualToRef:ref])
{
return sha;
}
}
}
NSError* error = nil;
GTReference* gtRef = [GTReference referenceByLookingUpReferencedNamed:ref.ref
inRepository:self.gtRepo
error:&error];
if (error)
{
NSLog(@"Error looking up ref for %@", ref.ref);
return nil;
}
const git_oid* refOid = gtRef.git_oid;
if (refOid)
{
char buffer[41];
buffer[40] = '\0';
git_oid_fmt(buffer, refOid);
NSString* shaForRef = [NSString stringWithUTF8String:buffer];
PBGitSHA* result = [PBGitSHA shaWithString:shaForRef];
return result;
}
return nil;
}
- (PBGitCommit *)commitForRef:(PBGitRef *)ref
{
if (!ref)
return nil;
return [self commitForSHA:[self shaForRef:ref]];
}
- (PBGitCommit *)commitForSHA:(PBGitSHA *)sha
{
if (!sha)
return nil;
NSArray *revList = revisionList.projectCommits;
if (!revList) {
[revisionList forceUpdate];
revList = revisionList.projectCommits;
}
for (PBGitCommit *commit in revList)
if ([[commit sha] isEqual:sha])
return commit;
return nil;
}
- (BOOL)isOnSameBranch:(PBGitSHA *)branchSHA asSHA:(PBGitSHA *)testSHA
{
if (!branchSHA || !testSHA)
return NO;
if ([testSHA isEqual:branchSHA])
return YES;
NSArray *revList = revisionList.projectCommits;
NSMutableSet *searchSHAs = [NSMutableSet setWithObject:branchSHA];
for (PBGitCommit *commit in revList) {
PBGitSHA *commitSHA = [commit sha];
if ([searchSHAs containsObject:commitSHA]) {
if ([testSHA isEqual:commitSHA])
return YES;
[searchSHAs removeObject:commitSHA];
[searchSHAs addObjectsFromArray:commit.parents];
}
else if ([testSHA isEqual:commitSHA])
return NO;
}
return NO;
}
- (BOOL)isSHAOnHeadBranch:(PBGitSHA *)testSHA
{
if (!testSHA)
return NO;
PBGitSHA *headSHA = [self headSHA];
if ([testSHA isEqual:headSHA])
return YES;
return [self isOnSameBranch:headSHA asSHA:testSHA];
}
- (BOOL)isRefOnHeadBranch:(PBGitRef *)testRef
{
if (!testRef)
return NO;
return [self isSHAOnHeadBranch:[self shaForRef:testRef]];
}
- (BOOL) checkRefFormat:(NSString *)refName
{
BOOL result = [GTReference isValidReferenceName:refName];
return result;
}
- (BOOL) refExists:(PBGitRef *)ref
{
NSError *gtError = nil;
GTReference *gtRef = [GTReference referenceByLookingUpReferencedNamed:ref.ref inRepository:self.gtRepo error:>Error];
if (gtRef) {
return YES;
}
return NO;
}
// useful for getting the full ref for a user entered name
// EX: name: master
// ref: refs/heads/master
- (PBGitRef *)refForName:(NSString *)name
{
if (!name)
return nil;
int retValue = 1;
NSString *output = [self outputInWorkdirForArguments:[NSArray arrayWithObjects:@"show-ref", name, nil] retValue:&retValue];
if (retValue)
return nil;
// the output is in the format: <SHA-1 ID> <space> <reference name>
// with potentially multiple lines if there are multiple matching refs (ex: refs/remotes/origin/master)
// here we only care about the first match
NSArray *refList = [output componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([refList count] > 1) {
NSString *refName = [refList objectAtIndex:1];
return [PBGitRef refFromString:refName];
}
return nil;
}
- (NSArray*)branches
{
return [self.branchesSet array];
}
// Returns either this object, or an existing, equal object
- (PBGitRevSpecifier*) addBranch:(PBGitRevSpecifier*)branch
{
if ([[branch parameters] count] == 0)
branch = [self headRef];
// First check if the branch doesn't exist already
if ([self.branchesSet containsObject:branch]) {
return branch;
}
NSIndexSet *newIndex = [NSIndexSet indexSetWithIndex:[self.branches count]];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:newIndex forKey:@"branches"];
[self.branchesSet addObject:branch];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:newIndex forKey:@"branches"];
return branch;
}
- (BOOL) removeBranch:(PBGitRevSpecifier *)branch
{
if ([self.branchesSet containsObject:branch]) {
NSIndexSet *oldIndex = [NSIndexSet indexSetWithIndex:[self.branches indexOfObject:branch]];
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:oldIndex forKey:@"branches"];
[self.branchesSet removeObject:branch];
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:oldIndex forKey:@"branches"];
return YES;
}
return NO;
}
- (void) readCurrentBranch
{
self.currentBranch = [self addBranch: [self headRef]];
}
- (NSString *) workingDirectory
{
const char* workdir = git_repository_workdir(self.gtRepo.git_repository);
if (workdir)
{
NSString* result = [[NSString stringWithUTF8String:workdir] stringByStandardizingPath];
return result;
}
else
{
return self.fileURL.path;
}
}
#pragma mark Remotes
- (NSArray *) remotes
{
int retValue = 1;
NSString *remotes = [self outputInWorkdirForArguments:[NSArray arrayWithObject:@"remote"] retValue:&retValue];
if (retValue || [remotes isEqualToString:@""])
return nil;
return [remotes componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}
- (BOOL) hasRemotes
{
return ([self remotes] != nil);
}
- (PBGitRef *) remoteRefForBranch:(PBGitRef *)branch error:(NSError **)error
{
if ([branch isRemote]) {
return [branch remoteRef];
}
NSString *branchRef = branch.ref;
if (branchRef) {
NSError *branchError = nil;
GTBranch *gtBranch = [GTBranch branchWithName:branchRef repository:self.gtRepo error:&branchError];
if (gtBranch) {
NSError *trackingError = nil;
BOOL trackingSuccess = NO;
GTBranch *trackingBranch = [gtBranch trackingBranchWithError:&trackingError success:&trackingSuccess];
if (trackingBranch && trackingSuccess) {
NSString *trackingBranchRefName = trackingBranch.reference.name;
PBGitRef *trackingBranchRef = [PBGitRef refFromString:trackingBranchRefName];
return trackingBranchRef;
}
}
}
if (error != NULL) {
NSString *info = [NSString stringWithFormat:@"There is no remote configured for the %@ '%@'.\n\nPlease select a branch from the popup menu, which has a corresponding remote tracking branch set up.\n\nYou can also use a contextual menu to choose a branch by right clicking on its label in the commit history list.", [branch refishType], [branch shortName]];
*error = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"No remote configured for branch", NSLocalizedDescriptionKey,
info, NSLocalizedRecoverySuggestionErrorKey,
nil]];
}
return nil;
}
- (NSString *) infoForRemote:(NSString *)remoteName
{
int retValue = 1;
NSString *output = [self outputInWorkdirForArguments:[NSArray arrayWithObjects:@"remote", @"show", remoteName, nil] retValue:&retValue];
if (retValue)
return nil;
return output;
}
#pragma mark Repository commands
- (void) cloneRepositoryToPath:(NSString *)path bare:(BOOL)isBare
{
if (!path || [path isEqualToString:@""])
return;
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"clone", @"--no-hardlinks", @"--", @".", path, nil];
if (isBare)
[arguments insertObject:@"--bare" atIndex:1];
NSString *description = [NSString stringWithFormat:@"Cloning the repository %@ to %@", [self projectName], path];
NSString *title = @"Cloning Repository";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inRepository:self];
}
- (void) beginAddRemote:(NSString *)remoteName forURL:(NSString *)remoteURL
{
NSArray *arguments = [NSArray arrayWithObjects:@"remote", @"add", @"-f", remoteName, remoteURL, nil];
NSString *description = [NSString stringWithFormat:@"Adding the remote %@ and fetching tracking branches", remoteName];
NSString *title = @"Adding a remote";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inRepository:self];
}
- (void) beginPruneRemoteForRef:(PBGitRef *)ref
{
NSMutableArray *arguments = [NSMutableArray arrayWithObject:@"prune"];
if (![ref isRemote]) {
NSError *error = nil;
ref = [self remoteRefForBranch:ref error:&error];
if (!ref) {
if (error)
[self.windowController showErrorSheet:error];
return;
}
}
NSString *remoteName = [ref remoteName];
[arguments addObject:remoteName];
NSString *description = [NSString stringWithFormat:@"Deleting all stale remote-tracking branches from %@", remoteName];
NSString *title = @"Pruning remote";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inRepository:self];
}
- (void) beginFetchFromRemoteForRef:(PBGitRef *)ref
{
NSMutableArray *arguments = [NSMutableArray arrayWithObject:@"fetch"];
if (![ref isRemote]) {
NSError *error = nil;
ref = [self remoteRefForBranch:ref error:&error];
if (!ref) {
if (error)
[self.windowController showErrorSheet:error];
return;
}
}
NSString *remoteName = [ref remoteName];
[arguments addObject:remoteName];
NSString *description = [NSString stringWithFormat:@"Fetching all tracking branches from %@", remoteName];
NSString *title = @"Fetching from remote";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inRepository:self];
}
- (void) beginPullFromRemote:(PBGitRef *)remoteRef forRef:(PBGitRef *)ref
{
NSMutableArray *arguments = [NSMutableArray arrayWithObject:@"pull"];
// a nil remoteRef means lookup the ref's default remote
if (!remoteRef || ![remoteRef isRemote]) {
NSError *error = nil;
remoteRef = [self remoteRefForBranch:ref error:&error];
if (!remoteRef) {
if (error)
[self.windowController showErrorSheet:error];
return;
}
}
NSString *remoteName = [remoteRef remoteName];
[arguments addObject:remoteName];
NSString *description = [NSString stringWithFormat:@"Pulling all tracking branches from %@", remoteName];
NSString *title = @"Pulling from remote";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inRepository:self hideSuccessScreen:true];
}
- (void) beginPushRef:(PBGitRef *)ref toRemote:(PBGitRef *)remoteRef
{
NSMutableArray *arguments = [NSMutableArray arrayWithObject:@"push"];
// a nil remoteRef means lookup the ref's default remote
if (!remoteRef || ![remoteRef isRemote]) {
NSError *error = nil;
remoteRef = [self remoteRefForBranch:ref error:&error];
if (!remoteRef) {
if (error)
[self.windowController showErrorSheet:error];
return;
}
}
NSString *remoteName = [remoteRef remoteName];
[arguments addObject:remoteName];
NSString *branchName = nil;
if ([ref isRemote] || !ref) {
branchName = @"all updates";
}
else if ([ref isTag]) {
branchName = [NSString stringWithFormat:@"tag '%@'", [ref tagName]];
[arguments addObject:@"tag"];
[arguments addObject:[ref tagName]];
}
else {
branchName = [ref shortName];
[arguments addObject:branchName];
}
NSString *description = [NSString stringWithFormat:@"Pushing %@ to %@", branchName, remoteName];
NSString *title = @"Pushing to remote";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inRepository:self hideSuccessScreen:true];
}
- (BOOL) checkoutRefish:(id <PBGitRefish>)ref
{
NSString *refName = nil;
if ([ref refishType] == kGitXBranchType)
refName = [ref shortName];
else
refName = [ref refishName];
int retValue = 1;
NSArray *arguments = [NSArray arrayWithObjects:@"checkout", refName, nil];
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *message = [NSString stringWithFormat:@"There was an error checking out the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
[self.windowController showErrorSheetTitle:@"Checkout failed!" message:message arguments:arguments output:output];
return NO;
}
[self reloadRefs];
[self readCurrentBranch];
return YES;
}
- (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id <PBGitRefish>)ref
{
if (!files || ([files count] == 0))
return NO;
NSString *refName = nil;
if ([ref refishType] == kGitXBranchType)
refName = [ref shortName];
else
refName = [ref refishName];
int retValue = 1;
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"checkout", refName, @"--", nil];
[arguments addObjectsFromArray:files];
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *message = [NSString stringWithFormat:@"There was an error checking out the file(s) from the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
[self.windowController showErrorSheetTitle:@"Checkout failed!" message:message arguments:arguments output:output];
return NO;
}
return YES;
}
- (BOOL) mergeWithRefish:(id <PBGitRefish>)ref
{
NSString *refName = [ref refishName];
int retValue = 1;
NSArray *arguments = [NSArray arrayWithObjects:@"merge", refName, nil];
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *headName = [[[self headRef] ref] shortName];
NSString *message = [NSString stringWithFormat:@"There was an error merging %@ into %@.", refName, headName];
[self.windowController showErrorSheetTitle:@"Merge failed!" message:message arguments:arguments output:output];
return NO;
}
[self reloadRefs];
[self readCurrentBranch];
return YES;
}
- (BOOL) cherryPickRefish:(id <PBGitRefish>)ref
{
if (!ref)
return NO;
NSString *refName = [ref refishName];
int retValue = 1;
NSArray *arguments = [NSArray arrayWithObjects:@"cherry-pick", refName, nil];
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *message = [NSString stringWithFormat:@"There was an error cherry picking the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
[self.windowController showErrorSheetTitle:@"Cherry pick failed!" message:message arguments:arguments output:output];
return NO;
}
[self reloadRefs];
[self readCurrentBranch];
return YES;
}
- (BOOL) rebaseBranch:(id <PBGitRefish>)branch onRefish:(id <PBGitRefish>)upstream
{
if (!upstream)
return NO;
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"rebase", [upstream refishName], nil];
if (branch)
[arguments addObject:[branch refishName]];
int retValue = 1;
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *branchName = @"HEAD";
if (branch)
branchName = [NSString stringWithFormat:@"%@ '%@'", [branch refishType], [branch shortName]];
NSString *message = [NSString stringWithFormat:@"There was an error rebasing %@ with %@ '%@'.", branchName, [upstream refishType], [upstream shortName]];
[self.windowController showErrorSheetTitle:@"Rebase failed!" message:message arguments:arguments output:output];
return NO;
}
[self reloadRefs];
[self readCurrentBranch];
return YES;
}
- (BOOL) createBranch:(NSString *)branchName atRefish:(id <PBGitRefish>)ref
{
if (!branchName || !ref)
return NO;
int retValue = 1;
NSArray *arguments = [NSArray arrayWithObjects:@"branch", branchName, [ref refishName], nil];
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *message = [NSString stringWithFormat:@"There was an error creating the branch '%@' at %@ '%@'.", branchName, [ref refishType], [ref shortName]];
[self.windowController showErrorSheetTitle:@"Create Branch failed!" message:message arguments:arguments output:output];
return NO;
}
[self reloadRefs];
return YES;
}
- (BOOL) createTag:(NSString *)tagName message:(NSString *)message atRefish:(id <PBGitRefish>)target
{
if (!tagName)
return NO;
NSError *error = nil;
GTObject *object = [self.gtRepo lookupObjectByRefspec:[target refishName] error:&error];
GTTag *newTag = nil;
if (object && !error) {
newTag = [self.gtRepo createTagNamed:tagName target:object tagger:self.gtRepo.userSignatureForNow message:message error:&error];
}
if (!newTag || error) {
[self.windowController showErrorSheet:error];
return NO;
}
[self reloadRefs];
return YES;
}
- (BOOL) deleteRemote:(PBGitRef *)ref
{
if (!ref || ([ref refishType] != kGitXRemoteType))
return NO;
int retValue = 1;
NSArray *arguments = [NSArray arrayWithObjects:@"remote", @"rm", [ref remoteName], nil];
NSString * output = [self outputForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *message = [NSString stringWithFormat:@"There was an error deleting the remote: %@\n\n", [ref remoteName]];
[self.windowController showErrorSheetTitle:@"Delete remote failed!" message:message arguments:arguments output:output];
return NO;
}
// remove the remote's branches
NSString *remoteRef = [kGitXRemoteRefPrefix stringByAppendingString:[ref remoteName]];
for (PBGitRevSpecifier *rev in [self.branchesSet copy]) {
PBGitRef *branch = [rev ref];
if ([[branch ref] hasPrefix:remoteRef]) {
[self removeBranch:rev];
PBGitCommit *commit = [self commitForRef:branch];
[commit removeRef:branch];
}
}
[self reloadRefs];
return YES;
}
- (BOOL) deleteRef:(PBGitRef *)ref
{
if (!ref)
return NO;
if ([ref refishType] == kGitXRemoteType)
return [self deleteRemote:ref];
int retValue = 1;
NSArray *arguments = [NSArray arrayWithObjects:@"update-ref", @"-d", [ref ref], nil];
NSString * output = [self outputForArguments:arguments retValue:&retValue];
if (retValue) {
NSString *message = [NSString stringWithFormat:@"There was an error deleting the ref: %@\n\n", [ref shortName]];
[self.windowController showErrorSheetTitle:@"Delete ref failed!" message:message arguments:arguments output:output];
return NO;
}
[self removeBranch:[[PBGitRevSpecifier alloc] initWithRef:ref]];
PBGitCommit *commit = [self commitForRef:ref];
[commit removeRef:ref];
[self reloadRefs];
return YES;
}
#pragma mark GitX Scripting
- (void)handleRevListArguments:(NSArray *)arguments inWorkingDirectory:(NSURL *)workingDirectory
{
if (![arguments count])
return;
PBGitRevSpecifier *revListSpecifier = nil;
// the argument may be a branch or tag name but will probably not be the full reference
if ([arguments count] == 1) {
PBGitRef *refArgument = [self refForName:[arguments lastObject]];
if (refArgument) {
revListSpecifier = [[PBGitRevSpecifier alloc] initWithRef:refArgument];
revListSpecifier.workingDirectory = workingDirectory;
}
}
if (!revListSpecifier) {
revListSpecifier = [[PBGitRevSpecifier alloc] initWithParameters:arguments];
revListSpecifier.workingDirectory = workingDirectory;
}
self.currentBranch = [self addBranch:revListSpecifier];
[PBGitDefaults setShowStageView:NO];
[self.windowController showHistoryView:self];
}
- (void)handleBranchFilterEventForFilter:(PBGitXBranchFilterType)filter additionalArguments:(NSMutableArray *)arguments inWorkingDirectory:(NSURL *)workingDirectory
{
self.currentBranchFilter = filter;
[PBGitDefaults setShowStageView:NO];
[self.windowController showHistoryView:self];
// treat any additional arguments as a rev-list specifier
if ([arguments count] > 1) {
[arguments removeObjectAtIndex:0];
[self handleRevListArguments:arguments inWorkingDirectory:workingDirectory];
}
}
- (void)handleGitXScriptingArguments:(NSAppleEventDescriptor *)argumentsList inWorkingDirectory:(NSURL *)workingDirectory
{
NSMutableArray *arguments = [NSMutableArray array];
uint argumentsIndex = 1; // AppleEvent list descriptor's are one based
while(1) {
NSAppleEventDescriptor *arg = [argumentsList descriptorAtIndex:argumentsIndex++];
if (arg)
[arguments addObject:[arg stringValue]];
else
break;
}
if (![arguments count])
return;
NSString *firstArgument = [arguments objectAtIndex:0];
if ([firstArgument isEqualToString:@"-c"] || [firstArgument isEqualToString:@"--commit"]) {
[PBGitDefaults setShowStageView:YES];
[self.windowController showCommitView:self];
return;
}
if ([firstArgument isEqualToString:@"--all"]) {
[self handleBranchFilterEventForFilter:kGitXAllBranchesFilter additionalArguments:arguments inWorkingDirectory:workingDirectory];
return;