bin/refs: Sort listing
authorDan Nicholson <dbn@endlessos.org>
Sun, 29 Jan 2023 13:49:18 +0000 (06:49 -0700)
committerDan Nicholson <dbn@endlessos.org>
Wed, 8 Feb 2023 05:59:30 +0000 (22:59 -0700)
The output is much more readable sorted. I can't think of any reason
you'd want it unsorted (which is essentially dentry order).

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

index c687a5ffc78aa4b1a799faa36e511e4512ad21b0..0233b8f801753fabec6db0b44b79b23c341a16aa 100644 (file)
@@ -47,6 +47,16 @@ static GOptionEntry options[] = {
   { NULL }
 };
 
+static int
+collection_ref_cmp (OstreeCollectionRef *a,
+                    OstreeCollectionRef *b)
+{
+  int ret = g_strcmp0 (a->collection_id, b->collection_id);
+  if (ret == 0)
+    ret = g_strcmp0 (a->ref_name, b->ref_name);
+  return ret;
+}
+
 static gboolean
 do_ref_with_collections (OstreeRepo    *repo,
                          const char    *refspec_prefix,
@@ -66,10 +76,12 @@ do_ref_with_collections (OstreeRepo    *repo,
 
   if (!opt_delete && !opt_create)
     {
-      g_hash_table_iter_init (&hashiter, refs);
-      while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
+      g_autoptr(GList) ordered_keys = g_hash_table_get_keys (refs);
+      ordered_keys = g_list_sort (ordered_keys, (GCompareFunc) collection_ref_cmp);
+
+      for (GList *iter = ordered_keys; iter != NULL; iter = iter->next)
         {
-          const OstreeCollectionRef *ref = hashkey;
+          OstreeCollectionRef *ref = iter->data;
           g_print ("(%s, %s)\n", ref->collection_id, ref->ref_name);
         }
     }
@@ -179,12 +191,22 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
 
   if (is_list)
     {
-      GLNX_HASH_TABLE_FOREACH_KV (refs, const char *, ref, const char *, value)
+      g_autoptr(GList) ordered_keys = g_hash_table_get_keys (refs);
+      ordered_keys = g_list_sort (ordered_keys, (GCompareFunc) g_strcmp0);
+
+      for (GList *iter = ordered_keys; iter != NULL; iter = iter->next)
         {
+          const char *ref = iter->data;
+
           if (opt_alias)
-            g_print ("%s -> %s\n", ref, value);
+            {
+              const char *alias = g_hash_table_lookup (refs, ref);
+              g_print ("%s -> %s\n", ref, alias);
+            }
           else
-            g_print ("%s\n", ref);
+            {
+              g_print ("%s\n", ref);
+            }
         }
     }
   else if (opt_create)
index 3653d035a6f930095b50816b146bb315be8d26a3..1ddb3a21e4db0c09b48238be5c331812248a35c3 100755 (executable)
@@ -42,6 +42,10 @@ done
 ${CMD_PREFIX} ostree --repo=repo refs | wc -l > refscount
 assert_file_has_content refscount "^10$"
 
+${CMD_PREFIX} ostree --repo=repo refs > refs
+sort refs > refs-sorted
+assert_files_equal refs refs-sorted
+
 ${CMD_PREFIX} ostree --repo=repo refs foo > refs
 assert_not_file_has_content refs foo