Convert to text using gitattributes config

This commit is contained in:
Alberto Fanjul 2021-11-30 08:59:42 +01:00
parent d720f8fbaf
commit 098768374e
8 changed files with 213 additions and 12 deletions

View file

@ -44,7 +44,10 @@ class Gitg.DiffViewFileInfo : Object
return;
}
var bytes = new Bytes(blob.get_raw_content());
uint8[]? raw_content = blob.get_raw_content();
if (TextConv.has_textconv_command(repository, file))
raw_content = TextConv.get_textconv_content_from_raw(repository, file, raw_content);
var bytes = new Bytes(raw_content);
new_file_input_stream = new GLib.MemoryInputStream.from_bytes(bytes);
}
else if (location != null)
@ -52,7 +55,18 @@ class Gitg.DiffViewFileInfo : Object
// Try to read from disk
try
{
new_file_input_stream = yield location.read_async(Priority.DEFAULT, cancellable);
if (TextConv.has_textconv_command(repository, file))
{
uint8[]? content = null;
yield location.load_contents_async(cancellable, out content, null);
content = TextConv.get_textconv_content_from_raw(repository, file, content);
var bytes = new Bytes(content);
new_file_input_stream = new GLib.MemoryInputStream.from_bytes(bytes);
}
else
{
new_file_input_stream = yield location.read_async(Priority.DEFAULT, cancellable);
}
}
catch
{
@ -102,4 +116,4 @@ class Gitg.DiffViewFileInfo : Object
new_file_content_type = GLib.ContentType.guess(basename, buffer, out uncertain);
}
}
}

View file

@ -86,7 +86,7 @@ class Gitg.DiffViewFileRendererTextSplit : Gtk.Box, DiffSelectable, DiffViewFile
public bool highlight
{
get { return d_renderer_left.highlight; }
get { return d_renderer_left != null && d_renderer_left.highlight; }
construct set
{

View file

@ -393,18 +393,21 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
return null;
}
content = blob.get_raw_content();
if (TextConv.has_textconv_command(repository, file))
content = TextConv.get_textconv_content(repository, file);
else
content = blob.get_raw_content();
}
else
{
// Try to read from disk
try
{
string etag;
// Read it all into a buffer so we can guess the content type from
// it. This isn't really nice, but it's simple.
yield location.load_contents_async(cancellable, out content, out etag);
yield location.load_contents_async(cancellable, out content, null);
if (TextConv.has_textconv_command(repository, file))
content = TextConv.get_textconv_content_from_raw(repository, file, content);
}
catch
{

View file

@ -851,9 +851,92 @@ public class Gitg.DiffView : Gtk.Grid
}
if (current_is_binary)
{
current_file.add_binary_renderer();
}
try {
var new_file = delta.get_new_file();
var old_file = delta.get_old_file();
if (TextConv.has_textconv_command(repository, old_file) || TextConv.has_textconv_command(repository, new_file))
{
uint8[] n_textconv = TextConv.get_textconv_content(repository, new_file);
uint8[] o_textconv = TextConv.get_textconv_content(repository, old_file);
current_is_binary = false;
var opts = new Ggit.DiffOptions();
opts.flags = Ggit.DiffOption.INCLUDE_UNTRACKED |
Ggit.DiffOption.IGNORE_WHITESPACE |
Ggit.DiffOption.DISABLE_PATHSPEC_MATCH |
Ggit.DiffOption.RECURSE_UNTRACKED_DIRS;
opts.n_context_lines = 3;
opts.n_interhunk_lines = 3;
var bdiff = new Ggit.Diff.buffers(o_textconv, old_file.get_path(), n_textconv, new_file.get_path(), opts);
bdiff.foreach(
(delta, progress) => {
if (cancellable != null && cancellable.is_cancelled())
{
return 1;
}
deltakey = key_for_delta(delta);
if (infomap.has_key(deltakey))
{
info = infomap[deltakey];
}
else
{
info = new DiffViewFileInfo(repository, delta, new_is_workdir);
}
current_file = new Gitg.DiffViewFile(info);
current_file.add_text_renderer(handle_selection);
return 0;
},
(delta, binary) => {
if (cancellable != null && cancellable.is_cancelled())
{
return 1;
}
return 0;
},
(delta, hunk) => {
if (cancellable != null && cancellable.is_cancelled())
{
return 1;
}
if (!current_is_binary)
{
maxlines = int.max(maxlines, hunk.get_old_start() + hunk.get_old_lines());
maxlines = int.max(maxlines, hunk.get_new_start() + hunk.get_new_lines());
add_hunk();
current_hunk = hunk;
current_lines = new Gee.ArrayList<Ggit.DiffLine>();
}
return 0;
},
(delta, hunk, line) => {
if (cancellable != null && cancellable.is_cancelled())
{
return 1;
}
if (!current_is_binary)
{
current_lines.add(line);
}
return 0;
}
);
add_hunk();
add_file();
}
} catch (Error error) {
stderr.printf (@"Error: $(error.message)\n");
}
if (current_is_binary)
current_file.add_binary_renderer();
}
return 0;
},

