diff --git a/PBGitCommitController.h b/PBGitCommitController.h index f831748..3a4b5d8 100644 --- a/PBGitCommitController.h +++ b/PBGitCommitController.h @@ -40,6 +40,7 @@ - (void) readCachedFiles:(NSNotification *)notification; - (void) readOtherFiles:(NSNotification *)notification; - (void) readUnstagedFiles:(NSNotification *)notification; +- (void) stageHunk: (NSString *)hunk reverse:(BOOL)reverse; - (NSMenu *) menuForTable:(NSTableView *)table; diff --git a/PBGitCommitController.m b/PBGitCommitController.m index 0a25870..299821b 100644 --- a/PBGitCommitController.m +++ b/PBGitCommitController.m @@ -341,4 +341,19 @@ return a; } + +- (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse +{ + NSMutableArray *array = [NSMutableArray arrayWithObjects:@"apply", @"--cached", nil]; + if (reverse) + [array addObject:@"--reverse"]; + + int ret; + NSString *error = [repository outputForArguments:array + inputString:hunk + retValue: &ret]; + if (ret) + NSLog(@"Error: %@", error); + [self refresh:self]; // TODO: We should do this smarter by checking if the file diff is empty, which is faster. +} @end diff --git a/PBGitCommitView.xib b/PBGitCommitView.xib index 76d760e..b0163e4 100644 --- a/PBGitCommitView.xib +++ b/PBGitCommitView.xib @@ -3,12 +3,12 @@ 1050 9F33 - 672 + 670 949.34 352.00 YES - + YES @@ -708,7 +708,6 @@ YES - YES YES YES YES @@ -1454,7 +1453,7 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilderKit com.apple.InterfaceBuilderKit - {{37, 206}, {852, 432}} + {{312, 206}, {852, 432}} com.apple.InterfaceBuilder.CocoaPlugin diff --git a/PBWebChangesController.h b/PBWebChangesController.h index 89eb554..006621a 100644 --- a/PBWebChangesController.h +++ b/PBWebChangesController.h @@ -15,7 +15,10 @@ IBOutlet NSArrayController *unstagedFilesController; IBOutlet NSArrayController *cachedFilesController; IBOutlet PBGitCommitController *controller; + + PBChangedFile *selectedFile; + BOOL selectedFileIsCached; } -- (void) showDiff:(PBChangedFile *)file cached:(BOOL)cached; +- (void) refresh; @end diff --git a/PBWebChangesController.m b/PBWebChangesController.m index 40ec2e5..461f710 100644 --- a/PBWebChangesController.m +++ b/PBWebChangesController.m @@ -12,6 +12,9 @@ - (void) awakeFromNib { + selectedFile = nil; + selectedFileIsCached = NO; + startFile = @"commit"; [super awakeFromNib]; @@ -19,12 +22,9 @@ [cachedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"cachedFileSelected"]; } -static PBChangedFile *lastFileSelected = nil; - - (void) didLoad { - if (lastFileSelected) - [self showDiff: lastFileSelected cached:NO]; + [self refresh]; } - (BOOL) amend @@ -46,20 +46,25 @@ static PBChangedFile *lastFileSelected = nil; else [unstagedFilesController setSelectionIndexes:[NSIndexSet indexSet]]; - PBChangedFile *file = [[object selectedObjects] objectAtIndex:0]; + selectedFile = [[object selectedObjects] objectAtIndex:0]; + selectedFileIsCached = object == cachedFilesController; - [self showDiff: file cached: object == cachedFilesController]; + [self refresh]; } -- (void) showDiff:(PBChangedFile *)file cached:(BOOL) cached +- (void) refresh { - if (!finishedLoading) { - lastFileSelected = file; + if (!finishedLoading) return; - } id script = [view windowScriptObject]; - NSLog(@"Showing diff.."); - [script callWebScriptMethod:@"showFileChanges" withArguments:[NSArray arrayWithObjects:file, [NSNumber numberWithBool:cached], nil]]; + [script callWebScriptMethod:@"showFileChanges" + withArguments:[NSArray arrayWithObjects:selectedFile, [NSNumber numberWithBool:selectedFileIsCached], nil]]; +} + +- (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse +{ + [controller stageHunk: hunk reverse:reverse]; + [self refresh]; } @end diff --git a/html/test.html b/html/test.html index 02eabb3..87e304a 100644 --- a/html/test.html +++ b/html/test.html @@ -4,17 +4,18 @@ + + - - -
+
diff --git a/html/views/commit/commit.css b/html/views/commit/commit.css index b343a70..848776e 100644 --- a/html/views/commit/commit.css +++ b/html/views/commit/commit.css @@ -31,4 +31,24 @@ table.diff { #notification { margin-top: 30px; +} + +table.diff tr td a.stagebutton { + display: block; + width: 40px; + padding: 0 2px 0 2px; + margin-bottom: 4px; + margin-right: 10px; + + border: 1px solid #3465a4; + background-color: #cce5ff; + color: #204a87; + + font-family: 'Lucida Grande'; + font-size: 80%; + text-decoration: none; + text-align: center; + + -webkit-border-radius: 2px; + float: left; } \ No newline at end of file diff --git a/html/views/commit/commit.js b/html/views/commit/commit.js index 5e4f7b4..af0fb13 100644 --- a/html/views/commit/commit.js +++ b/html/views/commit/commit.js @@ -12,22 +12,72 @@ var showNewFile = function(file) } var showFileChanges = function(file, cached) { - var diff = $("diff"); - diff.style.display = 'none'; + $("diff").style.display = 'none'; hideNotification(); if (file.status == 0) // New file? return showNewFile(file); + var changes; if (cached) { $("title").innerHTML = "Staged changes for " + file.path; - diff.innerHTML = file.cachedChangesAmend_(Controller.amend()).escapeHTML(); + changes = file.cachedChangesAmend_(Controller.amend()); } else { $("title").innerHTML = "Unstaged changes for " + file.path; - diff.innerHTML = file.unstagedChanges().escapeHTML(); + changes = file.unstagedChanges(); } + if (changes == "") { + notify("This file has no more changes", 1); + return; + } + + displayDiff(changes, cached); + $("diff").style.display = ''; +} + +var diffHeader; +var originalDiff; + +var displayDiff = function(diff, cached) +{ + diffHeader = diff.split("\n").slice(0,4).join("\n"); + originalDiff = diff; + + $("diff").innerHTML = diff.escapeHTML(); highlightDiffs(); - diff.style.display = ''; -} \ No newline at end of file + hunkHeaders = $("diff").getElementsByClassName("hunkheader"); + + for (i = 0; i < hunkHeaders.length; ++i) { + var header = hunkHeaders[i]; + if (cached) + header.innerHTML = "Unstage" + header.innerHTML; + else + header.innerHTML = "Stage" + header.innerHTML; + } +} + +var addHunk = function(hunk, reverse) +{ + hunkHeader = hunk.nextSibling.data.split("\n")[0]; + if (m = hunkHeader.match(/@@.*@@/)) + hunkHeader = m; + + start = originalDiff.indexOf(hunkHeader); + end = originalDiff.indexOf("\n@@", start + 1); + end2 = originalDiff.indexOf("\ndiff", start + 1); + if (end2 < end && end2 > 0) + end = end2; + + if (end == -1) + end = originalDiff.length; + + hunkText = originalDiff.substring(start, end); + hunkText = diffHeader + "\n" + hunkText + "\n"; + + if (Controller.stageHunk_reverse_) + Controller.stageHunk_reverse_(hunkText, reverse); + else + alert(hunkText); +}