[gitg] Implemented create patch commit action



commit c88ab4e71d928e2c5a8c4a9b157a6709fce7be56
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Mon Jul 21 11:09:34 2014 +0300

    Implemented create patch commit action

 gitg/Makefile.am                          |    1 +
 gitg/gitg-commit-action-create-patch.vala |  182 +++++++++++++++++++++++++++++
 gitg/history/gitg-history.vala            |    5 +
 libgitg/Makefile.am                       |    1 -
 libgitg/gitg-diff-view-request-patch.vala |  135 ---------------------
 libgitg/gitg-diff-view.vala               |    3 -
 libgitg/resources/diff-view.html          |    3 -
 libgitg/resources/diff-view.js            |   10 +--
 po/POTFILES.in                            |    2 +-
 po/POTFILES.skip                          |    2 +-
 10 files changed, 191 insertions(+), 153 deletions(-)
---
diff --git a/gitg/Makefile.am b/gitg/Makefile.am
index ec6340a..f03a80f 100644
--- a/gitg/Makefile.am
+++ b/gitg/Makefile.am
@@ -66,6 +66,7 @@ gitg_gitg_VALASOURCES =                                               \
        gitg/gitg-create-branch-dialog.vala                     \
        gitg/gitg-commit-action-create-tag.vala                 \
        gitg/gitg-create-tag-dialog.vala                        \
+       gitg/gitg-commit-action-create-patch.vala               \
        gitg/preferences/gitg-preferences-commit.vala           \
        gitg/preferences/gitg-preferences-dialog.vala           \
        gitg/preferences/gitg-preferences-interface.vala        \
