summaryrefslogtreecommitdiffstats
path: root/mkosi.images/system/mkosi.sync
blob: d21ecd198081cf5b304e0aaa292f4e3508a92719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -e
set -o nounset

if ((${NO_SYNC:-0})); then
    exit 0
fi

PKG_SUBDIR="$(realpath --canonicalize-missing "pkg/$DISTRIBUTION" --relative-to "$PWD")"

if [[ -d "$PKG_SUBDIR/.git" ]] && [[ "$(git -C "$PKG_SUBDIR" rev-parse HEAD)" == "$GIT_COMMIT" ]]; then
    exit 0
fi

# The repository on Salsa has the full upstream sources, so it's a waste of space to
# redownload and duplicate everything, so do a sparse checkout as we only need the
# packaging directory anyway
if [[ -n "${GIT_SUBDIR:-}" ]]; then
    sparse=(--no-checkout --filter=tree:0)
else
    sparse=()
fi

if [[ ! -e "$PKG_SUBDIR" ]] || [[ -z "$(ls --almost-all "$PKG_SUBDIR")" ]]; then
    git clone "$GIT_URL" --branch "$GIT_BRANCH" "${sparse[@]}" "$PKG_SUBDIR"
    if [[ -n "${GIT_SUBDIR:-}" ]]; then
        # --no-cone is needed to check out only one top-level directory
        git -C "$PKG_SUBDIR" sparse-checkout set --no-cone "${GIT_SUBDIR:-}"
    fi
else
    git -C "$PKG_SUBDIR" remote set-url origin "$GIT_URL"
    git -C "$PKG_SUBDIR" fetch origin "$GIT_BRANCH"
fi

git -C "$PKG_SUBDIR" -c advice.detachedHead=false checkout "$GIT_COMMIT"