Interested in helping open source friends on HP-UX?
[git/mjg.git] / contrib / git-resurrect.sh
blobd7e97bbc76c27f61e83a5328831fd6889b22e662
1 #!/bin/sh
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
10 branches."
12 OPTIONS_KEEPDASHDASH=
13 OPTIONS_STUCKLONG=
14 OPTIONS_SPEC="\
15 git resurrect $USAGE
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"
26 . git-sh-setup
28 search_reflog () {
29 sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \
30 < "$GIT_DIR"/logs/HEAD
33 search_reflog_merges () {
34 git rev-parse $(
35 sed -ne 's~^[^ ]* \([^ ]*\) .*\tmerge '"$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"
43 search_merges () {
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} "
55 dry_run=
56 early_exit=q
57 scan_reflog=t
58 scan_reflog_merges=
59 scan_merges=
60 scan_merge_targets=
61 new_name=
63 while test "$#" != 0; do
64 case "$1" in
65 -b|--branch)
66 shift
67 new_name="$1"
69 -n|--dry-run)
70 dry_run=t
72 --no-dry-run)
73 dry_run=
75 -k|--keep-going)
76 early_exit=
78 --no-keep-going)
79 early_exit=q
81 -m|--merges)
82 scan_merges=t
84 --no-merges)
85 scan_merges=
87 -l|--reflog)
88 scan_reflog=t
90 --no-reflog)
91 scan_reflog=
93 -r|--reflog_merges)
94 scan_reflog_merges=t
96 --no-reflog_merges)
97 scan_reflog_merges=
99 -t|--merge-targets)
100 scan_merge_targets=t
102 --no-merge-targets)
103 scan_merge_targets=
105 -a|--all)
106 scan_reflog=t
107 scan_reflog_merges=t
108 scan_merges=t
109 scan_merge_targets=t
112 shift
113 break
116 usage
118 esac
119 shift
120 done
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"
129 branch="$1"
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)"
135 else
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)"
143 else
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
158 hint=
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
167 done \
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
178 else
179 printf "Most recent: "
180 git --no-pager log -1 --pretty=tformat:"%h %s" $newest
181 echo "** $new_name already exists, doing nothing"