summaryrefslogtreecommitdiff
path: root/bin/change-git-remotes
blob: bd031a402631d28648582d9e356759310af64b6a (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
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
#!/bin/sh

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

if [ $# -eq 1 ] && [ "x$1" = 'x-q' ]; then
  quiet=true
elif [ $# -ne 0 ]; then
  printf 'change-git-remotes [-q]\n'
  exit 1
else
  quiet=false
fi

{
  # shellcheck disable=SC2016
  eval "$(
    printf '%s\n' "${repo_names}" \
    | tr ' ' '\n' \
    | sed '
      s/^.*$/echo "\0 ${repo_paths__\0}"/
    '
  )"
  printf '%s\n' "builder ${base_dir}"
} \
| while read -r git_name git_dir; do
  if ! remotes=$(
    git -C "${git_dir}" remote 2>/dev/null
  ); then
    ${quiet} \
    || >&2 printf '"%s" (%s) seems to be no git directory.\n' \
      "${git_dir}" \
      "${git_name}"
    continue
  fi
  if [ "${remotes}" != 'origin' ]; then
    ${quiet} \
    || >&2 printf '"%s" (%s) has multiple remotes - skipping.\n' \
      "${git_dir}" \
      "${git_name}"
    continue
  fi
  url=$(
    git -C "${git_dir}" remote get-url origin
  )
  case "${git_name}" in
    'archlinux32')
      new_urls=$(
        printf 'git@git.archlinux32.org:archlinux32/packages.git\n'
        printf 'https://git%s.archlinux32.org/archlinux32/packages\n' \
          '' 2 3 4
      )
    ;;
    'builder')
      new_urls=$(
        printf 'git@git.archlinux32.org:archlinux32/builder.git\n'
        printf 'https://git%s.archlinux32.org/archlinux32/builder\n' \
          '' 2 3 4
      )
    ;;
    *)
      new_urls="${url}"
    ;;
  esac
  if [ "${new_urls}" = "${url}" ]; then
    ${quiet} \
    || printf 'No alternative remotes are known for %s - skipping.\n' \
      "${git_name}"
    continue
  fi
  printf '%s\n' "${new_urls}" \
  | while read -r new_url; do
    if [ "${new_url}" = "${url}" ]; then
      continue
    fi
    git -C "${git_dir}" remote set-url origin "${new_url}"
    git -C "${git_dir}" remote set-url origin "${new_url}"
    if git -C "${git_dir}" fetch >/dev/null 2>&1; then
      ${quiet} \
      || >&2 printf '"%s" (%s): remote changed: "%s" -> "%s".\n' \
        "${git_dir}" \
        "${git_name}" \
        "${url}" \
        "${new_url}"
      break
    fi
    git -C "${git_dir}" remote set-url origin "${url}"
  done
done