From b252de1a761a8eb199f0207e737a9aacb2293c7d Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 21 Oct 2008 23:31:45 +0200 Subject: [PATCH 1/2] CommitView: Allow committing per hunk --- PBGitCommitController.h | 1 + PBGitCommitController.m | 15 +++++++++ PBGitCommitView.xib | 7 ++-- PBWebChangesController.h | 5 ++- PBWebChangesController.m | 29 ++++++++++------- html/test.html | 11 ++++--- html/views/commit/commit.css | 20 ++++++++++++ html/views/commit/commit.js | 62 ++++++++++++++++++++++++++++++++---- 8 files changed, 122 insertions(+), 28 deletions(-) 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); +} From 9e2618c3ea0e0517e156c3e04b9dba356311f361 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 21 Oct 2008 23:31:56 +0200 Subject: [PATCH 2/2] Reorder project --- GitX.xcodeproj/project.pbxproj | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index 5f8b284..9c326a3 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -334,7 +334,6 @@ F56174540E05887E001DCD79 /* Git */ = { isa = PBXGroup; children = ( - F5E424120EA3E4DD0046E362 /* Diff */, F5E927E30E883D6800056E75 /* Commit */, F5E927E10E883D2E00056E75 /* History */, F5945E150E02B0C200706420 /* PBGitRepository.h */, @@ -396,6 +395,8 @@ children = ( F5E424140EA3E4E10046E362 /* PBDiffWindowController.h */, F5E424130EA3E4E10046E362 /* PBDiffWindowController.m */, + F5E424160EA3E4EB0046E362 /* PBWebDiffController.m */, + F5E424170EA3E4EB0046E362 /* PBWebDiffController.h */, ); name = Diff; sourceTree = ""; @@ -417,15 +418,6 @@ name = Graphing; sourceTree = ""; }; - F5E424120EA3E4DD0046E362 /* Diff */ = { - isa = PBXGroup; - children = ( - F5E424160EA3E4EB0046E362 /* PBWebDiffController.m */, - F5E424170EA3E4EB0046E362 /* PBWebDiffController.h */, - ); - name = Diff; - sourceTree = ""; - }; F5E927E10E883D2E00056E75 /* History */ = { isa = PBXGroup; children = ( @@ -449,6 +441,8 @@ F5E927E30E883D6800056E75 /* Commit */ = { isa = PBXGroup; children = ( + 93F7857D0EA3ABF100C1F443 /* PBCommitMessageView.h */, + 93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */, F5E927F60E883E7200056E75 /* PBChangedFile.h */, F5E927F70E883E7200056E75 /* PBChangedFile.m */, F593DF760E9E636C003A8559 /* PBFileChangesTableView.h */, @@ -464,8 +458,6 @@ F59116E80E843BCB0072CCB1 /* PBGitCommitController.m */, F5E927FA0E883F0700056E75 /* PBWebChangesController.h */, F5E927FB0E883F0700056E75 /* PBWebChangesController.m */, - 93F7857D0EA3ABF100C1F443 /* PBCommitMessageView.h */, - 93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */, ); name = Commit; sourceTree = "";