如何解决如何在提交时获取git子模块的状态?
我想检索特定提交的git submodule status --cached --recursive
。我知道我可以做到:
git checkout [commit_hash] && git submodule status --cached --recursive
但这会更改当前的git状态,分支和文件内容,之后都需要再次将其全部撤消。
不幸的是,docs for git submodule status并不表示可以传递提交哈希,也不起作用:
$ git submodule status --cached --recursive bc7cecf1eef3
error: pathspec 'bc7cecf1eef3' did not match any file(s) known to git
请注意,当前子模块可能与该提交时的子模块不同,因此无法使用git submodule foreach ...
。在那个子模块状态中,我实际上只需要(path,commit_sha)对。
解决方法
我仍然希望有人能找到一个更简单的解决方案,但是以下python脚本可以工作。我将其包含在github.com/TamaMcGlinn/submodule_bundler中,因为我需要它来为offline transferral of repositories that include submodules生成捆绑包。
#!/usr/bin/env python3
""" Print the commit of each submodule (recursively) at some commit"""
import os
import argparse
import subprocess
import re
def print_submodule_commits(root_subdir,root_commit):
for result in submodule_commits(root_subdir,root_commit):
print(f'{result["subdir"]} {result["commit"]}')
def submodule_commits(subdir='.',commit='HEAD',prefix=''):
is_subdir = subdir != '.'
if is_subdir:
previous_dir = os.getcwd()
os.chdir(subdir)
git_ls_tree = subprocess.check_output(['git','ls-tree','-r',commit])
ls_tree_lines = filter(None,git_ls_tree.decode("utf-8").split("\n"))
submodule_regex = re.compile(r'^[0-9]+\s+commit')
for line in ls_tree_lines:
if submodule_regex.match(line):
line_split = line.split()
commit_hash = line_split[2]
subdirectory = line_split[3]
submodule_prefix = subdirectory
if prefix != '':
submodule_prefix = f'{prefix}/{subdirectory}'
yield {'subdir': submodule_prefix,'commit': commit_hash}
yield from submodule_commits(subdirectory,commit_hash,submodule_prefix)
if is_subdir:
os.chdir(previous_dir)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Print the commit of each submodule (recursively) at some commit')
parser.add_argument('commit',metavar='commit_hash',type=str,default='HEAD',nargs='?',help='commit to examine; defaults to HEAD')
args = parser.parse_args()
print_submodule_commits('.',args.commit)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。