[gitg] Add command line arguments to select initial ref in history



commit b35e9452bb9886fc63fa4c484db7ade617697082
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Wed Aug 5 19:14:53 2015 +0200

    Add command line arguments to select initial ref in history
    
      -a, --all                         to select all commits
      -s, --select-reference REFERENCE  to select REFERENCE
    
    https://bugzilla.gnome.org/show_bug.cgi?id=740033

 gitg/Makefile.am                            |    1 +
 gitg/history/gitg-history-command-line.vala |   93 +++++++++++++++++++++++++++
 gitg/history/gitg-history-refs-list.vala    |   36 ++++++++++-
 gitg/history/gitg-history.vala              |    5 ++
 libgitg/gitg-repository.vala                |    5 ++
 5 files changed, 138 insertions(+), 2 deletions(-)
---
diff --git a/gitg/Makefile.am b/gitg/Makefile.am
index f49e98c..348ae99 100644
--- a/gitg/Makefile.am
+++ b/gitg/Makefile.am
@@ -79,6 +79,7 @@ gitg_gitg_VALASOURCES =                                               \
        gitg/gitg-window.vala                                   \
        gitg/gitg.vala                                          \
        gitg/history/gitg-history-action-interface.vala         \
+       gitg/history/gitg-history-command-line.vala             \
        gitg/history/gitg-history-paned.vala                    \
        gitg/history/gitg-history-refs-list.vala                \
        gitg/history/gitg-history.vala                          \
diff --git a/gitg/history/gitg-history-command-line.vala b/gitg/history/gitg-history-command-line.vala
new file mode 100644
index 0000000..a9053ef
--- /dev/null
+++ b/gitg/history/gitg-history-command-line.vala
@@ -0,0 +1,93 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2014 - Jesse van den Kieboom
+ *
+ * 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 GitgHistory
+{
+
+class CommandLine : Object, GitgExt.CommandLine
+{
+       // Do this to pull in config.h before glib.h (for gettext...)
+       private const string version = Gitg.Config.VERSION;
+
+       private static string? s_select_reference;
+       private string? d_select_reference;
+
+       public string select_reference
+       {
+               get { return d_select_reference; }
+       }
+
+       private static bool select_all_commits()
+       {
+               s_select_reference = "*";
+               return true;
+       }
+
+       private static const OptionEntry[] s_entries = {
+               { "all", 'a', OptionFlags.IN_MAIN | OptionFlags.NO_ARG, OptionArg.CALLBACK, (void 
*)select_all_commits,
+                 N_("Select all commits by default in the history activity (shorthand for --select-reference 
'*')"), null },
+               { "select-reference", 's', OptionFlags.IN_MAIN, OptionArg.STRING, ref s_select_reference,
+                 N_("Select the specified reference by default in the history activity"), N_("REFERENCE") },
+
+               {null}
+       };
+
+       public OptionGroup get_option_group()
+       {
+               var group = new OptionGroup("", "", "");
+               group.add_entries(s_entries);
+
+               return group;
+       }
+
+       public void parse_finished()
+       {
+               d_select_reference = s_select_reference;
+       }
+
+       public void apply(GitgExt.Application application)
+       {
+               var history = application.get_activity_by_id("/org/gnome/gitg/Activities/History") as 
Activity;
+
+               if (history == null)
+               {
+                       return;
+               }
+
+               if (d_select_reference == "*")
+               {
+                       history.refs_list.select_all_commits();
+               }
+               else if (d_select_reference != null)
+               {
+                       try
+                       {
+                               
history.refs_list.select_ref(application.repository.lookup_reference_dwim(d_select_reference));
+                       }
+                       catch (Error e)
+                       {
+                               stderr.printf("Failed to lookup reference %s: %s\n", d_select_reference, 
e.message);
+                       }
+               }
+       }
+}
+
+}
+
+// ex: ts=4 noet
diff --git a/gitg/history/gitg-history-refs-list.vala b/gitg/history/gitg-history-refs-list.vala
index b1d0db7..1d71d9d 100644
--- a/gitg/history/gitg-history-refs-list.vala
+++ b/gitg/history/gitg-history-refs-list.vala
@@ -424,6 +424,7 @@ public class RefsList : Gtk.ListBox
        private Gee.HashMap<Gitg.Ref, RefRow> d_ref_map;
        private Gtk.ListBoxRow? d_selected_row;
        private Gitg.Remote[] d_remotes;
+       private RefRow? d_all_commits;
 
        public signal void changed();
 
@@ -528,6 +529,8 @@ public class RefsList : Gtk.ListBox
 
        private void clear()
        {
+               d_all_commits = null;
+
                d_header_map = new Gee.HashMap<string, RemoteHeader>();
                d_ref_map = new Gee.HashMap<Gitg.Ref, RefRow>();
 
@@ -815,6 +818,35 @@ public class RefsList : Gtk.ListBox
                return false;
        }
 
+       public bool select_all_commits()
+       {
+               if (d_all_commits != null)
+               {
+                       select_row(d_all_commits);
+                       return true;
+               }
+
+               return false;
+       }
+
+       public bool select_ref(Gitg.Ref reference)
+       {
+               // Find by name because the supplied reference might be a separate
+               // instance
+               var refname = reference.get_name();
+
+               foreach (var ourref in d_ref_map.keys)
+               {
+                       if (ourref.get_name() == refname)
+                       {
+                               select_row(d_ref_map[ourref]);
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
        private void refresh()
        {
                freeze_notify();
@@ -830,7 +862,7 @@ public class RefsList : Gtk.ListBox
                        return;
                }
 
-               var all_commits = add_ref_row(null);
+               d_all_commits = add_ref_row(null);
 
                add_header(Gitg.RefType.BRANCH, _("Branches"));
                add_header(Gitg.RefType.REMOTE, _("Remotes"));
@@ -888,7 +920,7 @@ public class RefsList : Gtk.ListBox
                        else
                        {
                                // Select all
-                               select_row(all_commits);
+                               select_row(d_all_commits);
                        }
                }
 
diff --git a/gitg/history/gitg-history.vala b/gitg/history/gitg-history.vala
index 7f76e0b..1ef2fd6 100644
--- a/gitg/history/gitg-history.vala
+++ b/gitg/history/gitg-history.vala
@@ -368,6 +368,11 @@ namespace GitgHistory
                        store_mainline(config, string.joinv(",", d_mainline));
                }
 
+               public RefsList refs_list
+               {
+                       get { return d_main.refs_list; }
+               }
+
                private void reload()
                {
                        var view = d_main.commit_list_view;
diff --git a/libgitg/gitg-repository.vala b/libgitg/gitg-repository.vala
index effcb8b..4b63add 100644
--- a/libgitg/gitg-repository.vala
+++ b/libgitg/gitg-repository.vala
@@ -143,6 +143,11 @@ public class Repository : Ggit.Repository
                return base.lookup_reference(name) as Ref;
        }
 
+       public new Ref lookup_reference_dwim(string short_name) throws Error
+       {
+               return base.lookup_reference_dwim(short_name) as Ref;
+       }
+
        public new Ref create_reference(string name, Ggit.OId oid, string message) throws Error
        {
                return base.create_reference(name, oid, message) as Ref;


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