summaryrefslogtreecommitdiff
path: root/bin/clean-git
blob: 0b892a2ffddf4ebc61cf7a31f81b6ad32b378665 (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
#!/bin/sh

# clean git repositories

# shellcheck disable=SC2119,SC2120

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

for repo in ${repo_names}; do
  eval 'repo_path="${repo_paths__'"${repo}"'}"'
  while [ "${repo_path}" != "${repo_path%/}" ]; do
    repo_path="${repo_path%/}"
  done
  remote=$(
    git -C "${repo_path}" remote -v \
    | awk '{print $2}' \
    | sort -u
  )
  if printf '%s\n' "${remote}" \
  | wc -l \
  | grep -xqF 1 ; then
    printf 'recloning %s (%s) ...\n' \
      "${repo}" \
      "${repo_path}"
    git clone --bare "${repo_path}" "${repo_path}.new"
    git -C "${repo_path}.new" remote set-url origin "${remote}"
    mv "${repo_path}" "${repo_path}.old"
    mv "${repo_path}.new" "${repo_path}"
    rm -rf --one-file-system "${repo_path}.old"
  else
    printf 'cleaning %s (%s) ...\n' \
      "${repo}" \
      "${repo_path}"
    ionice -n 7 git -C "${repo_path}" gc
  fi
  printf '... done\n'
done