[gitg/fix-diff-view: 2/2] Support repository being null



commit 6a06e436f807c4f533c24df8e60ed1247bfdaeb3
Author: Gaurav Agrawal <agrawalgaurav1999 gmail com>
Date:   Fri May 24 22:21:14 2019 +0530

    Support repository being null
    
    repository can be null, so is not correct to access to its properties without checking it.

 libgitg/gitg-diff-view-commit-details.vala | 87 +++++++++++++++---------------
 libgitg/gitg-diff-view.vala                | 45 +++++++++-------
 2 files changed, 70 insertions(+), 62 deletions(-)
---
diff --git a/libgitg/gitg-diff-view-commit-details.vala b/libgitg/gitg-diff-view-commit-details.vala
index f90f1e41..c0ea3e1f 100644
--- a/libgitg/gitg-diff-view-commit-details.vala
+++ b/libgitg/gitg-diff-view-commit-details.vala
@@ -281,63 +281,66 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
 
        private string parse_ini_file(string subject_text)
        {
-               string result = subject_text.dup();
-               GLib.KeyFile file = new GLib.KeyFile();
-
-               try
+               string result = subject_text;
+               if (config_file != null)
                {
-                       debug ("parsing %s", config_file);
-                       if (file.load_from_file(config_file , GLib.KeyFileFlags.NONE))
+                       try
                        {
-                               foreach (string group in file.get_groups())
+                               debug ("parsing %s", config_file);
+                               GLib.KeyFile file = new GLib.KeyFile();
+                               if (file.load_from_file(config_file , GLib.KeyFileFlags.NONE))
                                {
-                                       if (group.has_prefix("gitg.custom-link"))
+                                       result = subject_text.dup();
+                                       foreach (string group in file.get_groups())
                                        {
-                                               string custom_link_regexp = file.get_string (group, "regexp");
-                                               string custom_link_replacement = file.get_string (group, 
"replacement");
-                                               debug ("found group: %s", custom_link_regexp);
-                                               bool custom_color = file.has_key (group, "color");
-                                               string color = null;
-                                               if (custom_color)
+                                               if (group.has_prefix("gitg.custom-link"))
                                                {
-                                                       string custom_link_color = file.get_string (group, 
"color");
-                                                       color = custom_link_color;
-                                               }
+                                                       string custom_link_regexp = file.get_string (group, 
"regexp");
+                                                       string custom_link_replacement = file.get_string 
(group, "replacement");
+                                                       debug ("found group: %s", custom_link_regexp);
+                                                       bool custom_color = file.has_key (group, "color");
+                                                       string color = null;
+                                                       if (custom_color)
+                                                       {
+                                                               string custom_link_color = file.get_string 
(group, "color");
+                                                               color = custom_link_color;
+                                                       }
 
-                                               var custom_regex = new Regex (custom_link_regexp);
-                                               try
-                                               {
-                                                       GLib.MatchInfo matchInfo;
+                                                       var custom_regex = new Regex (custom_link_regexp);
+                                                       try
+                                                       {
+                                                               GLib.MatchInfo matchInfo;
 
-                                                       custom_regex.match (subject_text, 0, out matchInfo);
+                                                               custom_regex.match (subject_text, 0, out 
matchInfo);
 
-                                                       while (matchInfo.matches ())
-                                                       {
-                                                               string text = matchInfo.fetch(0);
-                                                               string link = text.dup();
-                                                               debug ("found: %s", link);
-                                                               if (custom_link_replacement != null)
+                                                               while (matchInfo.matches ())
                                                                {
-                                                                       link = custom_regex.replace(link, 
text.length, 0, custom_link_replacement);
+                                                                       string text = matchInfo.fetch(0);
+                                                                       string link = text.dup();
+                                                                       debug ("found: %s", link);
+                                                                       if (custom_link_replacement != null)
+                                                                       {
+                                                                               link = 
custom_regex.replace(link, text.length, 0, custom_link_replacement);
+                                                                       }
+                                                                       if (color != null) {
+                                                                               result = result.replace(text, 
"<a href=\"%s\" title=\"%s\" style=\"color:%s\">%s</a>".printf(link, link, color, text));
+                                                                       } else {
+                                                                               result = result.replace(text, 
"<a href=\"%s\" title=\"%s\">%s</a>".printf(link, link, text));
+                                                                       }
+
+                                                                       matchInfo.next();
                                                                }
-                                                               if (color != null) {
-                                                                       result = result.replace(text, "<a 
href=\"%s\" title=\"%s\" style=\"color:%s\">%s</a>".printf(link, link, color, text));
-                                                               } else {
-                                                                       result = result.replace(text, "<a 
href=\"%s\" title=\"%s\">%s</a>".printf(link, link, text));
-                                                               }
-
-                                                               matchInfo.next();
                                                        }
-                                               }
-                                               catch(Error e)
-                                               {
+                                                       catch(Error e)
+                                                       {
+                                                       }
                                                }
                                        }
                                }
+                       } catch (Error e)
+                       {
+                               warning ("Cannot read %s %s", config_file, e.message);
                        }
-               } catch (Error e)
-               {
-                       warning ("Cannot read %s %s", config_file, e.message);
                }
                return result;
        }
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index b5ae0c8b..9b7afd5e 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -131,8 +131,11 @@ public class Gitg.DiffView : Gtk.Grid
                get { return d_repository; }
                set {
                        d_repository = value;
-                       config_file = "%s/.git/config".printf(d_repository.get_workdir().get_path());
-                       d_commit_details.config_file = config_file;
+                       if (d_repository != null)
+                       {
+                               config_file = "%s/.git/config".printf(d_repository.get_workdir().get_path());
+                               d_commit_details.config_file = config_file;
+                       }
                }
        }
        public bool new_is_workdir { get; set; }