diff --git a/gitg/gitg-commit-action-create-patch.vala b/gitg/gitg-commit-action-create-patch.vala
new file mode 100644
index 0000000..c232c0f
--- /dev/null
+++ b/gitg/gitg-commit-action-create-patch.vala
@@ -0,0 +1,182 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2014 - 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
+{
+
+class CommitActionCreatePatch : GitgExt.UIElement, GitgExt.Action, GitgExt.CommitAction, Object
+{
+       // Do this to pull in config.h before glib.h (for gettext...)
+       private const string version = Gitg.Config.VERSION;
+
+       public GitgExt.Application? application { owned get; construct set; }
+       public GitgExt.RefActionInterface action_interface { get; construct set; }
+       public Gitg.Commit commit { get; construct set; }
+       public Ggit.Diff? diff { get; set; }
+
+       private static Regex s_subject_regex;
+
+       static construct
+       {
+               try
+               {
+                       s_subject_regex = new Regex("[^\\d\\w \\_\\-]");
+               }
+               catch (Error e)
+               {
+                       stderr.printf(@"Failed to compile subject regex: $(e.message)\n");
+               }
+       }
+
+       public CommitActionCreatePatch(GitgExt.Application        application,
+                                   GitgExt.RefActionInterface action_interface,
+                                   Gitg.Commit                commit)
+       {
+               Object(application:      application,
+                      action_interface: action_interface,
+                      commit:           commit);
+       }
+
+       public string id
+       {
+               owned get { return "/org/gnome/gitg/commit-actions/create-patch"; }
+       }
+
+       public string display_name
+       {
+               owned get { return _("Create Patch"); }
+       }
+
+       public string description
+       {
+               owned get { return _("Create a patch from the selected commit"); }
+       }
+
+       private string patch_filename(int i)
+       {
+               string subject = commit.get_subject();
+
+               // Remove anything that is not:
+               //   a) alpha numeric
+               //   b) underscore or hyphens
+               //   c) single space
+               try
+               {
+                       subject = s_subject_regex.replace(subject, subject.length, 0, "");
+                       subject = subject.replace(" ", "-");
+
+                       subject = "%04d-%s".printf(i, subject);
+               }
+               catch
+               {
+                       return "";
+               }
+
+               return subject + ".patch";
+       }
+
+       private Ggit.Diff create_diff_from_commit() throws Error
+       {
+               var settings = new Settings("org.gnome.gitg.preferences.diff");
+
+               var opts = new Ggit.DiffOptions();
+
+               if (settings.get_boolean("ignore-whitespace"))
+               {
+                       opts.flags |= Ggit.DiffOption.IGNORE_WHITESPACE;
+               }
+
+               var nc = settings.get_int("context-lines");
+
+               opts.n_context_lines = nc;
+               opts.n_interhunk_lines = nc;
+
+               return commit.get_diff(opts);
+       }
+
+       private void create_patch(File file) throws Error
+       {
+               // Create diff if needed
+               if (diff == null)
+               {
+                       diff = create_diff_from_commit();
+               }
+
+               var opts = new Ggit.DiffFormatEmailOptions();
+
+               opts.summary = commit.get_message();
+               opts.patch_number = 1;
+               opts.total_patches = 1;
+               opts.id = commit.get_id();
+               opts.author = commit.get_author();
+
+               var content = diff.format_email(opts);
+
+               file.replace_contents(content.data[0:content.length],
+                                     null,
+                                     false,
+                                     FileCreateFlags.NONE,
+                                     null,
+                                     null);
+       }
+
+       public void activate()
+       {
+               var chooser = new Gtk.FileChooserDialog(_("Save Patch File"), null,
+                                                       Gtk.FileChooserAction.SAVE,
+                                                       _("_Cancel"),
+                                                       Gtk.ResponseType.CANCEL,
+                                                       _("_Save Patch"),
+                                                       Gtk.ResponseType.OK);
+
+               chooser.do_overwrite_confirmation = true;
+               chooser.set_current_name(patch_filename(1));
+
+               try
+               {
+                       chooser.set_current_folder_file(application.repository.get_workdir());
+               } catch {}
+
+               chooser.set_transient_for((Gtk.Window)application);
+
+               chooser.show();
+               chooser.response.connect((dialog, id) => {
+                       if (id == Gtk.ResponseType.OK)
+                       {
+                               try
+                               {
+                                       create_patch(chooser.get_file());
+                               }
+                               catch (Error e)
+                               {
+                                       application.show_infobar(_("Failed to create patch"),
+                                                                e.message,
+                                                                Gtk.MessageType.ERROR);
+                               }
+                       }
+
+                       chooser.destroy();
+                       finished();
+               });
+       }
+}
+
+}
+
+// ex:set ts=4 noet
diff --git a/gitg/history/gitg-history.vala b/gitg/history/gitg-history.vala
index ab9e322..e16d3e1 100644
--- a/gitg/history/gitg-history.vala
+++ b/gitg/history/gitg-history.vala
@@ -522,6 +522,11 @@ namespace GitgHistory
                                                                                 af,
                                                                                 commit));
 
+                               add_commit_action(actions,
+                                                 new Gitg.CommitActionCreatePatch(application,
+                                                                                  af,
+                                                                                  commit));
+
                                var exts = new Peas.ExtensionSet(Gitg.PluginsEngine.get_default(),
                                                                 typeof(GitgExt.CommitAction),
                                                                 "application",
diff --git a/libgitg/Makefile.am b/libgitg/Makefile.am
index 93d4426..09cf80d 100644
--- a/libgitg/Makefile.am
+++ b/libgitg/Makefile.am
@@ -62,7 +62,6 @@ libgitg_libgitg_1_0_la_VALASOURCES =                  \
        libgitg/gitg-diff-view-request.vala             \
        libgitg/gitg-diff-view-request-icon.vala        \
        libgitg/gitg-diff-view-request-resource.vala    \
-       libgitg/gitg-diff-view-request-patch.vala       \
        libgitg/gitg-diff-view-request-diff.vala        \
        libgitg/gitg-repository-list-box.vala           \
        libgitg/gitg-when-mapped.vala                   \
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index 0c608d8..588cad5 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -229,7 +229,6 @@ namespace Gitg
                        strings.set_string_member("stage", _("stage"));
                        strings.set_string_member("unstage", _("unstage"));
                        strings.set_string_member("loading_diff", _("Loading diff…"));
-                       strings.set_string_member("get_patch", _("Get Patch"));
 
                        o.set_object_member("strings", strings);
 
@@ -282,8 +281,6 @@ namespace Gitg
                                        return new DiffViewRequestIcon(view, request, uri);
                                case "diff":
                                        return new DiffViewRequestDiff(view, request, uri);
-                               case "patch":
-                                       return new DiffViewRequestPatch(view, request, uri);
                                case "internal":
                                        return new DiffViewRequestInternal(view, request, uri);
                        }
