#!/bin/sh setglobal USAGE = '"[-a] [-r] [-m] [-t] [-n] [-b ] '" setglobal LONG_USAGE = '"git-resurrect attempts to find traces of a branch tip called , and tries to resurrect it. Currently, the reflog is searched for checkout messages, and with -r also merge messages. With -m and -t, the history of all refs is scanned for Merge into other/Merge into (respectively) commit subjects, which is rather slow but allows you to resurrect other people's topic branches.'" setglobal OPTIONS_KEEPDASHDASH = '' setglobal OPTIONS_STUCKLONG = '' setglobal OPTIONS_SPEC = ""\ git resurrect $USAGE -- b,branch= save branch as instead of a,all same as -l -r -m -t k,keep-going full rev-list scan (instead of first match) l,reflog scan reflog for checkouts (enabled by default) r,reflog-merges scan for merges recorded in reflog m,merges scan for merges into other branches (slow) t,merge-targets scan for merges of other branches into n,dry-run don't recreate the branch"" source git-sh-setup proc search_reflog { sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \ < "$GIT_DIR"/logs/HEAD } proc search_reflog_merges { git rev-parse $[ sed -ne 's~^[^ ]* \([^ ]*\) .*\tmerge '"$1"':.*~\1^2~p' \ < "$GIT_DIR"/logs/HEAD] } setglobal _x40 = '"[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'" setglobal _x40 = ""$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"" proc search_merges { git rev-list --all --grep="Merge branch '$1'" \ --pretty=tformat:"%P %s" | sed -ne "/^$_x40 \($_x40\) Merge .*/ {s//\1/p;$early_exit}" } proc search_merge_targets { git rev-list --all --grep="Merge branch '[^']*' into $branch\$" \ --pretty=tformat:"%H %s" --all | sed -ne "/^\($_x40\) Merge .*/ {s//\1/p;$early_exit} " } setglobal dry_run = '' setglobal early_exit = 'q' setglobal scan_reflog = 't' setglobal scan_reflog_merges = '' setglobal scan_merges = '' setglobal scan_merge_targets = '' setglobal new_name = '' while test "$Argc" != 0 { match $1 { with -b|--branch shift setglobal new_name = $1 with -n|--dry-run setglobal dry_run = 't' with --no-dry-run setglobal dry_run = '' with -k|--keep-going setglobal early_exit = '' with --no-keep-going setglobal early_exit = 'q' with -m|--merges setglobal scan_merges = 't' with --no-merges setglobal scan_merges = '' with -l|--reflog setglobal scan_reflog = 't' with --no-reflog setglobal scan_reflog = '' with -r|--reflog_merges setglobal scan_reflog_merges = 't' with --no-reflog_merges setglobal scan_reflog_merges = '' with -t|--merge-targets setglobal scan_merge_targets = 't' with --no-merge-targets setglobal scan_merge_targets = '' with -a|--all setglobal scan_reflog = 't' setglobal scan_reflog_merges = 't' setglobal scan_merges = 't' setglobal scan_merge_targets = 't' with -- shift break with * usage } shift } test "$Argc" = 1 || usage setglobal all_strategies = ""$scan_reflog$scan_reflog_merges$scan_merges$scan_merge_targets"" if test -z $all_strategies { die "must enable at least one of -lrmt" } setglobal branch = $1 test -z $new_name && setglobal new_name = $branch if test ! -z $scan_reflog { if test -r "$GIT_DIR"/logs/HEAD { setglobal candidates = $[search_reflog $branch] } else { die 'reflog scanning requested, but' \ '$GIT_DIR/logs/HEAD not readable' } } if test ! -z $scan_reflog_merges { if test -r "$GIT_DIR"/logs/HEAD { setglobal candidates = ""$candidates $[search_reflog_merges $branch]"" } else { die 'reflog scanning requested, but' \ '$GIT_DIR/logs/HEAD not readable' } } if test ! -z $scan_merges { setglobal candidates = ""$candidates $[search_merges $branch]"" } if test ! -z $scan_merge_targets { setglobal candidates = ""$candidates $[search_merge_targets $branch]"" } setglobal candidates = $[git rev-parse $candidates | sort -u] if test -z $candidates { setglobal hint = '' test "z$all_strategies" != "ztttt" \ && setglobal hint = '" (maybe try again with -a)'" die "no candidates for $branch found$hint" } echo "** Candidates for $branch **" for cmt in [$candidates] { git --no-pager log --pretty=tformat:"%ct:%h [%cr] %s" --abbrev-commit -1 $cmt } \ | sort -n | cut -d: -f2- setglobal newest = $[git rev-list -1 $candidates] if test ! -z $dry_run { printf "** Most recent: " git --no-pager log -1 --pretty=tformat:"%h %s" $newest } elif ! git rev-parse --verify --quiet $new_name >/dev/null { printf "** Restoring $new_name to " git --no-pager log -1 --pretty=tformat:"%h %s" $newest git branch $new_name $newest } else { printf "Most recent: " git --no-pager log -1 --pretty=tformat:"%h %s" $newest echo "** $new_name already exists, doing nothing" }