commit:     28d8d469ed8db68225fd50755655dcda61bd9a78
Author:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
AuthorDate: Fri Jul  1 18:53:30 2022 +0000
Commit:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
CommitDate: Fri Jul  1 19:01:18 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=28d8d469

git: mark repository as safe for newer gits

While this doesn't solve the odd permissions issue for communication
b/t gemato & git & portage, it does stop it manifesting.

This fixes compatibility with >=dev-vcs/git-2.35.2.

Bug: https://bugs.gentoo.org/838223
Bug: https://bugs.gentoo.org/838271
Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>

 lib/portage/sync/modules/git/git.py | 43 ++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)

diff --git a/lib/portage/sync/modules/git/git.py 
b/lib/portage/sync/modules/git/git.py
index 98670e1f9..381e31700 100644
--- a/lib/portage/sync/modules/git/git.py
+++ b/lib/portage/sync/modules/git/git.py
@@ -1,8 +1,9 @@
-# Copyright 2005-2020 Gentoo Authors
+# Copyright 2005-2022 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 import io
 import logging
+import re
 import subprocess
 
 import portage
@@ -104,15 +105,19 @@ class GitSync(NewBase):
 
         exitcode = portage.process.spawn_bash(
             "cd %s ; exec %s" % (portage._shell_quote(self.repo.location), 
git_cmd),
-            **self.spawn_kwargs
+            **self.spawn_kwargs,
         )
         if exitcode != os.EX_OK:
             msg = "!!! git clone error in %s" % self.repo.location
             self.logger(self.xterm_titles, msg)
             writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
             return (exitcode, False)
+
+        self.add_safe_directory()
+
         if not self.verify_head():
             return (1, False)
+
         return (os.EX_OK, True)
 
     def update(self):
@@ -152,6 +157,8 @@ class GitSync(NewBase):
                 " %s" % 
self.repo.module_specific_options["sync-git-pull-extra-opts"]
             )
 
+        self.add_safe_directory()
+
         try:
             remote_branch = portage._unicode_decode(
                 subprocess.check_output(
@@ -184,7 +191,7 @@ class GitSync(NewBase):
             exitcode = portage.process.spawn(
                 gc_cmd,
                 cwd=portage._unicode_encode(self.repo.location),
-                **self.spawn_kwargs
+                **self.spawn_kwargs,
             )
             if exitcode != os.EX_OK:
                 msg = "!!! git gc error in %s" % self.repo.location
@@ -207,7 +214,7 @@ class GitSync(NewBase):
 
         exitcode = portage.process.spawn_bash(
             "cd %s ; exec %s" % (portage._shell_quote(self.repo.location), 
git_cmd),
-            **self.spawn_kwargs
+            **self.spawn_kwargs,
         )
 
         if exitcode != os.EX_OK:
@@ -231,7 +238,7 @@ class GitSync(NewBase):
         exitcode = portage.process.spawn(
             merge_cmd,
             cwd=portage._unicode_encode(self.repo.location),
-            **self.spawn_kwargs
+            **self.spawn_kwargs,
         )
 
         if exitcode != os.EX_OK:
@@ -341,3 +348,29 @@ class GitSync(NewBase):
         except subprocess.CalledProcessError:
             ret = (1, False)
         return ret
+
+    def add_safe_directory(self):
+        # Add safe.directory to system gitconfig if not already configured.
+        # Workaround for bug #838271 and bug #838223.
+        location_escaped = re.escape(self.repo.location)
+        result = subprocess.run(
+            [
+                self.bin_command,
+                "config",
+                "--get",
+                "safe.directory",
+                f"^{location_escaped}$",
+            ]
+        )
+        if result.returncode == 1:
+            result = subprocess.run(
+                [
+                    self.bin_command,
+                    "config",
+                    "--system",
+                    "--add",
+                    "safe.directory",
+                    self.repo.location,
+                ]
+            )
+        return result.returncode == 0

Reply via email to