]> git.xonotic.org Git - xonotic/div0-gittools.git/blob - git-identify-revision
add a tool to identify the revision of current work tree or index by content
[xonotic/div0-gittools.git] / git-identify-revision
1 #!/bin/sh
2
3 # situation: work tree is unknown rev
4 # arguments: git rev-list arguments
5 # we now identify the one revision with least differences
6
7 case "$1" in
8         --cached)
9                 use_worktree=false
10                 shift
11                 ;;
12         *)
13                 use_worktree=true
14                 ;;
15 esac
16
17 diffopts="-M -C"
18
19 if $use_worktree; then
20         echo >&2 "Saving index..."
21         oldindex=`git write-tree`
22         # set up resetting
23         trap 'echo >&2 "Restoring index..."; git reset "$oldindex" .' EXIT
24         trap 'exit 1' INT TERM
25
26         echo >&2 "Creating index..."
27         git add -A
28 fi
29
30 echo >&2 "Listing candidates..."
31 allrevs=`git rev-list "$@"`
32
33 echo >&2 "Evaluating candidates..."
34 bestrev=
35 bestrevscore=
36 for rev in $allrevs; do
37         score=`git diff $diffopts --cached "$rev" | wc -l`
38         if [ -z "$bestrevscore" ] || [ "$score" -lt "$bestrevscore" ]; then
39                 echo >&2 "Improved to $rev (score: $score)"
40                 bestrev=$rev
41                 bestrevscore=$score
42         fi
43 done
44 echo >&2 "Done."
45
46 echo "$bestrev"
47 git diff $diffopts --cached "$bestrev" >&2