From dc95a3f9a3e0b9842177a99991a298964d359674 Mon Sep 17 00:00:00 2001 From: The-going <48602507+The-going@users.noreply.github.com> Date: Wed, 10 Jun 2020 19:15:08 +0300 Subject: [PATCH] Fixed checking the target git sources (#1998) * Exit if you can't create a directory Otherwise the entire folder structure will crash * git rev-parse --git-dir will always return ".git" line if we are in the git repository folder. When there is a symbolic link in the path, some functions will not return what is expected. This condition is always true. $(git rev-parse --show-toplevel) != $(pwd) This commit fixes this threat --- lib/general.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/general.sh b/lib/general.sh index 55ee16b98c..a03891d66b 100644 --- a/lib/general.sh +++ b/lib/general.sh @@ -253,21 +253,24 @@ fetch_from_repo() else local workdir=$dir fi - mkdir -p $SRC/cache/sources/$workdir + + mkdir -p $SRC/cache/sources/$workdir 2>/dev/null || \ + exit_with_error "No path or no write permission" "$SRC/cache/sources/$workdir" + cd $SRC/cache/sources/$workdir # check if existing remote URL for the repo or branch does not match current one # may not be supported by older git versions - local current_url=$(git remote get-url origin 2>/dev/null) - if [[ -n $current_url && $(git rev-parse --is-inside-work-tree 2>/dev/null) == true && \ - $(git rev-parse --show-toplevel) == $(pwd) && \ - $current_url != $url ]]; then + # Check the folder as a git repository. + # Then the target URL matches the local URL. + + if [[ "$(git rev-parse --git-dir 2>/dev/null)" == ".git" && \ + "$url" != "$(git remote get-url origin 2>/dev/null)" ]]; then display_alert "Remote URL does not match, removing existing local copy" rm -rf .git * fi - if [[ $(git rev-parse --is-inside-work-tree 2>/dev/null) != true || \ - $(git rev-parse --show-toplevel) != $(pwd) ]]; then + if [[ "$(git rev-parse --git-dir 2>/dev/null)" != ".git" ]]; then display_alert "Creating local copy" git init -q . git remote add origin $url