From 98af13877e176b5198cd4b64ba64ccbadf3362d4 Mon Sep 17 00:00:00 2001 From: Cinghio Pinghio Date: Mon, 8 Mar 2021 10:41:17 +0100 Subject: [PATCH] [git ls-files] add feture to use `git ls-files` as file list factory --- stowsh | 45 ++++++++++++++++++++++++++++++++++------ tests/git/installed/file | 1 + tests/git/installed/link | 1 + tests/git/pkg/file | 0 tests/git/pkg/link | 1 + tests/git/run | 25 ++++++++++++++++++++++ 6 files changed, 67 insertions(+), 6 deletions(-) create mode 120000 tests/git/installed/file create mode 120000 tests/git/installed/link create mode 100644 tests/git/pkg/file create mode 120000 tests/git/pkg/link create mode 100755 tests/git/run diff --git a/stowsh b/stowsh index ea914d3..bfaee2f 100755 --- a/stowsh +++ b/stowsh @@ -50,6 +50,36 @@ stowsh_setpaths() { deperr "GNU findutils" return 1 fi + if [[ "${USEGIT}" -eq 1 ]]; then + flist="list_git_files" + else + flist="list_files" + fi +} + +list_files() { + if [[ "$1" == "-d" ]]; + then + $findcmd . -mindepth 1 -type d -printf "%P\0" + else + $findcmd . -type f -printf "%P\0" -or -type l -printf "%P\0" + fi +} + +list_git_files() { + gitfiles=( `git ls-files` ) + if [[ "${#gitfiles[@]}" == 0 ]]; then return; fi + + if [[ "$1" == "-d" ]]; + then + printf "%s\n" "${gitfiles[@]}" |\ + xargs -n1 dirname |\ + sort | uniq |\ + sed '/^\.$/d' |\ + tr '\n' '\0' + else + printf "%s\0" "${gitfiles[@]}" + fi } stowsh_install() { @@ -63,7 +93,7 @@ stowsh_install() { while IFS= read -r -d '' d; do commands+=("mkdir -p '$target/$d'") - done < <($findcmd . -mindepth 1 -type d -print0 | sed "s|\./||g") + done < <($flist -d) while IFS= read -r -d '' f; do local targetf="$target/$f" @@ -80,7 +110,7 @@ stowsh_install() { return fi fi - done < <($findcmd . -type f -print0 -or -type l -print0 | sed "s|\./||g") + done < <($flist) for cmd in "${commands[@]}"; do _runcommands "$cmd" @@ -113,11 +143,11 @@ stowsh_uninstall() { return fi fi - done < <($findcmd . -type f -print0 -or -type l -print0 | sed "s|\./||g") + done < <($flist) while IFS= read -r -d '' d; do commands+=("[[ -d '$target/$d' ]] && $findcmd '$target/$d' -type d -empty -delete") - done < <($findcmd . -mindepth 1 -type d -print0 | sed "s|\./||g") + done < <($flist -d) for cmd in "${commands[@]}"; do _runcommands "$cmd" @@ -125,19 +155,20 @@ stowsh_uninstall() { } stowsh_help() { - echo "Usage: $0 [-D] [-n] [-s] [-v[v]] [-t TARGET] PACKAGES..." + echo "Usage: $0 [-D] [-n] [-s] [-g] [-v[v]] [-t TARGET] PACKAGES..." } if [ "$0" = "$BASH_SOURCE" ]; then UNINSTALL=0 DRYRUN=0 SKIP=0 + USEGIT=0 TARGET="$PWD" PACKAGES=() while [[ "$@" ]]; do if [[ $1 =~ ^- ]]; then OPTIND=1 - while getopts ":vhDsnt:" opt; do + while getopts ":vhDsngt:" opt; do case $opt in h) stowsh_help @@ -151,6 +182,8 @@ if [ "$0" = "$BASH_SOURCE" ]; then ;; v) VERBOSE=$((VERBOSE + 1)) ;; + g) USEGIT=1 + ;; t) TARGET="$OPTARG" ;; *) echo "'$OPTARG' is an invalid option/flag" diff --git a/tests/git/installed/file b/tests/git/installed/file new file mode 120000 index 0000000..22f03be --- /dev/null +++ b/tests/git/installed/file @@ -0,0 +1 @@ +../pkg/file \ No newline at end of file diff --git a/tests/git/installed/link b/tests/git/installed/link new file mode 120000 index 0000000..22f03be --- /dev/null +++ b/tests/git/installed/link @@ -0,0 +1 @@ +../pkg/file \ No newline at end of file diff --git a/tests/git/pkg/file b/tests/git/pkg/file new file mode 100644 index 0000000..e69de29 diff --git a/tests/git/pkg/link b/tests/git/pkg/link new file mode 120000 index 0000000..1a010b1 --- /dev/null +++ b/tests/git/pkg/link @@ -0,0 +1 @@ +file \ No newline at end of file diff --git a/tests/git/run b/tests/git/run new file mode 100755 index 0000000..1e6c435 --- /dev/null +++ b/tests/git/run @@ -0,0 +1,25 @@ +#!/bin/bash + +stowsh=../../stowsh + +notingit=( 'pkg/not_in_git' 'pkg/also_not_in git' ) + +fail=0 +mkdir -p "uninstalled" +rm -rf "dest" +cp -r "uninstalled" "dest" +touch "${notingit[@]}" +$stowsh -vv -g -t "dest" "pkg" +diff -qr "dest" "installed" || fail=1 +$stowsh -vv -D -g -t "dest" "pkg" +diff -qr "dest" "uninstalled" || fail=1 + +rm "${notingit[@]}" +if [[ $fail == 1 ]] ; then + echo "FAIL" + exit 1 +else + echo "OK" + rm -rf "dest" + exit 0 +fi