如何对速记git status的输出进行排序? [英] How to sort output of shorthand git status?

查看:231
本文介绍了如何对速记git status的输出进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 git status --short --branch 的输出进行排序,以便:

  • 索引中的文件显示在底部
  • 在工作树中修改但未在索引中修改的文件显示在该文件的上方
  • 顶部的未跟踪更改

如果这需要传递到其他命令来对输出中的行进行排序,那么最好保留Git配置的输出颜色.

我可以创建一些聪明的别名来替我做到吗?请注意,我在Windows上使用Git(如果有的话).

解决方案

您可以告诉git生成颜色代码,但是要按自定义顺序排序,您将需要编写一些脚本.这是一个简短的python示例,您可以从 git -c color.ui = always status --short --branch :

 #!/bin/env python导入系统,重新#此处定义的自定义排序顺序:order = {'A':1,'M':3,'??':2,'##':0}ansi_re = re.compile(r'\ x1b [^ m] * m')打印''.join(sorted(sys.stdin.readlines(),cmp = lambda x,y:cmp(order.get(ansi_re.sub('',x)[0:2],0),order.get(ansi_re.sub('',y)[0:2],0)))) 

或单线可憎:

  git -c color.ui =总是状态--short --branch |python -c'导入sys,重新;\order = {"A":1,"M":3,"??":2,"##":0};ansi_re = re.compile(r"\ x1b [^ m] * m"); \打印" .join(sorted(sys.stdin.readlines(),cmp = lambda x,y:\cmp(order.get(ansi_re.sub(",x)[0:2],0),order.get(ansi_re.sub(",y)[0:2],0)))))' 

简短说明.

python脚本读取 stdin ,这是git status的彩色列表输出,并且在剥离ANSI颜色代码后,针对每个状态的自定义优先级比较前两个状态字符在字典中定义.

ANSI颜色代码的删除基于:如何从python中的字符串中删除ANSI转义序列

不同状态代码的完整列表可以在 解决方案

You can tell git to generate colour codes, but to sort in a custom order you will have to script a bit. Here's a short python example that you can pipe from git -c color.ui=always status --short --branch:

#!/bin/env python

import sys, re

# custom sorting order defined here:
order = { 'A ' : 1, ' M' : 3, '??' : 2, '##' : 0 }

ansi_re = re.compile(r'\x1b[^m]*m')

print ''.join(sorted(
    sys.stdin.readlines(),
    cmp=lambda x,y: cmp(
        order.get(ansi_re.sub('', x)[0:2],0),
        order.get(ansi_re.sub('', y)[0:2],0))))

Or a one-liner abomination:

git -c color.ui=always status --short --branch | python -c 'import sys, re; \
  order = {"A ":1," M":3,"??":2,"##":0}; ansi_re = re.compile(r"\x1b[^m]*m");\
  print "".join(sorted(sys.stdin.readlines(),cmp=lambda x,y: \
  cmp(order.get(ansi_re.sub("", x)[0:2],0), order.get(ansi_re.sub("", y)[0:2],0))))'

Short explanation.

The python script reads the stdin, which is the coloured listing output of git status, and after stripping the ANSI colour codes, compares the first two status characters with respect to the custom priority for each status defined in a dictionary.

The ANSI colour code removals are based: on How can I remove the ANSI escape sequences from a string in python

And the full list of the different status codes can be found at the git status help page.

这篇关于如何对速记git status的输出进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