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

# Function to check status of a git repository
check-repo() {
  local repo_path="$1"
  local relative_path="${repo_path#$PWD/}"

  pushd "$repo_path" >/dev/null

  print -n "${relative_path}: "

  local status_output=$(git status --porcelain)

  if [[ -z "$status_output" ]]; then
    print -P "%F{green}CLEAN%f"
    popd >/dev/null
    return
  fi

  local messages=()

  # Check for untracked files (??), modified files (M or space+M), and staged files (non-? non-space in first column)
  if echo "$status_output" | grep -q "^??"; then
    messages+=("%F{yellow}UNTRACKED%f")
  fi
  if echo "$status_output" | grep -q "^.[M]" || echo "$status_output" | grep -q "^M"; then
    messages+=("%F{red}MODIFIED%f")
  fi
  if echo "$status_output" | grep -q "^[^? ]"; then
    messages+=("%F{blue}STAGED%f")
  fi

  print -P "${(j: :)messages}"
  popd >/dev/null
}

check-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
    check-repo "$repo_path"
  done
}