@@ -593,33 +596,35 @@ public class Gitg.DiffView : Gtk.Grid
 
        private void read_ini_file(Gtk.TextBuffer buffer)
        {
-               GLib.KeyFile file = new GLib.KeyFile();
-
-               try
+               if (config_file != null)
                {
-                       if (file.load_from_file(config_file , GLib.KeyFileFlags.NONE))
+                       try
                        {
-                               foreach (string group in file.get_groups())
+                               GLib.KeyFile file = new GLib.KeyFile();
+                               if (file.load_from_file(config_file , GLib.KeyFileFlags.NONE))
                                {
-                                       if (group.has_prefix("gitg.custom-link"))
+                                       foreach (string group in file.get_groups())
                                        {
-                                               string custom_link_regexp = file.get_string (group, "regexp");
-                                               string custom_link_replacement = file.get_string (group, 
"replacement");
-                                               bool custom_color = file.has_key (group, "color");
-                                               Gdk.RGBA color = d_color_link;
-                                               if (custom_color)
+                                               if (group.has_prefix("gitg.custom-link"))
                                                {
-                                                       string custom_link_color = file.get_string (group, 
"color");
-                                                       color = Gdk.RGBA();
-                                                       color.parse(custom_link_color);
+                                                       string custom_link_regexp = file.get_string (group, 
"regexp");
+                                                       string custom_link_replacement = file.get_string 
(group, "replacement");
+                                                       bool custom_color = file.has_key (group, "color");
+                                                       Gdk.RGBA color = d_color_link;
+                                                       if (custom_color)
+                                                       {
+                                                               string custom_link_color = file.get_string 
(group, "color");
+                                                               color = Gdk.RGBA();
+                                                               color.parse(custom_link_color);
+                                                       }
+                                                       apply_link_tags(buffer, new Regex 
(custom_link_regexp), custom_link_replacement, color, custom_color, true);
                                                }
-                                               apply_link_tags(buffer, new Regex (custom_link_regexp), 
custom_link_replacement, color, custom_color, true);
                                        }
                                }
+                       } catch (Error e)
+                       {
+                               warning ("Cannot read %s: %s", config_file, e.message);
                        }
-               } catch (Error e)
-               {
-                       warning ("Cannot read %s: %s", config_file, e.message);
                }
        }
 


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