-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It leverages `[pick(1)]` to find a file and perform an action on it. [pick(1)]: https://github.com/calleerlandsson/pick
- Loading branch information
1 parent
3960cf9
commit 1dea373
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/bin/sh | ||
# | ||
# `pick-file` is a generic tool listing all files in the current directory, the | ||
# method that is used to do so is dependent on what type of directory it is. | ||
# It can also be override by passing the listing command to with | ||
# `[-f|--file-command]`. | ||
# | ||
# Unless configured otherwise, the command will try to use `git ls-files` if it | ||
# is executed inside a git directory. Else, plain old `find` is used. | ||
# | ||
# Options: | ||
# | ||
# See: `pick-file --help` | ||
# | ||
# Examples: | ||
# | ||
# Stage a file in `git`: | ||
# | ||
# % git add $(pick-file) | ||
# | ||
# Run a specific test file: | ||
# | ||
# % your-test-command $(pick-file) | ||
# | ||
|
||
command -v pick >/dev/null 2>&1 || { | ||
echo >&2 "pick(1) is not installed." | ||
exit 78 | ||
} | ||
|
||
inside_git_repo() { | ||
git rev-parse --is-inside-work-tree 2> /dev/null 1> /dev/null | ||
} | ||
|
||
usage() { | ||
echo "usage: $(basename $0) [(-f | --file-command) <argument>]" | ||
echo "usage: $(basename $0) [(-l | --lines) <argument>]" | ||
echo "usage: $(basename $0) [(-h | --help)]" | ||
} | ||
|
||
parse_options() { | ||
while [ $# -gt 0 ]; do | ||
case $1 in | ||
-l | --lines ) | ||
shift | ||
flags="-X" | ||
lines=$1 | ||
;; | ||
|
||
-f | --file-command ) | ||
shift | ||
file_command=$1 | ||
;; | ||
|
||
-h | --help ) | ||
usage | ||
exit | ||
;; | ||
esac | ||
shift | ||
done | ||
} | ||
|
||
flags="" | ||
lines=0 | ||
if inside_git_repo; then | ||
file_command="git ls-files --cached --exclude-standard --others" | ||
else | ||
file_command="find * -type f" | ||
fi | ||
|
||
parse_options "$@" || exit $? | ||
|
||
eval "$file_command | env LINES=$lines pick $flags" |