如果将相同的实用程序脚本签入到不同的 SCM 目录,但在任何地方都应该相同,我如何验证它们是否相同?

If the same utility script is checked in to different SCM directories yet should be the same everywhere, how can I verify they are the same?

承认这不是一个好情况,但有时您会发现相同的实用程序脚本(或 Makefiles 之类的东西)已被签入相同或不同存储库中的不同目录以用于相同或不同的项目。它们甚至可能已被签入驻留在源代码树不同级别的命名不一致的目录或项目目录中。

如何找到位于不同目录中的同名和(可能)相同用途的文件,并轻松发现哪些相同,哪些不同,有何不同?

对于 Makefile 示例,假设某人正在单独开发新的改进变体 projectX,并更新了 Makefile 以自动执行以前的手动任务。您有责任确保其他旧项目赶上进度,但可能有人已经完成了一些更新。

您可以使用下面的脚本 运行 diffTree.sh Makefile 并得到如下输出:

bash$ diffTree.sh Makefile
diff ./newprojects/projectX/Makefile ./projectB/Makefile
1,2d0
< CONFIGPARAM = "foo"
17c5
<       echo "I did that thing for you"
---
>       echo "You should do that thing"
diff ./projectB/Makefile ./projectC/Makefile
diff ./projectC/Makefile ./projectD/Makefile
end of files
bash$ _

这告诉您项目 B 和 C 和 D 完全相同,但是 newprojects/projectX/Makefile 不同于 B,因此也同样不同于 C 和 D。

如果不同位置的所有同名文件都相同,你会得到这样的结果:

bash$ diffTree.sh sameFile.h
diff ./newprojects/projectX/src/headers/sameFile.h ./projectB/src/headers/sameFile.h
diff ./projectB/src/headers/sameFile.h ./projectC/src/headers/sameFile.h
diff ./projectC/src/headers/sameFile.h ./projectD/src/headers/sameFile.h
end of files
bash$ _

如果您指定的文件名在树中根本不存在,您将得到:

bash$ diffTree.sh foo
end of files
bash$ _

这是脚本:

#!/usr/bin/env bash
set `find . -name  -print ; echo no files`
while [[ $# -gt 1 ]]
  do 
    if [[  = no &&  = files ]]
      then 
        shift
      else 
        echo diff  
        diff  
      fi
    shift
  done
echo end of files

它似乎可以处理文件名中的通配符,但可能需要做一些事情才能使它更安全。同样,允许 'ignore whitespace' 等差异选项将是一种改进。

显然,如果您碰巧有一个名为 'no' 的文件,而另一个名为 'files' 的文件出现在 'find' 命令的输出中间,那么它就会完全崩溃,但是由于所有文件名都以 './' 为前缀,'no' 按字母顺序排在 'files' 之后,我认为这足够安全。