Add "Get Patch" button to Diff View

This commit is contained in:
Sindhu S 2013-08-21 00:20:06 +05:30
parent 6fcf6c7c10
commit 8bcc579289
7 changed files with 142 additions and 1 deletions

View file

@ -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 \

View file

@ -0,0 +1,118 @@
/*
* 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

View file

@ -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;

View file

@ -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);

View file

@ -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 */

View file

@ -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/>

View file

@ -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();
});
}
}