-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-run
executable file
·63 lines (52 loc) · 1.37 KB
/
git-run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# Runs a command on files that have recently changed.
set -e
usage() {
(
echo "usage: ${0##*/} [-h|--help] COMMAND PATTERN [-N]"
echo "Runs the given command on files matching the given pattern that"
echo "have changed within in the latest N commits. Or if there are any"
echo "uncomitted changes, run the command on those matching files."
echo
echo "options:"
(
echo " -h, --help: show usage help"
echo " -N: number of commits to consider, default is -1"
) | column -ts:
) >&2
exit 1
}
if echo "$*" | grep -Eq -- '--help\b|-h\b' || [[ -z $1 ]] || [[ -z $2 ]]; then
usage
fi
set -u
run() {
echo -n $'\e[38;5;40m$\e[38;5;63m '
echo -n "$@"
echo $'\e[0m'
eval "$@"
return $?
}
COMMAND="$1"
PATTERN="$2"
LOOKBACK=${3:--1}
tf="$(mktemp -t tmp.XXXXXXXXXX)"
trap 'rm -f $tf' EXIT
write_lines() {
while read -r MATCHING_FILE; do
echo " $MATCHING_FILE \\" >>"$tf"
done
}
CHANGED=$(git status --porcelain | sed -e 's/^.. //' | grep -iE "$PATTERN" || true)
if [[ -n $CHANGED ]]; then
echo "$CHANGED"
else
git log --pretty="" --name-only "$LOOKBACK" | grep -iE "$PATTERN" || true
fi | write_lines
if [[ -s $tf ]]; then
run "$COMMAND \\
$(sort -u <"$tf" | sed '$ s/\\$//g')"
else
echo "no matching files" >&2
exit 1
fi