summaryrefslogtreecommitdiff
path: root/backup_repos.sh
blob: e34d1ea9c258494c03ed3e6f68880afde8a7daf3 (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
#!/usr/bin/env sh

export GIT_TERMINAL_PROMPT=0
BASE=/mnt/ssd-data/repos/

pick_branch() {
  git -C "$1" for-each-ref --format='%(refname:short)' 'refs/remotes/origin/*' \
    | sed 's#^origin/##' \
    | grep -E '^(main|master|dev)$' \
    | head -n1
}

repos=$(2>1 ssh git.l-3.space | awk '/tslil\// {print $2}')

echo Found $repos
printf "\n\n"

for repo in $repos; do
  REMOTE="ssh://git.l-3.space/$repo.git"
  DIR="$BASE/$repo"
  echo Looking at "$REMOTE" for "$DIR"
  if [ -d "$DIR/.git" ]; then
    echo "Syncing existing repo $DIR from $REMOTE ..."
    if git -C "$DIR" fetch -q origin \
      && BRANCH="$(pick_branch "$DIR")" && [ -n "$BRANCH" ] \
      && git -C "$DIR" reset --hard -q "origin/$BRANCH" \
      && git -C "$DIR" clean -fdxq; then
      echo "OK: $DIR synced to remote from $BRANCH."
    else
      echo "Sync failed, removing $DIR and re-cloning..."
      rm -rf "$DIR"
      git clone -q "$REMOTE" "$DIR"
      echo "OK: $DIR re-cloned from $REMOTE."
    fi
  else
    echo "No repo at $DIR, cloning..."
    git clone -q "$REMOTE" "$DIR"
    echo "OK: $DIR cloned from $REMOTE."
  fi
  printf "\n\n"
done