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
17 b,branch= save branch as <newname> instead of <name>
18 a,all same as -l -r -m -t
19 k,keep-going full rev-list scan (instead of first match)
20 l,reflog scan reflog for checkouts (enabled by default)
21 r,reflog-merges scan for merges recorded in reflog
22 m,merges scan for merges into other branches (slow)
23 t,merge-targets scan for merges of other branches into <name>
24 n,dry-run don't recreate the branch"
29 sed -ne 's~^\([^ ]*\) .* checkout: moving from '"$1"' .*~\1~p' \
30 < "$GIT_DIR"/logs/HEAD
33 search_reflog_merges () {
35 sed -ne 's~^[^ ]* \([^ ]*\) .* merge '"$1"':.*~\1^2~p' \
36 < "$GIT_DIR"/logs/HEAD
40 _x40="[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
41 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
44 git rev-list --all --grep="Merge branch '$1'" \
45 --pretty=tformat:"%P %s" |
46 sed -ne "/^$_x40 \($_x40\) Merge .*/ {s//\1/p;$early_exit}"
49 search_merge_targets () {
50 git rev-list --all --grep="Merge branch '[^']*' into $branch\$" \
51 --pretty=tformat:"%H %s" --all |
52 sed -ne "/^\($_x40\) Merge .*/ {s//\1/p;$early_exit} "
63 while test "$#" != 0; do
122 test "$#" = 1 || usage
124 all_strategies="$scan_reflog$scan_reflog_merges$scan_merges$scan_merge_targets"
125 if test -z "$all_strategies"; then
126 die "must enable at least one of -lrmt"
130 test -z "$new_name" && new_name="$branch"
132 if test ! -z "$scan_reflog"; then
133 if test -r "$GIT_DIR"/logs/HEAD; then
134 candidates="$(search_reflog $branch)"
136 die 'reflog scanning requested, but' \
137 '$GIT_DIR/logs/HEAD not readable'
140 if test ! -z "$scan_reflog_merges"; then
141 if test -r "$GIT_DIR"/logs/HEAD; then
142 candidates="$candidates $(search_reflog_merges $branch)"
144 die 'reflog scanning requested, but' \
145 '$GIT_DIR/logs/HEAD not readable'
148 if test ! -z "$scan_merges"; then
149 candidates="$candidates $(search_merges $branch)"
151 if test ! -z "$scan_merge_targets"; then
152 candidates="$candidates $(search_merge_targets $branch)"
155 candidates="$(git rev-parse $candidates | sort -u)"
157 if test -z "$candidates"; then
159 test "z$all_strategies" != "ztttt" \
160 && hint=" (maybe try again with -a)"
161 die "no candidates for $branch found$hint"
164 echo "** Candidates for $branch **"
165 for cmt in $candidates; do
166 git --no-pager log --pretty=tformat:"%ct:%h [%cr] %s" --abbrev-commit -1 $cmt
168 | sort -n | cut -d: -f2-
170 newest="$(git rev-list -1 $candidates)"
171 if test ! -z "$dry_run"; then
172 printf "** Most recent: "
173 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
174 elif ! git rev-parse --verify --quiet $new_name >/dev/null; then
175 printf "** Restoring $new_name to "
176 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
177 git branch $new_name $newest
179 printf "Most recent: "
180 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
181 echo "** $new_name already exists, doing nothing"