~ruther/guix-config

ref: 4b59ae9a24e3a8d49d5724e3be38bdf8ed65997b guix-config/home/dotfiles/scripts/setup_mirrored_repo -rw-r--r-- 1.9 KiB
4b59ae9a — Rutherther feat(home): Add setup_mirrored_repo script 4 months ago
                                                                                
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
#!/usr/bin/env bash

set -euxo pipefail

inspect_main_repo()
{
  # This script will print information about a repository.

  if [[ $# -ne 1 ]]; then
    echo "Usage: $0 repo"
    exit 1
  fi

  repo="$1"

  hut git show "$repo" | head -n1 | cut -d' ' -f2 | tr -d '()'
}

ensure_repo_created_forgejo()
{
  local repo
  local instance
  local repo_name
  local visibility
  local args

  repo="$2"
  host="$1"
  repo_name=$(echo "$repo" | cut -d'/' -f2)
  if ! fj --host "$host" repo view "$repo"; then
    visibility=$(inspect_main_repo "~ruther/$repo_name")
    args=""
    if [[ $visibility == "private" ]]; then
      args="$args -P"
    fi

    fj --host "$host" repo create "$repo_name" $args --description "Mirror of https://git.ditigal.xyz/~ruther/$repo_name"
  else
    echo "Repo already exists."
  fi
}

ensure_repo_created_srht()
{
  local repo
  local instance
  local repo_name
  local visibility

  repo="$2"
  instance="$1"
  repo_name=$(echo "$repo" | cut -d'/' -f2)
  if ! hut --instance "$instance" git show "~ruther/$repo_name"; then
    visibility=$(inspect_main_repo "$repo")
    hut --instance "$instance" git create "$repo_name" --visibility "$visibility" --description "Mirror of https://git.ditigal.xyz/$repo"
  else
    echo "Repo already exists."
  fi
}

# Fails if not repo
git status

repo="$(git remote get-url origin | awk -F'/' '{ print $NF }')"

# 1. Get url from remote origin (fetch url), the last part is name of repo
# 2. Ensure created on sr.ht
ensure_repo_created_srht "sr.ht" "$repo"

# 3. Ensure created on codeberg
#   only if not --no-codeberg
ensure_repo_created_forgejo "codeberg.org" "Rutherther/$repo"
# 4. Remove and read push urls

remotes=(
  "git@codeberg.org:Rutherther/$repo"
  "git@git.sr.ht:~ruther/$repo"
  "git@ditigal.xyz:~ruther/$repo")

set +e
for remote in "${remotes[@]}"; do
  git remote set-url --delete --push origin "$remote" 2>/dev/null
  git remote set-url --add --push origin "$remote"
done

git remote show origin