aboutsummaryrefslogtreecommitdiff
path: root/zsh/functions/sync-repos.zsh
blob: fdc087652bffcaeae78aced4978ac0dae775c108 (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
#!/usr/bin/env zsh

# Function to check and process git repositories
sync-repo() {
  local repo_path="$1"
  local relative_path="${repo_path#$PWD/}"

  pushd "$repo_path"

  print -n "${relative_path}: "

  local remotes=($(git remote))

  if [[ ${#remotes[@]} -eq 0 ]]; then
    print -P "%F{red}NO REMOTES%f"
    popd
    return
  fi

  # Check for required remotes
  local has_origin=0
  local has_conchos=0
  local existing_remotes=""

    for remote in $remotes; do
        [[ $remote == "origin" ]] && has_origin=1
        [[ $remote == "conchos" ]] && has_conchos=1
        existing_remotes="${existing_remotes:+$existing_remotes,}$remote"
    done

    # If missing any required remote
    if [[ $has_origin -eq 0 || $has_conchos -eq 0 ]]; then
      print -P "%F{yellow}MISSING REMOTES ${existing_remotes}%f"
      popd
      return
    fi

    # Try pushing to both remotes
    if git push --all origin >/dev/null 2>&1 && git push --all conchos >/dev/null 2>&1; then
        print -P "%F{green}UPDATED%f"
    else
        print -P "%F{red}OUTDATED%f"
    fi
    popd
}

sync-repos() {
  local start_dir="${1:-.}"
  fd -H -t d '^\.git$' -x echo {} | sort | sed 's/\/\.git$//' | rg -v "${SYNC_REPOS_IGNORE}" | while read -r repo_path; do
    sync-repo "$repo_path"
  done
}