git - 使用 git 状态信息增强 "ls"?

标签 git

是否有现有的命令,或者一些技巧或脚本可以让我显示“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 状态信息。

最佳答案

使用 Git 状态 short format信息,这是一个 Bash 脚本,它使用 Awk 和 column 命令为您提供自定义状态输出。

#!/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 或您最熟悉的任何语言来实现它。


这是一个示例输出:

B                                renamed in index
A                                untracked
dont_delete_git_pre-commit_hook  untracked

刚意识到,制表符显示为空格。在 Awk 脚本 print f ""stat;column -t -s "" 命令中,双引号之间有一个制表符(不是空格)。您可以使用制表符以外的分隔符。


注意到上述脚本中状态标志处理的问题并已更正。

关于git - 使用 git 状态信息增强 "ls"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8727003/

相关文章:

Android Studio Git .gitignore 与项目 > 设置 > 版本控制 > 忽略文件

python - 无法使用 gitpython 创建/添加新分支到 git repo

android - 我应该将 Android 应用程序的发布 key 存储提交到团队存储库吗?

git - 我自己使用 Git,公司使用 Subversion

Git 在我的本地项目中找不到分支

git - 在 Visual Studio Team Explorer 中切换源代码管理上下文

git - 大量 git commit 压缩

git - 前端+后端存储库中每个子任务的每个功能\故事\史诗VS分支?

git - 我如何吹走我解决 git 冲突的尝试

git - 多个 Git 存储库