aboutsummaryrefslogtreecommitdiffstats
path: root/.local/bin/migrate-to-sourcehut
blob: d7e471a24d052a3f41f665a47f7312ce476d3246 (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
#!/bin/bash
set -euo pipefail
set -x

REPOS_DIR="$HOME/src/"
SOURCEHUT_USER="eugenetriguba"

main() {
  parse_args "$@"

  cd "$REPOS_DIR"
  for repo in */; do
    if [ -d "$repo/.git" ]; then
      echo "Processing $repo..."
      cd "$repo"

      if ! git remote | grep -q "^origin$"; then
        echo "No origin remote found for $repo, skipping..."
        cd ..
        continue
      fi

      if ! git remote get-url origin | grep -q "git.sr.ht"; then
        echo "Migrating $repo to SourceHut..."

        local repo_name
        repo_name=$(git remote get-url origin | cut -d '/' -f 2 | cut -d '.' -f 1)
        git remote remove origin
        git remote add origin "git@git.sr.ht:~${SOURCEHUT_USER}/${repo_name}"

        local current_branch
        current_branch=$(git branch --show-current)
        if [ "$current_branch" = "master" ]; then
          echo "Renaming master branch to main..."
          git branch -m master main
        fi

        git push --push-option visiblity="private" -u origin main
        git push origin main --tags
      fi

      cd ..
    fi
  done
}

parse_args() {
  while [[ $# -gt 0 ]]; do
    case "$1" in
    --repos-dir)
      REPOS_DIR="$2"
      shift 2
      ;;
    --sourcehut-user)
      SOURCEHUT_USER="$2"
      shift 2
      ;;
    *)
      echo "Usage: $0 [--repos-dir <path>] [--sourcehut-user <username>]"
      exit 1
      ;;
    esac

    if [[ -z "$REPOS_DIR" || ! -d "$REPOS_DIR" ]]; then
      echo "Invalid or empty REPOS_DIR: $REPOS_DIR"
      exit 1
    fi

    if [[ -z "$SOURCEHUT_USER" ]]; then
      echo "SOURCEHUT_USER must be specified."
      exit 1
    fi
  done
}

main "$@"