forked from jwiegley/git-scripts
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-closest-match
More file actions
executable file
·25 lines (23 loc) · 883 Bytes
/
git-closest-match
File metadata and controls
executable file
·25 lines (23 loc) · 883 Bytes
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
#!/bin/sh
# find the closest match from all (or a limited amount) of the reachable trees to a specified tree (where tree is referenced by it's checksum)
# very useful to process the results of `git fsck --unreachable | cut -d\ -f3`
spec=$1
mode=${2:-diff} # num: number of lines or diff: actual diff/log message?
range=${3:-30} # 'all' or most recent <num> in current branch?. 'all' can be quite slow
if [ "$range" = 'all' ]; then
all=`git-rev-list --all | awk '/^commit/ {print $NF}'`
else
all=`git log -n $range | awk '/^commit/ {print $NF}'`
fi
commit=`for i in $all; do
echo -n "$i "
# why is there no git diff --shortnumstat ?
git diff -M $spec $i | wc -l
done | sort -k 2 -n | head -n 1 | cut -f 1 -d ' '`
if [ "$mode" = diff ]; then
git log --no-walk $commit | cat -
git diff -M $spec $commit | cat -
else
echo -n "$commit: "
git diff -M $spec $commit | wc -l
fi