diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-07-01 16:07:29 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-07-01 16:07:29 +0200 |
commit | 30a898e1040ec91cea1ceeb2180bdcf7265f2407 (patch) | |
tree | 027b532b59f44afc2d6bbb598f9f7998cb97f764 /bin/common | |
parent | 9d91de38af90ee1d4033666d10613eb03f3d747f (diff) |
checkpoint
Diffstat (limited to 'bin/common')
-rwxr-xr-x | bin/common/gt | 116 | ||||
-rwxr-xr-x | bin/common/gt-cmd | 18 | ||||
-rwxr-xr-x | bin/common/gt-st | 24 | ||||
-rwxr-xr-x | bin/common/gt-sync | 21 |
4 files changed, 108 insertions, 71 deletions
diff --git a/bin/common/gt b/bin/common/gt index 7609e90..48321af 100755 --- a/bin/common/gt +++ b/bin/common/gt @@ -5,21 +5,26 @@ # dependencies: # - git # - $EDITOR: -e +# - parallel: optional, if installed will run the commands on all repos with parallel repos=$HOME/sync/share/git-track.txt # prevent file not found errors touch "$repos" || exit 1 +which parallel >/dev/null 2>&1 && parallel=1 + help() { cat >&2 <<EOF usage: gt [OPTION] -a PATH add repo --s update and show status of each repo +-y show sync status -c COMMAND run 'git COMMAND' in each repo -h show this help -l list repos -e edit repos in \$EDITOR -f FILE use FILE as list of repos +-u pull in repos out of sync +-s show status of files and remotes EOF } @@ -31,53 +36,15 @@ fetch() { done } -# Print repositories prettily -# This function function prints animations (eg. clearing the line) -# to stderr and the final status line is outputted to stdout. -status() { - while read -r repo; do - repo_pretty="$(printf '%s' "$repo" | sed "s@$HOME@~@")" - - if [ ! -d "$repo" ]; then - printf '%s missing\n' "$repo_pretty" - continue - fi - - # absolute path - cd "$repo" - - # replace line with status - printf >&2 '\r\033[0K' - - status="$(git status --porcelain 2>/dev/null | - awk '{print $1}' | - sort | uniq | tr -s '?' | - tr -d '\n')" - remote="$(git branch -v 2>/dev/null | - sed '/^*/!d;s/ahead/↑/;s/behind/↓/;s/[^↓↑]*//g')" - - printf '%s %s %s\n' "$repo_pretty" "$status" "$remote" - done <"$repos" -} - -# run git command in each repo -# $1: command -repos_cmd() { - while read -r repo; do - if [ ! -d "$repo" ]; then - printf '%s missing\n' "$repo_pretty" - continue - fi - - repo_pretty="$(printf '%s' "$repo" | sed "s@$HOME@~@")" - printf ''\''%s'\'' in %s' "$1" "$repo_pretty" - ( - cd "$repo" - git "$1" >/dev/null 2>&1 - [ $? -gt 0 ] && s="x" || s="o" - printf '\r\033[0K%s: %s\n' "$repo_pretty" "$s" - ) - done <"$repos" +sync_status() { + if [ "$parallel" ]; then + parallel --colsep ' ' gt-sync {1} {2} <"$repos" + else + IFS=' ' + while read -r repo remote_url; do + gt-sync "$repo" "$remote_url" + done <"$repos" + fi } # no options @@ -86,30 +53,49 @@ if [ -z "$1" ]; then exit 1 fi -while getopts ":a:c:f:lshe" opt; do +[ "$(wc -l <"$repos")" -gt 0 ] || exit 0 + +while getopts ":a:c:f:lsheuy" opt; do case "$opt" in a) - cd "$OPTARG" || exit 1 - r="$(git rev-parse --show-toplevel)" - [ "$r" ] || exit 2 + if ! cd "$OPTARG" 2>/dev/null; then + >&2 printf 'Not a directory.\n' + exit 1 + fi + + repo="$(git rev-parse --show-toplevel)" + remote_url="$(git remote show -n origin | awk '/^ Fetch/ {print $NF}')" - if grep "$r" "$repos" >/dev/null 2>&1; then + if grep "^$repo " "$repos" >/dev/null 2>&1; then printf >&2 'added already.\n' - exit 2 + exit 3 fi - printf '%s\n' "$r" >>"$repos" + printf '%s %s\n' "$repo" "$remote_url" >>"$repos" printf >&2 'added.\n' ;; c) - f_command=1 - f_arg="$OPTARG" + cut -f 1 -d ' ' "$repos" | + if [ "$parallel" ]; then + parallel gt-cmd "{}" "$OPTARG" + else + xargs -I{} gt-cmd "{}" "$OPTARG" + fi ;; - s) f_status=1 ;; - l) cat "$repos" ;; + s) cut -f 1 -d ' ' "$repos" | xargs -I{} gt-st {} ;; + y) sync_status ;; + l) cut -f 1 -d ' ' "$repos" ;; e) $EDITOR "$repos" ;; f) repos="$OPTARG" ;; + u) + sync_status | awk -F ':' '/x$/ {print $1}' | + if [ "$parallel" ]; then + parallel gt-cmd {} pull + else + xargs -I{} gt-cmd {} pull + fi + ;; h) help ;; :) printf >&2 -- '-%s requires argument\n' "$OPTARG" @@ -122,16 +108,4 @@ while getopts ":a:c:f:lshe" opt; do esac done -# commands hereafter must happen in order - -[ "$(wc -l <"$repos")" -gt 0 ] || exit 0 - -if [ "$f_command" ]; then - repos_cmd "$f_arg" -fi - -if [ "$f_status" ]; then - status -fi - # eval "herbe $(status | sed 's/"/\"/g;s/.*/"&"/' | tr '\n' ' ')" diff --git a/bin/common/gt-cmd b/bin/common/gt-cmd new file mode 100755 index 0000000..66efd50 --- /dev/null +++ b/bin/common/gt-cmd @@ -0,0 +1,18 @@ +#!/bin/sh + +# path to repo +repo="$1" +# git command +command="$2" +[ "$#" -lt 2 ] && exit 1 + +repo_pretty="$(printf '%s' "$repo" | sed "s@^$HOME@~@")" + +if [ ! -d "$repo" ]; then + printf '%s missing\n' "$repo_pretty" + exit 1 +fi + +git -C "$repo" "$command" >/dev/null 2>&1 +[ $? -gt 0 ] && s="x" || s="o" +printf '%s: %s\n' "$repo_pretty" "$s" diff --git a/bin/common/gt-st b/bin/common/gt-st new file mode 100755 index 0000000..98184b9 --- /dev/null +++ b/bin/common/gt-st @@ -0,0 +1,24 @@ +#!/bin/sh + +# Print status of the repo locally + +# path to repo +repo="$1" +[ "$1" ] || exit 1 + +repo_pretty="$(printf '%s' "$repo" | sed "s@$HOME@~@")" + +if [ ! -d "$repo" ]; then + printf '%s missing\n' "$repo_pretty" + exit 1 +fi + +# replace line with status +status="$(git -C "$repo" status --porcelain 2>/dev/null | + awk '{print $1}' | + sort | uniq | tr -s '?' | + tr -d '\n')" +remote="$(git -C "$repo" branch -v 2>/dev/null | + sed '/^*/!d;s/ahead/↑/;s/behind/↓/;s/[^↓↑]*//g')" + +printf '%s %s %s\n' "$repo_pretty" "$status" "$remote" diff --git a/bin/common/gt-sync b/bin/common/gt-sync new file mode 100755 index 0000000..0dc986c --- /dev/null +++ b/bin/common/gt-sync @@ -0,0 +1,21 @@ +#!/bin/sh + +# Print if repo is out of sync or not + +# path to repo +repo="$1" +# remote for repo +remote="$2" +[ "$#" -lt 2 ] && exit 1 + +repo_pretty="$(printf '%s' "$repo" | sed "s@$HOME@~@")" + +ref="$(git -C "$repo" symbolic-ref HEAD)" +local_hash="$(git -C "$repo" rev-parse "$ref")" +remote_hash="$(git ls-remote "$remote" "$ref" | cut -f 1)" + +if [ "$remote_hash" != "$local_hash" ]; then + printf '%s: x\n' "$repo_pretty" +else + printf '%s: o\n' "$repo_pretty" +fi |