Skip to content

Instantly share code, notes, and snippets.

@pieter
Created May 29, 2009 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pieter/119903 to your computer and use it in GitHub Desktop.
Save pieter/119903 to your computer and use it in GitHub Desktop.
From 8fcde7e91237ccdde4c39b22d512fc62f1c28288 Mon Sep 17 00:00:00 2001
From: Pieter de Bie <pdebie@ai.rug.nl>
Date: Fri, 29 May 2009 12:18:49 +0100
Subject: [PATCH] WIP: Allow reverting of hunks
---
PBGitCommitController.m | 18 ++++++++++++++++++
PBWebChangesController.m | 6 ++++++
html/views/commit/commit.js | 28 +++++++++++++++++++++++++---
3 files changed, 49 insertions(+), 3 deletions(-)
diff --git a/PBGitCommitController.m b/PBGitCommitController.m
index 54859e8..74cf620 100644
--- a/PBGitCommitController.m
+++ b/PBGitCommitController.m
@@ -382,4 +382,22 @@
// TODO: We should do this smarter by checking if the file diff is empty, which is faster.
[self refresh:self];
}
+
+-(void)revertHunk:(NSString *)hunk
+{
+ NSMutableArray *array = [NSMutableArray arrayWithObjects:@"apply", @"--reverse", nil];
+
+ int ret = 1;
+ NSString *error = [repository outputForArguments:array
+ inputString:hunk
+ retValue:&ret];
+
+ // FIXME: show this error, rather than just logging it
+ if (ret)
+ NSLog(@"Error: %@", error);
+
+ // TODO: We should do this smarter by checking if the file diff is empty, which is faster.
+ [self refresh:self];
+}
+
@end
diff --git a/PBWebChangesController.m b/PBWebChangesController.m
index 1e2dd0d..681b34e 100644
--- a/PBWebChangesController.m
+++ b/PBWebChangesController.m
@@ -87,6 +87,12 @@
[self refresh];
}
+- (void) revertHunk:(NSString *)hunk
+{
+ [controller revertHunk:hunk];
+ [self refresh];
+}
+
- (void) setStateMessage:(NSString *)state
{
id script = [view windowScriptObject];
diff --git a/html/views/commit/commit.js b/html/views/commit/commit.js
index 4143540..2443661 100644
--- a/html/views/commit/commit.js
+++ b/html/views/commit/commit.js
@@ -88,12 +88,14 @@ var displayDiff = function(diff, cached)
var header = hunkHeaders[i];
if (cached)
header.innerHTML = "<a href='#' class='stagebutton' onclick='addHunk(this, true); return false'>Unstage</a>" + header.innerHTML;
- else
+ else {
header.innerHTML = "<a href='#' class='stagebutton' onclick='addHunk(this, false); return false'>Stage</a>" + header.innerHTML;
+ header.innerHTML = "<a href='#' class='stagebutton' onclick='revert(this.nextSibling); return false'>Revert</a>" + header.innerHTML;
+ }
}
}
-var addHunk = function(hunk, reverse)
+var getHunkText = function(hunk)
{
hunkHeader = hunk.nextSibling.data.split("\n")[0];
if (m = hunkHeader.match(/@@.*@@/))
@@ -108,11 +110,31 @@ var addHunk = function(hunk, reverse)
if (end == -1)
end = originalDiff.length;
- hunkText = originalDiff.substring(start, end);
+ var hunkText = originalDiff.substring(start, end);
hunkText = diffHeader + "\n" + hunkText + "\n";
+ return hunkText;
+}
+
+var addHunk = function(hunk, reverse)
+{
+ var hunkText = getHunkText(hunk);
+
if (Controller.stageHunk_reverse_)
Controller.stageHunk_reverse_(hunkText, reverse);
else
alert(hunkText);
}
+
+var revert = function(hunk)
+{
+ var hunkText = getHunkText(hunk);
+
+ if (Controller.revertHunk_)
+ Controller.revertHunk_(hunkText);
+ else {
+ alert(hunkText);
+ Controller.log_(hunkText);
+ }
+
+}
\ No newline at end of file
--
1.6.3.1+GitX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment