]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
msgmerge: Make --sort-by-file sort messages without file reference correctly.
authorBruno Haible <bruno@clisp.org>
Fri, 17 Jul 2020 22:01:57 +0000 (00:01 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 17 Jul 2020 22:01:57 +0000 (00:01 +0200)
Reported by <wangqr@wangqr.tk> in <https://savannah.gnu.org/bugs/?58778>.

* gettext-tools/src/write-catalog.c (cmp_by_filepos): When a->filepos_count and
b->filepos_count are both 0, compare the msgid strings.
* gettext-tools/tests/msgmerge-29: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

gettext-tools/src/write-catalog.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/msgmerge-29 [new file with mode: 0644]

index 504bbaccdde59c9c4636df3d8a4c33e115e19921..c25916d1b403dc57257b85771c256bf833448f58 100644 (file)
@@ -1,5 +1,5 @@
 /* GNU gettext - internationalization aids
-   Copyright (C) 1995-1998, 2000-2008, 2012, 2019 Free Software
+   Copyright (C) 1995-1998, 2000-2008, 2012, 2019-2020 Free Software
    Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -423,23 +423,22 @@ cmp_by_filepos (const void *va, const void *vb)
   int cmp;
 
   /* No filepos is smaller than any other filepos.  */
-  if (a->filepos_count == 0)
-    {
-      if (b->filepos_count != 0)
-        return -1;
-    }
-  if (b->filepos_count == 0)
-    return 1;
-
-  /* Compare on the file names...  */
-  cmp = strcmp (a->filepos[0].file_name, b->filepos[0].file_name);
+  cmp = (a->filepos_count != 0) - (b->filepos_count != 0);
   if (cmp != 0)
     return cmp;
 
-  /* If they are equal, compare on the line numbers...  */
-  cmp = a->filepos[0].line_number - b->filepos[0].line_number;
-  if (cmp != 0)
-    return cmp;
+  if (a->filepos_count != 0)
+    {
+      /* Compare on the file names...  */
+      cmp = strcmp (a->filepos[0].file_name, b->filepos[0].file_name);
+      if (cmp != 0)
+        return cmp;
+
+      /* If they are equal, compare on the line numbers...  */
+      cmp = a->filepos[0].line_number - b->filepos[0].line_number;
+      if (cmp != 0)
+        return cmp;
+    }
 
   /* If they are equal, compare on the msgid strings.  */
   /* Because msgids normally contain only ASCII characters or are UTF-8
index 07d41323bca2399dfac1bbbf70b0892e5c52bcbb..6b7ec29eaf1a9e9fc689c90cd30c165e00ebe3df 100644 (file)
@@ -62,7 +62,7 @@ TESTS = gettext-1 gettext-2 \
        msgmerge-13 msgmerge-14 msgmerge-15 msgmerge-16 msgmerge-17 \
        msgmerge-18 msgmerge-19 msgmerge-20 msgmerge-21 msgmerge-22 \
        msgmerge-23 msgmerge-24 msgmerge-25 msgmerge-26 msgmerge-27 \
-       msgmerge-28 \
+       msgmerge-28 msgmerge-29 \
        msgmerge-compendium-1 msgmerge-compendium-2 msgmerge-compendium-3 \
        msgmerge-compendium-4 msgmerge-compendium-5 msgmerge-compendium-6 \
        msgmerge-properties-1 msgmerge-properties-2 \
diff --git a/gettext-tools/tests/msgmerge-29 b/gettext-tools/tests/msgmerge-29
new file mode 100644 (file)
index 0000000..4213178
--- /dev/null
@@ -0,0 +1,49 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test --sort-by-file option, when no file positions are present.
+
+cat <<\EOF > mm-test29.po
+msgid "a"
+msgstr "a"
+
+#~ msgid "c"
+#~ msgstr "c"
+
+#~ msgid "d"
+#~ msgstr "d"
+
+#~ msgid "b"
+#~ msgstr "b"
+EOF
+
+cat <<EOF > mm-test29.pot
+msgid "a"
+msgstr ""
+EOF
+
+: ${MSGMERGE=msgmerge}
+${MSGMERGE} -q --sort-by-file -o mm-test29.tmp mm-test29.po mm-test29.pot || Exit 1
+LC_ALL=C tr -d '\r' < mm-test29.tmp > mm-test29.po2 || Exit 1
+${MSGMERGE} -q --sort-by-file -o mm-test29.tmp mm-test29.po2 mm-test29.pot || Exit 1
+LC_ALL=C tr -d '\r' < mm-test29.tmp > mm-test29.po3 || Exit 1
+
+cat <<\EOF > mm-test29.ok
+msgid "a"
+msgstr "a"
+
+#~ msgid "b"
+#~ msgstr "b"
+
+#~ msgid "c"
+#~ msgstr "c"
+
+#~ msgid "d"
+#~ msgstr "d"
+EOF
+
+: ${DIFF=diff}
+${DIFF} mm-test29.ok mm-test29.po2 || Exit 1
+${DIFF} mm-test29.ok mm-test29.po3 || Exit 1
+
+Exit 0