From 8a57b4a8d797b7ae98de93fa87848631875597ff Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Wed, 27 Mar 2013 09:07:42 +0100 Subject: [PATCH] add a tool to identify the revision of current work tree or index by content --- git-identify-revision | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 git-identify-revision diff --git a/git-identify-revision b/git-identify-revision new file mode 100755 index 0000000..64e9495 --- /dev/null +++ b/git-identify-revision @@ -0,0 +1,47 @@ +#!/bin/sh + +# situation: work tree is unknown rev +# arguments: git rev-list arguments +# we now identify the one revision with least differences + +case "$1" in + --cached) + use_worktree=false + shift + ;; + *) + use_worktree=true + ;; +esac + +diffopts="-M -C" + +if $use_worktree; then + echo >&2 "Saving index..." + oldindex=`git write-tree` + # set up resetting + trap 'echo >&2 "Restoring index..."; git reset "$oldindex" .' EXIT + trap 'exit 1' INT TERM + + echo >&2 "Creating index..." + git add -A +fi + +echo >&2 "Listing candidates..." +allrevs=`git rev-list "$@"` + +echo >&2 "Evaluating candidates..." +bestrev= +bestrevscore= +for rev in $allrevs; do + score=`git diff $diffopts --cached "$rev" | wc -l` + if [ -z "$bestrevscore" ] || [ "$score" -lt "$bestrevscore" ]; then + echo >&2 "Improved to $rev (score: $score)" + bestrev=$rev + bestrevscore=$score + fi +done +echo >&2 "Done." + +echo "$bestrev" +git diff $diffopts --cached "$bestrev" >&2 -- 2.39.2