]> git.r.bdr.sh - rbdr/dotfiles/commitdiff
Add repo sync functionality
authorRuben Beltran del Rio <redacted>
Wed, 18 Dec 2024 20:26:19 +0000 (21:26 +0100)
committerRuben Beltran del Rio <redacted>
Wed, 18 Dec 2024 20:26:19 +0000 (21:26 +0100)
runcoms/zshrc
zsh/functions/check-repos.zsh [new file with mode: 0644]
zsh/functions/sync-repos.zsh [new file with mode: 0644]

index 068e2be8f6d3c66728e7a5b720e8ac3676d906e2..ebf6bf12bf9b1ee665ca2006d2f81035698f2dff 100644 (file)
@@ -7,6 +7,8 @@ source "${ZDOTDIR:-$HOME}/.dotfiles/zsh/functions/short-uptime.zsh"
 source "${ZDOTDIR:-$HOME}/.dotfiles/zsh/functions/figlet-sample.zsh"
 source "${ZDOTDIR:-$HOME}/.dotfiles/zsh/functions/prettify-json.zsh"
 source "${ZDOTDIR:-$HOME}/.dotfiles/zsh/functions/status.zsh"
+source "${ZDOTDIR:-$HOME}/.dotfiles/zsh/functions/sync-repos.zsh"
+source "${ZDOTDIR:-$HOME}/.dotfiles/zsh/functions/check-repos.zsh"
 
 ################################################################################
 # Load Modules
diff --git a/zsh/functions/check-repos.zsh b/zsh/functions/check-repos.zsh
new file mode 100644 (file)
index 0000000..c5a9a10
--- /dev/null
@@ -0,0 +1,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
+}
diff --git a/zsh/functions/sync-repos.zsh b/zsh/functions/sync-repos.zsh
new file mode 100644 (file)
index 0000000..fdc0876
--- /dev/null
@@ -0,0 +1,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
+}