Remove intrusive useless logging
[csit.git] / resources / libraries / bash / function / branch.sh
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 set -exuo pipefail
15
16 # This library defines functions related to handling VPP and CSIT git branches.
17 # Keep functions ordered alphabetically, please.
18
19 # TODO: Add a link to bash style guide.
20
21
22 function checkout_csit_for_vpp () {
23
24     set -exuo pipefail
25
26     # This should be useful mainly for vpp-csit jobs (and timed csit-vpp jobs),
27     # which want to use csit oper branches (especially for vpp stable branches).
28     # This allows the Jenkins job to checkout CSIT master branch,
29     # and use this function to compute and checkout the final CSIT branch.
30     # When the refspec is overriden, the computation is still performed,
31     # in order to show (on Sandbox) the computation is correct.
32     #
33     # On failure, working directory could remain changed to ${CSIT_DIR}.
34     # TODO: It could be possible to use ERR trap to force popd,
35     #   but with "set -x" the noise is not worth it,
36     #   especially if several levels of pushd are to be supported.
37     #
38     # Arguments:
39     # - ${1} - Git branch of VPP code, e.g. GERRIT_BRANCH set by Jenkins.
40     #   This is not read from GERRIT_BRANCH directly,
41     #   because in csit-vpp jobs that refers to CSIT branch instead.
42     #   Required.
43     # Variables read:
44     # - CSIT_REF - If set and non-empty, override the computed refspec.
45     # - CSIT_DIR - Path to existing root of local CSIT git repository.
46     #   The repository could be cloned with "--depth 1",
47     #   but it is required to be cloned with "--no-single-branch",
48     #   as otherwise the "git checkout" this function performs probably fails.
49     # Directoried updated:
50     # - ${CSIT_DIR} - Probably "git checkout"ed into new refspec.
51     # Functions called:
52     # - die - Print to stderr and exit, defined in "common" library.
53
54     case "${1}" in
55         "stable/"*)
56             branch_id="origin/${1/stable\//oper-rls}"
57             ;;
58         *)  # This includes "master".
59             branch_id="origin/oper"
60     esac
61     # Get the latest verified version of the required branch.
62     pushd "${CSIT_DIR}" || die
63     csit_branches="$(git branch -r | grep -E "${branch_id}-[0-9]+")" || {
64         # We might be in time when VPP has cut their new branch,
65         # but CSIT not, yet. Use master oper branch in this case.
66         csit_branches="$(git branch -r | grep -E "origin/oper-[0-9]+")" || die
67     }
68     # The xargs is there just to remove leading (or trailing) spaces.
69     csit_branch="$(echo "${csit_branches}" | tail -n 1 | xargs)" || die
70     if [[ -z "${csit_branch}" ]]; then
71         die "No verified CSIT branch found - exiting."
72     fi
73     # Remove 'origin/' from the branch name.
74     csit_branch="${csit_branch#origin/}" || die
75     override_ref="${CSIT_REF-}"
76     if [[ -n "${override_ref}" ]]; then
77         git fetch --depth=1 https://gerrit.fd.io/r/csit "${override_ref}"
78         git checkout FETCH_HEAD
79     else
80         git checkout "${csit_branch}"
81     fi
82     popd
83 }