forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBCreateBranchSheet.m
107 lines (73 loc) · 2.73 KB
/
PBCreateBranchSheet.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
//
// PBCreateBranchSheet.m
// GitX
//
// Created by Nathan Kinsinger on 12/13/09.
// Copyright 2009 Nathan Kinsinger. All rights reserved.
//
#import "PBCreateBranchSheet.h"
#import "PBGitRepository.h"
#import "PBGitDefaults.h"
#import "PBGitCommit.h"
#import "PBGitRef.h"
@interface PBCreateBranchSheet ()
- (void) beginCreateBranchSheetAtRefish:(id <PBGitRefish>)ref inRepository:(PBGitRepository *)repo;
@end
@implementation PBCreateBranchSheet
@synthesize repository;
@synthesize startRefish;
@synthesize shouldCheckoutBranch;
@synthesize branchNameField;
@synthesize errorMessageField;
#pragma mark -
#pragma mark PBCreateBranchSheet
+ (void) beginCreateBranchSheetAtRefish:(id <PBGitRefish>)ref inRepository:(PBGitRepository *)repo
{
PBCreateBranchSheet *sheet = [[self alloc] initWithWindowNibName:@"PBCreateBranchSheet"];
[sheet beginCreateBranchSheetAtRefish:ref inRepository:repo];
}
- (void) beginCreateBranchSheetAtRefish:(id <PBGitRefish>)ref inRepository:(PBGitRepository *)repo
{
self.repository = repo;
self.startRefish = ref;
[self window]; // loads the window (if it wasn't already)
[self.errorMessageField setStringValue:@""];
self.shouldCheckoutBranch = [PBGitDefaults shouldCheckoutBranch];
// when creating a local branch tracking a remote branch preset the branch name to the name of the remote branch
if ([self.startRefish refishType] == kGitXRemoteBranchType) {
NSMutableArray *components = [[[self.startRefish shortName] componentsSeparatedByString:@"/"] mutableCopy];
if ([components count] > 1) {
[components removeObjectAtIndex:0];
NSString *branchName = [components componentsJoinedByString:@"/"];
[self.branchNameField setStringValue:branchName];
}
}
[NSApp beginSheet:[self window] modalForWindow:[self.repository.windowController window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
}
#pragma mark IBActions
- (IBAction) createBranch:(id)sender
{
NSString *name = [self.branchNameField stringValue];
PBGitRef *ref = [PBGitRef refFromString:[kGitXBranchRefPrefix stringByAppendingString:name]];
if (![self.repository checkRefFormat:[ref ref]]) {
[self.errorMessageField setStringValue:@"Invalid name"];
[self.errorMessageField setHidden:NO];
return;
}
if ([self.repository refExists:ref]) {
[self.errorMessageField setStringValue:@"Branch already exists"];
[self.errorMessageField setHidden:NO];
return;
}
[self closeCreateBranchSheet:self];
[self.repository createBranch:name atRefish:self.startRefish];
[PBGitDefaults setShouldCheckoutBranch:self.shouldCheckoutBranch];
if (self.shouldCheckoutBranch)
[self.repository checkoutRefish:ref];
}
- (IBAction) closeCreateBranchSheet:(id)sender
{
[NSApp endSheet:[self window]];
[[self window] orderOut:self];
}
@end