bin/refs: Add option to print revisions
authorDan Nicholson <dbn@endlessos.org>
Sun, 29 Jan 2023 16:23:13 +0000 (09:23 -0700)
committerDan Nicholson <dbn@endlessos.org>
Wed, 8 Feb 2023 05:59:30 +0000 (22:59 -0700)
Allow printing the revision along with the ref. This is very convenient
for looping over the refs in a shell as well as for quickly seeing which
refs are pointed to the same commit.

bash/ostree
man/ostree-refs.xml
src/ostree/ot-builtin-refs.c
tests/test-refs.sh

index 5958176f46ce4b2c49c5bdbb28cc1991fc14589b..ea915b4bb4ae9e6bf9a47d5559f640815a827b14 100644 (file)
@@ -953,6 +953,7 @@ _ostree_pull() {
 _ostree_refs() {
     local boolean_options="
         $main_boolean_options
+        --revision -r
         --alias -A
         --collections -c
         --delete
index 97d69fd768894820555fa84329560b1afd2d4335..b54039a38f3f2df89f50f0bd3ec883f82107592c 100644 (file)
@@ -98,6 +98,15 @@ License along with this library. If not, see <https://www.gnu.org/licenses/>.
                 </para></listitem>
             </varlistentry>
 
+            <varlistentry>
+                <term><option>--revision</option>, <option>-r</option></term>
+
+                <listitem><para>
+                    When listing refs, also print their revisions. The revisions
+                    will be separated by a tab character.
+                </para></listitem>
+            </varlistentry>
+
             <varlistentry>
                 <term><option>--alias</option>, <option>-A</option></term>
 
index 0233b8f801753fabec6db0b44b79b23c341a16aa..c49d7507bdea36b2430410f128c8b7bae8b48da6 100644 (file)
@@ -27,6 +27,7 @@
 
 static gboolean opt_delete;
 static gboolean opt_list;
+static gboolean opt_revision;
 static gboolean opt_alias;
 static char *opt_create;
 static gboolean opt_collections;
@@ -40,6 +41,7 @@ static gboolean opt_force;
 static GOptionEntry options[] = {
   { "delete", 0, 0, G_OPTION_ARG_NONE, &opt_delete, "Delete refs which match PREFIX, rather than listing them", NULL },
   { "list", 0, 0, G_OPTION_ARG_NONE, &opt_list, "Do not remove the prefix from the refs", NULL },
+  { "revision", 'r', 0, G_OPTION_ARG_NONE, &opt_revision, "Show revisions in listing", NULL },
   { "alias", 'A', 0, G_OPTION_ARG_NONE, &opt_alias, "If used with --create, create an alias, otherwise just list aliases", NULL },
   { "create", 0, 0, G_OPTION_ARG_STRING, &opt_create, "Create a new ref for an existing commit", "NEWREF" },
   { "collections", 'c', 0, G_OPTION_ARG_NONE, &opt_collections, "Enable listing collection IDs for refs", NULL },
@@ -82,7 +84,16 @@ do_ref_with_collections (OstreeRepo    *repo,
       for (GList *iter = ordered_keys; iter != NULL; iter = iter->next)
         {
           OstreeCollectionRef *ref = iter->data;
-          g_print ("(%s, %s)\n", ref->collection_id, ref->ref_name);
+
+          if (opt_revision)
+            {
+              const char *rev = g_hash_table_lookup (refs, ref);
+              g_print ("(%s, %s)\t%s\n", ref->collection_id, ref->ref_name, rev);
+            }
+          else
+            {
+              g_print ("(%s, %s)\n", ref->collection_id, ref->ref_name);
+            }
         }
     }
   else if (opt_create)
@@ -203,6 +214,11 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
               const char *alias = g_hash_table_lookup (refs, ref);
               g_print ("%s -> %s\n", ref, alias);
             }
+          else if (opt_revision)
+            {
+              const char *rev = g_hash_table_lookup (refs, ref);
+              g_print ("%s\t%s\n", ref, rev);
+            }
           else
             {
               g_print ("%s\n", ref);
index 1ddb3a21e4db0c09b48238be5c331812248a35c3..53b36f6bbcc7cd8b30c73d7eb6cc67afab4d4d7a 100755 (executable)
@@ -55,6 +55,14 @@ assert_file_has_content refs foo
 ${CMD_PREFIX} ostree --repo=repo refs foo | wc -l > refscount.foo
 assert_file_has_content refscount.foo "^5$"
 
+rm -f expected-refs-revs
+for ref in foo/test-{1..5}; do
+    rev=$(${CMD_PREFIX} ostree --repo=repo rev-parse $ref)
+    echo -e "${ref}\t${rev}" >> expected-refs-revs
+done
+${CMD_PREFIX} ostree --repo=repo refs --list --revision foo > refs-revs
+assert_files_equal refs-revs expected-refs-revs
+
 ${CMD_PREFIX} ostree --repo=repo refs --delete 2>/dev/null || true
 ${CMD_PREFIX} ostree --repo=repo refs | wc -l > refscount.delete1
 assert_file_has_content refscount.delete1 "^10$"