volcano.sh/volcano@v1.9.0/hack/sync-scheduler-code.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2019 The Volcano Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -e
    18  
    19  # set USE_HTTPS to false if you are using git@github.com/xxx/xx.git as remote
    20  USE_HTTPS=${USE_HTTPS:-true}
    21  
    22  # set CHECK_GIT_REMOTE false if you already set remote correctly
    23  CHECK_GIT_REMOTE=${CHECK_GIT_REMOTE:-true}
    24  
    25  # Useful Defaults
    26  ## Target github organization name
    27  TARGET_ORG=${TARGET_ORG:-"volcano-sh"}
    28  ## main repo uptream configs
    29  UPSTREAM=${UPSTREAM:-"upstream"}
    30  UPSTREAM_HEAD=${UPSTREAM_HEAD:-"master"}
    31  UPSTREAM_REPO_NAME=${UPSTREAM_REPO_NAME:-"volcano"}
    32  ## out of tree upstream configs
    33  OOT_UPSTREAM=${OOT_UPSTREAM:-"scheduler-upstream"}
    34  OOT_UPSTREAM_HEAD=${OOT_UPSTREAM_HEAD:-"master"}
    35  OOT_UPSTREAM_REPO_NAME=${OOT_UPSTREAM_REPO_NAME:-"scheduler"}
    36  # sub-directories to sync from upstream scheduler
    37  PATH_TO_SYNC=("pkg/scheduler" "pkg/apis/scheduling" "pkg/apis/utils")
    38  # branch name for working changes
    39  WORKING_BRANCH_NAME=${WORKING_BRANCH_NAME:-"sync-scheduler-code"}
    40  
    41  
    42  PROTO_HEAD="git@"
    43  SPLITER=":"
    44  if [[ ${USE_HTTPS} ]]; then
    45    PROTO_HEAD="https://"
    46    SPLITER="/"
    47  fi
    48  
    49  function check-and-add-upstream() {
    50    local upstream=$1
    51    local upstream_url=$2
    52    git remote -v | grep -qE "^${upstream}\b.*${upstream_url}" || rc="$?"
    53    if [[ "${rc}" -ne "0" ]]; then
    54      printf "[INFO] git remote not found, adding: %s\t%s\n" ${upstream} ${upstream_url}
    55      git remote add ${upstream} ${upstream_url}
    56    fi
    57  }
    58  
    59  if [[ ${CHECK_GIT_REMOTE} ]]; then
    60    UPSTREAM_URL="${PROTO_HEAD}github.com${SPLITER}${TARGET_ORG}/${UPSTREAM_REPO_NAME}.git"
    61    OOT_UPSTREAM_URL="${PROTO_HEAD}github.com${SPLITER}${TARGET_ORG}/${OOT_UPSTREAM_REPO_NAME}.git"
    62    check-and-add-upstream ${UPSTREAM} ${UPSTREAM_URL}
    63    check-and-add-upstream ${OOT_UPSTREAM} ${OOT_UPSTREAM_URL}
    64  fi
    65  
    66  printf "[INFO] updating remote %s, %s.\n" ${UPSTREAM} ${OOT_UPSTREAM}
    67  git remote update --prune ${UPSTREAM} ${OOT_UPSTREAM}
    68  
    69  printf "[INFO] creating branch %s based on %s.\n" ${WORKING_BRANCH_NAME} "${UPSTREAM}/${UPSTREAM_HEAD}"
    70  git checkout -b ${WORKING_BRANCH_NAME} --track "${UPSTREAM}/${UPSTREAM_HEAD}"
    71  
    72  
    73  function sync-code(){
    74    local PATHS=$1
    75    local target_head=$(git log -1 --pretty=format:"%H" ${OOT_UPSTREAM}/${OOT_UPSTREAM_HEAD})
    76  
    77    for PTS in ${PATHS[*]}
    78    do
    79      # printf "[INFO] checking in %s code with history from %s.\n" ${PTS} ${OOT_UPSTREAM}
    80  
    81      if [ -d ${PTS} ]; then
    82        #### need test
    83        local base=$(git log -1 --pretty=format:"%H" ${PTS})
    84        printf "[INFO] %-20s already exists, updating code to %s.\n" ${PTS} "${OOT_UPSTREAM}/${OOT_UPSTREAM_HEAD}"
    85        git read-tree --prefix=${PTS} -u ${base}:${PTS} ${target_head}:${PTS}
    86      else
    87        printf "[INFO] %-20s not exist, checking in code with history from %s.\n" ${PTS} ${OOT_UPSTREAM}
    88        git read-tree --prefix=${PTS} -u ${target_head}:${PTS}
    89        git checkout -- ${PTS}
    90      fi
    91    done
    92    printf "[INFO] folders (%s) are now up to date with %s.\n" "${PATHS[*]}" "${target_head}"
    93  }
    94  
    95  # Check in code with history from upstream repo
    96  sync-code "${PATH_TO_SYNC[*]}"
    97  
    98  
    99  NEW_COMMIT=$(git write-tree)
   100  PARENT_A=$(git rev-parse ${WORKING_BRANCH_NAME})
   101  PARENT_B=$(git rev-parse ${OOT_UPSTREAM}/${OOT_UPSTREAM_HEAD})
   102  
   103  printf "[INFO] generating merge commit %s with parents:\n" ${NEW_COMMIT}
   104  printf "\t%-20s\t%s\n" ${UPSTREAM} ${PARENT_A}
   105  printf "\t%-20s\t%s\n" ${OOT_UPSTREAM} ${PARENT_B}
   106  
   107  
   108  # commit subtree updates
   109  FINAL_COMMIT=$(echo "update in-tree ${OOT_UPSTREAM} code" |\
   110   git commit-tree ${NEW_COMMIT} -p ${PARENT_A} -p ${PARENT_B})
   111  
   112  git reset ${FINAL_COMMIT}
   113  
   114  printf "[INFO] done.\n"