-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforall-git-dirs.sh
More file actions
executable file
·78 lines (70 loc) · 2.32 KB
/
forall-git-dirs.sh
File metadata and controls
executable file
·78 lines (70 loc) · 2.32 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# Given a directory, call a given command in bash on all its git subdirectories recursively
# or down to a given number of directory levels.
#
# For usage type: $0 --help
#
# author: andreasl
script_name="${0##*/}"
IFS= read -r -d '' script_description <<HELP_EOF
Usage:
${script_name} [-q|--quiet] [-d|--depth <number>] [<path>] [-- <command>]
Examples:
${script_name} # list the found git repositories
${script_name} -d 2 -- ls # list the found git repositories and call \`ls\` from all git repos in this file level and one level below
${script_name} -q -d 2 -- ls # call \`ls\` from all git repos in this file level and one level below but do not list the found git repos
${script_name} -p path/to/dir -- ls # call \`ls\` from all git repos below the given path
${script_name} -q -- realpath . # print the paths of all git repos below the current path
${script_name} -h # print the usage message
${script_name} --help # print the usage message
Note:
If you want to use subshell related-variables, like e.g. \$PWD, wrap them into single quotation
marks '' so that they expanded not immediately.
HELP_EOF
use_maxdepth=false
search_dir='.'
while [ $# -gt 0 ]; do
key="$1"
case $key in
-q | --quiet)
quiet="True"
;;
-d | --depth)
use_maxdepth=true
depth="${2}"
shift # past argument
;;
-p | --path)
search_dir="${2}"
shift # past argument
;;
--)
shift # past argument
command="$*"
break
;;
-h | --help)
printf -- "$script_description"
exit 0
;;
*) # unknown option
;;
esac
shift # past argument or value
done
function_called_by_find() {
[ -n "$quiet" ] || printf "\e[1m${PWD}\e[0m\n"
eval "$command"
}
# export the function and the command, since the subshell you open below in find
# should know about the function and the command
export -f function_called_by_find
export quiet
export command
if [ "$use_maxdepth" == "true" ]; then
find "$search_dir" -maxdepth "$depth" -type d -iname "*.git" -execdir \
bash -c "function_called_by_find ;" \;
else
find "$search_dir" -type d -iname "*.git" -execdir \
bash -c "function_called_by_find ;" \;
fi