100
libgitg/gitg-textconv.vala Executable file
View file

@ -0,0 +1,100 @@
/*
*
* Copyright (C) 2021 - Alberto Fanjul
*
* 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/>.
*/
using Ggit;
namespace Gitg
{
public class TextConv
{
public static bool has_textconv_command(Repository repository, DiffFile file)
{
return get_textconv_command(repository, file) != null;
}
private static string? get_textconv_command(Repository repository, DiffFile file)
{
string? command = null;
var path = file.get_path();
var diffattr = repository.get_attribute(path, "diff", Ggit.AttributeCheckFlags.FILE_THEN_INDEX);
if (diffattr != null)
{
var textconv_key = "diff.%s.textconv".printf(diffattr);
command = repository.get_config().get_entry(textconv_key).get_value();
}
return command;
}
public static uint8[] get_textconv_content(Repository repository, DiffFile file)
{
uint8[] content = "".data;
if (file != null)
{
var oid = file.get_oid();
uint8[]? raw_content = null;
if (!oid.is_zero()) {
var blob = repository.lookup<Ggit.Blob>(oid);
raw_content = blob.get_raw_content();
}
content = get_textconv_content_from_raw(repository, file, raw_content);
}
return content;
}
public static uint8[] get_textconv_content_from_raw(Repository repository, DiffFile file, uint8[]? raw_content)
{
uint8[] content = "".data;
if (raw_content != null)
{
var command = get_textconv_command(repository, file);
if (command != null)
{
content = textconv(command, raw_content);
}
}
return content;
}
private static uint8[] textconv(string command, uint8[]? data)
{
var subproc = new Subprocess(STDIN_PIPE | STDOUT_PIPE, command, "/dev/stdin");
var input = new MemoryInputStream.from_data(data, GLib.free);
subproc.get_stdin_pipe ().splice (input, CLOSE_TARGET);
var end_pipe = subproc.get_stdout_pipe ();
var output = new DataInputStream (end_pipe);
string lines = "";
string? line = null;
do {
line = output.read_line();
if (line != null) {
line = line.replace("\f", "");
lines += line+"\n";
}
} while (line != null);
return lines.data;
}
}
}
// ex:ts=4 noet

View file

@ -67,6 +67,7 @@ sources = files(
'gitg-sidebar.vala',
'gitg-stage-status-enumerator.vala',
'gitg-stage.vala',
'gitg-textconv.vala',
'gitg-theme.vala',
'gitg-utils.vala',
'gitg-when-mapped.vala',

View file

@ -133,7 +133,7 @@ gsettings_desktop_schemas_dep = dependency('gsettings-desktop-schemas')
gtk_dep = dependency('gtk+-3.0', version: '>= 3.20.0')
gspell_dep = dependency('gspell-1', version: '>= 1.8.1')
gtksourceview_dep = dependency('gtksourceview-4', version: '>= 4.0.3')
libgit2_glib_dep = dependency('libgit2-glib-1.0', version: ['>= 0.27.8'])
libgit2_glib_dep = dependency('libgit2-glib-1.0', version: ['>= 1.0.0'])
libpeas_dep = dependency('libpeas-1.0')
libsecret_dep = dependency('libsecret-1')
libxml_dep = dependency('libxml-2.0', version: '>= 2.9.0')

View file

@ -64,7 +64,7 @@
{
"type" : "git",
"url" : "https://github.com/libgit2/libgit2.git",
"branch" : "maint/v0.28"
"branch" : "maint/v1.0"
}
]
},