[gitg/wip/format-patch: 9/9] Add "Get Patch" button to Diff View



commit 9ba8d87104cf056cc02c5eff181f1afcab1e79b1
Author: Sindhu S <sindhus live in>
Date:   Wed Aug 21 00:20:06 2013 +0530

    Add "Get Patch" button to Diff View

 libgitg/Makefile.am                       |    1 +
 libgitg/gitg-diff-view-request-patch.vala |  119 +++++++++++++++++++++++++++++
 libgitg/gitg-diff-view-request.vala       |    5 +
 libgitg/gitg-diff-view.vala               |    4 +-
 libgitg/resources/diff-view.css           |    8 ++
 libgitg/resources/diff-view.html          |    1 +
 libgitg/resources/diff-view.js            |    6 ++
 7 files changed, 143 insertions(+), 1 deletions(-)
---
diff --git a/libgitg/Makefile.am b/libgitg/Makefile.am
index 722de80..40f725f 100644
--- a/libgitg/Makefile.am
+++ b/libgitg/Makefile.am
@@ -58,6 +58,7 @@ VALA_FILES =                                  \
        gitg-diff-view.vala                     \
        gitg-diff-view-request.vala             \
        gitg-diff-view-request-resource.vala    \
+       gitg-diff-view-request-patch.vala       \
        gitg-diff-view-request-diff.vala        \
        gitg-repository-list-box.vala           \
        gitg-when-mapped.vala                   \
diff --git a/libgitg/gitg-diff-view-request-patch.vala b/libgitg/gitg-diff-view-request-patch.vala
new file mode 100644
index 0000000..b126f6d
--- /dev/null
+++ b/libgitg/gitg-diff-view-request-patch.vala
@@ -0,0 +1,119 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2012 - Sindhu S
+ *
+ * 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 DiffViewRequestPatch : DiffViewRequest
+       {
+               private Ggit.Commit? d_commit;
+
+               public DiffViewRequestPatch (DiffView? view, WebKit.URISchemeRequest request, Soup.URI uri)
+               {
+                       base(view, request, uri);
+
+                       if (has_view)
+                       {
+                               d_commit = view.commit;
+                               // set the view to null else it won't run the request
+                               d_view = null;
+                               d_hasView = false;
+                       }
+               }
+
+               private void create_patch (Gitg.Commit selected_commit, File file)
+               {
+                       string commit_message = selected_commit.get_message();
+                       string sha1 = selected_commit.get_id().to_string();
+                       string legacydate = "Mon Sep 17 00:00:00 2001";
+                       string author = selected_commit.get_author().get_name();
+                       string author_email = selected_commit.get_author().get_email();
+                       string datetime = selected_commit.get_author().get_time().format("%a, %e %b %Y %T 
%z");
+                       string patch_content = "";
+                       try
+                       {
+                               Ggit.DiffList diff = selected_commit.get_diff(null);
+                               Ggit.DiffPatch patch;
+                               Ggit.DiffDelta delta;
+                               var number_of_deltas = diff.get_num_deltas();
+                               
+                               
+                               patch_content += "From %s %s".printf(sha1, legacydate);
+                               patch_content += "\nFrom: %s <%s>".printf(author, author_email);
+                               patch_content += "\nDate: %s".printf(datetime);
+                               patch_content += "\nSubject: [PATCH] %s\n\n".printf(commit_message);
+                               for (var i = 0; i < number_of_deltas; i++) {
+                                       diff.get_patch(i, out patch, out delta);
+                                       patch_content += patch.to_string();
+                               }
+                               patch_content += "--\n";
+                               patch_content += "Gitg\n\n";
+
+                               FileUtils.set_contents(file.get_path(), patch_content);
+                       }
+                       catch (Error e)
+                       {
+                               // TODO: Route error message to Infobar?
+                               stdout.printf("Failed: %s".printf(e.message));
+                       }
+               }
+
+               public override void run_after_async()
+               {
+                       var selected_commit = (Gitg.Commit) d_commit;
+                       string commit_subject = selected_commit.get_subject();
+                       
+                       try
+                       {
+                               var subject_regex = new Regex("[^\\d\\w \\_\\-]");
+                               
+                               // remove anything that's not:
+                               // a) alpha numeric
+                               // b) underscore or hyphens
+                               // c) single space
+                               commit_subject = subject_regex.replace(commit_subject, commit_subject.length, 
0, "");
+                               // replace single space with hyphen
+                               commit_subject = commit_subject.replace(" ", "-");
+                       }
+                       catch (Error e)
+                       {
+                               // use an empty default filename
+                               commit_subject = "";
+                       }
+                       
+                       var chooser = new Gtk.FileChooserDialog("Save Patch File", null,
+                                                               Gtk.FileChooserAction.SAVE,
+                                                               Gtk.Stock.CANCEL,
+                                                               Gtk.ResponseType.CANCEL,
+                                                               Gtk.Stock.SAVE,
+                                                               Gtk.ResponseType.OK);
+                       chooser.do_overwrite_confirmation = true;
+                       chooser.set_current_name(commit_subject + ".patch");
+
+                       chooser.show();
+                       chooser.response.connect((dialog, id) => {
+                               if (id != -6) {
+                                       create_patch (selected_commit, chooser.get_file());
+                               }
+                               chooser.destroy();
+                       });
+               }
+       }
+}
+
+// ex:ts=4 noet
diff --git a/libgitg/gitg-diff-view-request.vala b/libgitg/gitg-diff-view-request.vala
index c70d671..86d174e 100644
--- a/libgitg/gitg-diff-view-request.vala
+++ b/libgitg/gitg-diff-view-request.vala
@@ -86,6 +86,9 @@ namespace Gitg
                        return null;
                }
 
