கிட் நிலை 2.0

காலையில் யார் முதலில் வருகிறாரோ அவர் git status தட்டச்சு செய்து காபி குடிக்கும் எவரும் இந்த சிறிய உதவியாளரை விரும்புவார்கள். குறிப்பாக பல வலைத் திட்டங்களை இணையாக உருவாக்கும்போது, ஒரு சிறிய கண்ணோட்டம் விலைமதிப்பற்றது: பணி மரம் எங்கே சுத்தமாக இருக்கிறது, இணைக்கப்படாத மாற்றங்கள் எங்கே உள்ளன, மற்றும் இழுத்தல்/தள்ளுதல் எங்கே நிலுவையில் உள்ளது? ஒரு சிறிய ஷெல் கருவி மட்டுமே உங்களுக்குத் தேவை - அது பாதைகளில் உள்ள இடைவெளிகள்/யூனிகோடை வலுவாகக் கையாளும் வரை மற்றும் சிக்கிய ரிமோட்களில் மூச்சுத் திணறாமல் இருக்கும் வரை.


#!/usr/bin/env bash

set -Eeuo pipefail
export LC_ALL=C

# Check if a command exists (no output).
have() { command -v "$1" >/dev/null 2>&1; }

# Ensure we have a `sort` that supports -z (NUL-delimited) input.
SORT_BIN="sort"
if ! "$SORT_BIN" -z </dev/null 2>/dev/null; then
  if have gsort && gsort -z </dev/null 2>/dev/null; then
    SORT_BIN="gsort"
  else
    printf 'Error: This script requires "sort -z" (GNU coreutils). Install coreutils (gsort).\n' >&2
    exit 1
  fi
fi

# Use GNU `timeout` if available; otherwise try `gtimeout` (macOS); otherwise no timeout.
TIMEOUT_BIN="timeout"
if ! have "$TIMEOUT_BIN"; then
  if have gtimeout; then
    TIMEOUT_BIN="gtimeout"
  else
    TIMEOUT_BIN=""
  fi
fi

# Require git.
if ! have git; then
  printf 'Error: "git" not found.\n' >&2
  exit 1
fi

# Remove a leading "./" from a path for cleaner output.
trim_dot_slash() {
  case "$1" in
    ./*) printf '%s\n' "${1#./}" ;;
    *)   printf '%s\n' "$1" ;;
  esac
}

# Legend + divider (as requested)
printf '\n🟢: clean\n🟡: behind/ahead\n🔴: modified\n\n----------------------------------\n\n'

# Find all .git directories, NUL-delimited; sort NUL-delimited; iterate safely.
find . -type d -name .git -print0 \
| "$SORT_BIN" -z \
| while IFS= read -r -d '' gitdir; do
    repo="${gitdir%/.git}"
    display_path="$(trim_dot_slash "$repo")"

    # Skip anything that isn't a proper work tree (safety check).
    if ! git -C "$repo" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
      continue
    fi

    # Working tree status; include untracked files for a strict "red" signal.
    status_out="$(git -C "$repo" status --porcelain=v1 || true)"

    # Upstream divergence check (only if an upstream is configured).
    ahead=0
    behind=0
    if git -C "$repo" rev-parse --abbrev-ref --symbolic-full-name '@{u}' >/dev/null 2>&1; then
      # Refresh refs; protect with timeout so a hanging remote doesn't stall the loop.
      if [ -n "$TIMEOUT_BIN" ]; then
        "$TIMEOUT_BIN" 10s git -C "$repo" fetch --all --prune >/dev/null 2>&1 || true
      else
        git -C "$repo" fetch --all --prune >/dev/null 2>&1 || true
      fi
      # Count commits only on our side (ahead) and only on upstream's side (behind).
      ahead="$(git  -C "$repo" rev-list --count --left-only  HEAD...@{u} 2>/dev/null || echo 0)"
      behind="$(git -C "$repo" rev-list --count --right-only HEAD...@{u} 2>/dev/null || echo 0)"
    fi

    # Decide the signal:
    # - RED if the working tree isn't clean
    # - YELLOW if clean but ahead/behind of upstream
    # - GREEN otherwise
    if [ -n "$status_out" ]; then
      printf '🔴 %s\n' "$display_path"
    else
      if [ "${ahead:-0}" -gt 0 ] || [ "${behind:-0}" -gt 0 ]; then
        printf '🟡 %s\n' "$display_path"
      else
        printf '🟢 %s\n' "$display_path"
      fi
    fi
  done

ஸ்கிரிப்ட் இன்னும்... இருக்க வேண்டும். chmod +x ~/path/to/script.sh அதை இயக்கக்கூடியதாக மாற்றவும், மதிப்புமிக்க தட்டச்சுகளைச் சேமிக்க ஒரு மாற்றுப்பெயரை அமைக்கவும் முடியும்: இங்கே நீங்கள் அவருடையதைச் சேர்க்கவும் ~/.bashrc / ~/.zshrc / ~/.bash_profile நுழைவு alias gscan='bash /path/to/script.sh' கூடுதலாக. அப்போதிருந்து, ஒரு எளிய gscan விரும்பிய ரூட் கோப்பகத்தில்.

இரண்டாவது இயக்கம் குறிப்பிடத்தக்க வகையில் வேகமாக இருப்பதற்கான ஒரு காரணம்: முதல் இயக்கத்தின் போது, கோப்பு முறைமை இன்னும் எல்லாவற்றையும் ஸ்கேன் செய்ய வேண்டும்; அதன் பிறகு, மெட்டாடேட்டா மற்றும் பல விஷயங்கள் ஏற்கனவே செயலாக்கப்பட்டுள்ளன. .git-OS கர்னலின் பக்க தற்காலிக சேமிப்பில் கட்டமைப்புகள் இடம் பெற்றுள்ளன, மேலும் குறிப்புகள் மற்றும் கமிட் கிராஃப்கள் ஏற்கனவே சூடுபடுத்தப்பட்டுள்ளன. அடுத்த படி... fetch இப்போது இது பெரும்பாலும் சிறிய டெல்டாக்களை மட்டுமே கடத்துகிறது. டாஷ்போர்டு இல்லை, மேல்நிலை இல்லை - முனையத்தில் நேரடியாக ஒரு விரைவான நிலை ஸ்னாப்ஷாட்.

மீண்டும்