git filter-branch 只获取已更改的文件?

Ell*_*nce 5 git phpcs

我们想要运行git filter-branch一个大型代码库来重新格式化 PHP 文件。由于我们有超过 21k 次提交,phpcbf因此希望每次提交filter-branch. 是否可以只获取每次提交更改的文件并对其进行专门格式化?就像是...

git filter-branch --tree-filter \
 'FILES=$(<something> | grep .php) php /usr/local/bin/phpcbf.phar $FILES || true'
Run Code Online (Sandbox Code Playgroud)

Ell*_*nce 5

我找到了解决方案:

\n\n
git filter-branch --tree-filter \'phpcbf $(\\\n  git show $GIT_COMMIT --name-status | egrep ^[AM] |\\\n    grep .php | cut -f2)\' -- --all\n
Run Code Online (Sandbox Code Playgroud)\n\n

只是为了快速概述一下它\xe2\x80\x99s 正在做什么:

\n\n
    \n
  • git show $GIT_COMMIT --name-status将返回该提交的所有修改文件。
  • \n
  • egrep ^[AM]将雕像过滤为仅添加和修改。无需尝试格式化正在删除的文件。
  • \n
  • grep .php仅格式化 PHP 文件。
  • \n
  • cut -f2从列表中删除状态前缀,以便我们只获取原始文件路径。
  • \n
\n\n

有关更多详细信息,请参阅我的博客文章: https: //elliot.land/post/reformatting-your-codebase-with-git-filter-branch

\n