繁体   English   中英

Git grep 跨目录中的多个存储库

[英]Git grep across multiple repositories in a directory

我在服务器上有一个 bitbucket 个存储库的列表:

[user@lonapdbitbucket1 repositories]$ ls
1039 1044 1059 2165 2656 3958 3958 9284 9274 8274 7264 7263 8274

如果我进入这些存储库之一并运行 git grep 以搜索 Ansible 加密字符串,那么它工作正常 - git grep 设法找到 Ansible 加密字符串:

[user@lonapdbitbucket1 repositories]$ cd 1044 
[user@lonapdbitbucket1 repositories]$ git grep -P '\$ANSIBLE_VAULT;[0-9]\.[0-];AES256' $(git rev-list --all)

要跨多个回购协议执行此操作,我想将其转换为 bash 脚本:

# secret_scan.sh
repos_root=/var/lib/docker/volumes/bitbucket/_data/shared/data/repositories
git_grep_cmd=git grep -P '\$ANSIBLE_VAULT;[0-9]\.[0-];AES256' $(git rev-list --all)
for dir in ./*
do
    # below line is just to clean up the directory string
    repo_dir="$(d{dir#./}"
    cd "${repos_root}${repo_dir}"; \
    eval "git_grep_cmd"
done

不幸的是,这不起作用:

[user@lonapdbitbucket1 repositories]$ ./secret_scan.sh
fatal: not a git repository (or any parent up to mount point /var/lib)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
[user@lonapdbitbucket1 repositories]$ _

任何人都可以在这里提出解决方案,基本上 cd 到多个存储库,然后在每个存储库上运行 git grep,复制结果,就像我在命令行上做的一样?

我不确定你想用 eval 和 repo_dir 实现什么。 这应该很简单:

repos_root=/var/lib/docker/volumes/bitbucket/_data/shared/data/repositories
for dir in *
do
    cd "$repos_root$dir";
    git grep -P '\$ANSIBLE_VAULT;[0-9]\.[0-];AES256' $(git rev-list --all)
done

由于您的repos_root是绝对的,因此您无需处理返回原始目录的问题。

但我怀疑git rev-list --all是否可以被替换,它的 output 会很大。 您是否要在所有提交中查找字符串? 要搜索字符串的完整历史记录,请选中搜索字符串的所有 Git 历史记录如何 grep Git 提交差异或特定单词的内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM