#!/usr/bin/env bash# Show how this clone stands against origin/master: divergence, committed and uncommitted changes.set-e# Exit on errordeclaresummary=0show_sync_status(){# show_sync_status — Display commit divergence between local HEAD and origin/master.## Fetches origin/master then prints:# - Number of commits ahead / behind# - Overall sync statelocalaheadbehindstateif!gitfetchoriginmaster2>/dev/null;thenecho"Error: could not fetch from origin">&2return1fiahead=$(gitrev-list--countorigin/master..HEAD)behind=$(gitrev-list--countHEAD..origin/master)if[[${ahead}-eq0&&${behind}-eq0]];thenstate="up to date"elif[[${ahead}-gt0&&${behind}-eq0]];thenstate="local ahead"elif[[${ahead}-eq0&&${behind}-gt0]];thenstate="local behind"elsestate="diverged"fiprintf"Sync with origin/master: %s\n""${state}"printf" ahead: %d commit(s)\n""${ahead}"printf" behind: %d commit(s)\n""${behind}"if[[${ahead}-gt0]];thenlocallocal_commitslocal_commits=$(gitlog--onelineorigin/master..HEAD)echo" local commits:"echo" ${local_commits//$'\n'/$'\n '}"fiif[[${behind}-gt0]];thenlocalorigin_commitsorigin_commits=$(gitlog--onelineHEAD..origin/master)echo" origin commits:"echo" ${origin_commits//$'\n'/$'\n '}"fi}show_committed_diff(){# show_committed_diff — List files committed locally that differ from origin/master.# With summary=1: prints the count instead of the full list.localfilesfiles=$(gitdiff--name-onlyorigin/masterHEAD2>/dev/null)echo"Files committed locally vs origin/master:"if[[-z"${files}"]];thenecho" (none)"elif[["${summary}"-eq0]];thenecho" ${files//$'\n'/$'\n '}"elseecho" $(echo"${files}"|wc-l|tr-d' ') file(s)"fi}show_uncommitted_changes(){# show_uncommitted_changes — List staged and unstaged changes in the worktree.# Untracked files are excluded.# With summary=1: prints the count instead of the full list.echo"Uncommitted changes (staged/unstaged):"localchangeschanges=$(gitstatus--short--untracked-files=no)if[[-z"${changes}"]];thenecho" (none)"elif[["${summary}"-eq0]];thenecho" ${changes//$'\n'/$'\n '}"elseecho" $(echo"${changes}"|wc-l|tr-d' ') file(s)"fi}main(){show_sync_statusechoshow_committed_diffechoshow_uncommitted_changes}if[["${BASH_SOURCE[0]}"=="${0}"]];then[[" $* "==*" --summary "*]]&&summary=1mainfi