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
15 b,branch= save branch as <newname> instead of <name>
16 a,all same as -l -r -m -t
17 k,keep-going full rev-list scan (instead of first match)
18 l,reflog scan reflog for checkouts (enabled by default)
19 r,reflog-merges scan for merges recorded in reflog
20 m,merges scan for merges into other branches (slow)
21 t,merge-targets scan for merges of other branches into <name>
22 n,dry-run don't recreate the branch"
27 sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \
28 < "$GIT_DIR"/logs/HEAD
31 search_reflog_merges () {
33 sed -ne 's~^[^ ]* \([^ ]*\) .*\tmerge '"$1"':.*~\1^2~p' \
34 < "$GIT_DIR"/logs/HEAD
38 _x40="[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
39 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
42 git rev-list --all --grep="Merge branch '$1'" \
43 --pretty=tformat:"%P %s" |
44 sed -ne "/^$_x40 \($_x40\) Merge .*/ {s//\1/p;$early_exit}"
47 search_merge_targets () {
48 git rev-list --all --grep="Merge branch '[^']*' into $branch\$" \
49 --pretty=tformat:"%H %s" --all |
50 sed -ne "/^\($_x40\) Merge .*/ {s//\1/p;$early_exit} "
61 while test "$#" != 0; do
120 test "$#" = 1 || usage
122 all_strategies="$scan_reflog$scan_reflog_merges$scan_merges$scan_merge_targets"
123 if test -z "$all_strategies"; then
124 die "must enable at least one of -lrmt"
128 test -z "$new_name" && new_name="$branch"
130 if test ! -z "$scan_reflog"; then
131 if test -r "$GIT_DIR"/logs/HEAD; then
132 candidates="$(search_reflog $branch)"
134 die 'reflog scanning requested, but' \
135 '$GIT_DIR/logs/HEAD not readable'
138 if test ! -z "$scan_reflog_merges"; then
139 if test -r "$GIT_DIR"/logs/HEAD; then
140 candidates="$candidates $(search_reflog_merges $branch)"
142 die 'reflog scanning requested, but' \
143 '$GIT_DIR/logs/HEAD not readable'
146 if test ! -z "$scan_merges"; then
147 candidates="$candidates $(search_merges $branch)"
149 if test ! -z "$scan_merge_targets"; then
150 candidates="$candidates $(search_merge_targets $branch)"
153 candidates="$(git rev-parse $candidates | sort -u)"
155 if test -z "$candidates"; then
157 test "z$all_strategies" != "ztttt" \
158 && hint=" (maybe try again with -a)"
159 die "no candidates for $branch found$hint"
162 echo "** Candidates for $branch **"
163 for cmt in $candidates; do
164 git --no-pager log --pretty=tformat:"%ct:%h [%cr] %s" --abbrev-commit -1 $cmt
166 | sort -n | cut -d: -f2-
168 newest="$(git rev-list -1 $candidates)"
169 if test ! -z "$dry_run"; then
170 printf "** Most recent: "
171 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
172 elif ! git rev-parse --verify --quiet $new_name >/dev/null; then
173 printf "** Restoring $new_name to "
174 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
175 git branch $new_name $newest
177 printf "Most recent: "
178 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
179 echo "** $new_name already exists, doing nothing"