[gitg/wip/commit] Added class for running hooks



commit 9799c45eecda059688b73a3f40cb04d6a4b20e76
Author: Jesse van den Kieboom <jessevdk gmail com>
Date:   Thu Jul 4 10:22:11 2013 +0200

    Added class for running hooks

 gitg/commit/gitg-commit.vala |   14 ++++-
 libgitg/Makefile.am          |    4 +-
 libgitg/gitg-hook.vala       |  162 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 178 insertions(+), 2 deletions(-)
---
diff --git a/gitg/commit/gitg-commit.vala b/gitg/commit/gitg-commit.vala
index cfdf60f..500e5a6 100644
--- a/gitg/commit/gitg-commit.vala
+++ b/gitg/commit/gitg-commit.vala
@@ -384,6 +384,11 @@ namespace GitgCommit
                        reload();
                }
 
+               private void do_commit(Dialog dlg)
+               {
+                       
+               }
+
                private void on_commit_clicked()
                {
                        var dlg = new Dialog();
@@ -392,7 +397,14 @@ namespace GitgCommit
                        dlg.set_default_response(Gtk.ResponseType.OK);
 
                        dlg.response.connect((d, id) => {
-                               d.destroy();
+                               if (id == Gtk.ResponseType.OK)
+                               {
+                                       do_commit(dlg);
+                               }
+                               else
+                               {
+                                       d.destroy();
+                               }
                        });
 
                        dlg.show();
diff --git a/libgitg/Makefile.am b/libgitg/Makefile.am
index a440222..6cc90db 100644
--- a/libgitg/Makefile.am
+++ b/libgitg/Makefile.am
@@ -27,6 +27,7 @@ AM_VALAFLAGS =                                \
        --pkg webkit2gtk-3.0            \
        --pkg gee-0.8                   \
        --pkg json-glib-1.0             \
+       --pkg gio-unix-2.0              \
        $(GITG_VALAFLAGS)               \
        --vapidir $(top_srcdir)/vapi    \
        --header libgitg.h              \
@@ -65,7 +66,8 @@ VALA_FILES =                                  \
        gitg-progress-bin.vala                  \
        gitg-stage.vala                         \
        gitg-stage-status-enumerator.vala       \
-       gitg-sidebar.vala
+       gitg-sidebar.vala                       \
+       gitg-hook.vala
 
 # Ignore all warnings for vala code...
 libgitg_1_0_la_CFLAGS =                \
diff --git a/libgitg/gitg-hook.vala b/libgitg/gitg-hook.vala
new file mode 100644
index 0000000..5fda372
--- /dev/null
+++ b/libgitg/gitg-hook.vala
@@ -0,0 +1,162 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2013 - Jesse van den Kieboom
+ *
+ * gitg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gitg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace Gitg
+{
+
+public class Hook : Object
+{
+       public Gee.HashMap<string, string> environment { get; set; }
+       public string name { get; set; }
+       public string[] arguments { get; set; }
+       public File? working_directory { get; set; }
+
+       private string[] d_output;
+
+       public string[] output
+       {
+               owned get { return d_output; }
+       }
+
+       construct
+       {
+               environment = new Gee.HashMap<string, string>();
+       }
+
+       public Hook(string name)
+       {
+               Object(name: name);
+       }
+
+       private string[]? flat_environment()
+       {
+               if (environment.size == 0)
+               {
+                       return null;
+               }
+
+               var env = Environment.list_variables();
+
+               var ret = new string[env.length + environment.size];
+               ret.length = 0;
+
+               foreach (var e in env)
+               {
+                       if (!environment.has_key(e))
+                       {
+                               ret += "%s=%s".printf(e, Environment.get_variable(e));
+                       }
+               }
+
+               foreach (var e in environment.keys)
+               {
+                       ret += "%s=%s".printf(e, environment[e]);
+               }
+
+               return ret;
+       }
+
+       private void stream_read_async(DataInputStream stream)
+       {
+               stream.read_line_async.begin(Priority.HIGH_IDLE, null, (obj, res) => {
+                       try
+                       {
+                               var s = stream.read_line_async.end(res);
+
+                               if (s.length != 0)
+                               {
+                                       d_output += s;
+
+                                       // Continue reading
+                                       stream_read_async(stream);
+                               }
+                       }
+                       catch {}
+               });
+       }
+
+       private void read_from_fd(int fd)
+       {
+               var stream = new UnixInputStream(fd, true);
+               var dstream = new DataInputStream(stream);
+
+               stream_read_async(dstream);
+       }
+
+       public async int run(Ggit.Repository repository) throws SpawnError
+       {
+               File? wd = working_directory;
+               SourceFunc callback = run.callback;
+
+               d_output = new string[256];
+               d_output.length = 0;
+
+               if (wd == null)
+               {
+                       wd = repository.get_workdir();
+               }
+
+               var hooksdir = repository.get_location().get_child("hooks");
+               var script = hooksdir.resolve_relative_path(name);
+               var args = new string[arguments.length + 1];
+
+               args.length = 0;
+
+               args += script.get_path();
+
+               foreach (var a in arguments)
+               {
+                       args += a;
+               }
+
+               Pid pid;
+               int pstdout;
+               int pstderr;
+
+               Process.spawn_async_with_pipes(wd.get_path(),
+                                              args,
+                                              flat_environment(),
+                                              SpawnFlags.DO_NOT_REAP_CHILD,
+                                              null,
+                                              out pid,
+                                              null,
+                                              out pstdout,
+                                              out pstderr);
+
+               read_from_fd(pstdout);
+               read_from_fd(pstderr);
+
+               int status = 0;
+
+               ChildWatch.add(pid, (p, st) => {
+                       status = st;
+
+                       Process.close_pid(p);
+                       callback();
+               }, Priority.LOW);
+
+               yield;
+
+               return status;
+       }
+}
+
+}
+
+// ex: ts=4 noet


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