遥控器的 url 可以包含在 `git for-each-ref` 命令的输出中吗?

Can the url of the remote be included in the output of `git for-each-ref` command?

我正在写一个脚本来列出我系统上的所有 git 存储库、它们的分支以及系统上的最新提交。所以我创建了这个打印出以下内容的脚本。

directory, branch(ref), date, commit hash, date, commit message, ref (again).

#!/bin/bash
IFS='
'
for directory in `ls -d /*/`; do
  # echo "$directory : $directory.git"
  if [[ -d $directory/.git ]] ; then
      # filter=/refs/heads
      filter=''
      for branch in $(git -C $directory for-each-ref --format='%(refname)' $filter); do
          echo $directory : $branch :  "$(git -C $directory log -n 1 --oneline  --pretty=format:'%Cred%h - %C(yellow)%ai - %C(green)%s %C(reset) %gD' $branch)" : $(echo $branch | rev | cut -d\/ -f-1 | rev)
      done
  fi
done

我没有的是遥控器的存储库 URL。可以将遥控器的 URL 作为 git for-each-ref 命令输出的一部分打印出来吗?

我想我可以使用 git remote -C $directory -v 将存储库的遥控器列出到一个查找列表中,我将使用该查找列表将 refs/remotes/xxxxx 中的每个 xxxxx 值放入一个变量中,该变量将添加到 echo 命令中。

您可以使用 git for-each-ref 生成任意脚本,稍后将使用 eval 对其进行评估。适应你的情况最长的examples from the documentation on git for-each-ref,我得出以下结论:

#!/bin/bash
IFS='
'
for directory in ""/*/ ; do
  # echo "$directory : $directory.git"
  if [[ -d $directory/.git ]] ; then
      # filter=/refs/heads
      filter=''
      fmt="
           dir=$directory"'
           ref=%(refname)
           refshort=%(refname:short)
           h=%(objectname:short)
           date=%(authordate:iso)
           subj=%(subject)

           printf "%s : %s : %s - %s - %s : %s" "$dir" "$ref" %(color:red)"$h" %(color:yellow)"$date" %(color:green)"$subj" %(color:reset)"$refshort"
           if [[ $ref == refs/remotes/* ]]
           then
               remotename="${ref#refs/remotes/}"
               remotename="${remotename%%/*}"
               printf " : %s" "$(git -C "$dir" remote get-url "$remotename")"
           fi
           printf "\n"
        '

        eval="$(git -C $directory for-each-ref --shell --format="$fmt" $filter)"
        eval "$eval"
  fi
done

请注意,我从您的实施中删除了 git log,但结果我也删除了 %gD 字段,因为我不太明白它的含义,并且无法理解'在git-for-each-ref里面找到获取方法。此外,我改变了从完整参考中获取短参考的方式;我的实现与您的不同,但会产生明确的结果。