aboutsummaryrefslogtreecommitdiffstats
path: root/git-obranch
blob: be93a7b475d32b1e3c7249863246e8345be56682 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/sh
# Requires: coreutils, curl, jq, git
set -uex

API_KEY="$(cat "$(dirname "$(readlink -f "$0")")"/gitea-api-token.txt)"

gitea_cmd () {
    curl -s -H 'accept: application/json' -H 'Content-Type: application/json' \
            -H "Authorization: token $API_KEY" \
            "https://src.opensuse.org/api/v1/$1" 2>/dev/null
}

[ "$#" -lt 2 ] && exit 1

if [ "$1" = "openSUSE:Factory" ] ; then
    OUT=$(osc develproject "$1" "$2")
    DEVPRJ="$(dirname "$OUT")"
    PKG="$(basename "$OUT")"
else
    DEVPRJ="$1"
    PKG="$2"
fi

osc co -u -c "$DEVPRJ" "$PKG" || /bin/true
cd "$PKG"

# Fork
# Whoami?
LOGIN=$(gitea_cmd "user" | jq -r '.login') || /bin/true

# Check whether the fork already exists
FORKS="$(gitea_cmd "repos/pool/$PKG/forks" \
         | jq -r '.[].full_name' | grep -s -v '^rpm/')" || /bin/true

if [ -n "$FORKS" ] ; then
    MY_FORK="$(echo "$FORKS"|grep -s "$LOGIN")" || /bin/true
else
    MY_FORK=""
fi

# If not, make a fork
if [ -z "$MY_FORK" ] ; then
    FRK_OUT="$(gitea_cmd "repos/pool/$PKG/forks")"
    MY_FORK="$(echo "$FRK_OUT" | jq -r '.[].full_name')"
fi

# Add ignore
echo '.osc/' >> .git/info/exclude
echo '_scmsync.obsinfo' >> .git/info/exclude

# Set URLs for the forked repository
git remote add myproj "https://src.opensuse.org/$MY_FORK.git"
git remote set-url --push myproj "gitea@src.opensuse.org:$MY_FORK.git"