增强的"ls"表示与git状态信息? [英] Enhanced "ls" with git status information?
问题描述
是否存在现有的命令,或者是否有一些技巧或脚本可以让我显示"ls"中显示的文件的状态?
Is there either an existing command, or some trick or script that allows me to show the status of the files shown in "ls"?
类似以下内容:
$ git ls status #Command could be anything `lsg` is fine too, whatever.
app contents modified
autotest up-to-date
config up-to-date
config.ru staged
db contents modified
doc contents modified
Gemfile modified
Gemfile.lock modified
lib up-to-date
log up-to-date
public up-to-date
Rakefile up-to-date
README up-to-date
script up-to-date
spec up-to-date
tmp up-to-date
vendor contents modidified
test.tmp removed
以任何方式:在目录列表中提供git状态信息.
In any way: having the git status information available in a directory listing.
推荐答案
Using the Git status short format information, here's a Bash script that uses Awk and the column
command to give you customized status output.
#!/bin/bash
git status --porcelain | \
awk 'BEGIN {FS=" "}
{
xstat = substr($0, 1, 1);
ystat = substr($0, 2, 1);
f = substr($0, 4);
ri = index(f, " -> ");
if (ri > 0) f = substr(f, 1, ri);
if (xstat == " " && ystat ~ "M|D") stat = "not updated";
else if (xstat == "M" && ystat ~ " |M|D") stat = "updated in index";
else if (xstat == "A" && ystat ~ " |M|D") stat = "added to index";
else if (xstat == "D" && ystat ~ " |M") stat = "deleted from index";
else if (xstat == "R" && ystat ~ " |M|D") stat = "renamed in index";
else if (xstat == "C" && ystat ~ " |M|D") stat = "copied in index";
else if (xstat ~ "M|A|R|C" && ystat == " ") stat = "index and work tree matches";
else if (xstat ~ " |M|A|R|C" && ystat == "M") stat = "work tree changed since index";
else if (xstat ~ " |M|A|R|C" && ystat == "D") stat = "deleted in work tree";
else if (xstat == "D" && ystat == "D") stat = "unmerged, both deleted";
else if (xstat == "A" && ystat == "U") stat = "unmerged, added by us";
else if (xstat == "U" && ystat == "D") stat = "unmerged, deleted by them";
else if (xstat == "U" && ystat == "A") stat = "unmerged, added by them";
else if (xstat == "D" && ystat == "U") stat = "unmerged, deleted by us";
else if (xstat == "A" && ystat == "A") stat = "unmerged, both added";
else if (xstat == "U" && ystat == "U") stat = "unmerged, both modified";
else if (xstat == "?" && ystat == "?") stat = "untracked";
else if (xstat == "!" && ystat == "!") stat = "ignored";
else stat = "unknown status";
print f " " stat;
}' | \
column -t -s " "
如果在PATH
的目录中创建可执行文件git-status-ls
($HOME/bin
应该是一个好地方),则可以在任何Git存储库中键入git status-ls
.或者,您可以为此创建一个Git别名单线.您还可以使用Perl,Python,C或您最熟悉的任何语言来实现此功能.
If you create an executable git-status-ls
in a directory on your PATH
($HOME/bin
should be a good place), you can type git status-ls
in any Git repo. Or you could create a Git alias one-liner for this. You could also implement this using Perl, Python, C or whatever language you're most comfortable with.
以下是示例输出:
B renamed in index
A untracked
dont_delete_git_pre-commit_hook untracked
刚意识到,选项卡显示为空格.在Awk脚本print f " " stat;
和column -t -s " "
命令中,双引号之间有一个制表符(不是空格).您可以使用制表符以外的分隔符.
Just realized, tabs are displaying as spaces. In the Awk script print f " " stat;
and in the column -t -s " "
command, there is a tab (not spaces) between the double-quotes. You could use a separator other than tab.
解决了上述脚本中状态标志处理的问题,并对其进行了纠正.
Noticed an issue with the status flags handling in the above script and corrected it.
这篇关于增强的"ls"表示与git状态信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!