[gnome-builder/wip/gtk4-port] plugins/copyright: add update copyright helper



commit aa47096d9467a1f5de1460eda5b77e2b19b13596
Author: Christian Hergert <chergert redhat com>
Date:   Thu May 19 17:40:46 2022 -0700

    plugins/copyright: add update copyright helper

 src/plugins/copyright/gbp-copyright-util.c | 111 +++++++++++++++++++++++++++++
 src/plugins/copyright/gbp-copyright-util.h |  30 ++++++++
 src/plugins/copyright/meson.build          |   7 ++
 src/plugins/copyright/test-copyright.c     |  60 ++++++++++++++++
 4 files changed, 208 insertions(+)
---
diff --git a/src/plugins/copyright/gbp-copyright-util.c b/src/plugins/copyright/gbp-copyright-util.c
new file mode 100644
index 000000000..f3a9e1335
--- /dev/null
+++ b/src/plugins/copyright/gbp-copyright-util.c
@@ -0,0 +1,111 @@
+/* gbp-copyright-util.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-copyright-util"
+
+#include "config.h"
+
+#include "gbp-copyright-util.h"
+
+static inline gboolean
+is_dash (const char *token)
+{
+  return token[0] == '-' && token[1] == 0;
+}
+
+static inline gboolean
+isalnumdigit (char c)
+{
+  return c >= '0' && 'c' <= '9';
+}
+
+static inline gboolean
+is_year (const char *token)
+{
+  return token[0] != 0 && isalnumdigit (token[0]) &&
+         token[1] != 0 && isalnumdigit (token[1]) &&
+         token[2] != 0 && isalnumdigit (token[2]) &&
+         token[3] != 0 && isalnumdigit (token[3]) &&
+         token[4] == 0;
+}
+
+static char *
+replace_copyright_year (const char * const *tokens,
+                        guint               n_tokens,
+                        const char         *with_year)
+{
+  g_autoptr(GPtrArray) ar = NULL;
+  int dash = -1;
+
+  ar = g_ptr_array_sized_new (n_tokens + 3);
+  memcpy (ar->pdata, tokens, sizeof (char *) * n_tokens);
+  ar->pdata[n_tokens] = NULL;
+  ar->len = n_tokens + 1;
+
+  for (guint i = 0; i < n_tokens; i++)
+    {
+      if (i > 0 && is_dash (tokens[i]))
+        dash = i;
+      else if (g_strcmp0 (tokens[i], with_year) == 0)
+        return NULL;
+    }
+
+  if (dash >= 0)
+    {
+      if (dash+1 < n_tokens && is_year (tokens[dash+1]))
+        g_ptr_array_index (ar, dash+1) = (gpointer)with_year;
+      else
+        g_ptr_array_insert (ar, dash+1, (gpointer)with_year);
+    }
+  else
+    {
+      g_ptr_array_insert (ar, 2, (gpointer)"-");
+      g_ptr_array_insert (ar, 3, (gpointer)with_year);
+
+      /* Maybe swallow trailing - like "2022- " */
+      if (ar->pdata[4] != NULL && ((char *)ar->pdata[4])[0] == '-')
+        ar->pdata[4] = &((char *)ar->pdata[4])[1];
+    }
+
+  return g_strjoinv (NULL, (char **)(gpointer)ar->pdata);
+}
+
+char *
+gbp_update_copyright (const char *input,
+                      const char *with_year)
+{
+  static GRegex *regex;
+  g_auto(GStrv) tokens = NULL;
+  guint n_tokens;
+
+  if (input == NULL || input[0] == 0)
+    return NULL;
+
+  if (regex == NULL)
+    regex = g_regex_new ("([0-9]{4})", G_REGEX_OPTIMIZE, 0, NULL);
+
+  tokens = g_regex_split (regex, input, 0);
+  n_tokens = g_strv_length (tokens);
+
+  if (n_tokens < 2 || n_tokens > 6)
+    return NULL;
+
+  return replace_copyright_year ((const char * const *)tokens, n_tokens, with_year);
+}
diff --git a/src/plugins/copyright/gbp-copyright-util.h b/src/plugins/copyright/gbp-copyright-util.h
new file mode 100644
index 000000000..caf27912a
--- /dev/null
+++ b/src/plugins/copyright/gbp-copyright-util.h
@@ -0,0 +1,30 @@
+/* gbp-copyright-util.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+char *gbp_update_copyright (const char *input,
+                            const char *with_year);
+
+G_END_DECLS
diff --git a/src/plugins/copyright/meson.build b/src/plugins/copyright/meson.build
index 54ef08e3e..5ba17ab71 100644
--- a/src/plugins/copyright/meson.build
+++ b/src/plugins/copyright/meson.build
@@ -3,7 +3,14 @@ if get_option('plugin_copyright')
     'copyright-plugin.c',
     'gbp-copyright-buffer-addin.c',
     'gbp-copyright-preferences-addin.c',
+    'gbp-copyright-util.c',
   )
 
   install_data('org.gnome.builder.plugins.copyright.gschema.xml', install_dir: schema_dir)
+
+  test_copyright = executable('test-copyright',
+    ['test-copyright.c', 'gbp-copyright-util.c'],
+    dependencies: [ libglib_dep ],
+  )
+  test('test-copyright', test_copyright)
 endif
diff --git a/src/plugins/copyright/test-copyright.c b/src/plugins/copyright/test-copyright.c
new file mode 100644
index 000000000..e34517a37
--- /dev/null
+++ b/src/plugins/copyright/test-copyright.c
@@ -0,0 +1,60 @@
+/* test-copyright.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "gbp-copyright-util.h"
+
+#define TEST_YEAR "2042"
+
+static void
+test_update_copyright (void)
+{
+  static const struct {
+    const char *input;
+    const char *output; /* NULL indicates no change requested */
+  } copyright_year_tests[] = {
+    { "1234", "1234-"TEST_YEAR },
+    { " 1234-", " 1234-"TEST_YEAR },
+    { "-1234", "-1234-"TEST_YEAR }, /* odd, but expected */
+    { "-", NULL },
+    { "", NULL },
+    { "# Copyright 2019 Foo", "# Copyright 2019-"TEST_YEAR" Foo" },
+    { "# Copyright "TEST_YEAR" Foo", NULL },
+    { "# Copyright -"TEST_YEAR" Foo", NULL },
+    { "/* Copyright "TEST_YEAR"- Foo */", NULL },
+    { "# Copyright 2019- Foo", "# Copyright 2019-"TEST_YEAR" Foo" },
+    { "# Copyright - ", NULL },
+  };
+
+  for (guint i = 0; i < G_N_ELEMENTS (copyright_year_tests); i++)
+    {
+      g_autofree char *replaced = gbp_update_copyright (copyright_year_tests[i].input, TEST_YEAR);
+
+      g_assert_cmpstr (replaced, ==, copyright_year_tests[i].output);
+    }
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/Plugins/Copyright/update_copyright", test_update_copyright);
+  return g_test_run ();
+}


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