Skip to content

Commit

Permalink
Allow try to have a custom list of ignored paths (#91)
Browse files Browse the repository at this point in the history
* Custom ignore list

* clarify manpage

---------

Co-authored-by: Michael Greenberg <[email protected]>
  • Loading branch information
angelhof and mgree authored Jun 29, 2023
1 parent abe88f3 commit 8fbb288
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
3 changes: 1 addition & 2 deletions completions/try.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ _try() {

case "${cmd}" in
try)
opts="-n -y -v -h -D -U summary commit explore"
opts="-n -y -v -h -i -D -U summary commit explore"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in

-D)
COMPREPLY=($(compgen -d "${cur}"))
return 0
Expand Down
6 changes: 5 additions & 1 deletion docs/try.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
try - run a command in an overlay

# SYNOPSIS
| try [-n] [-D DIR] CMD [ARG ...]
| try [-ny] [-i PATTERN] [-D DIR] [-U PATH] CMD [ARG ...]
| try summary [DIR]
| try commit [DIR]
| try explore
Expand Down Expand Up @@ -40,6 +40,10 @@ While using *try* you can choose to commit the result to the filesystem or compl

## Options

-i PATTERN

: Ignore paths that match PATTERN on summary and commit. This option can be passed multiple times; the patterns given will be used in as arguments to `-e` in a call to `grep -v`.

-D *DIR*

: Specifies DIR as the overlay directory (implies -n). The use of -D also implies that *DIR* already exists.
Expand Down
17 changes: 16 additions & 1 deletion test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,24 @@ test_mkdir_on_file()
diff -qr expected target
}

test_ignore_flag()
{
local try_workspace=$1
cd "$try_workspace/"

touch expected.bar

## Ignore changes to foo
"$try" -y -i foo1 -i foo2 "touch foo1.txt; touch foo2.txt; touch bar.txt"

diff -q expected.bar bar.txt &&
[ ! -f foo1.txt ] &&
[ ! -f foo2.txt ]
}

test_dev()
{
local try_workspace=$1
cp $RESOURCE_DIR/file.txt.gz "$try_workspace/"
cd "$try_workspace/"

"$try" -y "head -c 5 /dev/urandom > target"
Expand Down Expand Up @@ -353,6 +367,7 @@ if [ "$#" -eq 0 ]; then
run_test test_explore
run_test test_empty_summary
run_test test_mkdir_on_file
run_test test_ignore_flag
run_test test_dev

# uncomment this to force a failure
Expand Down
21 changes: 14 additions & 7 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ summary() {
fi

## Finds all potential changes
changed_files=$(find_upperdir_changes "$SANDBOX_DIR")
changed_files=$(find_upperdir_changes "$SANDBOX_DIR" "$IGNORE_FILE")
summary_output=$(process_changes "$SANDBOX_DIR" "$changed_files")

if [ -z "$summary_output" ]
Expand Down Expand Up @@ -296,7 +296,7 @@ commit() {
exit 1
fi

changed_files=$(find_upperdir_changes "$SANDBOX_DIR")
changed_files=$(find_upperdir_changes "$SANDBOX_DIR" "$IGNORE_FILE")
summary_output=$(process_changes "$SANDBOX_DIR" "$changed_files")

while IFS= read -r summary_line; do
Expand Down Expand Up @@ -328,15 +328,16 @@ is_whiteout_file() {
}

## Defines which changes we want to ignore in the summary and commit
## TODO: Make this be parametrizable, through a file for example
ignore_changes() {
grep -v -e .rkr -e Rikerfile
ignore_file="$1"
grep -v -f "$ignore_file"
}

## Lists all upperdir changes in raw format
find_upperdir_changes() {
sandbox_dir="$1"
find "$sandbox_dir/upperdir/" -type f -o \( -type c -size 0 \) -o -type d | ignore_changes
ignore_file="$2"
find "$sandbox_dir/upperdir/" -type f -o \( -type c -size 0 \) -o -type d | ignore_changes "$ignore_file"
}


Expand Down Expand Up @@ -415,10 +416,11 @@ sandbox_valid_or_empty() {
usage() {
cmd="$(basename $0)"
cat >&2 <<EOF
Usage: $cmd [-nvhy] [-D DIR] [-U PATH] CMD [ARG ...]
Usage: $cmd [-nvhy] [-i PATTERN] [-D DIR] [-U PATH] CMD [ARG ...]
-n don't prompt for commit
-y assume yes to all prompts (implies -n is not used)
-i PATTERN ignore paths that match PATTERN on summary and commit
-D DIR work in DIR (implies -n)
-U PATH path to unionfs helper (e.g., mergerfs, unionfs-fuse)
Expand All @@ -439,11 +441,16 @@ EOF
# "commit" - commit the result directory automatically when we're done
NO_COMMIT="interactive"

while getopts ":yvnD:U:" opt
# Includes all patterns given using the `-i` flag
# Each pattern is preceded by an -e so as to be immediately passed to grep.
IGNORE_FILE=$(mktemp)

while getopts ":yvni:D:U:" opt
do
case "$opt" in
(y) NO_COMMIT="commit";;
(n) NO_COMMIT="show";;
(i) echo $OPTARG >>"$IGNORE_FILE";;
(D) if ! [ -d "$OPTARG" ]
then
printf "%s: no such directory $OPTARG\n" "$(basename $0)" >&2
Expand Down

0 comments on commit 8fbb288

Please sign in to comment.