@@ -1226,55 +1226,76 @@ stream_expansion_resolve_platform (GPtrArray **expanded_deps, GError **error)
12261226 return TRUE;
12271227}
12281228
1229+ /* #GCompareFunc for sorting a #GPtrArray of #ModulemdBuildConfig objects */
1230+ static gint
1231+ modulemd_module_stream_build_config_sort (gconstpointer a , gconstpointer b )
1232+ {
1233+ return modulemd_build_config_compare (* (ModulemdBuildConfig * * )a ,
1234+ * (ModulemdBuildConfig * * )b );
1235+ }
1236+
12291237/*
1230- * stream_expansion_dedup :
1238+ * stream_expansion_sort_dedup :
12311239 * @expanded_deps: (inout): A pointer to a pointer to a #GPtrArray of pointers
12321240 * to #ModulemdBuildConfig objects that is the set of stream expanded
12331241 * dependencies.
12341242 * @error: (out): A #GError that will return the reason for an error.
12351243 *
1236- * This method goes through the stream expanded dependencies and drops any
1237- * duplicates.
1244+ * This method sorts the stream expanded dependencies and drops any duplicates.
12381245 *
1239- * Returns: TRUE if deduplication succeeded, FALSE otherwise.
1246+ * Returns: TRUE if sorting and deduplication succeeded, FALSE otherwise.
12401247 */
12411248static gboolean
1242- stream_expansion_dedup (GPtrArray * * expanded_deps , GError * * error )
1249+ stream_expansion_sort_dedup (GPtrArray * * expanded_deps , GError * * error )
12431250{
12441251 g_autoptr (GPtrArray ) deduped_expanded_deps = NULL ;
1252+ g_autoptr (GPtrArray ) sorted_expanded_deps = NULL ;
12451253 ModulemdBuildConfig * dep = NULL ;
12461254 gboolean duplicate ;
12471255
12481256 g_return_val_if_fail (error == NULL || * error == NULL , FALSE);
12491257
1250- g_debug ("Expansion: stream_expansion_dedup called with %d deps" ,
1258+ g_debug ("Expansion: stream_expansion_sort_dedup called with %d deps" ,
12511259 (* expanded_deps )-> len );
12521260
1253- deduped_expanded_deps = g_ptr_array_new_with_free_func (g_object_unref );
1254-
1255- /*
1256- * this is horribly inefficient, but it's the best one can do without a way
1257- * to sort the objects
1261+ /* make a copy of the current expanded dependencies array (without
1262+ * incrementing the reference counts) and sort it
12581263 */
1259-
1260- /* for every expanded dependency... */
1264+ sorted_expanded_deps = g_ptr_array_sized_new ((* expanded_deps )-> len );
12611265 for (guint i = 0 ; i < (* expanded_deps )-> len ; i ++ )
12621266 {
1263- dep = g_ptr_array_index (* expanded_deps , i );
1267+ g_ptr_array_add (sorted_expanded_deps ,
1268+ g_ptr_array_index (* expanded_deps , i ));
1269+ }
1270+
1271+ g_debug ("Expansion: sorting %d deps" , sorted_expanded_deps -> len );
1272+ g_ptr_array_sort (sorted_expanded_deps ,
1273+ modulemd_module_stream_build_config_sort );
1274+
1275+ deduped_expanded_deps = g_ptr_array_new_with_free_func (g_object_unref );
1276+
1277+ /* for every sorted expanded dependency... */
1278+ for (guint i = 0 ; i < sorted_expanded_deps -> len ; i ++ )
1279+ {
1280+ dep = g_ptr_array_index (sorted_expanded_deps , i );
12641281
12651282 duplicate = FALSE;
12661283
1267- /* for every previously deduplicated dependency... */
1268- for (guint j = 0 ; j < deduped_expanded_deps -> len ; j ++ )
1284+ /* check if this dependency is a duplicate of the previous de-duplicated
1285+ * dependency
1286+ */
1287+ if (deduped_expanded_deps -> len > 0 )
12691288 {
12701289 if (modulemd_build_config_equals (
1271- dep , g_ptr_array_index (deduped_expanded_deps , j )))
1290+ dep ,
1291+ g_ptr_array_index (deduped_expanded_deps ,
1292+ deduped_expanded_deps -> len - 1 )))
12721293 {
12731294 duplicate = TRUE;
1274- break ;
12751295 }
12761296 }
12771297
1298+ /* if not a duplicate, copy and add to the de-duplicated list */
12781299 if (!duplicate )
12791300 {
12801301 g_ptr_array_add (deduped_expanded_deps ,
@@ -1284,6 +1305,7 @@ stream_expansion_dedup (GPtrArray **expanded_deps, GError **error)
12841305
12851306 if (deduped_expanded_deps -> len == 0 )
12861307 {
1308+ /* this should be impossible unless we started with none */
12871309 g_set_error_literal (error ,
12881310 MODULEMD_ERROR ,
12891311 MMD_ERROR_UPGRADE ,
@@ -1507,10 +1529,10 @@ modulemd_module_stream_expand_v2_to_v3_deps (ModulemdModuleStreamV2 *v2_stream,
15071529 } /* iterate through all of the V2 stream dependencies */
15081530
15091531 /*
1510- * Eliminate any duplicate entries from the combined expanded dependencies
1511- * from all of StreamV2's #ModulemdDependencies.
1532+ * Sort and eliminate any duplicate entries from the combined expanded
1533+ * dependencies from all of StreamV2's #ModulemdDependencies.
15121534 */
1513- if (!stream_expansion_dedup (& all_expanded_deps , & nested_error ))
1535+ if (!stream_expansion_sort_dedup (& all_expanded_deps , & nested_error ))
15141536 {
15151537 g_propagate_prefixed_error (
15161538 error ,
0 commit comments