Skip to content

Commit 136f1ea

Browse files
committed
speedup git_stack
1 parent 22cfb71 commit 136f1ea

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

git_stack.sh

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,51 @@ ARGS=${*:-status --branch --short}
99

1010
NUM="$(echo $REPOS | wc -w)"
1111
I=0
12+
running_jobs=0
13+
MAX_PARALLEL=4
14+
tmp_files=()
15+
16+
function run_git_command {
17+
local dir=$1
18+
local args=$2
19+
local tmp_file=$3
20+
git -c color.status=always -C "$dir" $args >> "$tmp_file" 2>&1 &
21+
}
22+
1223
for D in $REPOS; do
1324
I=$(( $I + 1 ))
14-
echo ""
15-
echo " ------------ ( $I / $NUM ) $D ------------"
1625
FULL_DIR="$BASEDIR/$D"
1726
if [ "$ARGS" == "walk" ]; then
27+
echo " ------------- ( $I / $NUM ) $D -------------"
1828
pushd $FULL_DIR
1929
git status
2030
bash
2131
popd
2232
else
23-
git -C "$FULL_DIR" $ARGS
33+
tmp_file=$(mktemp)
34+
tmp_files+=("$tmp_file")
35+
echo " ------------- ( $I / $NUM ) $D -------------" > "$tmp_file"
36+
echo " -- Started -- ( $I / $NUM ) $D -------------"
37+
run_git_command "$FULL_DIR" "$ARGS" "$tmp_file"
38+
39+
(( running_jobs++ ))
40+
if (( running_jobs >= MAX_PARALLEL )); then
41+
wait -n
42+
(( running_jobs-- ))
43+
fi
2444
fi
2545
done
26-
if [ "$ARGS" == "walk" ]; then
27-
echo " ------------ DONE ------------"
46+
47+
if [ "$ARGS" != "walk" ]; then
48+
wait
2849
fi
50+
51+
echo " ------------- DONE -------------"
52+
53+
# Print the buffered output
54+
for tmp_file in "${tmp_files[@]}"; do
55+
echo ""
56+
cat "$tmp_file"
57+
rm "$tmp_file"
58+
done
59+

0 commit comments

Comments
 (0)