如何使用git檢測複製和貼上程式碼?
手冊頁,注意到這部分:
A particularly useful way is to see if an added file has lines created by copy-and-paste from existing files. Sometimes this indicates that the developer was being sloppy and did not refactor the code properly. You can first find the commit that introduced the file with: git log –diff-filter=A –pretty=short — foo and then annotate the change between the commit and its parents, using commit^! notation: git blame -C -C -f $commit^! — foo
這聽起來很有趣,但是我不太瞭解它是如何工作的,為什麼.我想知道它是否可以用於git鉤子來檢測複製&貼上程式碼.
一些git專家可能會解釋使用上述git命令在一起的效果,以及是否可以使用這樣的東西來使git顯示是否有程式碼重複(可能通過使用git似乎在重新命名檔案時計算的“相似性索引” )?
您可以單獨斷開命令.
$git log --diff-filter=A --pretty=short -- foo
顯示檔案“foo”的日誌. –diff-filter選項僅顯示新增檔案的提交(“A”),並以縮寫格式(–pretty = short選項)顯示. (The– 是一個標準,說“沒有什麼可以選擇”,其後的一切都是應用日誌的檔名列表.)
然後:
$git blame -C -C -f $commit^! -- foo
git blame使用上次提交的資訊來註釋檔案的每一行.雙-C -C選項會積極地檢查從其他檔案複製的行. -f選項顯示原始提交的檔名(這意味著如果一行從另一個檔案複製,您將看到從其中複製的檔案的名稱). $commit ^!是一個$commit的符號; ^!字尾意味著排除所有$commit的父母.
所以基本上,第一個命令(git log)可以幫助你找到引入複製行的提交;第二個(git blame)可以幫助您找到由git log返回的任何可疑提交的來源.
http://stackoverflow.com/questions/1939635/how-can-i-detect-copy-pasted-code-using-git