Skip to content

Commit 7b63935

Browse files
heipeiPieter de Bie
authored andcommitted
PBWebChanges: Allow discarding of hunks
This enables a "discard" button for unstaged hunks which simply gets rid of the changes (by using "git apply --reverse"). To avoid repetition, the stageHunk method was split into a more generic processHunk method. The "discard" functionality is called through discardHunk. The NSAlert shown when discarding can be bypassed by pressing "Alt" while clicking the discard-button. Signed-off-by: Johannes Gilger <[email protected]>
1 parent 76f1469 commit 7b63935

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

PBGitCommitController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- (void) readOtherFiles:(NSNotification *)notification;
4343
- (void) readUnstagedFiles:(NSNotification *)notification;
4444
- (void) stageHunk: (NSString *)hunk reverse:(BOOL)reverse;
45+
- (void)discardHunk:(NSString *)hunk;
4546

4647
- (NSString *)parentTree;
4748

PBGitCommitController.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ - (NSArray *) linesFromNotification:(NSNotification *)notification;
1717
- (void) doneProcessingIndex;
1818
- (NSMutableDictionary *)dictionaryForLines:(NSArray *)lines;
1919
- (void) addFilesFromDictionary:(NSMutableDictionary *)dictionary staged:(BOOL)staged tracked:(BOOL)tracked;
20+
- (void)processHunk:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse;
2021
@end
2122

2223
@implementation PBGitCommitController
@@ -366,7 +367,19 @@ - (IBAction) commit:(id) sender
366367

367368
- (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse
368369
{
369-
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"apply", @"--cached", nil];
370+
[self processHunk:hunk stage:TRUE reverse:reverse];
371+
}
372+
373+
- (void)discardHunk:(NSString *)hunk
374+
{
375+
[self processHunk:hunk stage:FALSE reverse:TRUE];
376+
}
377+
378+
- (void)processHunk:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse
379+
{
380+
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"apply", nil];
381+
if (stage)
382+
[array addObject:@"--cached"];
370383
if (reverse)
371384
[array addObject:@"--reverse"];
372385

@@ -382,4 +395,5 @@ - (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse
382395
// TODO: We should do this smarter by checking if the file diff is empty, which is faster.
383396
[self refresh:self];
384397
}
398+
385399
@end

PBWebChangesController.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ - (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse
8787
[self refresh];
8888
}
8989

90+
- (void)discardHunk:(NSString *)hunk altKey:(BOOL)altKey
91+
{
92+
int ret = NSAlertDefaultReturn;
93+
if (!altKey) {
94+
ret = [[NSAlert alertWithMessageText:@"Discard hunk"
95+
defaultButton:nil
96+
alternateButton:@"Cancel"
97+
otherButton:nil
98+
informativeTextWithFormat:@"Are you sure you wish to discard the changes in this hunk?\n\n You cannot undo this operation."] runModal];
99+
}
100+
101+
if (ret == NSAlertDefaultReturn) {
102+
[controller discardHunk:hunk];
103+
[self refresh];
104+
}
105+
}
106+
90107
- (void) setStateMessage:(NSString *)state
91108
{
92109
id script = [view windowScriptObject];

html/views/commit/commit.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ table.diff {
6060
float: right;
6161
}
6262

63-
.diff a.stagebutton {
63+
.diff a.hunkbutton {
6464
width: 40px;
6565
padding: 0 2px 0 2px;
6666
margin-bottom: 4px;
67-
margin-right: 10px;
67+
margin-right: 1px;
68+
float: right;
6869

6970
border: 1px solid #3465a4;
7071
background-color: #cce5ff;

html/views/commit/commit.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,28 @@ var displayDiff = function(diff, cached)
8787
for (i = 0; i < hunkHeaders.length; ++i) {
8888
var header = hunkHeaders[i];
8989
if (cached)
90-
header.innerHTML = "<a href='#' class='stagebutton' onclick='addHunk(this, true); return false'>Unstage</a>" + header.innerHTML;
91-
else
92-
header.innerHTML = "<a href='#' class='stagebutton' onclick='addHunk(this, false); return false'>Stage</a>" + header.innerHTML;
90+
header.innerHTML = "<a href='#' class='hunkbutton' onclick='addHunk(this, true); return false'>Unstage</a>" + header.innerHTML;
91+
else {
92+
header.innerHTML = "<a href='#' class='hunkbutton' onclick='addHunk(this, false); return false'>Stage</a>" + header.innerHTML;
93+
header.innerHTML = "<a href='#' class='hunkbutton' onclick='discardHunk(this, event); return false'>Discard</a>" + header.innerHTML;
94+
}
9395
}
9496
}
9597

96-
var addHunk = function(hunk, reverse)
98+
var getNextText = function(element)
99+
{
100+
// gets the next DOM sibling which has type "text" (e.g. our hunk-header)
101+
next = element;
102+
while (next.nodeType != 3) {
103+
next = next.nextSibling;
104+
}
105+
return next;
106+
}
107+
108+
var getHunkText = function(hunk)
97109
{
98-
hunkHeader = hunk.nextSibling.data.split("\n")[0];
110+
hunk = getNextText(hunk);
111+
hunkHeader = hunk.data.split("\n")[0];
99112
if (m = hunkHeader.match(/@@.*@@/))
100113
hunkHeader = m;
101114

@@ -108,11 +121,29 @@ var addHunk = function(hunk, reverse)
108121
if (end == -1)
109122
end = originalDiff.length;
110123

111-
hunkText = originalDiff.substring(start, end);
124+
var hunkText = originalDiff.substring(start, end);
112125
hunkText = diffHeader + "\n" + hunkText + "\n";
113126

127+
return hunkText;
128+
}
129+
130+
var addHunk = function(hunk, reverse)
131+
{
132+
var hunkText = getHunkText(hunk);
133+
114134
if (Controller.stageHunk_reverse_)
115135
Controller.stageHunk_reverse_(hunkText, reverse);
116136
else
117137
alert(hunkText);
118138
}
139+
140+
var discardHunk = function(hunk, event)
141+
{
142+
var hunkText = getHunkText(hunk);
143+
144+
if (Controller.discardHunk_altKey_) {
145+
Controller.discardHunk_altKey_(hunkText, event.altKey == true);
146+
} else {
147+
alert(hunkText);
148+
}
149+
}

0 commit comments

Comments
 (0)