Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git feature #30

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions stowsh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -113,31 +143,32 @@ 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"
done;
}

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
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions tests/git/installed/file
1 change: 1 addition & 0 deletions tests/git/installed/link
Empty file added tests/git/pkg/file
Empty file.
1 change: 1 addition & 0 deletions tests/git/pkg/link
25 changes: 25 additions & 0 deletions tests/git/run
Original file line number Diff line number Diff line change
@@ -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