git-r3.eclass: Support more flexible EGIT_OVERRIDE_* APIs for user
authorMichał Górny <mgorny@gentoo.org>
Sat, 18 Nov 2017 09:14:31 +0000 (10:14 +0100)
committerMichał Górny <mgorny@gentoo.org>
Tue, 28 Nov 2017 09:14:47 +0000 (10:14 +0100)
Introduce a new, more flexible override API in git-r3, in replacement
of the LIVE_* API that was pretty much a legacy of git-2. This means to
solve the two major limitations of the old API:

1. The variables were based on package names without categories.
Therefore, they weren't suitable whenever two packages had the same
category. This is quite common when dealing with various programming
language bindings/reimplementations, and we can't really rely on every
new programming language inventing its own VCS.

2. The overrides weren't suitable for packages checking out multiple
repositories (LLVM, wine, glibc).

The new mode for overrides uses the repository name (as guessed
by git-r3) transformed into correct variable name. The specifically
defined variables are:

- EGIT_OVERRIDE_REPO_${NAME} -- to override the repository URI,

- EGIT_OVERRIDE_BRANCH_${NAME} -- to override the branch,

- EGIT_OVERRIDE_COMMIT_${NAME} -- to override the commit id or tag,

- EGIT_OVERRIDE_COMMIT_DATE_${NAME} -- to request last commit older than
  the specified date.

eclass/git-r3.eclass

index caf4e8d003e0d34f909efbee0d4e9679519c9cf4..55a987b7954596031882c5f1fe83c163ac3b0554 100644 (file)
@@ -553,6 +553,7 @@ _git-r3_is_local_repo() {
 git-r3_fetch() {
        debug-print-function ${FUNCNAME} "$@"
 
+       # process repos first since we create repo_name from it
        local repos
        if [[ ${1} ]]; then
                repos=( ${1} )
@@ -562,12 +563,6 @@ git-r3_fetch() {
                repos=( ${EGIT_REPO_URI} )
        fi
 
-       local branch=${EGIT_BRANCH:+refs/heads/${EGIT_BRANCH}}
-       local remote_ref=${2:-${EGIT_COMMIT:-${branch:-HEAD}}}
-       local local_id=${3:-${CATEGORY}/${PN}/${SLOT%/*}}
-       local local_ref=refs/git-r3/${local_id}/__main__
-       local commit_date=${4:-${EGIT_COMMIT_DATE}}
-
        [[ ${repos[@]} ]] || die "No URI provided and EGIT_REPO_URI unset"
 
        local r
@@ -591,6 +586,54 @@ git-r3_fetch() {
                )
        fi
 
+       # get the default values for the common variables and override them
+       local branch_name=${EGIT_BRANCH}
+       local commit_id=${2:-${EGIT_COMMIT}}
+       local commit_date=${4:-${EGIT_COMMIT_DATE}}
+
+       # support new override API for EAPI 6+
+       if ! has "${EAPI:-0}" 0 1 2 3 4 5; then
+               # get the name and do some more processing:
+               # 1) kill .git suffix,
+               # 2) underscore (remaining) non-variable characters,
+               # 3) add preceding underscore if it starts with a digit,
+               # 4) uppercase.
+               local override_name=${GIT_DIR##*/}
+               override_name=${override_name%.git}
+               override_name=${override_name//[^a-zA-Z0-9_]/_}
+               override_name=${override_name^^}
+
+               local varmap=(
+                       REPO:repos
+                       BRANCH:branch_name
+                       COMMIT:commit_id
+                       COMMIT_DATE:commit_date
+               )
+
+               local localvar livevar live_warn=
+               for localvar in "${varmap[@]}"; do
+                       livevar=EGIT_OVERRIDE_${localvar%:*}_${override_name}
+                       localvar=${localvar#*:}
+
+                       if [[ -n ${!livevar} ]]; then
+                               [[ ${localvar} == repos ]] && repos=()
+                               live_warn=1
+                               ewarn "Using ${livevar}=${!livevar}"
+                               declare "${localvar}=${!livevar}"
+                       fi
+               done
+
+               if [[ ${live_warn} ]]; then
+                       ewarn "No support will be provided."
+               fi
+       fi
+
+       # set final variables after applying overrides
+       local branch=${branch_name:+refs/heads/${branch_name}}
+       local remote_ref=${commit_id:-${branch:-HEAD}}
+       local local_id=${3:-${CATEGORY}/${PN}/${SLOT%/*}}
+       local local_ref=refs/git-r3/${local_id}/__main__
+
        # try to fetch from the remote
        local success saved_umask
        if [[ ${EVCS_UMASK} ]]; then