+               protected virtual void run_after_async()
+               {}
+
                private async InputStream? run_impl(Cancellable? cancellable) throws GLib.Error
                {
                        SourceFunc callback = run_impl.callback;
@@ -104,6 +107,8 @@ namespace Gitg
                                return null;
                        });
 
+                       run_after_async();
+
                        // Wait for it to finish, yield to caller
                        yield;
 
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index bd076e9..081c85f 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -138,6 +138,8 @@ namespace Gitg
                                        return new DiffViewRequestResource(view, request, uri);
                                case "diff":
                                        return new DiffViewRequestDiff(view, request, uri);
+                               case "patch":
+                                       return new DiffViewRequestPatch(view, request, uri);
                        }
 
                        return null;
@@ -191,7 +193,7 @@ namespace Gitg
                        if (did != null)
                        {
                                uint64 i = uint64.parse(did);
-                               
+
                                if (i == d_diffid)
                                {
                                        request.run(d_cancellable);
diff --git a/libgitg/resources/diff-view.css b/libgitg/resources/diff-view.css
index c4f755f..bd20e0a 100644
--- a/libgitg/resources/diff-view.css
+++ b/libgitg/resources/diff-view.css
@@ -244,4 +244,12 @@ span.hunk_header, span.file_path {
   vertical-align: middle;
 }
 
+.format_patch_button {
+  float:right;
+  margin-top:13px;
+  padding: 7px;
+  margin-right: 3px;
+  color:black;
+}
+
 /* vi:ts=2:et */
diff --git a/libgitg/resources/diff-view.html b/libgitg/resources/diff-view.html
index 82f92e7..4758b26 100644
--- a/libgitg/resources/diff-view.html
+++ b/libgitg/resources/diff-view.html
@@ -9,6 +9,7 @@
     <div id="templates">
       <!-- Commit template -->
       <div class="commit">
+        <button class="format_patch_button">Get Patch</button>
         <img class="avatar"/>
         <p>
           <span class="author"></span><br/>
diff --git a/libgitg/resources/diff-view.js b/libgitg/resources/diff-view.js
index ec989e3..3b37240 100644
--- a/libgitg/resources/diff-view.js
+++ b/libgitg/resources/diff-view.js
@@ -231,6 +231,12 @@ function update_diff(id, lsettings)
                if ('commit' in j)
                {
                        $('#diff_header').html(write_commit(j.commit));
+                       $(".format_patch_button").click(function()
+                       {
+                               var patch_request = new XMLHttpRequest();
+                               patch_request.open("GET", "gitg-diff:/patch/?id=" + j.commit.id + "&viewid=" 
+ params.viewid);
+                               patch_request.send();
+                       });
                }
        }
 


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