-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-similarity
executable file
·52 lines (42 loc) · 1.19 KB
/
git-similarity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /bin/sh
#
# git-similarity: script to compute similarity of two files
. git-sh-setup # for die() etc
TAB=$'\t'
# should probably use OPTIONS_SPEC, but not yet
usage()
{
echo "usage: git similarity file1 file2"
}
case $# in
2) ;;
*) usage 1>&2; exit 1;;
esac
test -f "$1" || die "cannot find file $1, or not a regular file"
test -f "$2" || die "cannot find file $2, or not a regular file"
test "x$1" != "x$2" || die "file names $1 and $2 are identical"
TF=$(mktemp) || exit 1
trap "rm -f $TF" 0 1 2 3 15
export GIT_INDEX_FILE=$TF
# Create a tree holding (just) the argument file.
# TODO: handle files outside the repository entirely, by
# using git hash-object and git mktree directly. (This
# will also remove the need for the temporary-file index.)
maketree() {
rm -f $TF
git add "$1" || exit 1
git write-tree || exit 1
}
# Use git diff-tree here for repeatibility. We expect output of
# the form Rnnn$TAB$file1$TAB$file2, but if we get two lines,
# with D and A, we'll just print 000 here.
print_similarity() {
set $(git diff-tree -r --name-status --find-renames=01 $1 $2)
case "$1" in
R*) echo "${1#R}";;
*) echo "000";;
esac
}
h1=$(maketree "$1")
h2=$(maketree "$2")
print_similarity $h1 $h2