[gitg/vala] Added support for showing diff for commit



commit ce3347b8ec7297fc5857004bc4529bf9462d5b28
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Wed Jul 11 08:44:23 2012 +0200

    Added support for showing diff for commit

 libgitg-gtk/gitg-gtk-diff-view-request-diff.vala |   49 ++++++++++++++++++-
 libgitg-gtk/gitg-gtk-diff-view.vala              |   59 +++++++++++++++++++++-
 libgitg-gtk/resources/base.js                    |    9 +++-
 libgitg-gtk/test.vala                            |    7 +--
 4 files changed, 115 insertions(+), 9 deletions(-)
---
diff --git a/libgitg-gtk/gitg-gtk-diff-view-request-diff.vala b/libgitg-gtk/gitg-gtk-diff-view-request-diff.vala
index 86e87c0..4a327bf 100644
--- a/libgitg-gtk/gitg-gtk-diff-view-request-diff.vala
+++ b/libgitg-gtk/gitg-gtk-diff-view-request-diff.vala
@@ -147,6 +147,44 @@ namespace GitgGtk
 			builder.end_object();
 		}
 
+		private void signature_to_json(Json.Builder builder, Ggit.Signature sig)
+		{
+			builder.begin_object();
+
+			builder.set_member_name("name");
+			builder.add_string_value(sig.get_name());
+
+			builder.set_member_name("email");
+			builder.add_string_value(sig.get_email());
+
+			builder.set_member_name("time");
+			builder.add_int_value(sig.get_time().to_unix());
+
+			builder.end_object();
+		}
+
+		private void commit_to_json(Json.Builder builder, Ggit.Commit commit)
+		{
+			builder.begin_object();
+
+			builder.set_member_name("id");
+			builder.add_string_value(commit.get_id().to_string());
+
+			builder.set_member_name("subject");
+			builder.add_string_value(commit.get_subject());
+
+			builder.set_member_name("message");
+			builder.add_string_value(commit.get_message());
+
+			builder.set_member_name("committer");
+			signature_to_json(builder, commit.get_committer());
+
+			builder.set_member_name("author");
+			signature_to_json(builder, commit.get_author());
+
+			builder.end_object();
+		}
+
 		private InputStream? run_diff(Ggit.Diff? diff, Cancellable? cancellable) throws GLib.Error
 		{
 			if (diff == null)
@@ -158,7 +196,15 @@ namespace GitgGtk
 			var builder = new Json.Builder();
 			DiffState state = new DiffState();
 
-			builder.begin_array();
+			builder.begin_object();
+
+			if (d_view.commit != null)
+			{
+				builder.set_member_name("commit");
+				commit_to_json(builder, d_view.commit);
+			}
+
+			builder.set_member_name("diff").begin_array();
 
 			diff.foreach(
 				(delta, progress) => {
@@ -210,6 +256,7 @@ namespace GitgGtk
 			}
 
 			builder.end_array();
+			builder.end_object();
 
 			var gen = new Json.Generator();
 			gen.set_root(builder.get_root());
diff --git a/libgitg-gtk/gitg-gtk-diff-view.vala b/libgitg-gtk/gitg-gtk-diff-view.vala
index d590f07..82cd721 100644
--- a/libgitg-gtk/gitg-gtk-diff-view.vala
+++ b/libgitg-gtk/gitg-gtk-diff-view.vala
@@ -22,12 +22,14 @@ namespace GitgGtk
 	public class DiffView : WebKit.WebView
 	{
 		private Ggit.Diff? d_diff;
+		private Ggit.Commit? d_commit;
 
 		private static Gee.HashMap<string, GitgGtk.DiffView> s_diffmap;
 		private static uint64 s_diff_id;
 
 		public File? custom_css { get; construct; }
 		public File? custom_js { get; construct; }
+		public Ggit.DiffOptions? options { get; construct set; }
 
 		private bool d_loaded;
 
@@ -37,6 +39,20 @@ namespace GitgGtk
 			set
 			{
 				d_diff = value;
+				d_commit = null;
+
+				update();
+			}
+		}
+
+		public Ggit.Commit? commit
+		{
+			get { return d_commit; }
+			set
+			{
+				d_commit = value;
+				d_diff = null;
+
 				update();
 			}
 		}
@@ -96,12 +112,51 @@ namespace GitgGtk
 
 		private void update()
 		{
-			if (!d_loaded || d_diff == null)
+			if (!d_loaded || (d_diff == null && d_commit == null))
 			{
 				return;
 			}
 
-			execute_script("update_diff();");
+			if (d_commit != null)
+			{
+				d_diff = null;
+
+				var repo = d_commit.get_owner();
+
+				try
+				{
+					var parents = d_commit.get_parents();
+
+					// Create a new diff from the parents to the commit tree
+					for (var i = 0; i < parents.size(); ++i)
+					{
+						var parent = parents.get(0);
+
+						if (i == 0)
+						{
+							d_diff = new Ggit.Diff.tree_to_tree(repo,
+							                                    options,
+							                                    parent.get_tree(),
+							                                    d_commit.get_tree());
+						}
+						else
+						{
+							var d = new Ggit.Diff.tree_to_tree(repo,
+							                                   options,
+							                                   parent.get_tree(),
+							                                   d_commit.get_tree());
+
+							d_diff.merge(d);
+						}
+					}
+				}
+				catch {}
+			}
+
+			if (d_diff != null)
+			{
+				execute_script("update_diff();");
+			}
 		}
 	}
 }
diff --git a/libgitg-gtk/resources/base.js b/libgitg-gtk/resources/base.js
index 61fbd1d..3932a4b 100644
--- a/libgitg-gtk/resources/base.js
+++ b/libgitg-gtk/resources/base.js
@@ -75,9 +75,9 @@ function write_diff(res)
 	var content = $('#diff');
 	content.empty();
 
-	for (var i = 0; i < res.length; ++i)
+	for (var i = 0; i < res.diff.length; ++i)
 	{
-		content.append(diff_file(res[i]));
+		content.append(diff_file(res.diff[i]));
 	}
 }
 
@@ -88,6 +88,11 @@ function update_diff()
 	r.onload = function(e) {
 		j = JSON.parse(r.responseText);
 
+		if ('commit' in j)
+		{
+			console.log(JSON.stringify(j.commit, null, 2));
+		}
+
 		write_diff(j);
 	}
 
diff --git a/libgitg-gtk/test.vala b/libgitg-gtk/test.vala
index 6d13052..a66b2ef 100644
--- a/libgitg-gtk/test.vala
+++ b/libgitg-gtk/test.vala
@@ -19,14 +19,15 @@ class Test
 			return;
 		}
 
-		var opts = new Ggit.DiffOptions(Ggit.DiffFlags.NORMAL,
+		v.options = new Ggit.DiffOptions(Ggit.DiffFlags.NORMAL,
 		                                3,
 		                                3,
 		                                null,
 		                                null,
 		                                null);
 
-		var diff = new Ggit.Diff.workdir_to_index(repo, opts);
+		var commit = repo.get_head().lookup() as Ggit.Commit;
+		v.commit = commit;
 
 		v.key_press_event.connect((vv, ev) => {
 			var state = ev.state & Gtk.accelerator_get_default_mod_mask();
@@ -42,8 +43,6 @@ class Test
 			}
 		});
 
-		v.diff = diff;
-
 		wnd.add(sw);
 		wnd.show_all();
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]