3 USAGE="[-a] [-r] [-m] [-t] [-n] [-b <newname>] <name>"
4 LONG_USAGE="git-resurrect attempts to find traces of a branch tip
5 called <name>, and tries to resurrect it. Currently, the reflog is
6 searched for checkout messages, and with -r also merge messages. With
7 -m and -t, the history of all refs is scanned for Merge <name> into
8 other/Merge <other> into <name> (respectively) commit subjects, which
9 is rather slow but allows you to resurrect other people's topic
16 b,branch= save branch as <newname> instead of <name>
17 a,all same as -l -r -m -t
18 k,keep-going full rev-list scan (instead of first match)
19 l,reflog scan reflog for checkouts (enabled by default)
20 r,reflog-merges scan for merges recorded in reflog
21 m,merges scan for merges into other branches (slow)
22 t,merge-targets scan for merges of other branches into <name>
23 n,dry-run don't recreate the branch"
28 sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \
29 < "$GIT_DIR"/logs/HEAD
32 search_reflog_merges () {
34 sed -ne 's~^[^ ]* \([^ ]*\) .*\tmerge '"$1"':.*~\1^2~p' \
35 < "$GIT_DIR"/logs/HEAD
39 _x40="[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
40 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
43 git rev-list --all --grep="Merge branch '$1'" \
44 --pretty=tformat:"%P %s" |
45 sed -ne "/^$_x40 \($_x40\) Merge .*/ {s//\1/p;$early_exit}"
48 search_merge_targets () {
49 git rev-list --all --grep="Merge branch '[^']*' into $branch\$" \
50 --pretty=tformat:"%H %s" --all |
51 sed -ne "/^\($_x40\) Merge .*/ {s//\1/p;$early_exit} "
62 while test "$#" != 0; do
121 test "$#" = 1 || usage
123 all_strategies="$scan_reflog$scan_reflog_merges$scan_merges$scan_merge_targets"
124 if test -z "$all_strategies"; then
125 die "must enable at least one of -lrmt"
129 test -z "$new_name" && new_name="$branch"
131 if test ! -z "$scan_reflog"; then
132 if test -r "$GIT_DIR"/logs/HEAD; then
133 candidates="$(search_reflog $branch)"
135 die 'reflog scanning requested, but' \
136 '$GIT_DIR/logs/HEAD not readable'
139 if test ! -z "$scan_reflog_merges"; then
140 if test -r "$GIT_DIR"/logs/HEAD; then
141 candidates="$candidates $(search_reflog_merges $branch)"
143 die 'reflog scanning requested, but' \
144 '$GIT_DIR/logs/HEAD not readable'
147 if test ! -z "$scan_merges"; then
148 candidates="$candidates $(search_merges $branch)"
150 if test ! -z "$scan_merge_targets"; then
151 candidates="$candidates $(search_merge_targets $branch)"
154 candidates="$(git rev-parse $candidates | sort -u)"
156 if test -z "$candidates"; then
158 test "z$all_strategies" != "ztttt" \
159 && hint=" (maybe try again with -a)"
160 die "no candidates for $branch found$hint"
163 echo "** Candidates for $branch **"
164 for cmt in $candidates; do
165 git --no-pager log --pretty=tformat:"%ct:%h [%cr] %s" --abbrev-commit -1 $cmt
167 | sort -n | cut -d: -f2-
169 newest="$(git rev-list -1 $candidates)"
170 if test ! -z "$dry_run"; then
171 printf "** Most recent: "
172 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
173 elif ! git rev-parse --verify --quiet $new_name >/dev/null; then
174 printf "** Restoring $new_name to "
175 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
176 git branch $new_name $newest
178 printf "Most recent: "
179 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
180 echo "** $new_name already exists, doing nothing"