summaryrefslogtreecommitdiff
path: root/bin/change-git-remotes
blob: bc8e3f2ca20254383cf4c57e0c9edea573563a44 (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
90
#!/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}"
  printf '%s\n' "releng ${releng_directory}"
} \
| while read -r git_name git_dir; do
  if [ -z "${git_dir}" ]; then
    continue
  fi
  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 'gitolite@git.archlinux32.org:packages.git\n'
        printf 'https://git%s.archlinux32.org/packages\n' \
          '' 2 3 4
      )
    ;;
    'builder'|'releng')
      new_urls=$(
        printf 'gitolite@git.archlinux32.org:%s.git\n' \
          "${git_name}"
        printf 'https://git%s.archlinux32.org/'"${git_name}"'\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
    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