Skip to content

GitX Reset implementation #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Classes/Controllers/PBGitResetController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// PBGitResetController.h
// GitX
//
// Created by Tomasz Krasnyk on 10-11-27.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <ObjectiveGit/ObjectiveGit.h>
#import "PBResetSheet.h"

@class PBGitRepository;
@protocol PBGitRefish;

@interface PBGitResetController : NSObject {
__unsafe_unretained PBGitRepository *repository;
}
- (id) initWithRepository:(PBGitRepository *) repo;

// actions
- (void) resetToRefish: (id<PBGitRefish>) spec type: (GTRepositoryResetType) type;
- (void) resetHardToHead;

@end
32 changes: 32 additions & 0 deletions Classes/Controllers/PBGitResetController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// PBGitResetController.m
// GitX
//
// Created by Tomasz Krasnyk on 10-11-27.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "PBGitResetController.h"
#import "PBGitRepository.h"
#import "PBGitRefish.h"
#import "PBResetSheet.h"


@implementation PBGitResetController

- (id) initWithRepository:(PBGitRepository *) repo {
if ((self = [super init])){
repository = repo;
}
return self;
}

- (void) resetHardToHead {
[self resetToRefish: [PBGitRef refFromString: @"HEAD"] type: GTRepositoryResetTypeMixed];
}

- (void) resetToRefish:(id<PBGitRefish>) refish type:(GTRepositoryResetType)type {
[PBResetSheet beginResetSheetForRepository: repository refish: refish andType: type];
}

@end
1 change: 1 addition & 0 deletions Classes/Controllers/PBRefController.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- (void) showConfirmPushRefSheet:(PBGitRef *)ref remote:(PBGitRef *)remoteRef;

- (void) checkout:(PBRefMenuItem *)sender;
- (void) reset:(PBRefMenuItem *)sender;
- (void) merge:(PBRefMenuItem *)sender;
- (void) cherryPick:(PBRefMenuItem *)sender;
- (void) rebaseHeadBranch:(PBRefMenuItem *)sender;
Expand Down
8 changes: 8 additions & 0 deletions Classes/Controllers/PBRefController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "PBCreateTagSheet.h"
#import "PBGitDefaults.h"
#import "PBDiffWindowController.h"
#include "PBGitResetController.h"

#import <ObjectiveGit/ObjectiveGit.h>

Expand Down Expand Up @@ -149,6 +150,13 @@ - (void) checkout:(PBRefMenuItem *)sender
[historyController.repository checkoutRefish:refish];
}

#pragma mark Reset

- (void) reset:(PBRefMenuItem *)sender
{
id <PBGitRefish> refish = [sender refish];
[historyController.repository.resetController resetToRefish: refish type: GTRepositoryResetTypeMixed];
}

#pragma mark Cherry Pick

