xref: /aosp_15_r20/external/openthread/script/git-tool (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/bin/bash
2#
3#  Copyright (c) 2020, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29#    Description:
30#      A tool to download projects with the ability to apply dependent PRs.
31#
32
33set -euxo pipefail
34
35apply_dependencies()
36{
37    local project_name
38
39    project_name=$(git remote get-url origin | grep -oE '[^/:]+/[^/:]+\.git$' | cut -d. -f1)
40    echo "project name: ${project_name}"
41
42    git config user.name || git config user.name 'OpenThread Git'
43    git config user.email || git config user.email 'git@openthread'
44
45    while read -r dependency; do
46        echo "${dependency}"
47        depends_on_pr="$(echo "${dependency}" | tr -d '\r\n' | cut -d# -f2)"
48        echo "pr: #${depends_on_pr}"
49        git pull --no-edit --no-rebase origin "pull/${depends_on_pr}/merge"
50    done < <(grep -E "^Depends-On: *${project_name}" || true)
51}
52
53get_pr_body()
54{
55    local pr
56
57    [[ ${GITHUB_ACTIONS+x} && ${GITHUB_REF-} =~ ^refs/pull/[0-9]+/merge ]] || {
58        echo 'Not a pull request.' >&2
59        return 0
60    }
61
62    pr="$(echo "${GITHUB_REF}" | cut -d/ -f3)"
63
64    # do not print GITHUB_TOKEN
65    set +x
66    curl -H "authorization: Bearer ${GITHUB_TOKEN}" -H 'content-type: application/json' \
67        "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${pr}" \
68        | jq -r .body
69    set -x
70}
71
72try_update()
73{
74    cd "$1"
75
76    get_pr_body | apply_dependencies
77}
78
79try_clone()
80{
81    local dest_dir
82
83    if [[ $1 == no-depend ]]; then
84        shift
85        git clone "$@" 2>&1
86    else
87        dest_dir="$(git clone "$@" 2>&1 | tee | cut -d\' -f2)"
88
89        cd "${dest_dir}"
90        get_pr_body | apply_dependencies
91    fi
92}
93
94print_help()
95{
96    echo "EXAMPLES:
97    $0 http://github.com/openthread/ot-br-posix.git
98    $0 http://github.com/openthread/ot-ns.git --depth 1 /tmp/ot-ns"
99}
100
101main()
102{
103    if [[ $# == 0 ]]; then
104        print_help
105        return 0
106    fi
107
108    case $1 in
109        update)
110            shift
111            try_update "$@"
112            ;;
113        clone)
114            shift
115            try_clone "$@"
116            ;;
117        *)
118            print_help
119            return 1
120            ;;
121    esac
122}
123
124main "$@"
125