-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-finder-exec
executable file
·135 lines (117 loc) · 3 KB
/
git-finder-exec
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
#
# Requirements:
# - git
# - git-finder (This script is part of bash-stdops project)
#
# Author: James Cherti
# URL: https://github.com/jamescherti/bash-stdops
#
# Description:
# ------------
# The git-finder-exec recursively finds all Git repositories starting from the
# current directory using the git-finder script.
#
# It then executes the command provided as an argument in the directory of each
# Git repository.
#
# Example usage:
# git-finder-exec pwd
#
# License:
# --------
# Copyright (C) 2012-2025 James Cherti
#
# Distributed under terms of the GNU General Public License version 3.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
set -euo pipefail
# Global variables
# ONLY_SHOW_WHEN_THERE_IS_OUTPUT=1
# shellcheck disable=SC2317
error_handler() {
local errno="$?"
echo "Error: ${BASH_SOURCE[1]}:${BASH_LINENO[0]}" \
"(${BASH_COMMAND} exited with status $errno)" >&2
exit "${errno}"
}
title() {
local repo_path="$1"
shift
local cmd=("$@")
if [[ -t 1 ]] && tput colors &>/dev/null; then
echo -e "\e[33m${repo_path}\e[0m: ${cmd[*]}"
else
echo "${repo_path}: ${cmd[*]}"
fi
}
run_command() {
local repo_path="$1"
shift
local cmd=("$@")
local errno=0
title "$repo_path" "${cmd[@]}"
"${cmd[@]}" || return "$?"
}
# run_command_redir_output() {
# local repo_path="$1"
# shift
#
# local cmd=("$@")
#
# local errno=0
# local title_displayed=0
# output=$("${cmd[@]}" | sed 's/[[:space:]]*$//' 2>&1) || errno="$?"
# if [[ "$ONLY_SHOW_WHEN_THERE_IS_OUTPUT" -eq 0 ]] || [[ $output != "" ]]; then
# title "$repo_path" "${cmd[@]}"
# title_displayed=1
# # shellcheck disable=SC2001
# echo "$output" | sed 's/^/ /'
# fi
#
# if [[ $errno -ne 0 ]]; then
# if [[ $title_displayed -eq 0 ]]; then
# title "$repo_path" "${cmd[@]}"
# title_displayed=1
# fi
#
# echo " (Error: Exited with status $errno)"
# return 1
# fi
# }
main() {
trap "error_handler" ERR
set -o errtrace
local command=("$@")
if [[ "$#" -eq 0 ]]; then
echo "Usage: $0 <command> [args...]" >&2
exit 1
fi
local list_repo_path
mapfile -t list_repo_path < <(git-finder ".")
local errno=0
for repo_path in "${list_repo_path[@]}"; do
pushd . &>/dev/null
cd "$repo_path"
run_command "$repo_path" "${command[@]}" || errno=1
popd &>/dev/null
done
if [[ $errno -ne 0 ]]; then
echo
echo "Failed."
fi
exit "$errno"
}
main "$@"