1
0

update internal scripts

This commit is contained in:
Konstantin Demin 2023-12-23 09:11:04 +03:00
parent 5fd6a13726
commit d2061dda65
Signed by: krd
GPG Key ID: 1F33CB0BA4731BC6
3 changed files with 74 additions and 40 deletions

View File

@ -1,14 +1,11 @@
#!/bin/sh #!/bin/sh
set -f -e set -ef
set -o noglob errexit
GIT_OPTIONAL_LOCKS=0
export GIT_OPTIONAL_LOCKS
path_gitignore='.config/dotfiles/gitignore' path_gitignore='.config/dotfiles/gitignore'
gen_gitignore() { gen_gitignore() {
git rev-parse --git-dir >/dev/null 2>/dev/null git rev-parse --git-dir >/dev/null 2>&1
touch "$1"
{ {
echo '*' echo '*'
git ls-files | sed -E 's:^:!/:' git ls-files | sed -E 's:^:!/:'
@ -20,18 +17,18 @@ me=$(readlink -e "$0")
topdir=$(printf '%s' "${me}" | sed -E 's:/[^/]+/[^/]+/[^/]+$::') topdir=$(printf '%s' "${me}" | sed -E 's:/[^/]+/[^/]+/[^/]+$::')
cd "${topdir}" cd "${topdir}"
## end-point installation export GIT_OPTIONAL_LOCKS=0
dir=$(dirname "${me}")'/repo.git'
if [ -s "${dir}/packed-refs" ] ; then dir="${me%/*}/repo.git"
if [ -d "${dir}" ] ; then
## end-point installation
GIT_DIR="${dir}" GIT_DIR="${dir}"
GIT_WORK_TREE="${topdir}" GIT_WORK_TREE="${topdir}"
export GIT_DIR GIT_WORK_TREE export GIT_DIR GIT_WORK_TREE
gen_gitignore "${GIT_WORK_TREE}/${path_gitignore}" || true gen_gitignore "${GIT_WORK_TREE}/${path_gitignore}"
fi else
## development tree
## development tree gen_gitignore "${path_gitignore}"
if [ -s '.git/packed-refs' ] ; then
gen_gitignore "${path_gitignore}" || true
fi fi
exit 1 exit 1

View File

@ -1,6 +1,5 @@
#!/bin/sh #!/bin/sh
set -f -e set -ef
set -o noglob errexit
gh_repo='rockdrilla/dotfiles' gh_repo='rockdrilla/dotfiles'
gh_br='main' gh_br='main'
@ -13,28 +12,61 @@ d_repo='.config/dotfiles/repo.git'
u_tarball="${GITHUB:-https://github.com}/${gh_repo}/archive/refs/heads/${gh_br}.tar.gz" u_tarball="${GITHUB:-https://github.com}/${gh_repo}/archive/refs/heads/${gh_br}.tar.gz"
have_cmd() {
command -v "$1" >/dev/null 2>&1
}
fetch() {
if have_cmd curl ; then
curl -sSL ${2:+ -o "$2" } "$1"
return
fi
if have_cmd wget ; then
if [ -n "$2" ] ; then
wget -q -O - "$1" > "$2"
else
wget -q -O - "$1"
fi
return
fi
if have_cmd /usr/lib/apt/apt-helper ; then
if [ -n "$2" ] ; then
/usr/lib/apt/apt-helper download-file "$1" "$2"
return
fi
__fetch_t=$(mktemp) || return 1
set +e
(
set -e
/usr/lib/apt/apt-helper download-file "$1" "${__fetch_t}"
cat "${__fetch_t}"
)
__fetch_r=$?
rm -f "${__fetch_t}" ; unset __fetch_t
return ${__fetch_r}
fi
echo 'no method is available to fetch URLs' >&2
return 1
}
main() { main() {
## dry run to test connectivity ## dry run to test connectivity
curl -sSL "${u_gitignore}" >/dev/null fetch "${u_gitignore}" >/dev/null
umask 0077 umask 0077
if git_avail ; then if have_cmd git ; then
if [ -s "${HOME}/${d_repo}/info/refs" ] ; then if [ -s "${HOME}/${d_repo}/info/refs" ] ; then
dot_update dot_update
else else
dot_install dot_install
fi fi
else else
echo 'git is missing, proceed "raw" installation.' echo 'git is missing, proceed "raw" installation.' >&2
dot_install_raw dot_install_raw
fi fi
echo 'installed.' echo 'installed.' >&2
}
git_avail() {
command git --version >/dev/null 2>/dev/null
} }
dot_install() { dot_install() {
@ -42,7 +74,7 @@ dot_install() {
git_env git_env
mkdir -p "${GIT_DIR}" mkdir -p "${GIT_DIR}"
git init git init
git branch -M ${gh_br} || true git branch -M "${gh_br}" || true
git_config git_config
git_update git_update
} }
@ -52,9 +84,13 @@ dot_update() {
git_update git_update
} }
find_fast() {
find "$@" -printf . -quit | grep -Fq .
}
dot_install_raw() { dot_install_raw() {
tf_tar=$(mktemp) tf_tar=$(mktemp)
curl -sSL "${u_tarball}" > "${tf_tar}" fetch "${u_tarball}" "${tf_tar}"
td_tree=$(mktemp -d) td_tree=$(mktemp -d)
@ -65,11 +101,12 @@ dot_install_raw() {
rm -f "${tf_tar}" rm -f "${tf_tar}"
tf_list=$(mktemp) tf_list=$(mktemp)
curl -sSL "${u_gitignore}" | \ fetch "${u_gitignore}" \
sed -En '/^!\/(.+)$/{s//\1/;p;}' > "${tf_list}" | sed -En '/^!\/(.+)$/{s//\1/;p;}' \
> "${tf_list}"
td_backup=$(mktemp -d) td_backup=$(mktemp -d)
while read f ; do while read -r f ; do
if [ -f "${HOME}/$f" ] ; then if [ -f "${HOME}/$f" ] ; then
if cmp_files "${td_tree}" "${HOME}" "$f" ; then if cmp_files "${td_tree}" "${HOME}" "$f" ; then
continue continue
@ -86,8 +123,7 @@ dot_install_raw() {
tar -C "${td_tree}" -cf . - | tar -C "${HOME}" -xf - tar -C "${td_tree}" -cf . - | tar -C "${HOME}" -xf -
rm -rf "${td_tree}" rm -rf "${td_tree}"
n_bak=$(find "${td_backup}/" -mindepth 1 | wc -l) if find_fast "${td_backup}/" -mindepth 1 ; then
if [ "${n_bak}" != 0 ] ; then
echo "backed-up files are here: ${td_backup}/" echo "backed-up files are here: ${td_backup}/"
find "${td_backup}/" -mindepth 1 -ls find "${td_backup}/" -mindepth 1 -ls
else else
@ -118,12 +154,12 @@ git_config() {
git_update() { git_update() {
git remote update -p git remote update -p
git pull || git reset --hard origin/main git pull || git reset --hard "origin/${gh_br}"
git gc --aggressive --prune=all --force || git gc || true git gc --aggressive --prune=all --force || git gc || true
} }
tar_test() { tar_test() {
tar --wildcards -tf "$@" >/dev/null 2>/dev/null tar --wildcards -tf "$@" >/dev/null 2>&1
} }
tar_try_extract() { tar_try_extract() {
@ -140,16 +176,17 @@ tar_try_extract() {
} }
cmp_files() { cmp_files() {
cmp -s "$1/$3" "$2/$3" >/dev/null 2>/dev/null cmp -s "$1/$3" "$2/$3" >/dev/null 2>&1
} }
backup_unconditionally() { backup_unconditionally() {
tf_list=$(mktemp) tf_list=$(mktemp)
curl -sSL "${u_gitignore}" | \ fetch "${u_gitignore}" \
sed -En '/^!\/(.+)$/{s//\1/;p;}' > "${tf_list}" | sed -En '/^!\/(.+)$/{s//\1/;p;}' \
> "${tf_list}"
td_backup=$(mktemp -d) td_backup=$(mktemp -d)
while read f ; do while read -r f ; do
if [ -f "${HOME}/$f" ] ; then if [ -f "${HOME}/$f" ] ; then
d=$(dirname "$f") d=$(dirname "$f")
if [ -n "$d" ] ; then if [ -n "$d" ] ; then
@ -160,8 +197,7 @@ backup_unconditionally() {
done < "${tf_list}" done < "${tf_list}"
rm -f "${tf_list}" rm -f "${tf_list}"
n_bak=$(find "${td_backup}/" -mindepth 1 | wc -l) if find_fast "${td_backup}/" -mindepth 1 ; then
if [ "${n_bak}" != 0 ] ; then
echo "backed-up files are here: ${td_backup}/" echo "backed-up files are here: ${td_backup}/"
find "${td_backup}/" -mindepth 1 -ls find "${td_backup}/" -mindepth 1 -ls
else else

View File

@ -31,6 +31,7 @@ z-update() {
z-reload() { z-reload() {
exec -a "${ZSH_ARGZERO}" "${ZSH_NAME}" "${argv[@]}" exec -a "${ZSH_ARGZERO}" "${ZSH_NAME}" "${argv[@]}"
echo "unable to reload (something went wrong), code $?" 1>&2 echo "unable to reload (something went wrong), code $?" 1>&2
return 1
} }
## reload or new session are required to regenerate compcache ## reload or new session are required to regenerate compcache