commit:     da8e7d193563a2b0c88a523083b2a3466ff4526e
Author:     Florian Schmaus <flow <AT> gentoo <DOT> org>
AuthorDate: Wed Dec 14 16:09:50 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Dec 21 01:28:02 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=da8e7d19

sync: git: only perform shallow updates if the repository is shallow

With the new default of shallow cloning/updating git repositories,
added with f2207e41792d ("GitSync.update: default to "--depth 1" (bug
824782 comment 17)"), existing *non-shallow* repositories would become
shallow after a "emerge --sync".

This adds a check to see if the repository is already shallow, and
only in this case performs a shallow clone.

Thanks to Douglas Freed for pointing out that this should consider
whether or not the user explicitly set sync-depth.

Thanks-to: Douglas Freed <dwfreed <AT> mtu.edu>
Bug: https://bugs.gentoo.org/824782
Bug: https://bugs.gentoo.org/887025
Closes: https://github.com/gentoo/portage/pull/960
Signed-off-by: Sam James <sam <AT> gentoo.org>

 NEWS                                |  2 ++
 lib/portage/sync/modules/git/git.py | 28 +++++++++++++++++++++++-----
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index e80c82227..98f9c71a8 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@ portage-3.0.42 (UNRELEASED)
 Features:
 * cnf: make.conf.example.loong: add for the loong arch (bug #884135).
 
+* git: only perform shallow updates if the repository is a shallow one
+
 Bug fixes:
 * glsa: Abort if a GLSA's arch list doesn't match the expected format (bug 
#882797).
 

diff --git a/lib/portage/sync/modules/git/git.py 
b/lib/portage/sync/modules/git/git.py
index 5fcc292cc..f16af0622 100644
--- a/lib/portage/sync/modules/git/git.py
+++ b/lib/portage/sync/modules/git/git.py
@@ -148,12 +148,31 @@ class GitSync(NewBase):
 
         if self.settings.get("PORTAGE_QUIET") == "1":
             git_cmd_opts += " --quiet"
+
+        # Default: Perform shallow updates (but only if the target is
+        # already a shallow repository).
+        sync_depth = 1
         if self.repo.sync_depth is not None:
-            if self.repo.sync_depth != 0:
-                git_cmd_opts += " --depth %d" % self.repo.sync_depth
+            sync_depth = self.repo.sync_depth
         else:
-            # default
-            git_cmd_opts += " --depth 1"
+            # If sync-depth is not explicitly set by the user,
+            # then check if the target repository is already a
+            # shallow one. And do not perform a shallow update if
+            # the target repository is not shallow.
+            is_shallow_cmd = ["git", "rev-parse", "--is-shallow-repository"]
+            is_shallow_res = portage._unicode_decode(
+                subprocess.check_output(
+                    is_shallow_cmd,
+                    cwd=portage._unicode_encode(self.repo.location),
+                )
+            )
+            if is_shallow_res == "false":
+                sync_depth = 0
+
+        shallow = False
+        if sync_depth > 0:
+            git_cmd_opts += f" --depth {sync_depth}"
+            shallow = True
 
         if self.repo.module_specific_options.get("sync-git-pull-extra-opts"):
             git_cmd_opts += (
@@ -181,7 +200,6 @@ class GitSync(NewBase):
             writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
             return (e.returncode, False)
 
-        shallow = self.repo.sync_depth is None or self.repo.sync_depth != 0
         if shallow:
             # For shallow fetch, unreachable objects may need to be pruned
             # manually, in order to prevent automatic git gc calls from

Reply via email to