From 0cffa112e0f94ff933620ddd498eed69592320d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Tue, 22 Mar 2022 14:33:40 +0200 Subject: [PATCH] build-llvm: Avoid unnecessary repeated git fetches if SYNC=1 is set If the SYNC variable is set when running build-all.sh, all the individual scripts will try to check out the intended version before building. All scripts that build projects from llvm-project (build-libcxx.sh, build-compiler-rt.sh, build-openmp.sh) call build-llvm.sh with CHECKOUT_ONLY=1 to make sure the right version is checked out (as one can call the scripts individually too). If the llvm-project directory was a full (non-shallow) checkout, the repeated fetch was quick as git knew that it had the fetched reference already. But in a shallow clone, a new shallow fetch would refetch the whole object tree for the targeted reference. Using "git cat-file -e" we can check whether a specific object exists in the local checkout, avoiding needlessly refetching everything. However, when fetching a tag using shallow fetch (git fetch --depth 1 origin "$LLVM_VERSION"), it would only fetch the commit object for that tag, not the tag itself. By specifying 'tag "$LLVM_VERSION"' in this case, we get the tag object itself, so that we later can check whether "$LLVM_VERSION" exists in the local checkout. But this requires guessing whether a specific "$LLVM_VERSION" is a tag or a commit. --- build-llvm.sh | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/build-llvm.sh b/build-llvm.sh index 3d64047..12cc0d1 100755 --- a/build-llvm.sh +++ b/build-llvm.sh @@ -79,18 +79,35 @@ if [ ! -d llvm-project ]; then cd llvm-project git init git remote add origin https://github.com/llvm/llvm-project.git - git fetch --depth 1 origin "$LLVM_VERSION" - git checkout FETCH_HEAD cd .. CHECKOUT=1 - unset SYNC fi if [ -n "$SYNC" ] || [ -n "$CHECKOUT" ]; then cd llvm-project - if [ -n "$SYNC" ]; then - git fetch --depth 1 origin "$LLVM_VERSION" - git checkout FETCH_HEAD + # Check if the intended commit or tag exists in the local repo. If it + # exists, just check it out instead of trying to fetch it. + # (Redoing a shallow fetch will refetch the data even if the commit + # already exists locally, unless fetching a tag with the "tag" + # argument.) + if git cat-file -e "$LLVM_VERSION" 2> /dev/null; then + # Exists; just check it out + git checkout "$LLVM_VERSION" + else + case "$LLVM_VERSION" in + llvmorg-*) + # If $LLVM_VERSION looks like a tag, fetch it with the + # "tag" keyword. This makes sure that the local repo + # gets the tag too, not only the commit itself. This allows + # later fetches to realize that the tag already exists locally. + git fetch --depth 1 origin tag "$LLVM_VERSION" + git checkout "$LLVM_VERSION" + ;; + *) + git fetch --depth 1 origin "$LLVM_VERSION" + git checkout FETCH_HEAD + ;; + esac fi cd .. fi