I would like to use git-diff
's "similarity index" calculation feature for files outside of git repository.
Here is the example output of git diff
for files not tracked by git (first diff, i.e. what I get) and tracked by git (second diff, i.e. what I would like, but for external files as well)
$ seq 1 3 > file1 ; cp file1 file2 ; echo 4 >> file2 # create files
$ git diff -C file1 file2 # show diff (no repo, -C has no effect)
diff --git 1/file1 2/file2
index 01e79c32a8c9..94ebaf900161 100644
--- 1/file1
+++ 2/file2
@@ -1,3 +1,4 @@
1
2
3
+4
$ git init > /dev/null # create repo
(master #%)$ (git add file1; git commit -m file1) > /dev/null # add file1
(master %)$ (git add file2; git commit -m file2) > /dev/null # add file1
(master %)$ git diff -C HEAD^ # show diff (in repo, -C works)
diff --git c/file1 w/file2
similarity index 75%
copy from file1
copy to file2
index 01e79c32a8c9..94ebaf900161 100644
--- c/file1
+++ w/file2
@@ -1,3 +1,4 @@
1
2
3
+4
I have already seen those questions:
- How to compare two files not in repo using git
- How can I use `git diff --color-words` outside a Git repository?
and some other related.
I read git diff
manual and even some git's diff source code and it looks like similarity index is always shown for renamed (status R) or copied (C) files, and only sometimes for modified (M) ones:
Status letters C and R are always followed by a score (denoting the percentage of similarity between the source and target of the move or copy). Status letter M may be followed by a score (denoting the percentage of dissimilarity) for file rewrites.
So far I found no way of forcing git to treat external files as copies (--find-copies
/-C
) or renames (--find-renames
/-M
) and unfortunately it is not explained in the manual (also not too obvious after looking at the source code either) when the score is shown for status M (modified), which is used when comparing files outside of repo (status can be seen with --raw
option).
Is this possible at all?
Or would it require adding new options to git-diff
(maybe --assume-copy
) to force the required status?