搜索关键字:repo status命令优化,repo status命令过滤无用信息,repo status命令只打印有修改的仓库,repo status过滤无关的仓库
repo工具用于在linux/ubuntu下管理较多的git仓库。repo工具用python语言编写,对git工具进行了包装,包装的目的主要是能够支持同时处理较多的git仓库。大多数情况下,我们通过repo init,repo sync,repo status就能够完成多数功能。
其中,repo status命令正常的输出包含了所有git仓库的状态信息,如果仓库数量很多的情况下,会打印大量无用信息。比如安卓源码项目AOSP,repo工具可能需要管理上百个git仓库,此时直接输入repo status命令,得到的结果几乎全部是空状态信息,如下 :
project art/ X9_3.0.0_PTG4.0
project bionic/ X9_3.0.0_PTG4.0
project bootable/recovery/ X9_3.0.0_PTG4.0
project build/blueprint/ X9_3.0.0_PTG4.0
project build/kati/ X9_3.0.0_PTG4.0
project build/make/ X9_3.0.0_PTG4.0
project build/soong/ X9_3.0.0_PTG4.0
project cts/ X9_3.0.0_PTG4.0
project dalvik/ X9_3.0.0_PTG4.0
project developers/build/ X9_3.0.0_PTG4.0
project developers/demos/ X9_3.0.0_PTG4.0
project developers/samples/android/ X9_3.0.0_PTG4.0
project development/ X9_3.0.0_PTG4.0
project device/amlogic/yukawa/ X9_3.0.0_PTG4.0
project device/amlogic/yukawa-kernel/ X9_3.0.0_PTG4.0
project device/common/ X9_3.0.0_PTG4.0
project device/generic/arm64/ X9_3.0.0_PTG4.0
project device/generic/armv7-a-neon/ X9_3.0.0_PTG4.0
project device/generic/car/ X9_3.0.0_PTG4.0
project device/generic/common/ X9_3.0.0_PTG4.0
project device/generic/goldfish/ X9_3.0.0_PTG4.0
project device/generic/goldfish-opengl/ X9_3.0.0_PTG4.0
为了让repo status命令只打印有修改的仓库,需要修改相关函数。打印仓库状态的函数是PrintWorkTreeStatus,代码路径是.repo/repo/project.py
相关代码修改后如下:
def PrintWorkTreeStatus(self, output_redir=None, quiet=False):
"""Prints the status of the repository to stdout.
Args:
output_redir: If specified, redirect the output to this object.
quiet: If True then only print the project name. Do not print
the modified files, branch name, etc.
"""
if not platform_utils.isdir(self.worktree):
if output_redir is None:
output_redir = sys.stdout
print(file=output_redir)
print('project %s/' % self.relpath, file=output_redir)
print(' missing (run "repo sync")', file=output_redir)
return
self.work_git.update_index('-q',
'--unmerged',
'--ignore-missing',
'--refresh')
rb = self.IsRebaseInProgress()
di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
df = self.work_git.DiffZ('diff-files')
do = self.work_git.LsOthers()
if not rb and not di and not df and not do and not self.CurrentBranch:
return 'CLEAN'
out = StatusColoring(self.config)
if output_redir is not None:
out.redirect(output_redir)
"""
out.project('project %-40s', self.relpath + '/ ')
if quiet:
out.nl()
return 'DIRTY'
branch = self.CurrentBranch
if branch is None:
out.nobranch('(*** NO BRANCH ***)')
else:
out.branch('branch %s', branch)
out.nl()
"""
branch = self.CurrentBranch
if rb:
out.important('prior sync failed; rebase still in progress')
out.nl()
paths = list()
paths.extend(di.keys())
paths.extend(df.keys())
paths.extend(do)
if paths != []:
out.nl()
out.project('project %-40s', self.relpath + '/')
if branch is None:
out.branch('(*** NO BRANCH ***)')
else:
out.branch('%s', branch)
out.nl()
for p in sorted(set(paths)):
try:
i = di[p]
except KeyError:
i = None
try:
f = df[p]
except KeyError:
f = None
if i:
i_status = i.status.upper()
else:
i_status = '-'
if f:
f_status = f.status.lower()
else:
f_status = '-'
if i and i.src_path:
line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
i.src_path, p, i.level)
else:
line = ' %s%s\t%s' % (i_status, f_status, p)
if i and not f:
out.added('%s', line)
elif (i and f) or (not i and f):
out.changed('%s', line)
elif not i and not f:
out.untracked('%s', line)
else:
out.write('%s', line)
out.nl()
return 'DIRTY'
修改说明:
1.通过三引号注释原有的输出内容
2.对上述输出内容进行判断