I want to get git
output in the format status -s
:
genesis% git status -s .
?? .config/aria2/
?? .config/htop/
?? .config/mc/
?? .config/pacman/
?? .config/remmina/
?? .config/teamviewer/
?? .config/zsh/
But I want this list with `\\0' terminated filenames to safely deal with files with weird characters in their names.
genesis% git status -z . | xargs -0n1 echo
?? misc/.config/aria2/
?? misc/.config/htop/
?? misc/.config/mc/
?? misc/.config/pacman/
?? misc/.config/remmina/
?? misc/.config/teamviewer/
?? misc/.config/zsh/
As you can see, the -z
output gives paths relative to the repository root (the additional misc
).
How can I get the relative pathnames like in the first example but still have them NUL
-terminated?
You can't quite get what you want directly. However, since -z
is intended for consumption by code rather than humans, you can add a bit of your own code to do what you want, using git rev-parse --show-cdup
:
$ git rev-parse --show-cdup
../
This says that there is one top level component "missing" from the current working directory, so after you get your limited-to-current-directory entries via -z
, you should strip off the first component of each name. If --show-cdup
printed ../../
you would need to strip off two, and so on.
Note that --show-cdup
prints nothing—or more precisely, a blank line—when there are no components to strip off. Hence, counting the number of ../
(or even just /
) in --show-cdup
's output gives you the correct number of leading path name components to strip.
(If you're providing some argument other than .
to git status
, you'll need even more: test whether the argument is a directory, if so cd
into it and run git rev-parse --show-cdup
, and if it's a file, get its dirname or .
, cd
into that, and run git rev-parse --show-cdup
. Do all this from a subprocess to avoid modifying the current working directory of the main process. This rather convoluted method avoids having to write path name resolvers to handle arbitrary numbers of ./
and ../
components, but you can also go the other route and go straight for full hard paths with realpath
, assuming POSIX-like environments anyway.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.