fetch2/git.py: Check if clonedir is not empty after shallow tarball clone

Message ID 20220210092119.3994-1-tomasz.dziendzielski@gmail.com
State New
Headers show
Series fetch2/git.py: Check if clonedir is not empty after shallow tarball clone | expand

Commit Message

Tomasz Dziendzielski Feb. 10, 2022, 9:21 a.m. UTC
If shallow tarball clone is used and if tar unpacking fails for any
reason it might end up with empty directory created. Since runfetchcmd
does not check any return code we might not detect the issue and we will
not clone from git. Then due to missing hash it will replace main
repository's git remote with the remote of failing recipe.

Signed-off-by: Tomasz Dziendzielski <tomasz.dziendzielski@gmail.com>
Signed-off-by: Jan Brzezanski <jan.brzezanski@gmail.com>
---
 lib/bb/fetch2/git.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Pavel Zhukov Feb. 10, 2022, 9:39 a.m. UTC | #1
On 2022-02-10 at 10:21 +01, "Tomasz Dziendzielski" 
<tomasz.dziendzielski@gmail.com> wrote...
> If shallow tarball clone is used and if tar unpacking fails for 
> any
> reason it might end up with empty directory created. Since 
> runfetchcmd
> does not check any return code we might not detect the issue and 
> we will
> not clone from git. Then due to missing hash it will replace 
> main
> repository's git remote with the remote of failing recipe.
Maybe we should also check if ud.clonedir is top git directory (if
os.path.exist(".git") or better "git rev-parse --show-toplevel" ==
ud.clonedir) ? to prevent this from hapenning in the future. 
>
> Signed-off-by: Tomasz Dziendzielski 
> <tomasz.dziendzielski@gmail.com>
> Signed-off-by: Jan Brzezanski <jan.brzezanski@gmail.com>
> ---
>  lib/bb/fetch2/git.py | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/bb/fetch2/git.py b/lib/bb/fetch2/git.py
> index 30da8e95..cd317633 100644
> --- a/lib/bb/fetch2/git.py
> +++ b/lib/bb/fetch2/git.py
> @@ -356,8 +356,8 @@ class Git(FetchMethod):
>  
>          repourl = self._get_repo_url(ud)
>  
> -        # If the repo still doesn't exist, fallback to cloning 
> it
> -        if not os.path.exists(ud.clonedir):
> +        # If the repo still doesn't exist or is empty, fallback 
> to cloning it
> +        if not os.path.exists(ud.clonedir) or not 
> os.listdir(ud.clonedir):
>              # We do this since git will use a "-l" option 
>              automatically for local urls where possible
>              if repourl.startswith("file://"):
>                  repourl = repourl[7:]

Patch

diff --git a/lib/bb/fetch2/git.py b/lib/bb/fetch2/git.py
index 30da8e95..cd317633 100644
--- a/lib/bb/fetch2/git.py
+++ b/lib/bb/fetch2/git.py
@@ -356,8 +356,8 @@  class Git(FetchMethod):
 
         repourl = self._get_repo_url(ud)
 
-        # If the repo still doesn't exist, fallback to cloning it
-        if not os.path.exists(ud.clonedir):
+        # If the repo still doesn't exist or is empty, fallback to cloning it
+        if not os.path.exists(ud.clonedir) or not os.listdir(ud.clonedir):
             # We do this since git will use a "-l" option automatically for local urls where possible
             if repourl.startswith("file://"):
                 repourl = repourl[7:]