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



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

    Add "Get Patch" button to Diff View

 libgitg/gitg-diff-view-request-resource.vala |   76 ++++++++++++++++++++++++++
 libgitg/gitg-diff-view-request.vala          |    5 ++
 libgitg/gitg-diff-view.vala                  |    2 +
 libgitg/resources/diff-view.css              |    7 +++
 libgitg/resources/diff-view.html             |    1 +
 libgitg/resources/diff-view.js               |    6 ++
 6 files changed, 97 insertions(+), 0 deletions(-)
---
diff --git a/libgitg/gitg-diff-view-request-resource.vala b/libgitg/gitg-diff-view-request-resource.vala
index 164b04c..69228b9 100644
--- a/libgitg/gitg-diff-view-request-resource.vala
+++ b/libgitg/gitg-diff-view-request-resource.vala
@@ -102,6 +102,82 @@ namespace Gitg
                        return stream;
                }
        }
+
+       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");
+                       try
+                       {
+                               Ggit.DiffList diff = selected_commit.get_diff(null);
+                               Ggit.DiffPatch patch;
+                               Ggit.DiffDelta delta;
+                               var number_of_deltas = diff.get_num_deltas();
+                               
+                               //write the patch to file
+                               FileOutputStream os = file.create (FileCreateFlags.PRIVATE);
+                               os.write("From %s %s".printf(sha1, legacydate).data);
+                               os.write("\nFrom: %s <%s>".printf(author, author_email).data);
+                               os.write("\nDate: %s".printf(datetime).data);
+                               os.write("\nSubject: [PATCH] %s\n\n".printf(commit_message).data);
+                               for(var i = 0; i < number_of_deltas; i++) {
+                                       diff.get_patch(i, out patch, out delta);
+                                       os.write(patch.to_string().data);
+                               }
+                               os.write("--\n".data);
+                               os.write("Gitg\n\n".data);
+                               os.close ();
+                       }
+                       catch (Error e)
+                       {
+                               //TODO: Route error message to Infobar?
+                               stdout.printf("Failed: %s".printf(e.message));
+                       }
+               }
+
+               public override void run_after_async()
+               {
+                       Gitg.Commit? selected_commit = (Gitg.Commit) d_commit;
+                       string commit_subject = d_commit.get_subject().replace(" ","-");
+                       
+                       Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog ("Save Patch File", null,
+                                                                                                        
Gtk.FileChooserAction.SAVE,
+                                                     Gtk.Stock.CANCEL,
+                                                     Gtk.ResponseType.CANCEL,
+                                                     Gtk.Stock.SAVE,
+                                                     Gtk.ResponseType.OK);
+                       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..a4fc903 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;
diff --git a/libgitg/resources/diff-view.css b/libgitg/resources/diff-view.css
index c4f755f..fec4d25 100644
--- a/libgitg/resources/diff-view.css
+++ b/libgitg/resources/diff-view.css
@@ -244,4 +244,11 @@ span.hunk_header, span.file_path {
   vertical-align: middle;
 }
 
+.format_patch_button {
+  float:right;
+  margin-top:13px;
+  padding: 7px;
+  margin-right: 3px;
+}
+
 /* 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..7694be9 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 r = new XMLHttpRequest();
+                               r.open("GET", "gitg-diff:/patch/?id=" + j.commit.id + "&viewid=" + 
params.viewid);
+                               r.send();
+                       });
                }
        }
 


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