From 0127a5e68be69a8c57bb43171bae3b1bb7ae4a6e Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Wed, 28 Feb 2024 11:38:00 +0100
Subject: [PATCH] gnulib-tool.py: Avoid exception when 'git log' output is
 unexpected.

* pygnulib/GLInfo.py (GLInfo.date): When the 'git log' output does not
contain a line with the expected 'Date:' pattern, pass the empty string
to GNU date.
---
 ChangeLog          |  7 +++++++
 pygnulib/GLInfo.py | 12 ++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index af0835269f..e667861d9c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-02-28  Bruno Haible  <bruno@clisp.org>
+
+	gnulib-tool.py: Avoid exception when 'git log' output is unexpected.
+	* pygnulib/GLInfo.py (GLInfo.date): When the 'git log' output does not
+	contain a line with the expected 'Date:' pattern, pass the empty string
+	to GNU date.
+
 2024-02-28  Bruno Haible  <bruno@clisp.org>
 
 	gnulib-tool: Avoid references to functions that get defined later.
diff --git a/pygnulib/GLInfo.py b/pygnulib/GLInfo.py
index 808f11b06f..8ffe1d309b 100644
--- a/pygnulib/GLInfo.py
+++ b/pygnulib/GLInfo.py
@@ -106,10 +106,14 @@ class GLInfo(object):
                     result = sp.check_output(args, cwd=DIRS['root']).decode("UTF-8")
                     # Get date as "Fri Mar 21 07:16:51 2008 -0600" from string
                     pattern = re.compile('^Date:[\t ]*(.*?)$', re.M)
-                    result = pattern.findall(result)[0]
-                    # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600"
-                    pattern = re.compile('^[^ ]* ([^ ]*) ([0-9]*) ([0-9:]*) ([0-9]*) ')
-                    result = pattern.sub('\\1 \\2 \\4 \\3 ', result)
+                    result = pattern.findall(result)
+                    if (len(result) > 0):
+                        result = result[0]
+                        # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600"
+                        pattern = re.compile('^[^ ]* ([^ ]*) ([0-9]*) ([0-9:]*) ([0-9]*) ')
+                        result = pattern.sub('\\1 \\2 \\4 \\3 ', result)
+                    else:
+                        result = ''
                     # Use GNU date to compute the time in GMT
                     args = ['date', '-d', result, '-u', '+%Y-%m-%d %H:%M:%S']
                     proc = sp.check_output(args)
-- 
2.39.5