2.44.1
[platform/upstream/at-spi2-core.git] / .gitlab-ci / search-common-ancestor.sh
1 #!/bin/bash
2
3 set -e
4
5 ancestor_horizon=28  # days (4 weeks)
6
7 # We need to add a new remote for the upstream target branch, since this script
8 # could be running in a personal fork of the repository which has out of date
9 # branches.
10 #
11 # Limit the fetch to a certain date horizon to limit the amount of data we get.
12 # If the branch was forked from origin/main before this horizon, it should
13 # probably be rebased.
14 if ! git ls-remote --exit-code upstream >/dev/null 2>&1 ; then
15     git remote add upstream https://gitlab.gnome.org/GNOME/${CI_PROJECT_NAME}.git
16 fi
17 git fetch --shallow-since="$(date --date="${ancestor_horizon} days ago" +%Y-%m-%d)" upstream
18
19 # Work out the newest common ancestor between the detached HEAD that this CI job
20 # has checked out, and the upstream target branch (which will typically be
21 # `upstream/main` or `upstream/gnome-40`).
22 # `${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}` or `${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}`
23 # are only defined if we’re running in a merge request pipeline,
24 # fall back to `${CI_DEFAULT_BRANCH}` or `${CI_COMMIT_BRANCH}` respectively
25 # otherwise.
26
27 source_branch="${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH}}"
28 git fetch --shallow-since="$(date --date="${ancestor_horizon} days ago" +%Y-%m-%d)" origin "${source_branch}"
29
30 newest_common_ancestor_sha=$(diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "upstream/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_DEFAULT_BRANCH}}") <(git rev-list --first-parent "origin/${source_branch}") | head -1)
31 if [ -z "${newest_common_ancestor_sha}" ]; then
32     echo "Couldn’t find common ancestor with upstream main branch. This typically"
33     echo "happens if you branched from main a long time ago. Please update"
34     echo "your clone, rebase, and re-push your branch."
35     exit 1
36 fi