summaryrefslogtreecommitdiffstats
path: root/.local/bin/gitrect-update
diff options
context:
space:
mode:
authorTyler Davis <tyler@gluecode.net>2024-05-07 19:47:56 -0700
committerTyler Davis <tyler@gluecode.net>2024-05-07 19:47:56 -0700
commit6ed88629a2523b3e37c89c9639695abb59390746 (patch)
tree3075f22e7a281270f5e44892284ae03bb39e8c6e /.local/bin/gitrect-update
parentf8bd43d79286e3a142c7728081a2705542f9eeb8 (diff)
downloaddotfiles-6ed88629a2523b3e37c89c9639695abb59390746.tar.gz
dotfiles-6ed88629a2523b3e37c89c9639695abb59390746.zip
bin: shell fixes for gitrect
Diffstat (limited to '.local/bin/gitrect-update')
-rwxr-xr-x.local/bin/gitrect-update218
1 files changed, 109 insertions, 109 deletions
diff --git a/.local/bin/gitrect-update b/.local/bin/gitrect-update
index b86d114..dcb1c84 100755
--- a/.local/bin/gitrect-update
+++ b/.local/bin/gitrect-update
@@ -10,7 +10,7 @@
# Operate on tempfile
TEMPFILE="/tmp/gitrect.temp.$(od -t x1 -An -N6 /dev/urandom |tr -d '\n ')"
-touch $TEMPFILE
+touch "$TEMPFILE"
# Reset all variables that might be set
DEFAULT_STATE="$HOME/.local/share/git/repolist"
@@ -18,9 +18,9 @@ WORKDIR="$HOME/.code"
verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand.
noconfirm=0
-if [ ! $(command -v git) ]; then
- echo "git command not found. git must be installed and available in \$PATH to continue"
- exit 1
+if [ ! "$(command -v git)" ]; then
+ echo "git command not found. git must be installed and available in \$PATH to continue"
+ exit 1
fi
usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE]
@@ -36,131 +36,131 @@ where:
"
while :; do
- case $1 in
- -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit.
- echo "$usage"
- exit
- ;;
- -f) # Takes an option argument, ensuring it has been specified.
- if [ -n "$2" ]; then
- DEFAULT_STATE=$2
- shift
- fi
- ;;
- -f=?*)
- DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder.
- ;;
- -f=) # Handle the case of an empty --file=
- printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2
- exit 1
- ;;
- -w) # Takes an option argument, ensuring it has been specified.
- if [ -n "$2" ]; then
- WORKDIR=$2
- shift
- fi
- ;;
- -w=?*)
- WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder.
- ;;
- -w=) # Handle the case of an empty --file=
- printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2
- exit 1
- ;;
- -y)
- noconfirm=1
- ;;
- -v)
- verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity.
- ;;
- --) # End of all options.
- shift
- break
- ;;
- -?*)
- printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
- ;;
- *) # Default case: If no more options then break out of the loop.
- break ;;
- esac
- shift
+ case $1 in
+ -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit.
+ echo "$usage"
+ exit
+ ;;
+ -f) # Takes an option argument, ensuring it has been specified.
+ if [ -n "$2" ]; then
+ DEFAULT_STATE=$2
+ shift
+ fi
+ ;;
+ -f=?*)
+ DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder.
+ ;;
+ -f=) # Handle the case of an empty --file=
+ printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2
+ exit 1
+ ;;
+ -w) # Takes an option argument, ensuring it has been specified.
+ if [ -n "$2" ]; then
+ WORKDIR=$2
+ shift
+ fi
+ ;;
+ -w=?*)
+ WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder.
+ ;;
+ -w=) # Handle the case of an empty --file=
+ printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2
+ exit 1
+ ;;
+ -y)
+ noconfirm=1
+ ;;
+ -v)
+ verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity.
+ ;;
+ --) # End of all options.
+ shift
+ break
+ ;;
+ -?*)
+ printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
+ ;;
+ *) # Default case: If no more options then break out of the loop.
+ break ;;
+ esac
+ shift
done
# Make sure local state folder is available
if [ ! -d "$HOME/.local/share" ]; then
- if [ $verbose -gt 0 ]; then
- echo "Creating directory: $HOME/.local/share"
- fi
- mkdir -p ~/.local/share
+ if [ $verbose -gt 0 ]; then
+ echo "Creating directory: $HOME/.local/share"
+ fi
+ mkdir -p "$HOME"/.local/share
fi
# Rest of the program here.
# If there are input files (for example) that follow the options, they
# will remain in the "$@" positional parameters.
-LIST=$(eval "find ${WORKDIR} -type d -name \.git")
+LIST=$(eval "find ${WORKDIR} -type d -name .git")
# for each directory, create a line in TEMPFILE equivalent to:
# short/path/here,origin=url1,upstream=url2,...
#
for dirpath in $LIST; do
- # shortpath trims the working directory prefix and the '.git' suffix from the directory
- shortpath=$(eval "echo $dirpath | sed -e 's,${WORKDIR}/,,' -e 's,/\.git/,,'")
- if [ $verbose -gt 1 ]; then
- echo "Repository found: ${shortpath}" >&2
- fi
-
- cd "${dirpath}/.." # change into the parent of the git directory
- rems=$(eval "git remote") # list all remotes
- len_rems=${#rems}
- if [ $len_rems -lt 1 ]; then
- # If the list of remotes is zero, skip this folder
- continue
- fi
-
- # We haven't skipped the path, so write it to the file
- printf "%s" $shortpath >>$TEMPFILE
- for r in $rems; do
- url=$(eval "git remote get-url ${r}")
- printf ",%s=%s" $r $url >>$TEMPFILE
- if [ $verbose -gt 1 ]; then
- echo "remote: ${r} ${url} " >&2
- fi
- done
- printf "\n" >>$TEMPFILE
+ # shortpath trims the working directory prefix and the '.git' suffix from the directory
+ shortpath=$(eval "echo $dirpath | sed -e 's,${WORKDIR}/,,' -e 's/\/\.git//'")
+ if [ $verbose -gt 1 ]; then
+ echo "Repository found: ${shortpath}" >&2
+ fi
+
+ cd "${dirpath}/.." || continue # change into the parent of the git directory
+ rems=$(eval "git remote") # list all remotes
+ len_rems=${#rems}
+ if [ "$len_rems" -lt 1 ]; then
+ # If the list of remotes is zero, skip this folder
+ continue
+ fi
+
+ # We haven't skipped the path, so write it to the file
+ printf "%s" "$shortpath" >>"$TEMPFILE"
+ for r in $rems; do
+ url=$(eval "git remote get-url ${r}")
+ printf ",%s=%s" "$r" "$url" >>"$TEMPFILE"
+ if [ $verbose -gt 1 ]; then
+ echo "remote: ${r} ${url} " >&2
+ fi
+ done
+ printf "\n" >>"$TEMPFILE"
done
# Sort results
-sort -o ${TEMPFILE} ${TEMPFILE}
+sort -o "${TEMPFILE}" "${TEMPFILE}"
diffval=$(eval "diff -b ${DEFAULT_STATE} ${TEMPFILE}")
if [ $? -eq 0 ]; then
- if [ $verbose -gt 0 ]; then
- echo "No change. Configuration already up to date." >&2
- fi
- # Cleanup temp file
- rm "${TEMPFILE}"
+ if [ $verbose -gt 0 ]; then
+ echo "No change. Configuration already up to date." >&2
+ fi
+ # Cleanup temp file
+ rm "${TEMPFILE}"
else
- if [ $verbose -gt 0 ]; then
- echo "Diff between current and updated state: " >&2
- echo "$diffval" >&2
- fi
- if [ $noconfirm -ne 0 ]; then
- # Overwite origiaal with the new
- mv "${TEMPFILE}" "${DEFAULT_STATE}"
- else
- printf 'Accept changes (y/n)? \n'
- old_stty_cfg=$(stty -g)
- stty raw -echo
- answer=$(while ! head -c 1 | grep -i '[ny]'; do true; done)
- stty $old_stty_cfg
- if [ "$answer" != "${answer#[Yy]}" ]; then
- echo "Updating file..."
- # Overwite origiaal with the new
- mv "${TEMPFILE}" "${DEFAULT_STATE}"
- else
- echo "Discarding changes..."
- rm "${TEMPFILE}"
- fi
- fi
+ if [ $verbose -gt 0 ]; then
+ echo "Diff between current and updated state: " >&2
+ echo "$diffval" >&2
+ fi
+ if [ $noconfirm -ne 0 ]; then
+ # Overwite origiaal with the new
+ mv "${TEMPFILE}" "${DEFAULT_STATE}"
+ else
+ printf 'Accept changes (y/n)? \n'
+ old_stty_cfg=$(stty -g)
+ stty raw -echo
+ answer=$(while ! head -c 1 | grep -i '[ny]'; do true; done)
+ stty "$old_stty_cfg"
+ if [ "$answer" != "${answer#[Yy]}" ]; then
+ echo "Updating file..."
+ # Overwite origiaal with the new
+ mv "${TEMPFILE}" "${DEFAULT_STATE}"
+ else
+ echo "Discarding changes..."
+ rm "${TEMPFILE}"
+ fi
+ fi
fi