diff --git a/libgitg/resources/diff-view.html b/libgitg/resources/diff-view.html
index cc0be60..8d0012f 100644
--- a/libgitg/resources/diff-view.html
+++ b/libgitg/resources/diff-view.html
@@ -35,9 +35,6 @@
     <div id="diff">
       <div id="diff_header">
         <div class="commit">
-          <div class="diff_actions">
-            <button data-id="format_patch" class="format_patch">Get Patch</button>
-          </div>
           <img class="avatar" data-id="avatar" src="gitg-diff:/icon/avatar-default-symbolic?size=50"/>
           <p>
             <span class="author" data-id="author"></span><br/>
diff --git a/libgitg/resources/diff-view.js b/libgitg/resources/diff-view.js
index f2ea5e4..5192bb3 100644
--- a/libgitg/resources/diff-view.js
+++ b/libgitg/resources/diff-view.js
@@ -24,8 +24,7 @@ var settings = {
        strings: {
                stage: 'stage',
                unstage: 'unstage',
-               loading_diff: 'Loading diff...',
-               get_patch: 'Get Patch'
+               loading_diff: 'Loading diff...'
        },
 };
 
@@ -154,9 +153,6 @@ function write_commit(content, commit)
        // Sha1
        elems.sha1.text(commit.id);
 
-       // Get patch string
-       elems.format_patch.text(settings.strings.get_patch);
-
        write_avatar(elems.avatar, commit);
 }
 
@@ -491,10 +487,6 @@ function update_diff(id, lsettings)
                {
                        write_commit($('#diff_header .commit'), j.commit);
                        $('#diff_header').show();
-
-                       $('.format_patch').click(function() {
-                               xhr_get('patch', {id: j.commit.id});
-                       });
                }
                else
                {
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 49a6295..cf8fd96 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -9,6 +9,7 @@ gitg/gitg-application.vala
 gitg/gitg-author-details-dialog.vala
 gitg/gitg-clone-dialog.vala
 gitg/gitg-commit-action-create-branch.vala
+gitg/gitg-commit-action-create-patch.vala
 gitg/gitg-commit-action-create-tag.vala
 gitg/gitg-create-tag-dialog.vala
 gitg/gitg-dash-view.vala
@@ -22,7 +23,6 @@ gitg/preferences/gitg-preferences-commit.vala
 gitg/preferences/gitg-preferences-history.vala
 gitg/preferences/gitg-preferences-interface.vala
 libgitg/gitg-date.vala
-libgitg/gitg-diff-view-request-patch.vala
 libgitg/gitg-diff-view.vala
 libgitg/gitg-stage.vala
 plugins/diff/gitg-diff.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index 730df40..f4aedc4 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -11,6 +11,7 @@ gitg/gitg-ref-action-delete.c
 gitg/gitg-ref-action-rename.c
 gitg/gitg-commit-action-create-branch.c
 gitg/gitg-commit-action-create-tag.c
+gitg/gitg-commit-action-create-patch.c
 gitg/gitg-create-tag-dialog.c
 gitg/gitg-window.c
 gitg/history/gitg-history.c
@@ -22,7 +23,6 @@ libgd/libgd/gd-header-bar.c
 libgd/libgd/gd-stack-switcher.c
 libgitg/gitg-date.c
 libgitg/gitg-diff-view.c
-libgitg/gitg-diff-view-request-patch.c
 libgitg/gitg-stage.c
 plugins/diff/gitg-diff.c
 plugins/files/gitg-files.c


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