add commandline git discard alias

copy, change m from merge to i, insert.  use m and M for mirror
add support functions for specfic cases copy
master
David Kebler 2024-03-13 10:13:08 -07:00
parent c42c9777a9
commit 94984e969d
2 changed files with 104 additions and 16 deletions

View File

@ -1 +1,3 @@
alias gitdis="git clean -df"
alias gitdisall="git restore ."
alias gitdis="git restore"

View File

@ -3,15 +3,16 @@
export COPY_MODULE_PATH="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
module_load debug
module_load confirm
module_load helpers
copy () {
if ! which rsync &> /dev/null; then echo rsync must be installed to use this function; return 6; fi
local SHOST; local DHOST; local sshargs; local args
local SRC; local DEST; local mirror; local quiet
local SRC; local DEST; local mirror; local quiet; local Mirror
local SPATH; local DPATH; local exec; local list; local verbose
local usesudo;local cmd;local noconfirm;local merge
local usesudo;local cmd;local noconfirm;local insert
args=()
@ -29,8 +30,9 @@ Available Script Options
great for generating a rsync command to be used elsewhere
-p, make path at destination if need be. careful will creating missing subdirectories
-e, rsync --exclude , as many of these as desired
-m, merge source inside destination instead of making folder at destination, good for changing destination folder name
-M, copy as "mirror" uses options. -a --numeric-ids --delete --delete-excluded --force
-i, insert source inside destination instead of making folder at destination, good for changing destination folder name
-m, copy as "mirror" uses options. -a --numeric-ids --delete --force
-M, copy as "mirror" above -m plus '--delete-excluded' added. A perfect mirror
-f, rsync --exclude-from <path>, can have multiple
-F, copy a filesystem, uses -M mirror plus -xHAXS, and uses sudo (i.e. -s script option)
-n, no confirm, will always confirm command unless this is set
@ -52,9 +54,10 @@ EOF
local OPTION
local OPTARG
local OPTIND
while getopts 'avMqpf:le:rcCmd:shnF' OPTION; do
while getopts 'avMqpf:le:rcCmd:shnFi' OPTION; do
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
case "$OPTION" in
# TODO add D and S for destion and source path
s)
usesudo=sudo
;;
@ -88,11 +91,15 @@ EOF
;;
p)
args+=("--mkpath")
;;
i)
insert=true
;;
m)
merge=true
mirror=true
;;
M)
Mirror=true
mirror=true
;;
l)
@ -146,6 +153,7 @@ EOF
[[ $DHOST && $SHOST ]] && echo this script can not process remote host to remote host transfer && return 5
# is source or destination is remote, create -e ssh sync option
if [[ $DHOST || $SHOST ]]; then
# echo remote copy, loading ssh
if ! module_load ssh &>/dev/null; then
echo unable to load ssh module
echo likely the uci network repo has not been installed
@ -157,6 +165,13 @@ EOF
>&2 echo fatal: the host ${DHOST:-$SHOST} is not valid, aborting remote copy
return 1
fi
# else
# echo local rsync copy
fi
[[ ! $SHOST ]] && SPATH=$(realpath $SPATH)
[[ ! $DHOST ]] && DPATH=$(realpath $DPATH)
# parse ssh arguments from the rest
debug $( ( IFS=$','; echo all arguments: "$*" ) )
for ((d=1; d<$#; ++d)); do
@ -172,23 +187,25 @@ EOF
args+=("${@:1}")
fi
args+=( "$(remove_end_spaces "-e '$(ssh -l "${sshargs[*]}")'")")
fi
# assemble options
[[ ! $exec ]] && args+=(--dry-run -v)
[[ ! $quiet ]] && args+=(--info=progress2 $verbose)
[[ $mirror ]] && args+=(-a --numeric-ids --delete --delete-excluded --force)
[[ $mirror ]] && args+=(-a --numeric-ids --delete --force)
[[ $Mirror ]] && args+=(--delete-excluded)
[[ ! -v PS1 ]] && noconfirm=true
if [[ ! $noconfirm && ! $list ]]; then
if [[ $merge ]]; then
if [[ $insert ]]; then
# todo mirror option
confirm The contents within $SPATH will be $([[ $mirror ]] && echo mirrored || echo placed ) inside $DPATH || return 0
confirm The contents within $([[ $SHOST ]] && echo $SHOST:)$SPATH will be \
$([[ $mirror ]] && echo mirrored || echo placed ) inside $([[ $DHOST ]] && echo $DHOST:)$DPATH || return 0
else
confirm the directory $(basename $SPATH) of $SPATH will be $([[ $mirror ]] && echo mirrored || echo put ) at destination $DPATH/$(basename $SPATH)? || return 0
confirm the directory $(basename $SPATH) of $([[ $SHOST ]] && echo $SHOST:)$SPATH will be \
$([[ $mirror ]] && echo mirrored || echo put ) at destination $([[ $DHOST ]] && echo $DHOST:)$DPATH/$(basename $SPATH)? || return 0
fi
fi
cmd="$usesudo $(which rsync) ${args[*]} $SRC$([[ $merge ]] && echo "/") $DEST"
cmd="$usesudo $(which rsync) ${args[*]} $SRC$([[ $insert ]] && echo "/") $DEST"
[[ $list ]] && echo "$cmd" && return 0
if [[ ! $quiet ]]; then [[ $exec ]] && echo executing the copy command || echo dry run of command, use -r to execute; fi
if eval $cmd; then
@ -201,7 +218,76 @@ EOF
fi
}
alias fcopy="copy -r -q -m -n -a "
# for copying chromium profile use
# --exclude=Singleton*
alias fcpy="copy -r -q -i -n -a "
# alias mirror="copy -i -a -M"
mirror_shell () {
local dir; local dest
#TODO don't overwrite hostname
[ $# -lt 2 ] && echo "both a directory and remote host need to be passed" && return 2
dir=$1
[[ ! -d $dir ]] && echo no directory $dir to mirror && return 1
dest=$2
shift 2
copy $@ -i -a -c -M "$dir" "$dest:$dir" --exclude=archive*
}
rpush () {
local dir; local dest
#TODO don't overwrite hostname
[ $# -lt 2 ] && echo "both a directory and remote host need to be passed" && return 2
dir=$1
[[ ! -d $dir ]] && echo no directory $dir to mirror && return 1
dest=$2
shift 2
copy "$@" -i -a "$dir" "$dest:$dir"
}
rpull () {
local dir; local src; local dest
#TODO don't overwrite hostname
[ $# -lt 2 ] && echo "remote pull, both a directory and remote host need to be passed" && return 2
dir=$1
src=$2
shift 2
if [[ ! $1 == "-"* ]]; then
dest=$1
shift
echo pulling to alternate directory $dest
fi
copy "$@" -l -i "$src:$dir" "${dest:-dir}"
copy "$@" -i "$src:$dir" "${dest:-dir}"
}
rpushmirror () {
rpush "$@" -M -c
}
rpullmirror () {
rpull "$@" -M -c
}
rbrowser_copy () {
local browser; local dest
[ $# -lt 2 ] && echo "both a browser directory (within /opt/chromium) and remote host need to be passed" && return 2
browser=$1
[[ ! -d /opt/chromium/$browser ]] && echo no browser directory /opt/chromium/$browser && return 1
dest=$2
shift 2
copy $@ -a -c -M "/opt/chromium/$browser" "$dest:/opt/chromium" --exclude=Singleton*
}