Subject: Re: [PATCH 3/3] completion: fix bugs with slashes in remote names

From: Junio C Hamano <[email-masked]>
Date: Mon, 03 Mar 2025 12:29:56 -0800

[email-masked] writes:

>>      local fer_pids=
>>      for ...
>>      do
>>          __git for-each-ref ... &
>>          fer_pids="$fer_pids $!"
>>      done
>>      test -z "$fer_pids" || wait $fer_pids
>> } | sort | uniq -u
>> That might cause spikes in cpu/memory/disk usage that aren't ideal
>> though.
>
> Yes, if there were a 100 remotes that's a bit of a fork-bomb.

And they are all competing for a shared common resource for read
access.

I wonder if a single invocation of for-each-ref for refs/remotes/
hierarchy would be sufficient?  That is, the calling shell script
can set up a shell function "foo" and then

    eval $(for-each-ref --format='foo %(refname)' --shell refs/remotes/)

and "foo" can do its thing avoiding as much forks as possible.  As
you are allowed bash-ism in completion script, you do not have to
limit yourself to POSIX parameter substitution but can use substring
expansion ${parameter:offset:length} and pattern substitution
${parameter/pattern/string} and other kind of bash exotics, as well
as shell arrays (e.g., define an array __git__remote_branches[] for
each remote, which may contain the remote-tracking branches for the
remote).