From 7dabd9300d9920843fd36448bf47e759a1401abb Mon Sep 17 00:00:00 2001 From: Gunjan Gupta Date: Mon, 12 Feb 2024 21:43:49 +0530 Subject: [PATCH] Add ability to freeze git resources --- lib/functions/general/git-ref2info.sh | 24 ++++++++++++++++++++++++ lib/functions/general/git.sh | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/lib/functions/general/git-ref2info.sh b/lib/functions/general/git-ref2info.sh index 7d23a47bb5..277e701402 100644 --- a/lib/functions/general/git-ref2info.sh +++ b/lib/functions/general/git-ref2info.sh @@ -75,6 +75,30 @@ function memoized_git_ref_to_info() { exit_with_error "Failed to fetch SHA1 of '${MEMO_DICT[GIT_SOURCE]}' '${ref_type}' '${ref_name}' - make sure it's correct" fi + if [[ ${ref_type} == "branch" ]]; then + { + flock -x 5 + flock -x 6 + + [[ -s "${SRC}"/output/info/git_sources.json ]] || echo '[]' >&5 + jq --arg source "${MEMO_DICT[GIT_SOURCE]}" \ + --arg branch "${ref_name}" \ + --arg sha1 "${sha1}" \ + "if (map(select(.source == \$source and .branch == \$branch))| length) !=0 then \ + (.[]|select(.source == \$source and .branch == \$branch)).sha1 |= \$sha1 \ + else \ + . + [{\"source\": \$source, \"branch\": \$branch, \"sha1\": \$sha1}] \ + end" /dev/fd/5 >&6 + cat /dev/fd/6 >"${SRC}"/output/info/git_sources.json + } 5<>"${SRC}"/output/info/git_sources.json 6<>"${SRC}"/output/info/git_sources.json.new + fi + + if [[ -f "${SRC}"/config/sources/git_sources.json && ${ref_type} == "branch" ]]; then + cached_revision=$(jq --raw-output '.[] | select(.source == "'${MEMO_DICT[GIT_SOURCE]}'" and .branch == "'$ref_name'") |.sha1' "${SRC}"/config/sources/git_sources.json) + display_alert "Found cached git version" "${cached_revision}" "info" + [[ -z "${cached_revision}" ]] || sha1=${cached_revision} + fi + MEMO_DICT+=(["SHA1"]="${sha1}") if [[ "${2}" == "include_makefile_body" ]]; then diff --git a/lib/functions/general/git.sh b/lib/functions/general/git.sh index 99628e89ed..9ee5322f7d 100644 --- a/lib/functions/general/git.sh +++ b/lib/functions/general/git.sh @@ -252,6 +252,11 @@ function fetch_from_repo() { esac fi + if [[ -f "${SRC}"/config/sources/git_sources.json && $ref_type == "branch" ]]; then + cached_revision=$(jq --raw-output '.[] | select(.source == "'$url'" and .branch == "'$ref_name'") |.sha1' "${SRC}"/config/sources/git_sources.json) + [[ -z "${cached_revision}" ]] || fetched_revision=${cached_revision} + fi + if [[ "${do_checkout:-"yes"}" == "yes" ]]; then display_alert "git checking out revision SHA" "${fetched_revision}" "git" regular_git checkout -f -q "${fetched_revision}" # Return the files that are tracked by git to the initial state.