aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/gi/git-cgit/git-cgit.sh
blob: d24fda15b3cfe63c16f4ed03c498aa08cf07a708 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env sh

# nixos-config - My current NixOS configuration
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of my nixos-config.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

NAME="git-cgit"

not_empty() {
    arg="$1"
    name="$2"

    if [ "$arg" = "" ]; then
        echo 1>&2 "flag '$name' is empty"
        exit 2
    fi
}

help() {
    cat <<EOF
Create a cgit repo to push the current repo to.

USAGE:
    $NAME [OPTIONS] --git URL..

OPTIONS:
    --help | -h
                            Display this help and exit.
    --git URL
                            git upstream URL.
                            Should be in the form:
                                'ssh://git@<url>/path/to/repo'
    --desc DESC
                            Optional description (will be read from './.git/description' otherwise).
    --defbranch
                            Optional default branch
    --cgit-owner
                            Optional cgit owner
EOF
}

git_url=""
ssh_url=""
url_path=""
desc="$(cat "$(git rev-parse --show-toplevel)/.git/description")"

cgit_owner="$(id --name -u)"
defbranch="$(git branch --show-current)"

while [ "$#" -ne 0 ]; do
    case "$1" in
    "--help" | "-h")
        help
        exit 0
        ;;
    "--git")
        shift 1
        git_url="$1"
        ;;
    "--desc")
        shift 1
        desc="$1"
        ;;
    "--defbranch")
        shift 1
        defbranch="$1"
        ;;
    "--cgit-owner")
        shift 1
        cgit_owner="$1"
        ;;
    esac
    shift 1
done

# In the form git@<url>
ssh_url="${git_url#ssh://}"
while [ "$(dirname "$ssh_url")" != "." ]; do
    ssh_url="$(dirname "$ssh_url")"
done
url_path="${git_url#ssh://"$ssh_url"}"
url_path="${url_path#/}"

not_empty "$git_url" "--git"
not_empty "$ssh_url" "--git <indirectly>"
not_empty "$url_path" "--git <indirectly>"

not_empty "$desc" "--desc"
not_empty "$defbranch" "--defbranch"
not_empty "$cgit_owner" "--cgit-owner"

cat <<EOF
Initializing repo with following values:
    --git -> '$git_url'
    <ssh_url> -> '$ssh_url'
    <url_path> -> '$url_path'

    --desc -> '$desc'
    --defbranch -> '$defbranch'
    --cgit-owner -> '$cgit_owner'

EOF

printf "Continue [y/N]? "
read -r continue
if [ "$continue" != "y" ]; then
    echo 1>&2 "Not continuing.."
    exit 1
fi

set -x

git remote remove origin
git remote add origin "$git_url"

git push --set-upstream origin "$defbranch"

ssh "$ssh_url" -- perms "$url_path" + READERS @all
ssh "$ssh_url" -- desc "$url_path" "$desc"

ssh "$ssh_url" -- config "$url_path" --add cgit.defbranch "$defbranch"
ssh "$ssh_url" -- config "$url_path" --add cgit.owner "$cgit_owner"

ssh "$ssh_url" -- info
# vim: ft=sh