[gitg/wip/albfan/gitattributes-textconv] Convert to text using gitattributes config




commit d82ab68694391e008a5bff8bcd2b28cd270d91be
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Tue Nov 30 08:59:42 2021 +0100

    Convert to text using gitattributes config

 libgitg/gitg-diff-view-file-info.vala              |  20 ++++-
 .../gitg-diff-view-file-renderer-text-split.vala   |   2 +-
 libgitg/gitg-diff-view-file-renderer-text.vala     |  11 ++-
 libgitg/gitg-diff-view.vala                        |  87 +++++++++++++++++-
 libgitg/gitg-textconv.vala                         | 100 +++++++++++++++++++++
 libgitg/meson.build                                |   1 +
 6 files changed, 211 insertions(+), 10 deletions(-)
---
diff --git a/libgitg/gitg-diff-view-file-info.vala b/libgitg/gitg-diff-view-file-info.vala
index 5fe72477..8d64bfc1 100644
--- a/libgitg/gitg-diff-view-file-info.vala
+++ b/libgitg/gitg-diff-view-file-info.vala
@@ -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);
 
        }
-}
\ No newline at end of file
+}
diff --git a/libgitg/gitg-diff-view-file-renderer-text-split.vala 
b/libgitg/gitg-diff-view-file-renderer-text-split.vala
index 53cc469e..696693a0 100644
--- a/libgitg/gitg-diff-view-file-renderer-text-split.vala
+++ b/libgitg/gitg-diff-view-file-renderer-text-split.vala
@@ -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
                {
diff --git a/libgitg/gitg-diff-view-file-renderer-text.vala b/libgitg/gitg-diff-view-file-renderer-text.vala
index b20dba7a..5745f8ee 100644
--- a/libgitg/gitg-diff-view-file-renderer-text.vala
+++ b/libgitg/gitg-diff-view-file-renderer-text.vala
@@ -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
                        {
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index f5c00d17..75952e21 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -848,9 +848,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, num_parents);
+                                                                                               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;
                                },
 
diff --git a/libgitg/gitg-textconv.vala b/libgitg/gitg-textconv.vala
new file mode 100755
index 00000000..5def89f5
--- /dev/null
+++ b/libgitg/gitg-textconv.vala
@@ -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
diff --git a/libgitg/meson.build b/libgitg/meson.build
index 56105239..6511a7e2 100644
--- a/libgitg/meson.build
+++ b/libgitg/meson.build
@@ -68,6 +68,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',


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