This patch should hopefully stop gnulib-tool.py from mangling
.gitignore files like it did previously. I think I did a decent job
translating the join calls into Python sets, but any criticism is
welcome.

I tried using gnulib-tool.py to bootstrap GNU Inetutils and it handles
the .gitignore files properly now. Now all that is left in that diff
is an extra newline and 'AC_REQUIRE([AM_PROG_CC_C_O])'.

In the test case:
$ find . -name .gitignore~ -type f -print
./import-tests/test-coreutils-1.result/.gitignore~
./import-tests/test-wget2-1.result/.gitignore~

Should these files be removed?

diff --git a/gnulib-tool-tests/init.sh b/gnulib-tool-tests/init.sh
index c3625ca..27c4818 100644
--- a/gnulib-tool-tests/init.sh
+++ b/gnulib-tool-tests/init.sh
@@ -133,6 +138,10 @@ do_import_test ()
   if test -n "$build_aux_dir"; then
     rmdir $build_aux_dir
   fi
+  gitignores=`find $tmp-result -name '.gitignore~' -type f -print`
+  if test -n "$gitignores"; then
+    rm $gitignores
+  fi
   # Remove autom4te.cache directory, since it may depend on the Autoconf 
version or M4 version.
   rm -rf $tmp-result/"$2"/autom4te.cache
   if test $rc != 0; then

Then have something like this in init.sh? I'm not sure if this would
also apply to the test directory tests.

Collin
From b163f88f90453f20564960265a3cb5236c33ba85 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Fri, 22 Mar 2024 19:43:41 -0700
Subject: [PATCH] gnulib-tool.py: Follow gnulib-tool changes, part 69.

Follow gnulib-tool change
2012-08-19  Bruno Haible  <br...@clisp.org>
gnulib-tool: Remove old file names from .cvsignore, .gitignore.

* pygnulib/GLImport.py (GLImport._update_ignorelist_): Add
gnulib-comp.m4 to the added file list.
(GLImport.execute): Remove unused variables. Use sets to match the
'join' invocations in gnulib-tool.sh.
---
 ChangeLog            | 11 +++++++++++
 gnulib-tool.py.TODO  | 11 -----------
 pygnulib/GLImport.py | 25 +++++++++----------------
 3 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c8f4063444..a4bc9eda0b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-03-22  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Follow gnulib-tool changes, part 69.
+	Follow gnulib-tool change
+	2012-08-19  Bruno Haible  <br...@clisp.org>
+	gnulib-tool: Remove old file names from .cvsignore, .gitignore.
+	* pygnulib/GLImport.py (GLImport._update_ignorelist_): Add
+	gnulib-comp.m4 to the added file list.
+	(GLImport.execute): Remove unused variables. Use sets to match the
+	'join' invocations in gnulib-tool.sh.
+
 2024-03-22  Collin Funk  <collin.fu...@gmail.com>
 
 	gnulib-tool.py: Don't remove newlines in testdir's Automake snippets.
diff --git a/gnulib-tool.py.TODO b/gnulib-tool.py.TODO
index b3d83eeeb4..83d13eb1cb 100644
--- a/gnulib-tool.py.TODO
+++ b/gnulib-tool.py.TODO
@@ -65,15 +65,4 @@ Date:   Sat May 20 13:24:37 2017 +0200
 
 --------------------------------------------------------------------------------
 
-commit 9bdf6c8a0cdeb13c12e4b65dee9538c5468dbe1d
-Author: Bruno Haible <br...@clisp.org>
-Date:   Sun Aug 19 14:06:50 2012 +0200
-
-    gnulib-tool: Remove old file names from .cvsignore, .gitignore.
-
-    * gnulib-tool (func_update_ignorelist): Don't use 'join -v 1' command
-    on the list of removed files.
-
---------------------------------------------------------------------------------
-
 ================================================================================
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index 66b63ef08e..ca266242e2 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -790,7 +790,6 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
         '''GLImport._update_ignorelist_(directory, ignore, dirs_added, dirs_removed)
 
         Update .gitignore or .cvsignore files.'''
-        result = ''
         destdir = self.config['destdir']
         if ignore == '.gitignore':
             anchor = '/'
@@ -802,26 +801,18 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
             if dirs_added or dirs_removed:
                 with codecs.open(joinpath(destdir, srcpath), 'rb', 'UTF-8') as file:
                     srcdata = file.read()
-                dirs_ignore = sorted(set(srcdata.split('\n')))
-                dirs_ignore = [ line
-                                for line in dirs_ignore
-                                if line.strip() ]
-                srcdata = lines_to_multiline(sorted(set(dirs_ignore)))
-                dirs_ignore += [ d
-                                 for d in dirs_added
-                                 if d not in dirs_ignore ]
-                dirs_ignore = [ d
-                                for d in dirs_ignore
-                                if d in dirs_removed ]
+                dirs_ignore = { filename
+                                if not filename.startswith(anchor) else filename[len(anchor):]
+                                for filename in srcdata.split('\n')
+                                if filename.strip() }
+                srcdata = lines_to_multiline(sorted(dirs_ignore))
                 dirs_ignore = [ '%s%s' % (anchor, d)
-                                for d in dirs_ignore ]
-                dirs_ignore = sorted(set(dirs_ignore))
-                destdata = lines_to_multiline(sorted(set(dirs_ignore)))
+                                for d in set(dirs_added).difference(dirs_ignore) ]
+                destdata = lines_to_multiline(sorted(dirs_ignore))
                 if srcdata != destdata:
                     if not self.config['dryrun']:
                         print('Updating %s (backup in %s)' % (srcpath, backupname))
                         copyfile2(joinpath(destdir, srcpath), joinpath(destdir, backupname))
-                        result = ''
                         with codecs.open(joinpath(destdir, srcpath), 'ab', 'UTF-8') as file:
                             file.write(destdata)
                     else:  # if self.config['dryrun']
@@ -1396,6 +1387,8 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
         if vc_files != False:
             # Update the .cvsignore and .gitignore files.
             ignorelist = list()
+            # Treat gnulib-comp.m4 like an added file, even if it already existed.
+            filetable['added'] += [joinpath(m4base, 'gnulib-comp.m4')]
             filetable['added'] = sorted(set(filetable['added']))
             filetable['removed'] = sorted(set(filetable['added']))
             for file in filetable['added']:
-- 
2.44.0

Reply via email to