开发者

Parsing the output of "git submodule foreach 'git status'"

开发者 https://www.devze.com 2023-04-12 19:38 出处:网络
I have a Git super project with many submodules.I\'d like to be able to go through the submodules and run git status so that I can quickly see which branch each submodule is on and whether there are u

I have a Git super project with many submodules. I'd like to be able to go through the submodules and run git status so that I can quickly see which branch each submodule is on and whether there are uncommitted changes. However when I run git submodule foreach 'git status' it is difficult to parse and I can't tell which submodule it's reporting the status for. It appears to just spew out my %PATH% variable (I'm using msysGit on Windows). My output appears similar to that shown here:

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-开发者_Python百科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)

As you can see, this is quite difficult to glance at (especially when the output is wrapped) and see what the output of 'git status'. In addition the name of each submodule is not shown so I have no way of knowing which submodule each section of output refers to.

Any help is appreciated! Thanks!


You fail to mention exactly what it is you want shown...

 git submodule foreach -q git ls-files -m 

would go a long way at showing just the modified files, for example. Other options include

 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

You could write a silly small script in the following vein:

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

To show just what you want using the submodule directory name for easier identification.

In case you couldn't work it out from the above ideas: don't go and try to parse output that wasn't designed for machine consumption. Git has a very rich set of subcommands that can help you out in a better way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号