Expand Down
8 changes: 8 additions & 0 deletions Classes/Views/PBRefMenuItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ + (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitReposito
[items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
[items addObject:[PBRefMenuItem separatorItem]];

NSString *resetTitle = [NSString stringWithFormat:@"Reset %@ to %@…", headRefName, targetRefName];
[items addObject:[PBRefMenuItem itemWithTitle: resetTitle action:@selector(reset:) enabled:YES]];
[items addObject:[PBRefMenuItem separatorItem]];

// create branch
NSString *createBranchTitle = [ref isRemoteBranch] ? [NSString stringWithFormat:@"Create branch that tracks %@…", targetRefName] : @"Create branch…";
[items addObject:[PBRefMenuItem itemWithTitle:createBranchTitle action:@selector(createBranch:) enabled:YES]];
Expand Down Expand Up @@ -153,6 +157,10 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target

[items addObject:[PBRefMenuItem itemWithTitle:@"Checkout Commit" action:@selector(checkout:) enabled:YES]];
[items addObject:[PBRefMenuItem separatorItem]];

NSString *resetTitle = [NSString stringWithFormat:@"Reset %@ to here…", headBranchName];
[items addObject:[PBRefMenuItem itemWithTitle: resetTitle action:@selector(reset:) enabled:YES]];
[items addObject:[PBRefMenuItem separatorItem]];

[items addObject:[PBRefMenuItem itemWithTitle:@"Create Branch…" action:@selector(createBranch:) enabled:YES]];
[items addObject:[PBRefMenuItem itemWithTitle:@"Create Tag…" action:@selector(createTag:) enabled:YES]];
Expand Down
37 changes: 37 additions & 0 deletions Classes/Views/PBResetSheet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// PBResetSheet.h
// GitX
//
// Created by Leszek Slazynski on 11-03-13.
// Copyright 2011 LSL. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <ObjectiveGit/ObjectiveGit.h>

@protocol PBGitRefish;
@class PBGitRepository;

typedef enum PBResetType {
PBResetTypeNone,
PBResetTypeSoft,
PBResetTypeMixed,
PBResetTypeHard,
PBResetTypeMerge,
PBResetTypeKeep
} PBResetType;

@interface PBResetSheet : NSWindowController {
IBOutlet NSSegmentedControl* resetType;
IBOutlet NSTabView* resetDesc;
PBResetType defaultType;
id<PBGitRefish> targetRefish;
PBGitRepository* repository;
}

+ (void) beginResetSheetForRepository:(PBGitRepository*) repo refish:(id<PBGitRefish>)refish andType:(GTRepositoryResetType)type;
- (IBAction)resetBranch:(id)sender;
- (IBAction)cancel:(id)sender;
- (GTRepositoryResetType) getSelectedResetType;

@end
129 changes: 129 additions & 0 deletions Classes/Views/PBResetSheet.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// PBResetSheet.m
// GitX
//
// Created by Leszek Slazynski on 11-03-13.
// Copyright 2011 LSL. All rights reserved.
//

#import "PBResetSheet.h"
#import "PBGitRefish.h"
#import "PBGitRepository.h"
#import "PBGitWindowController.h"
#include "PBGitCommit.h"

#import <ObjectiveGit/ObjectiveGit.h>

@interface PBGitCommit ()
@property (nonatomic, strong, readonly) GTCommit *gtCommit;
@end

static const char* StringFromResetType(GTRepositoryResetType type) {
switch(type)
{
case GTRepositoryResetTypeSoft:
return "soft";
case GTRepositoryResetTypeMixed:
return "mixed";
case GTRepositoryResetTypeHard:
return "hard";
}
}

@implementation PBResetSheet

static PBResetSheet* sheet;

- (void) beginResetSheetForRepository:(PBGitRepository*) repo refish:(id<PBGitRefish>)refish andType:(GTRepositoryResetType)type {
defaultType = type;
targetRefish = refish;
repository = repo;
[NSApp beginSheet: [self window]
modalForWindow: [[repository windowController] window]
modalDelegate: self
didEndSelector: nil
contextInfo: NULL];
}

+ (void) beginResetSheetForRepository:(PBGitRepository*) repo refish:(id<PBGitRefish>)refish andType:(GTRepositoryResetType)type {
if (!sheet) {
sheet = [[self alloc] initWithWindowNibName: @"PBResetSheet"];
}
[sheet beginResetSheetForRepository: repo refish: refish andType: type];
}

- (id) init {
if ( (self = [super initWithWindowNibName: @"PBResetSheet"]) ) {
defaultType = GTRepositoryResetTypeMixed;
}
return self;
}

- (void) windowDidLoad {
[resetType setSelectedSegment: defaultType - 1];
[resetDesc selectTabViewItemAtIndex: defaultType - 1];
}

- (GTRepositoryResetType) getSelectedResetType {
NSInteger selectedSegment = [resetType selectedSegment];

switch (selectedSegment) {
case 0:
return GTRepositoryResetTypeSoft;
case 1:
return GTRepositoryResetTypeMixed;
case 2:
return GTRepositoryResetTypeHard;
default:
NSAssert1(false, @"unknown reset method: %ld", selectedSegment);
return -1;
}
}

- (IBAction)resetBranch:(id)sender {
[NSApp endSheet:[self window]];
[[self window] orderOut:self];
GTRepositoryResetType type = [self getSelectedResetType];

//TODO: show alert and then reset the branch
NSInteger alertRet = [[NSAlert alertWithMessageText:@"Reset"
defaultButton:nil
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"Are you sure you want to perform a %s reset to %@?",
StringFromResetType(type),
[targetRefish refishName]]
runModal];

if(alertRet == NSAlertDefaultReturn)
{
GTRepository* repo = self->repository.gtRepo;
PBGitCommit* commit;

//test if we already have a commit
//if not, resolve to a commit
//this is somewhat ugly, so maybe this can be replaced with better code
if([targetRefish refishType] == kGitXCommitType)
{
commit = targetRefish;
}
else
{
commit = [self->repository commitForRef:targetRefish];
}

NSAssert1(commit != nil, @"could not resolve commit for %@", [targetRefish refishName]);
BOOL success = [repo resetToCommit:commit.gtCommit withResetType:type error:NULL];

NSAssert(success, @"reset was not successful");

[self->repository reloadRefs];
}
}

- (IBAction)cancel:(id)sender {
[NSApp endSheet:[self window]];
[[self window] orderOut:self];
}

@end
3 changes: 3 additions & 0 deletions Classes/git/PBGitRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {

@class PBGitWindowController;
@class PBGitCommit;
@class PBGitResetController;
@class PBGitSHA;
@class PBGitRepositoryWatcher;

@interface PBGitRepository : NSDocument {
__strong PBGitRepositoryWatcher *watcher;
__strong PBGitResetController *resetController;
__strong PBGitRevSpecifier *_headRef; // Caching
__strong PBGitSHA* _headSha;
__strong GTRepository* _gtRepo;
Expand All @@ -57,6 +59,7 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
@property (assign) NSInteger currentBranchFilter;

@property (readonly, strong) PBGitWindowController *windowController;
@property (readonly, strong) PBGitResetController *resetController;
@property (readonly, getter = getIndexURL) NSURL* indexURL;

@property (nonatomic, strong) PBGitHistoryList *revisionList;
Expand Down
3 changes: 3 additions & 0 deletions Classes/git/PBGitRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "PBGitRepositoryWatcher.h"
#import "GitRepoFinder.h"
#import "PBGitSubmodule.h"
#import "PBGitResetController.h"

#import <ObjectiveGit/GTRepository.h>
#import <ObjectiveGit/GTIndex.h>
Expand All @@ -40,6 +41,7 @@ @implementation PBGitRepository

@synthesize revisionList, branchesSet, currentBranch, refs, hasChanged, submodules;
@synthesize currentBranchFilter;
@synthesize resetController;

- (BOOL) isBareRepository
{
Expand Down Expand Up @@ -130,6 +132,7 @@ - (id) init
self.branchesSet = [NSMutableOrderedSet orderedSet];
self.submodules = [NSMutableArray array];
currentBranchFilter = [PBGitDefaults branchFilter];
resetController = [[PBGitResetController alloc] initWithRepository:self];
return self;
}

Expand Down
Loading