解析“git submodule foreach'git status'”的输出

时间:2011-10-11 16:21:04

标签: git git-submodules

我有一个包含许多子模块的Git超级项目。我希望能够遍历子模块并运行git status,以便我可以快速查看每个子模块所在的分支以及是否存在未提交的更改。但是,当我运行git submodule foreach 'git status'时,很难解析,我无法分辨哪个子模块报告了状态。它似乎只是喷出我的%PATH%变量(我在Windows上使用msysGit)。我的输出显示类似于此处显示的内容:

Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
nothing to commit (working directory clean)
Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
# Your branch is ahead of 'FlashDrive/My_Branch' by 10 commits.
#
nothing to commit (working directory clean)
Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   MyApp/Win32/Release/MyApp.exe
#   modified:   MyApp/WinCe600 (ARMV4I)/Release/MyApp.exe
#   modified:   MyApp/WinCe600 (ARMV4I)/Release/Startup.exe
#
no changes added to commit (use "git add" and/or "git commit -a")
Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
# Your branch is ahead of 'FlashDrive/My_Branch' by 8 commits.
#
nothing to commit (working directory clean)

正如您所看到的,这很难一瞥(特别是当输出被包装时)并看到'git status'的输出。另外,每个子模块的名称都没有显示,所以我无法知道每个输出部分所指的子模块。

任何帮助表示赞赏!谢谢!

1 个答案:

答案 0 :(得分:3)

你没有提到你想要展示的内容......

 git submodule foreach -q git ls-files -m 
例如,

在显示修改后的文件方面会有很长的路要走。其他选择包括

 git submodule foreach -q git diff --shortstat --staged # staged
 git submodule foreach -q git diff --shortstat          # unstaged
 git submodule foreach -q git diff --shortstat HEAD     # staged AND unstaged

你可以用以下方式编写一个愚蠢的小脚本:

 #!/bin/sh
 echo "$(basename .) $(git branch)"

使用子模块目录名称显示您想要的内容以便于识别。

如果您无法解决上述问题: 不要去尝试解析非设计用于机器消费的输出。 Git有一组非常丰富的子命令,可以更好地帮助你。